import {z} from 'zod' export type WebhookRequestV2 = {delivery_url: string, events: Array<'add_property_claim_note' | 'add_property_claim_room_note' | 'delete_property_claim_room_note' | 'delete_property_claim_note' | 'add_property_claim' | 'update_property_claim' | 'delete_property_claim' | 'delete_property_claim_media' | 'add_property_claim_media' | 'add_affected_atmosphere_reading' | 'update_affected_atmosphere_reading' | 'delete_affected_atmosphere_reading' | 'add_unaffected_atmosphere_reading' | 'update_unaffected_atmosphere_reading' | 'delete_unaffected_atmosphere_reading' | 'add_dehumidifier_reading' | 'update_dehumidifier_reading' | 'delete_dehumidifier_reading' | 'add_material_reading' | 'update_material_reading' | 'delete_material_reading' | 'add_floorplan_dimensions' | 'fixed_floorplan_dimensions' | 'add_equipment' | 'update_equipment' | 'add_room_dimensions' | 'update_room_dimensions' | 'delete_room_dimensions'>, authentication?: ({type: 'jwt' | 'oauth' | 'shared_secret', header_name?: ('Authorization' | 'ocp-apim-subscription-key') | undefined, shared_secret: string}) | undefined}; export type CreateApiV2WebhooksArgs = {'X-Encircle-Attribution'?: string | undefined, 'X-Encircle-Request'?: string | undefined, body: WebhookRequestV2}; export type CreateApiV2WebhooksOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookResponseV2 = z.object({id: z.number().int(), delivery_url: z.string(), events: z.array(z.enum(["add_property_claim_note", "add_property_claim_room_note", "delete_property_claim_room_note", "delete_property_claim_note", "add_property_claim", "update_property_claim", "delete_property_claim", "delete_property_claim_media", "add_property_claim_media", "add_affected_atmosphere_reading", "update_affected_atmosphere_reading", "delete_affected_atmosphere_reading", "add_unaffected_atmosphere_reading", "update_unaffected_atmosphere_reading", "delete_unaffected_atmosphere_reading", "add_dehumidifier_reading", "update_dehumidifier_reading", "delete_dehumidifier_reading", "add_material_reading", "update_material_reading", "delete_material_reading", "add_floorplan_dimensions", "fixed_floorplan_dimensions", "add_equipment", "update_equipment", "add_room_dimensions", "update_room_dimensions", "delete_room_dimensions"])), 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 /v2/webhooks */ export const createApiV2Webhooks = async (args: CreateApiV2WebhooksArgs, options: CreateApiV2WebhooksOptions) => { 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(/\/+$/, '')}/v2/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(webhookResponseV2, await res.json()) };