import {z} from 'zod' export type GetApiCustomersIdOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const country = z.object({id: z.string().optional(), name: z.string().optional(), code: z.string().optional(), has_regulation: z.boolean().optional(), currencies: z.array(z.string()).optional()}); export const state = z.object({id: z.number().int().optional(), name: z.string().optional(), country: z.string().optional()}); export const contact = z.object({id: z.string().optional(), customer: z.string().nullable().optional(), orgunit: z.string().nullable().optional(), name: z.string().max(64), email: z.string(), phone: z.string().max(12).nullable().optional(), invoicing: z.boolean().optional(), collections: z.boolean().optional(), created: z.string().optional(), updated: z.string().optional()}); export const aggregations = z.object({sales: z.number().optional(), sales_current_period: z.number().optional(), balance: z.number().optional()}); export const customer = z.object({id: z.string().optional(), name: z.string().max(100), address: z.string().max(64), country: country, state: state, currency: z.string().optional(), email: z.string().nullable().optional(), phone: z.string().max(16).nullable().optional(), website: z.string().nullable().optional(), zip_code: z.string().max(16).nullable().optional(), manager: z.string().max(64).nullable().optional(), reference: z.string().max(64).nullable().optional(), tax_id: z.string().max(32).nullable().optional(), tax_settings: z.object({}).optional(), metadata: z.object({}).optional(), send_invoices: z.boolean().optional(), send_collections: z.boolean().optional(), send_vouchers: z.boolean().optional(), status: z.enum(["active", "archived", "pending"]).optional(), contacts: z.array(contact).optional(), sources: z.array(z.string()).optional(), aggregations: aggregations.optional(), test_mode: z.boolean().optional(), created: z.string().optional(), updated: z.string().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 }; /** * Retrieve customer * * GET /customers/{id}/ */ export const getApiCustomersId = async (options?: GetApiCustomersIdOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };