import {z} from 'zod' export type GetApiV1WebhooksIdArgs = {id: string}; export type GetApiV1WebhooksIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventType = z.enum(["REFERRAL_CREATED", "REFERRAL_ACTIVATED", "REFERRAL_APPLICATION_STATUS_CHANGED", "TRANSFER_PROCESSED", "TRANSFER_FAILED", "EXPENSE_PAYMENT_UPDATED", "USER_UPDATED", "EMBEDDED_CARD_TRANSACTION_UPDATED", "EMBEDDED_CARD_UPDATED", "EMBEDDED_ACCOUNT_UPDATED", "EMBEDDED_CARD_SHIPPING_UPDATED", "EMBEDDED_DISPUTE_UPDATED", "PARTNERSHIP_INTEGRATION_CONNECTION_UPDATED", "EMBEDDED_CARD_AUTHORIZATION", "EMBEDDED_FRAUD_ALERT", "EMBEDDED_BILLING_GROUP_UPDATED", "EMBEDDED_LEGAL_ENTITY_UPDATED", "EMBEDDED_BILLING_STATEMENT_UPDATED", "EMBEDDED_USER_STATUS_UPDATED", "ACCOUNTING_RECORD_READY_FOR_EXPORT"]); export const webhookSubscription = z.object({id: z.string().min(1), group_id: z.string().nullable().optional(), url: z.string(), event_types: z.array(webhookEventType), status: z.enum(["ACTIVE", "INACTIVE"])}); 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 }; /** * Get Webhook * * GET /v1/webhooks/{id} */ export const getApiV1WebhooksId = async (args: GetApiV1WebhooksIdArgs, options?: GetApiV1WebhooksIdOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/${encodeURIComponent(String(args.id))}${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(webhookSubscription, await res.json()) };