import {z} from 'zod' export type OpenapiComponentsSchemasOffsetToken = string; export type GetApiProcessorTokensArgs = {offsetToken?: OpenapiComponentsSchemasOffsetToken | undefined, limit?: number | undefined, reference?: string | undefined}; export type GetApiProcessorTokensOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const moreItems = z.boolean(); export const openapiComponentsSchemasOffsetToken = z.string(); export const componentsSchemasProcessorTokenKey = z.string().min(5).max(120); export const schemasAgentKey = z.string().min(5).max(120); export const last4 = z.string().min(4).max(4); export const cardStatus = z.enum(["active", "archived"]); export const schemasReference = z.string().min(5).max(120); export const cardInfoSimplified = z.array(z.object({infoSource: z.string(), network: z.string(), primaryNetwork: z.boolean()})); export const openapiComponentsSchemasCreated2 = z.string(); export const cvcPresent = z.boolean(); export const lastActionDate = z.string(); export const openapiComponentsSchemasLastModified2 = z.string(); export const openapiComponentsSchemasVersion = z.number().int(); export const publicProcessorTokenObject = z.object({key: componentsSchemasProcessorTokenKey, agentKey: schemasAgentKey, last4: last4, status: cardStatus, reference: schemasReference.optional(), cardInfo: cardInfoSimplified, created: openapiComponentsSchemasCreated2, cvcPresent: cvcPresent, lastActionDate: lastActionDate.optional(), lastModified: openapiComponentsSchemasLastModified2.optional(), version: openapiComponentsSchemasVersion}); export const processorTokensPage = z.object({moreItems: moreItems.optional(), offsetToken: openapiComponentsSchemasOffsetToken.optional(), processorTokens: z.array(publicProcessorTokenObject)}); 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 Processor Tokens * * GET /processorTokens * * Also accepts auth: BearerToken */ export const getApiProcessorTokens = async (args: GetApiProcessorTokensArgs, options?: GetApiProcessorTokensOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offsetToken != null) query.append("offsetToken", String(args.offsetToken)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.reference != null) query.append("reference", String(args.reference)) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/processorTokens${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(processorTokensPage, await res.json()) };