import {z} from 'zod' export type GetApiApiV1CategoriesArgs = {query?: (string | null) | undefined, parent_category?: (number | null) | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiApiV1CategoriesOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const categorySchema = z.object({id: z.number().int().gte(1), name: z.string().max(255), path: z.string().max(255), parent_id: z.number().int().gte(1).nullable(), material_composition_type: z.enum(["textile", "non-textile"]).nullable(), parent: z.number().int().nullable().optional()}); export const pagination = z.object({per_page: z.number().int().gt(0), page: z.number().int().gt(0).nullable().optional(), pages: z.number().int().gte(0).nullable().optional(), total: z.number().int().gte(0).nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional(), next_cursor: z.string().nullable().optional()}); export const getCategoriesResponseSchema = z.object({items: z.array(categorySchema), pagination: pagination}); 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 Categories * * GET /api/v1/categories/ */ export const getApiApiV1Categories = async (args: GetApiApiV1CategoriesArgs, options: GetApiApiV1CategoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.parent_category != null) query.append("parent_category", String(args.parent_category)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/categories/${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(getCategoriesResponseSchema, await res.json()) };