| import path from 'path' |
|
|
| import { getOpenApiVersion } from '@/versions/lib/all-versions' |
| import { readCompressedJsonFileFallback } from '@/frame/lib/read-json-file' |
|
|
| export const WEBHOOK_DATA_DIR = 'src/webhooks/data' |
| export const WEBHOOK_SCHEMA_FILENAME = 'schema.json' |
|
|
| |
| const webhooksCache = new Map<string, Promise<Record<string, any>>>() |
| |
| |
| |
| const initialWebhooksCache = new Map<string, InitialWebhook[]>() |
|
|
| interface InitialWebhook { |
| name: string |
| actionTypes: string[] |
| data: { |
| bodyParameters?: Array<{ |
| childParamsGroups?: any[] |
| [key: string]: any |
| }> |
| [key: string]: any |
| } |
| } |
|
|
| |
| |
| export async function getInitialPageWebhooks(version: string): Promise<InitialWebhook[]> { |
| if (initialWebhooksCache.has(version)) { |
| return initialWebhooksCache.get(version) || [] |
| } |
| const allWebhooks = await getWebhooks(version) |
| const initialWebhooks: InitialWebhook[] = [] |
|
|
| |
| |
| |
| for (const [key, webhook] of Object.entries(allWebhooks)) { |
| const actionTypes = Object.keys(webhook) |
| const defaultAction = actionTypes.length > 0 ? actionTypes[0] : '' |
|
|
| const initialWebhook: InitialWebhook = { |
| name: key, |
| actionTypes, |
| data: defaultAction ? webhook[defaultAction] : {}, |
| } |
|
|
| |
| |
| if (initialWebhook.data.bodyParameters) { |
| for (const bodyParam of initialWebhook.data.bodyParameters) { |
| if (bodyParam.childParamsGroups) { |
| bodyParam.childParamsGroups = [] |
| } |
| } |
| } |
|
|
| initialWebhooks.push({ ...initialWebhook }) |
| } |
| initialWebhooksCache.set(version, initialWebhooks) |
| return initialWebhooks |
| } |
|
|
| |
| |
| |
| export async function getWebhook( |
| version: string, |
| webhookCategory: string, |
| ): Promise<Record<string, any> | undefined> { |
| const webhooks = await getWebhooks(version) |
| return webhooks[webhookCategory] |
| } |
|
|
| |
| export async function getWebhooks(version: string): Promise<Record<string, any>> { |
| const openApiVersion = getOpenApiVersion(version) |
| if (!webhooksCache.has(openApiVersion)) { |
| |
| |
| webhooksCache.set( |
| openApiVersion, |
| readCompressedJsonFileFallback( |
| path.join(WEBHOOK_DATA_DIR, openApiVersion, WEBHOOK_SCHEMA_FILENAME), |
| ), |
| ) |
| } |
|
|
| return webhooksCache.get(openApiVersion) || Promise.resolve({}) |
| } |
|
|