import {z} from 'zod' export type DeleteApiV2WebhooksWebhookIdArgs = {'X-Encircle-Attribution'?: string | undefined, 'X-Encircle-Request'?: string | undefined}; export type DeleteApiV2WebhooksWebhookIdOptions = {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 }; /** * Delete a webhook * * DELETE /v2/webhooks/{webhook_id} */ export const deleteApiV2WebhooksWebhookId = async (args: DeleteApiV2WebhooksWebhookIdArgs, options: DeleteApiV2WebhooksWebhookIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/${encodeURIComponent(String(args.webhook_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) 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()) };