export type GetApiCatalogProductsArgs = {q?: string | undefined, barcode?: string | undefined, category_ids?: Array | undefined, sku?: string | undefined, code?: string | undefined, mpn?: string | undefined, sort?: ('title' | '-title') | undefined}; export type GetApiCatalogProductsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; /** * Get Products * * GET /catalog/products */ export const getApiCatalogProducts = async (args: GetApiCatalogProductsArgs, options?: GetApiCatalogProductsOptions) => { const {baseUrl = "https://api.roapp.io/v2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() if (args.q != null) query.append("q", String(args.q)) if (args.barcode != null) query.append("barcode", String(args.barcode)) for (const item of args.category_ids ?? []) query.append("category_ids", String(item)) if (args.sku != null) query.append("sku", String(args.sku)) if (args.code != null) query.append("code", String(args.code)) if (args.mpn != null) query.append("mpn", String(args.mpn)) if (args.sort != null) query.append("sort", String(args.sort)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/catalog/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 undefined };