import {z} from 'zod' export type PriceInput = {currency: string, unit_price: number | string, tax_rate?: (number | string | null) | undefined, billing?: ('one_time' | 'recurring') | undefined, interval?: ('weekly' | 'biweekly' | 'monthly' | 'quarterly' | 'semiannual' | 'yearly' | null) | undefined, nickname?: (string | null) | undefined, lookup_key?: (string | null) | undefined, external_source?: (string | null) | undefined, external_id?: (string | null) | undefined}; export type ProductCreate = {name: string, description?: (string | null) | undefined, kind?: ('service' | 'product') | undefined, unit?: (string | null) | undefined, custom_fields?: ({[key: string]: unknown} | null) | undefined, fiscal_refs?: ({[key: string]: string} | null) | undefined, prices?: Array | undefined, origin?: ('local' | 'imported' | null) | undefined, external_source?: (string | null) | undefined, external_id?: (string | null) | undefined}; export type CreateApiApiProductsArgs = {'X-Workspace-Id'?: (string | null) | undefined, body: ProductCreate}; export type CreateApiApiProductsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const priceRead = z.object({id: z.string(), product_id: z.string(), currency: z.string(), unit_price: z.string(), tax_rate: z.string().nullable().optional(), billing: z.enum(["one_time", "recurring"]), interval: z.enum(["weekly", "biweekly", "monthly", "quarterly", "semiannual", "yearly"]).nullable().optional(), nickname: z.string().nullable().optional(), lookup_key: z.string().nullable().optional(), active: z.boolean(), external_source: z.string().nullable().optional(), external_id: z.string().nullable().optional(), created_at: z.string()}); export const productRead = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), kind: z.enum(["service", "product"]), unit: z.string().nullable().optional(), active: z.boolean(), origin: z.string(), external_source: z.string().nullable().optional(), external_id: z.string().nullable().optional(), custom_fields: z.record(z.string(), z.unknown()).nullable().optional(), fiscal_refs: z.record(z.string(), z.string()).nullable().optional(), prices: z.array(priceRead).optional(), created_at: z.string(), invoice_count: z.number().int().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 }; /** * Create Product * * POST /api/products */ export const createApiApiProducts = async (args: CreateApiApiProductsArgs, options: CreateApiApiProductsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/products${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(productRead, await res.json()) };