import {z} from 'zod' export type V3ContactAddress = {addressLine1?: string | undefined, addressLine2?: string | undefined, city?: string | undefined, region?: string | undefined, stateCode?: string | undefined, postalCode?: string | undefined, country?: string | undefined}; export type V3ContactIndividualEntity = {type: 'INDIVIDUAL', relationshipType: 'THIRD_PARTY' | 'SELF_OWNED', firstName: string, middleName?: string | undefined, lastName: string, dateOfBirth?: string | undefined, address?: V3ContactAddress | undefined}; export type V3ContactCompanyEntity = {type: 'COMPANY', relationshipType: 'THIRD_PARTY' | 'SELF_OWNED', legalName: string, registrationNumber?: string | undefined, dateOfIncorporation?: string | undefined, address?: V3ContactAddress | undefined}; export type V3CreateContactRequest = {description?: string | undefined, category?: 'DEFAULT' | undefined, entity: V3ContactIndividualEntity | V3ContactCompanyEntity}; export type UpdateApiPlatformV3ContactsContactIdArgs = {contactId: string, body: V3CreateContactRequest}; export type UpdateApiPlatformV3ContactsContactIdOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const v3ContactAddress = z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), stateCode: z.string().min(2).max(2).optional(), postalCode: z.string().optional(), country: z.string().min(2).max(2).optional()}); export const v3ContactIndividualEntity = z.object({type: z.literal("INDIVIDUAL"), relationshipType: z.enum(["THIRD_PARTY", "SELF_OWNED"]), firstName: z.string(), middleName: z.string().optional(), lastName: z.string(), dateOfBirth: z.string().optional(), address: v3ContactAddress.optional()}); export const v3ContactCompanyEntity = z.object({type: z.literal("COMPANY"), relationshipType: z.enum(["THIRD_PARTY", "SELF_OWNED"]), legalName: z.string(), registrationNumber: z.string().optional(), dateOfIncorporation: z.string().optional(), address: v3ContactAddress.optional()}); export const v3ContactResponse = z.object({id: z.string().optional(), description: z.string().optional(), category: z.string().optional(), entity: z.discriminatedUnion("type", [v3ContactIndividualEntity, v3ContactCompanyEntity]).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); 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 }; /** * Update contact * * PUT /platform/v3/contacts/{contactId} */ export const updateApiPlatformV3ContactsContactId = async (args: UpdateApiPlatformV3ContactsContactIdArgs, options?: UpdateApiPlatformV3ContactsContactIdOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/platform/v3/contacts/${encodeURIComponent(String(args.contactId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(v3ContactResponse, await res.json()) };