import {z} from 'zod' export type Webhook = {id?: string | undefined, version?: string | undefined, atCreate?: boolean | undefined, atDelete?: boolean | undefined, atUpdate?: boolean | undefined, createdDate?: number | undefined, deactivatedDate?: number | undefined, entityName: string, errorMessage?: string | undefined, lastModifiedDate?: number | undefined, requestMethod?: ('GET' | 'POST') | undefined, url: string}; export type UpdateApiWebhookIdIdArgs = {id: string, dryRun?: boolean | undefined, body: Webhook}; export type UpdateApiWebhookIdIdOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({id: z.string().optional(), version: z.string().optional(), atCreate: z.boolean().optional(), atDelete: z.boolean().optional(), atUpdate: z.boolean().optional(), createdDate: z.number().int().optional(), deactivatedDate: z.number().int().optional(), entityName: z.string(), errorMessage: z.string().optional(), lastModifiedDate: z.number().int().optional(), requestMethod: z.enum(["GET", "POST"]).optional(), url: 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 }; /** * update webhook * * PUT /webhook/id/{id} */ export const updateApiWebhookIdId = async (args: UpdateApiWebhookIdIdArgs, options: UpdateApiWebhookIdIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/webhook/id/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(webhook, await res.json()) };