import {z} from 'zod' export type GetApiMerchantHoldersArgs = {'filter[reference]'?: string | undefined, 'filter[id]'?: string | undefined, 'filter[status]'?: string | undefined, 'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'page[size]'?: number | undefined}; export type GetApiMerchantHoldersOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMerchantHoldersResponse = z.object({links: z.object({self: z.string(), prev: z.string().optional(), next: z.string().optional(), first: z.string().optional(), last: z.string().optional()}), results: z.array(z.object({id: z.string(), reference: z.string(), createdAt: z.string().optional(), status: z.array(z.string()).optional(), type: 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 }; /** * Search & list holders * * GET /merchant/holders */ export const getApiMerchantHolders = async (args: GetApiMerchantHoldersArgs, options?: GetApiMerchantHoldersOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[reference]'] != null) query.append("filter[reference]", String(args['filter[reference]'])) if (args['filter[id]'] != null) query.append("filter[id]", String(args['filter[id]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchant/holders${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(getApiMerchantHoldersResponse, await res.json()) };