import {z} from 'zod' export type ApplicationId = string; export type GetApiV2ApplicationsApplicationIdArgs = {application_id: ApplicationId}; export type GetApiV2ApplicationsApplicationIdOptions = {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 accountId = z.string().min(30).max(50); export const businessLegalStructure = z.enum(["ca_private_corp", "ca_public_corp", "ca_non_profit"]); export const baseRegistrationNumber = z.object({type: z.string(), value: z.string().min(1).max(50)}); export const germanRegistrationNumber = z.object({type: z.string(), value: z.string().min(1).max(50), registry: z.string()}); export const registrationNumber = z.discriminatedUnion("type", [baseRegistrationNumber, germanRegistrationNumber]); export const businessResponse = z.object({legal_name: z.string().min(2).max(250), country: z.string().min(2).max(2), legal_structure: businessLegalStructure.optional(), registered_address: z.object({line1: z.string().min(1).max(250), line2: z.string().min(1).max(250).optional(), street_number: z.string().min(1).max(50), city: z.string().min(1).max(250), region: z.string().min(1).max(250).optional(), postal_code: z.string().min(1).max(16)}).optional(), registration_number: registrationNumber.optional()}); export const applicantResponse = z.object({email: z.string().max(320), first_name: z.string().min(2).max(50).optional(), last_name: z.string().min(2).max(50).optional()}); export const dateTime = z.string().min(24).max(24); export const applicationFlow = z.object({next_step: z.object({type: z.literal("redirect"), url: z.string().max(1024), expires_at: dateTime.optional()})}); export const createdAt = z.string().min(24).max(24); export const updatedAt = z.string().min(24).max(24); export const applicationExpiresAt = z.string().min(24).max(24); export const getApplicationResponse = z.object({id: applicationId, status: applicationStatus, reference: reference.optional(), account: z.object({id: accountId}), business: businessResponse.nullable().optional(), applicant: applicantResponse.nullable().optional(), flow: applicationFlow.optional(), created_at: createdAt, updated_at: updatedAt, 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 }; /** * Get an application * * GET /v2/applications/{application_id} */ export const getApiV2ApplicationsApplicationId = async (args: GetApiV2ApplicationsApplicationIdArgs, options?: GetApiV2ApplicationsApplicationIdOptions) => { const {baseUrl = "https://api.getpaid.io", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/applications/${encodeURIComponent(String(args.application_id))}${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(getApplicationResponse, await res.json()) };