-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds webhooks without consuming response; future stories could act upon response and/or show status of webhooks in document history panel.
- Loading branch information
1 parent
362a161
commit 2640478
Showing
10 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,7 @@ Thumbs.db | |
# Docker Bind Mounts | ||
/mongo-data/ | ||
/nats-data/ | ||
|
||
# Python | ||
env | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from 'zod' | ||
|
||
//* https://datatracker.ietf.org/doc/html/rfc6750 does not require base64 encoding of Bearer tokens. | ||
export const WebhookConfigSchema = z.object({ | ||
URL: z.string().url(), | ||
token: z.string().min(1), | ||
}) | ||
|
||
export const WebhookConfigsSchema = z.array(WebhookConfigSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { ErrorData } from 'declarations' | ||
import { parseResponse } from 'utils/api' | ||
import { ErrorCode, HttpException, parseZodAsErrorData } from 'utils/errors' | ||
import { safeParseJSON } from 'utils/json' | ||
import log from '../lib/log' | ||
import { WebhookConfigsSchema } from './schema' | ||
import type { WebhookConfig, WebhookPayload } from './types' | ||
|
||
const WEBHOOK_ENV_VAR = 'UI_WEBHOOKS' | ||
|
||
function getAllWebhookConfigs(): ErrorData<WebhookConfig[]> { | ||
try { | ||
const fromEnvironment = process.env[WEBHOOK_ENV_VAR] ?? [] | ||
const [parseError, JSON] = safeParseJSON(fromEnvironment) | ||
const [schemaError, webhooks] = parseZodAsErrorData( | ||
WebhookConfigsSchema, | ||
JSON | ||
) | ||
|
||
if (parseError || schemaError) { | ||
throw parseError || schemaError | ||
} | ||
|
||
return [null, webhooks as WebhookConfig[]] | ||
} catch (error) { | ||
log.error(error) | ||
|
||
return [error, null] | ||
} | ||
} | ||
|
||
function getAllWebhookURLs(): ErrorData<string[]> { | ||
try { | ||
const [error, webhooks] = getAllWebhookConfigs() | ||
|
||
if (error) { | ||
throw error | ||
} | ||
|
||
const webhookURLs = webhooks.map(webhook => webhook.URL) | ||
|
||
return [null, webhookURLs] | ||
} catch (error) { | ||
log.error(error) | ||
|
||
return [error, null] | ||
} | ||
} | ||
|
||
function getWebhookConfig(URL: WebhookConfig['URL']): ErrorData<WebhookConfig> { | ||
try { | ||
const [error, webhooks] = getAllWebhookConfigs() | ||
|
||
if (error) { | ||
throw error | ||
} | ||
|
||
const [webhook = null] = webhooks.filter(webhook => webhook.URL === URL) | ||
|
||
return [null, webhook] | ||
} catch (error) { | ||
log.error(error) | ||
|
||
return [error, null] | ||
} | ||
} | ||
|
||
async function invokeWebhook( | ||
webhook: WebhookConfig, | ||
payload: WebhookPayload | ||
): Promise<ErrorData<any>> { | ||
try { | ||
const response = await fetch(webhook.URL, { | ||
method: 'POST', | ||
headers: { | ||
accept: 'application/json', | ||
authorization: `Bearer ${webhook.token}`, | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify(payload), | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new HttpException(ErrorCode.BadRequest, response.statusText) | ||
} | ||
|
||
return [null, await parseResponse(response)] | ||
} catch (error) { | ||
return [error, null] | ||
} | ||
} | ||
|
||
export { getAllWebhookURLs, getWebhookConfig, invokeWebhook } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Document } from 'documents/types' | ||
import type { ModelWithWorkflow } from 'models/types' | ||
import type { z } from 'zod' | ||
|
||
import type { WebhookConfigSchema } from './schema' | ||
|
||
export type WebhookConfig = z.infer<typeof WebhookConfigSchema> | ||
|
||
export type WebhookPayload = { | ||
model: ModelWithWorkflow | ||
document: Document | ||
state: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters