import {z} from 'zod' export type GetApiApiProductsAttributesArgs = {hierarchy?: string | undefined, max_level?: number | undefined, channels?: Array | undefined, with_roles?: boolean | undefined}; export type GetApiApiProductsAttributesOptions = {baseUrl?: string | undefined, FrontBearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const pM11Response200AttributesChannels = z.object({code: z.string()}); export const pM11Response200AttributesDescriptionTranslations = z.object({locale: z.string(), value: z.string()}); export const pM11Response200AttributesLabelTranslations = z.object({locale: z.string(), value: z.string()}); export const pM11Response200AttributesRolesParameters = z.object({name: z.string(), value: z.string()}); export const pM11Response200AttributesRoles = z.object({parameters: z.array(pM11Response200AttributesRolesParameters), type: z.string()}); export const pM11Response200AttributesTypeParameters = z.object({name: z.string(), value: z.string()}); export const pM11Response200AttributesValues = z.object({code: z.string()}); export const pM11Response200Attributes = z.object({channels: z.array(pM11Response200AttributesChannels).optional(), code: z.string(), default_value: z.string().optional(), description: z.string().optional(), description_translations: z.array(pM11Response200AttributesDescriptionTranslations), example: z.string().optional(), hierarchy_code: z.string().optional(), label: z.string().optional(), label_translations: z.array(pM11Response200AttributesLabelTranslations), locale: z.string().optional(), required: z.boolean(), requirement_level: z.string(), roles: z.array(pM11Response200AttributesRoles), transformations: z.string().optional(), type: z.string(), type_parameter: z.string(), type_parameters: z.array(pM11Response200AttributesTypeParameters).optional(), unique_code: z.string().optional(), validations: z.string().optional(), values: z.array(pM11Response200AttributesValues).optional(), values_list: z.string().optional(), variant: z.boolean()}); export const pM11Response200 = z.object({attributes: z.array(pM11Response200Attributes)}); 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 }; /** * PM11 - Get the product attribute configuration * * GET /api/products/attributes * * Also accepts auth: OAuth-2 */ export const getApiApiProductsAttributes = async (args: GetApiApiProductsAttributesArgs, options?: GetApiApiProductsAttributesOptions) => { const {baseUrl = "https://your-instance.mirakl.net", fetch: fetchFn = fetch, FrontBearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hierarchy != null) query.append("hierarchy", String(args.hierarchy)) if (args.max_level != null) query.append("max_level", String(args.max_level)) for (const item of args.channels ?? []) query.append("channels", String(item)) if (args.with_roles != null) query.append("with_roles", String(args.with_roles)) if (FrontBearerToken !== undefined) headers.set('authorization', `Bearer ${FrontBearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/products/attributes${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(pM11Response200, await res.json()) };