import {z} from 'zod' export type UserConsent = {consent?: boolean | undefined, updated_at?: string | undefined}; export type CustomerAddress = {address_line: string, address_line_2?: string | undefined, postal_code?: string | undefined, postal_place: string, country: unknown, latitude?: number | undefined, longitude?: number | undefined, type?: ('custom' | 'home' | 'other' | 'work') | undefined, custom_type?: string | undefined, comment?: string | undefined}; export type CustomerTerm = {id?: string | undefined, accepted_at?: string | undefined}; export type CreateApiAccountsAidCustomersUsersArgs = {aid: unknown, body: {metadata?: Record | undefined, id?: string | undefined, created_at?: string | undefined, created_by?: string | undefined, updated_at?: string | undefined, deleted_by?: string | undefined, deleted_at?: string | undefined, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, email?: (string | null) | undefined, phone_number?: (unknown | null) | undefined, attributes?: Record | undefined, status?: (string | null) | undefined, favorite_store?: (string | null) | undefined, enrolled_by?: ({type?: string | undefined, value: string | null}) | undefined, marketing_consent?: ({sms?: UserConsent | undefined, email?: UserConsent | undefined} | {[key: string]: UserConsent}) | undefined, type: 'customer' | 'company' | 'contact' | 'employee' | 'other', addresses?: Array | undefined, term?: CustomerTerm | undefined, company?: ({organization_number?: string | undefined, bussiness_name: string, department?: string | undefined, industry?: string | undefined, website?: string | undefined, number_of_employees?: string | undefined}) | undefined, gender?: (string | null) | undefined, date_of_birth?: (string | null) | undefined, contact_for?: Array<{customer_id: string}> | undefined, customer_id?: string | undefined, password?: string | undefined, pin?: string | undefined}}; export type CreateApiAccountsAidCustomersUsersOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const userConsent = z.object({consent: z.boolean().optional(), updated_at: z.string().optional()}); export const customerAddress = z.object({address_line: z.string(), address_line_2: z.string().optional(), postal_code: z.string().optional(), postal_place: z.string(), country: z.string(), latitude: z.number().optional(), longitude: z.number().optional(), type: z.enum(["custom", "home", "other", "work"]).optional(), custom_type: z.string().optional(), comment: z.string().optional()}); export const customerTerm = z.object({id: z.string().optional(), accepted_at: z.string().optional()}); export const customerResponse = z.object({metadata: z.object({}).optional(), id: z.string().optional(), created_at: z.string().optional(), created_by: z.string().optional(), updated_at: z.string().optional(), deleted_by: z.string().optional(), deleted_at: z.string().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), email: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), attributes: z.object({}).optional(), status: z.string().nullable().optional(), favorite_store: z.string().nullable().optional(), enrolled_by: z.object({type: z.string().optional(), value: z.string().nullable()}).optional(), marketing_consent: z.object({sms: userConsent.optional(), email: userConsent.optional()}).and(z.record(z.string(), userConsent)).optional(), type: z.enum(["customer", "company", "contact", "employee", "other"]), addresses: z.array(customerAddress).optional(), term: customerTerm.optional(), company: z.object({organization_number: z.string().optional(), bussiness_name: z.string(), department: z.string().optional(), industry: z.string().optional(), website: z.string().optional(), number_of_employees: z.string().optional()}).optional(), gender: z.string().nullable().optional(), date_of_birth: z.string().nullable().optional(), contact_for: z.array(z.object({customer_id: z.string()})).optional(), customer_id: z.string().max(255)}); 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 }; /** * aid_customers_post * * POST /accounts/{aid}/customers/users */ export const createApiAccountsAidCustomersUsers = async (args: CreateApiAccountsAidCustomersUsersArgs, options?: CreateApiAccountsAidCustomersUsersOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/customers/users${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(customerResponse, await res.json()) };