import {z} from 'zod' export type GetApiProductsAttributesAttributeIdTermsArgs = {attribute_id: number, context?: ('view' | 'embed') | undefined, page?: number | undefined, per_page?: number | undefined, search?: string | undefined, exclude?: Array | undefined, include?: Array | undefined, offset?: number | undefined, order?: ('asc' | 'desc') | undefined, orderby?: ('id' | 'include' | 'name' | 'slug' | 'term_group' | 'description' | 'count' | 'menu_order') | undefined, hide_empty?: boolean | undefined, product?: number | undefined, slug?: string | undefined}; export type GetApiProductsAttributesAttributeIdTermsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const productAttributeTerm = z.object({id: z.number().int().optional(), name: z.string().optional(), slug: z.string().optional(), description: z.string().optional(), menu_order: z.number().int().optional(), count: z.number().int().optional()}); export const getApiProductsAttributesAttributeIdTermsResponse = z.array(productAttributeTerm); 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 product attribute terms * * GET /products/attributes/{attribute_id}/terms */ export const getApiProductsAttributesAttributeIdTerms = async (args: GetApiProductsAttributesAttributeIdTermsArgs, options?: GetApiProductsAttributesAttributeIdTermsOptions) => { const {baseUrl = "https://example-store.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.context != null) query.append("context", String(args.context)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.exclude ?? []) query.append("exclude", String(item)) for (const item of args.include ?? []) query.append("include", String(item)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.order != null) query.append("order", String(args.order)) if (args.orderby != null) query.append("orderby", String(args.orderby)) if (args.hide_empty != null) query.append("hide_empty", String(args.hide_empty)) if (args.product != null) query.append("product", String(args.product)) if (args.slug != null) query.append("slug", String(args.slug)) const url = `${baseUrl.replace(/\/+$/, '')}/products/attributes/${encodeURIComponent(String(args.attribute_id))}/terms${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(getApiProductsAttributesAttributeIdTermsResponse, await res.json()) };