import {z} from 'zod' export type GetApiSourcesArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, page?: number | undefined, page_size?: number | undefined, customer?: string | undefined, gateway?: string | undefined, usage?: ('single' | 'reusable') | undefined, status?: string | undefined, id?: number | undefined}; export type GetApiSourcesOptions = {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 getApiSourcesResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(source)}); 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 sources * * GET /sources/ */ export const getApiSources = async (args: GetApiSourcesArgs, options?: GetApiSourcesOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.customer != null) query.append("customer", String(args.customer)) if (args.gateway != null) query.append("gateway", String(args.gateway)) if (args.usage != null) query.append("usage", String(args.usage)) if (args.status != null) query.append("status", String(args.status)) if (args.id != null) query.append("id", String(args.id)) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sources/${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(getApiSourcesResponse, await res.json()) };