import {z} from 'zod' export type CountryCode = string; export type Address = {line1: string, line2?: (string | null) | undefined, city: string, state?: (string | null) | undefined, zip_code: string, country: CountryCode}; export type CustomerAddress = {id?: string | undefined, line1: string, line2?: (string | null) | undefined, city: string, state?: (string | null) | undefined, zip_code: string, country: CountryCode}; export type CustomerAddresses = Array; export type CustomerEmail = {id?: string | undefined, email: string, type: 'MAIN_SENDER' | 'CC' | 'BCC'}; export type ExternalReference = {type: string, id: string, url?: string | undefined}; export type FieldAssignments = Array<{field_id: string, field_value_id: string}>; export type CustomerTaxId = {type: 'EU_VAT_REGISTRATION_NUMBER' | 'GENERIC_VAT_NUMBER', value: string}; export type CustomerTaxIds = Array; export type CustomerRequest = {name: string, name_on_invoice?: string | undefined, address?: Address | undefined, shipping_address?: Address | undefined, billing_addresses?: (CustomerAddresses | null) | undefined, shipping_addresses?: (CustomerAddresses | null) | undefined, emails?: Array | undefined, external_references?: Array | undefined, payment_terms?: number | undefined, send_invoices_automatically?: boolean | undefined, send_payment_reminders?: boolean | undefined, allow_online_payments?: boolean | undefined, fields?: (FieldAssignments | null) | undefined, tax_ids?: (CustomerTaxIds | null) | undefined}; export type CreateApiCustomersArgs = {body: CustomerRequest}; export type CreateApiCustomersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const countryCode = z.string(); export const address = z.object({line1: z.string(), line2: z.string().nullable().optional(), city: z.string(), state: z.string().nullable().optional(), zip_code: z.string(), country: countryCode}); export const customerAddress = z.object({id: z.string().optional(), line1: z.string(), line2: z.string().nullable().optional(), city: z.string(), state: z.string().nullable().optional(), zip_code: z.string(), country: countryCode}); export const customerAddresses = z.array(customerAddress); export const customerEmail = z.object({id: z.string().optional(), email: z.string(), type: z.enum(["MAIN_SENDER", "CC", "BCC"])}); export const externalReference = z.object({type: z.string(), id: z.string(), url: z.string().optional()}); export const fieldAssignments = z.array(z.object({field_id: z.string(), field_value_id: z.string()})); export const customerTaxId = z.object({type: z.enum(["EU_VAT_REGISTRATION_NUMBER", "GENERIC_VAT_NUMBER"]), value: z.string().min(1)}); export const customerTaxIds = z.array(customerTaxId); export const customer = z.object({id: z.string(), updated_at: z.string(), metadata: z.record(z.string(), z.string()).optional(), family_id: z.string().nullable().optional(), parent_customer_id: z.string().nullable().optional(), name: z.string(), name_on_invoice: z.string().optional(), address: address.optional(), shipping_address: address.optional(), billing_addresses: customerAddresses.nullable().optional(), shipping_addresses: customerAddresses.nullable().optional(), emails: z.array(customerEmail).optional(), external_references: z.array(externalReference).optional(), payment_terms: z.number().int().gte(0).optional(), send_invoices_automatically: z.boolean().optional(), send_payment_reminders: z.boolean().optional(), allow_online_payments: z.boolean().optional(), fields: fieldAssignments.nullable().optional(), tax_ids: customerTaxIds.nullable().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 }; /** * Creates a customer * * POST /customers */ export const createApiCustomers = async (args: CreateApiCustomersArgs, options?: CreateApiCustomersOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers${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(customer, await res.json()) };