import {z} from 'zod' export type GetApiV1ProductsArgs = {page?: number | undefined, limit?: number | undefined, sort?: string | undefined, 'filter[code]'?: string | undefined}; export type GetApiV1ProductsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const product = z.object({_id: z.string().optional(), deleted: z.boolean().optional(), created_at: z.string().optional(), created_by: z.string().optional(), updated_at: z.string().optional(), updated_by: z.string().optional(), deleted_at: z.string().nullable().optional(), deleted_by: z.string().nullable().optional(), code: z.string().optional(), name: z.string().optional(), price: z.number().optional(), categories: z.array(z.string()).optional(), attributes: z.record(z.string(), z.unknown()).optional()}); export const paging = z.object({page: z.number().int().optional(), limit: z.number().int().optional(), total: z.number().int().optional(), pages: z.number().int().optional()}); export const productListResponse = z.object({data: z.array(product).optional(), paging: paging.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 products * * GET /v1/products */ export const getApiV1Products = async (args: GetApiV1ProductsArgs, options: GetApiV1ProductsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = 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.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args['filter[code]'] != null) query.append("filter[code]", String(args['filter[code]'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(productListResponse, await res.json()) };