import {z} from 'zod' export type CustomerType = 'individual' | 'business'; export type CustomerStatus = 'ACTIVE' | 'INACTIVE'; export type UpdateCustomerDto = {name?: string | undefined, email?: string | undefined, type?: CustomerType | undefined, phone?: string | undefined, status?: CustomerStatus | undefined}; export type PatchApiV1CustomersCustomerIdArgs = {customerId: string, body: UpdateCustomerDto}; export type PatchApiV1CustomersCustomerIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customerType = z.enum(["individual", "business"]); export const customerStatus = z.enum(["ACTIVE", "INACTIVE"]); export const customer = z.object({id: z.string(), name: z.string().nullable().optional(), email: z.string(), phone: z.string().nullable().optional(), type: customerType.nullable().optional(), status: customerStatus, network: z.enum(["mainnet", "testnet"]), additionalData: z.object({}).optional(), createdAt: z.string(), updatedAt: z.string()}); export const customerResponse = z.object({statusCode: z.number().int(), message: z.string(), data: customer}); 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 Customer * * PATCH /v1/customers/{customerId} */ export const patchApiV1CustomersCustomerId = async (args: PatchApiV1CustomersCustomerIdArgs, options?: PatchApiV1CustomersCustomerIdOptions) => { const {baseUrl = "https://api.blockradar.co", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers/${encodeURIComponent(String(args.customerId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(customerResponse, await res.json()) };