import {z} from 'zod' export type CreateApiV0OrganizationsOrganizationIdCopRequestArgs = {'organization-id': string, body: {'account-number': string, 'bank-id': string, 'account-classification': 'personal' | 'business', name: string, 'requester-legal-person-url': string | string, 'secondary-reference-data'?: string | undefined}}; export type CreateApiV0OrganizationsOrganizationIdCopRequestOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV0OrganizationsOrganizationIdCopRequestResponse = z.object({"cop-request-url": z.union([z.string(), z.string()]), account: z.object({"account-number": z.string().min(8).max(8), "bank-id": z.string().min(6).max(6), "account-classification": z.enum(["personal", "business"]), name: z.string(), "requester-legal-person-url": z.union([z.string(), z.string()]), "secondary-reference-data": z.string().optional()}), "cop-response": z.object({status: z.enum(["verified", "unverified"]), result: z.enum(["match", "close-match", "no-match"]), reason: z.enum(["no-match-name", "account-not-supported", "match-name-business-personal", "match-name-personal-business", "close-match-name-business-personal", "no-match-secondary-ref", "close-match-name", "account-switched", "close-match-name-personal-business"]).optional(), "suggested-name": z.string().optional(), "suggested-account-classification": z.enum(["personal", "business"]).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 }; /** * Create a CoP request * * POST /v0/organizations/{organization-id}/cop-request */ export const createApiV0OrganizationsOrganizationIdCopRequest = async (args: CreateApiV0OrganizationsOrganizationIdCopRequestArgs, options?: CreateApiV0OrganizationsOrganizationIdCopRequestOptions) => { const {baseUrl = "https://api.griffin.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("Authorization", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v0/organizations/${encodeURIComponent(String(args['organization-id']))}/cop-request${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(createApiV0OrganizationsOrganizationIdCopRequestResponse, await res.json()) };