import {z} from 'zod' export type CreateCounterpartyRequestDto = {classification: 'individual' | 'business', tenant_id?: (string | null) | undefined, individual_first_name?: (string | null) | undefined, individual_last_name?: (string | null) | undefined, individual_address_country?: (string | null) | undefined, individual_date_of_birth?: (string | null) | undefined, individual_national_identification_number?: (string | null) | undefined, individual_street_address1?: (string | null) | undefined, individual_street_address2?: (string | null) | undefined, individual_city?: (string | null) | undefined, individual_state?: (string | null) | undefined, individual_postal_code?: (string | null) | undefined, business_legal_name?: (string | null) | undefined, business_dba?: (string | null) | undefined, business_address_country?: (string | null) | undefined, business_street_address1?: (string | null) | undefined, business_street_address2?: (string | null) | undefined, business_city?: (string | null) | undefined, business_state?: (string | null) | undefined, business_legal_entity_identifier?: (string | null) | undefined}; export type CreateApiV1EntitiesCounterpartiesArgs = {body: CreateCounterpartyRequestDto}; export type CreateApiV1EntitiesCounterpartiesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const counterpartyData = z.object({id: z.string(), workspace_id: z.string(), classification: z.enum(["individual", "business", "wallet_cluster"]), name: z.string(), tenant_id: z.string().nullable().optional(), individual_first_name: z.string().nullable().optional(), individual_last_name: z.string().nullable().optional(), individual_address_country: z.string().nullable().optional(), individual_date_of_birth: z.string().nullable().optional(), individual_national_identification_number: z.string().nullable().optional(), individual_street_address1: z.string().nullable().optional(), individual_street_address2: z.string().nullable().optional(), individual_city: z.string().nullable().optional(), individual_state: z.string().nullable().optional(), individual_postal_code: z.string().nullable().optional(), business_legal_name: z.string().nullable().optional(), business_dba: z.string().nullable().optional(), business_address_country: z.string().nullable().optional(), business_street_address1: z.string().nullable().optional(), business_street_address2: z.string().nullable().optional(), business_city: z.string().nullable().optional(), business_state: z.string().nullable().optional(), business_legal_entity_identifier: z.string().nullable().optional(), created_by: z.enum(["CUSTOMER", "SYSTEM"]), created_at: z.string(), updated_at: z.string()}); export const createCounterpartyResponseDto = z.object({data: counterpartyData}); 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 new counterparty * * POST /v1/entities/counterparties */ export const createApiV1EntitiesCounterparties = async (args: CreateApiV1EntitiesCounterpartiesArgs, options?: CreateApiV1EntitiesCounterpartiesOptions) => { const {baseUrl = "https://api.tesser.xyz", 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 (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/entities/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(createCounterpartyResponseDto, await res.json()) };