import {z} from 'zod' export type GetApiPaymentInstrumentsInstrumentIdTokensArgs = {instrumentId: string, 'filter[status]'?: string | undefined, 'filter[anyReference]'?: string | undefined, 'filter[createdAt]'?: string | undefined, 'page[cursor]'?: string | undefined, 'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'page[size]'?: number | undefined}; export type GetApiPaymentInstrumentsInstrumentIdTokensOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPaymentInstrumentsInstrumentIdTokensResponse = z.object({paging: z.object({next: z.string().optional()}).optional(), results: z.array(z.object({id: z.string(), createdAt: z.string(), updatedAt: z.string(), instrumentId: z.string(), status: z.enum(["created", "enabled", "disabled", "deleted"]), type: z.enum(["network", "vault", "psp", "networkOffers", "networkGateway"]), providerId: z.string(), providerConfigId: z.string().optional(), reference: z.string(), meta: z.record(z.string(), z.unknown()).optional()})).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 tokens * * GET /payment/instruments/{instrumentId}/tokens */ export const getApiPaymentInstrumentsInstrumentIdTokens = async (args: GetApiPaymentInstrumentsInstrumentIdTokensArgs, options?: GetApiPaymentInstrumentsInstrumentIdTokensOptions) => { 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[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[anyReference]'] != null) query.append("filter[anyReference]", String(args['filter[anyReference]'])) if (args['filter[createdAt]'] != null) query.append("filter[createdAt]", String(args['filter[createdAt]'])) if (args['page[cursor]'] != null) query.append("page[cursor]", String(args['page[cursor]'])) 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(/\/+$/, '')}/payment/instruments/${encodeURIComponent(String(args.instrumentId))}/tokens${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(getApiPaymentInstrumentsInstrumentIdTokensResponse, await res.json()) };