import {z} from 'zod' export type GetApiApiProductsArgs = {active?: (boolean | null) | undefined, kind?: (string | null) | undefined, q?: (string | null) | undefined, 'X-Workspace-Id'?: (string | null) | undefined}; export type GetApiApiProductsOptions = {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 getApiApiProductsResponse = z.array(productRead); 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 }; /** * List Products * * GET /api/products */ export const getApiApiProducts = async (args: GetApiApiProductsArgs, options: GetApiApiProductsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.active != null) query.append("active", String(args.active)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.q != null) query.append("q", String(args.q)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiProductsResponse, await res.json()) };