import {z} from 'zod' export type ProductCatalogTypeEnum = 'regular' | 'bundle'; export type GetApiPublicV2ProductCatalogItemsSearchArgs = {page?: number | undefined, per_page?: number | undefined, query?: string | undefined, order_by?: ('sku' | '-sku' | 'title' | '-title' | 'price' | '-price' | 'date_modified' | '-date_modified') | undefined, types?: Array | undefined, billing_types?: (Array<'one_time' | 'recurring'>) | undefined, exclude_uuids?: Array | undefined, category_id?: string | undefined, no_category?: boolean | undefined}; export type GetApiPublicV2ProductCatalogItemsSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const productCatalogTypeEnum = z.enum(["regular", "bundle"]); export const productCatalogSearchCatalogItemResponse = z.object({billing_cycle: z.number().int().nullable().optional(), billing_type: z.string().optional(), bundle_items_count: z.number().int().optional(), workspace_id: z.string().optional(), category_id: z.string().nullable().optional(), category_name: z.string().nullable().optional(), created_by: z.string().optional(), cost: z.number().nullable().optional(), currency: z.string().optional(), custom_fields: z.array(z.object({name: z.string().optional(), value: z.string().nullable().optional()})).nullable().optional(), date_created: z.string().optional(), date_modified: z.string().nullable().optional(), description: z.string().nullable().optional(), image_src: z.string().nullable().optional(), images: z.array(z.object({is_main: z.boolean().nullable().optional(), order: z.number().optional(), src: z.string().optional()})).nullable().optional(), max_tier_value: z.number().nullable().optional(), min_tier_value: z.number().nullable().optional(), modified_by: z.string().optional(), price: z.number().nullable().optional(), pricing_method: z.number().int().optional(), sku: z.string().optional(), title: z.string().optional(), tiers: z.array(z.object({min_qty: z.number().optional(), value: z.number().optional()})).nullable().optional(), type: productCatalogTypeEnum.optional(), uuid: z.string().optional(), highlights: z.record(z.string(), z.object({})).nullable().optional()}); export const listCatalogItemsSearchResponse = z.object({items: z.array(productCatalogSearchCatalogItemResponse).optional(), has_more_items: z.boolean().optional(), total: z.number().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 }; /** * List Catalog Items Search * * GET /public/v2/product-catalog/items/search * * Also accepts auth: oauth2 */ export const getApiPublicV2ProductCatalogItemsSearch = async (args: GetApiPublicV2ProductCatalogItemsSearchArgs, options?: GetApiPublicV2ProductCatalogItemsSearchOptions) => { const {baseUrl = "https://api.pandadoc.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.query != null) query.append("query", String(args.query)) if (args.order_by != null) query.append("order_by", String(args.order_by)) for (const item of args.types ?? []) query.append("types", String(item)) for (const item of args.billing_types ?? []) query.append("billing_types", String(item)) for (const item of args.exclude_uuids ?? []) query.append("exclude_uuids", String(item)) if (args.category_id != null) query.append("category_id", String(args.category_id)) if (args.no_category != null) query.append("no_category", String(args.no_category)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/v2/product-catalog/items/search${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(listCatalogItemsSearchResponse, await res.json()) };