import {z} from 'zod' export type GetApiSuppliersPublicPurchasedComponentsArgs = {search?: string | undefined, page?: string | undefined, page_size?: string | undefined}; export type GetApiSuppliersPublicPurchasedComponentsOptions = {baseUrl?: string | undefined, appId?: string | undefined, fetch?: typeof fetch | undefined}; export const purchasedComponentCustomProperty = z.object({key: z.string().optional(), value: z.union([z.string(), z.number(), z.boolean()]).nullable().optional()}); export const purchasedComponent = z.object({oem_part_number: z.string().optional(), internal_part_number: z.string().nullable().optional(), description: z.string().nullable().optional(), piece_price: z.number().optional(), id: z.number().int().optional(), properties: z.array(purchasedComponentCustomProperty).optional(), thumbnail_url: z.string().nullable().optional()}); export const getApiSuppliersPublicPurchasedComponentsResponse = z.array(purchasedComponent); 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 purchased components * * GET /suppliers/public/purchased_components */ export const getApiSuppliersPublicPurchasedComponents = async (args: GetApiSuppliersPublicPurchasedComponentsArgs, options?: GetApiSuppliersPublicPurchasedComponentsOptions) => { const {baseUrl = "https://api.paperlessparts.com/v1", fetch: fetchFn = fetch, appId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (appId !== undefined) headers.set("Authorization", appId) const url = `${baseUrl.replace(/\/+$/, '')}/suppliers/public/purchased_components${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(getApiSuppliersPublicPurchasedComponentsResponse, await res.json()) };