import {z} from 'zod' export type ObjectStatus = 'Active' | 'Inactive'; export type WebhookAction = 'None' | 'CaseFunctionRequest' | 'CaseChangeAdded' | 'PayrunFunctionRequest' | 'PayrunJobProcess' | 'PayrunJobFinish' | 'ReportFunctionRequest' | 'TaskChange'; export type Webhook = {id?: number | undefined, status?: ObjectStatus | undefined, created?: string | undefined, updated?: string | undefined, name: string, receiverAddress: string, action: WebhookAction, attributes?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiApiTenantsTenantIdWebhooksArgs = {tenantId: number, body: Webhook}; export type CreateApiApiTenantsTenantIdWebhooksOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const objectStatus = z.enum(["Active", "Inactive"]); export const webhookAction = z.enum(["None", "CaseFunctionRequest", "CaseChangeAdded", "PayrunFunctionRequest", "PayrunJobProcess", "PayrunJobFinish", "ReportFunctionRequest", "TaskChange"]); export const webhook = z.object({id: z.number().int().optional(), status: objectStatus.optional(), created: z.string().optional(), updated: z.string().optional(), name: z.string().max(128), receiverAddress: z.string().max(128), action: webhookAction, attributes: z.record(z.string(), z.unknown()).nullable().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 }; /** * POST /api/tenants/{tenantId}/webhooks */ export const createApiApiTenantsTenantIdWebhooks = async (args: CreateApiApiTenantsTenantIdWebhooksArgs, options: CreateApiApiTenantsTenantIdWebhooksOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/tenants/${encodeURIComponent(String(args.tenantId))}/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(webhook, await res.json()) };