export type JsonValue = string | number | boolean | Array | {[key: string]: JsonValue} | null; export type StripeWebhookEventRequest = {id: string, object?: 'event' | undefined, type: string, created?: number | undefined, livemode?: boolean | undefined, api_version?: (string | null) | undefined, data: {object: {[key: string]: JsonValue}, previous_attributes?: {[key: string]: JsonValue} | undefined} | {[key: string]: JsonValue}} | {[key: string]: JsonValue}; export type CreateApiApiV1StripeWebhookArgs = {'Stripe-Signature': string, body: StripeWebhookEventRequest}; export type CreateApiApiV1StripeWebhookOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Receive a signed Stripe event * * POST /api/v1/stripe-webhook */ export const createApiApiV1StripeWebhook = async (args: CreateApiApiV1StripeWebhookArgs, options?: CreateApiApiV1StripeWebhookOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "text/plain") if (args['Stripe-Signature'] != null) headers.set("Stripe-Signature", String(args['Stripe-Signature'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/stripe-webhook${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 res.text() };