import {z} from 'zod' export type TransliteratedAddressDto = {addressLine1?: string | undefined, addressLine2?: string | undefined, city?: string | undefined, state?: string | undefined}; export type BeneficiaryAddressDto = {addressLine1: string, addressLine2?: string | undefined, city: string, state?: string | undefined, postalCode: string, country: string, transliterated?: TransliteratedAddressDto | undefined}; export type CreateCounterpartyDto = {name: string, beneficiaryType: 'INDIVIDUAL' | 'BUSINESS', address?: BeneficiaryAddressDto | undefined}; export type CreateApiV1CounterpartiesArgs = {'X-API-Key'?: string | undefined, body: CreateCounterpartyDto}; export type CreateApiV1CounterpartiesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transliteratedAddressDto = z.object({addressLine1: z.string().min(1).max(1024).optional(), addressLine2: z.string().min(1).max(1024).optional(), city: z.string().min(1).max(256).optional(), state: z.string().min(1).max(256).optional()}); export const addressDto = z.object({addressLine1: z.string().min(1).max(1024), addressLine2: z.string().min(1).max(1024).optional(), city: z.string().min(1).max(256), state: z.string().min(1).max(256).optional(), postalCode: z.string().min(1).max(50), country: z.string().min(2).max(2), transliterated: transliteratedAddressDto.optional()}); export const counterpartyResponseDto = z.object({id: z.string(), name: z.string(), beneficiaryType: z.enum(["INDIVIDUAL", "BUSINESS"]), address: addressDto.nullable().optional(), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Create a counterparty * * POST /v1/counterparties */ export const createApiV1Counterparties = async (args: CreateApiV1CounterpartiesArgs, options?: CreateApiV1CounterpartiesOptions) => { const {baseUrl = "https://sandbox.api.infinite.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/counterparties${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(counterpartyResponseDto, await res.json()) };