import {z} from 'zod' export type GetApiV2WebhooksArgs = {company_nanoid: string, team_nanoid?: string | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV2WebhooksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2WebhooksResponse = z.object({success: z.literal(true), data: z.object({webhooks: z.array(z.object({webhook_nanoid: z.string().min(15).max(15), webhook_version: z.enum(["v1", "v2"]), url: z.string(), events: z.array(z.string()), is_active: z.boolean(), name: z.string().nullable(), description: z.string().nullable(), created_at: z.string(), updated_at: z.string()})), next_cursor: z.string().nullable()})}); 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 }; /** * List Webhooks * * GET /v2/webhooks */ export const getApiV2Webhooks = async (args: GetApiV2WebhooksArgs, options?: GetApiV2WebhooksOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company_nanoid != null) query.append("company_nanoid", String(args.company_nanoid)) if (args.team_nanoid != null) query.append("team_nanoid", String(args.team_nanoid)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhooks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2WebhooksResponse, await res.json()) };