import {z} from 'zod' export type CreateGenericAccountRequestDto = {tenant_id?: (string | null) | undefined, counterparty_id?: (string | null) | undefined, name: string, generic_account_number: string}; export type CreateApiV1AccountsGenericArgs = {body: CreateGenericAccountRequestDto}; export type CreateApiV1AccountsGenericOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const genericAccountData = z.object({id: z.string(), workspace_id: z.string(), tenant_id: z.string().nullable().optional(), counterparty_id: z.string().nullable().optional(), type: z.string(), name: z.string(), created_by: z.enum(["CUSTOMER", "SYSTEM"]), created_at: z.string(), updated_at: z.string(), generic_account_number: z.string()}); export const createGenericAccountResponseDto = z.object({data: genericAccountData}); 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 generic account * * POST /v1/accounts/generic */ export const createApiV1AccountsGeneric = async (args: CreateApiV1AccountsGenericArgs, options?: CreateApiV1AccountsGenericOptions) => { 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/accounts/generic${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(createGenericAccountResponseDto, await res.json()) };