import {z} from 'zod' export type WebhookType = 'conversation-post-processing-complete' | 'api-key-expiration-soon' | 'agent-framework-resource-updated'; export type SrcAppEndpointsWebhookDestinationCreateWebhookDestinationRequest = {url: string, accepted_types: Array, retry_attempts?: number | undefined}; export type CreateApiV1OrganizationWebhookDestinationArgs = {organization: string, 'x-mongo-cluster-name'?: (string | null) | undefined, 'Sec-WebSocket-Protocol'?: Array | undefined, body: SrcAppEndpointsWebhookDestinationCreateWebhookDestinationRequest}; export type CreateApiV1OrganizationWebhookDestinationOptions = {baseUrl?: string | undefined, BearerAuthorization?: string | undefined, BearerAuthorizationOrganization?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const srcAppEndpointsWebhookDestinationCreateWebhookDestinationResponse = z.object({webhook_destination_id: z.string(), secret: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a webhook destination * * POST /v1/{organization}/webhook_destination/ */ export const createApiV1OrganizationWebhookDestination = async (args: CreateApiV1OrganizationWebhookDestinationArgs, options?: CreateApiV1OrganizationWebhookDestinationOptions) => { const {baseUrl = "https://api.amigo.ai", fetch: fetchFn = fetch, BearerAuthorization, BearerAuthorizationOrganization, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-mongo-cluster-name'] != null) headers.set("x-mongo-cluster-name", String(args['x-mongo-cluster-name'])) if (args['Sec-WebSocket-Protocol'] != null) headers.set("Sec-WebSocket-Protocol", args['Sec-WebSocket-Protocol'].join(',')) if (BearerAuthorization !== undefined) headers.set('authorization', `Bearer ${BearerAuthorization}`) if (BearerAuthorizationOrganization !== undefined) headers.set("X-ORG-ID", BearerAuthorizationOrganization) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.organization))}/webhook_destination/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(srcAppEndpointsWebhookDestinationCreateWebhookDestinationResponse, await res.json()) };