import {z} from 'zod' export type SourceCard = {brand?: string | undefined, last4?: string | undefined, exp_month?: (number | null) | undefined, exp_year?: (number | null) | undefined}; export type Source = {id?: number | undefined, customer?: (string | null) | undefined, gateway: string, usage?: ('single' | 'reusable') | undefined, status?: ('failed' | 'pending' | 'requires_authorization' | 'waiting_authorization' | 'consumed' | 'chargeable' | 'finalized') | undefined, amount?: (number | null) | undefined, description?: (string | null) | undefined, return_url?: (string | null) | undefined, gateway_data?: Record | undefined, client?: Record | undefined, metadata?: Record | undefined, card?: SourceCard | undefined, test_mode?: boolean | undefined, created?: string | undefined, updated?: string | undefined}; export type CreateApiSourcesArgs = {body: Source}; export type CreateApiSourcesOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceCard = z.object({brand: z.string().optional(), last4: z.string().max(4).optional(), exp_month: z.number().int().nullable().optional(), exp_year: z.number().int().nullable().optional()}); export const source = z.object({id: z.number().int().optional(), customer: z.string().nullable().optional(), gateway: z.string(), usage: z.enum(["single", "reusable"]).optional(), status: z.enum(["failed", "pending", "requires_authorization", "waiting_authorization", "consumed", "chargeable", "finalized"]).optional(), amount: z.number().nullable().optional(), description: z.string().max(256).nullable().optional(), return_url: z.string().nullable().optional(), gateway_data: z.object({}).optional(), client: z.object({}).optional(), metadata: z.object({}).optional(), card: sourceCard.optional(), test_mode: z.boolean().optional(), created: z.string().optional(), updated: z.string().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 }; /** * Create source (publishable) * * POST /sources/ */ export const createApiSources = async (args: CreateApiSourcesArgs, options?: CreateApiSourcesOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sources/${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(source, await res.json()) };