import {z} from 'zod' export type Channel = string; export type FilterType = string; export type EndpointPatch = {channels?: (Array | null) | undefined, description?: (string | null) | undefined, disabled?: (boolean | null) | undefined, filter_types?: (Array | null) | undefined, metadata?: ({[key: string]: string} | null) | undefined, rate_limit?: (number | null) | undefined, secret?: (string | null) | undefined, uid?: (string | null) | undefined, url?: (string | null) | undefined, version?: (number | null) | undefined}; export type PatchApiWebhooksEndpointIdArgs = {endpoint_id: string, body: EndpointPatch}; export type PatchApiWebhooksEndpointIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const channel = z.string().max(128); export const filterType = z.string().max(256); export const endpointOut = z.object({channels: z.array(channel).nullable().optional(), created_at: z.string(), description: z.string(), disabled: z.boolean().nullable().optional(), filter_types: z.array(filterType).nullable().optional(), id: z.string(), metadata: z.record(z.string(), z.string()), rate_limit: z.number().int().gte(0).nullable().optional(), uid: z.string().min(1).max(256).nullable().optional(), updated_at: z.string(), url: z.string().min(1), version: z.number().int().gte(1)}); 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 }; /** * Patch Webhook Endpoint * * PATCH /webhooks/{endpoint_id} */ export const patchApiWebhooksEndpointId = async (args: PatchApiWebhooksEndpointIdArgs, options?: PatchApiWebhooksEndpointIdOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.endpoint_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(endpointOut, await res.json()) };