import {z} from 'zod' export type GetApiCategoryHierarchiesFcwebArgs = {depth: number}; export type GetApiCategoryHierarchiesFcwebOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const listTreeNodeCategoryHierarchyNode = z.object({orNull: z.lazy(() => treeNodeCategoryHierarchyNode.optional()), async: z.boolean().optional(), empty: z.boolean().optional(), lazy: z.boolean().optional(), singleValued: z.boolean().optional(), distinct: z.boolean().optional(), ordered: z.boolean().optional(), sequential: z.boolean().optional(), traversableAgain: z.boolean().optional(), memoized: z.boolean().optional()}); export const category = z.object({isAlcohol: z.boolean().optional(), isMeat: z.boolean().optional(), isDonation: z.boolean().optional()}); export const categoryHierarchyNode = z.object({id: z.number().int(), hierarchyId: z.number().int(), parentId: z.number().int(), hierarchyCode: z.string(), sortOrder: z.number().int(), level: z.number().int(), category: category}); export const treeNodeCategoryHierarchyNode: z.ZodType = z.object({children: listTreeNodeCategoryHierarchyNode, value: categoryHierarchyNode, empty: z.boolean().optional(), leaf: z.boolean().optional()}); export const getApiCategoryHierarchiesFcwebResponse = z.array(treeNodeCategoryHierarchyNode); 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 }; /** * GET /category-hierarchies/fcweb */ export const getApiCategoryHierarchiesFcweb = async (args: GetApiCategoryHierarchiesFcwebArgs, options: GetApiCategoryHierarchiesFcwebOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.depth != null) query.append("depth", String(args.depth)) const url = `${baseUrl.replace(/\/+$/, '')}/category-hierarchies/fcweb${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(getApiCategoryHierarchiesFcwebResponse, await res.json()) };