import {z} from 'zod' export type PaginationFirst = number; export type PaginationAfter = string; export type ApplicationId = string; export type Reference = string; export type ApplicationStatus = 'initiated' | 'identity_verified' | 'submitted' | 'screened' | 'approved' | 'completed' | 'cancelled' | 'expired'; export type AccountLegalName = string; export type Country = unknown; export type QueryApplicationsRequest = {type: 'applications', first?: PaginationFirst | undefined, after?: PaginationAfter | undefined, filters?: ({id?: ApplicationId | undefined, reference?: Reference | undefined, statuses?: Array | undefined, legal_name?: AccountLegalName | undefined, country?: Country | undefined}) | undefined, sorts?: (Array<{field: 'id' | 'created_at' | 'status' | 'reference' | 'legal_name' | 'country', direction: 'ascending' | 'descending'}>) | undefined}; export type CreateApiV2ApplicationsQueryArgs = {'Getpaid-Idempotency-Key': string, body: QueryApplicationsRequest}; export type CreateApiV2ApplicationsQueryOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const applicationId = z.string().min(30).max(50); export const applicationStatus = z.enum(["initiated", "identity_verified", "submitted", "screened", "approved", "completed", "cancelled", "expired"]); export const reference = z.string().min(1).max(50); export const accountLegalName = z.string().min(1).max(250); export const country = z.string().min(2).max(2); export const email = z.string().max(320); export const createdAt = z.string().min(24).max(24); export const applicationExpiresAt = z.string().min(24).max(24); export const queryApplicationsResponse = z.object({type: z.literal("applications"), cursor: z.string().max(250).optional(), data: z.array(z.object({id: applicationId, status: applicationStatus, reference: reference.optional(), legal_name: accountLegalName.optional(), country: country.optional(), applicant_email: email.optional(), created_at: createdAt, expires_at: applicationExpiresAt}))}); 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 }; /** * Query applications * * POST /v2/applications/query */ export const createApiV2ApplicationsQuery = async (args: CreateApiV2ApplicationsQueryArgs, options?: CreateApiV2ApplicationsQueryOptions) => { const {baseUrl = "https://api.getpaid.io", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Getpaid-Idempotency-Key'] != null) headers.set("Getpaid-Idempotency-Key", String(args['Getpaid-Idempotency-Key'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/applications/query${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(queryApplicationsResponse, await res.json()) };