import {z} from 'zod' export type WebhookRequest = {delivery_url: string, events: Array<'add_property_claim' | 'delete_property_claim' | 'update_property_claim' | 'add_property_inspection' | 'delete_property_inspection' | 'update_property_inspection' | 'add_property_claim_media' | 'delete_property_claim_media' | 'add_property_inspection_media' | 'delete_property_inspection_media' | 'add_property_claim_note' | 'delete_property_claim_note' | 'add_property_claim_room_note' | 'delete_property_claim_room_note' | 'add_dehumidifier' | 'update_dehumidifier' | 'add_air_mover' | 'update_air_mover' | 'add_air_scrubber' | 'update_air_scrubber' | 'property_claim_media' | 'property_inspection_media'>, authentication?: ({type: 'jwt' | 'oauth' | 'shared_secret', header_name?: ('Authorization' | 'ocp-apim-subscription-key') | undefined, shared_secret: string}) | undefined}; export type CreateApiV1WebhooksArgs = {'X-Encircle-Attribution'?: string | undefined, 'X-Encircle-Request'?: string | undefined, body: WebhookRequest}; export type CreateApiV1WebhooksOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookResponse = z.object({id: z.number().int(), delivery_url: z.string(), events: z.array(z.enum(["add_property_claim", "delete_property_claim", "update_property_claim", "add_property_inspection", "delete_property_inspection", "update_property_inspection", "add_property_claim_media", "delete_property_claim_media", "add_property_inspection_media", "delete_property_inspection_media", "add_property_claim_note", "delete_property_claim_note", "add_property_claim_room_note", "delete_property_claim_room_note", "add_dehumidifier", "update_dehumidifier", "add_air_mover", "update_air_mover", "add_air_scrubber", "update_air_scrubber", "property_claim_media", "property_inspection_media"])), authentication: z.object({type: z.enum(["jwt", "oauth", "shared_secret"]), header_name: z.enum(["Authorization", "ocp-apim-subscription-key"]), shared_secret: z.string()}).optional()}); 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 * * POST /v1/webhooks */ export const createApiV1Webhooks = async (args: CreateApiV1WebhooksArgs, options: CreateApiV1WebhooksOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Encircle-Attribution'] != null) headers.set("X-Encircle-Attribution", String(args['X-Encircle-Attribution'])) if (args['X-Encircle-Request'] != null) headers.set("X-Encircle-Request", String(args['X-Encircle-Request'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks${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(webhookResponse, await res.json()) };