import {z} from 'zod' export type GetApiCatalogsRulesArgs = {filter?: string | undefined, 'page[limit]'?: number | undefined, 'page[offset]'?: number | undefined}; export type GetApiCatalogsRulesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pageMeta = z.object({results: z.object({total: z.number().int().optional()}).optional(), page: z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), current: z.number().int().optional(), total: z.number().int().optional()}).optional()}); export const ruleSchedule = z.object({valid_from: z.string().nullable().optional(), valid_to: z.string().nullable().optional()}); export const prioritizedPricebooks = z.array(z.object({id: z.string(), priority: z.number().int()})); export const ruleMeta = z.object({similarity_score: z.number().int().nullable().optional(), active: z.boolean().optional(), resolved_for_shopper: z.boolean().optional(), release_id: z.string().nullable().optional()}); export const rule = z.object({id: z.string(), attributes: z.object({name: z.string(), description: z.string().optional(), account_ids: z.array(z.string()).optional(), account_tag_ids: z.array(z.string()).optional(), customer_ids: z.array(z.string()).optional(), channels: z.array(z.string()).optional(), tags: z.array(z.string()).optional(), schedules: z.array(ruleSchedule).optional(), catalog_id: z.string(), created_at: z.string(), updated_at: z.string(), pricebook_ids: prioritizedPricebooks.optional(), search_profile_slug: z.string().nullable().optional()}), type: z.literal("catalog_rule"), meta: ruleMeta.optional()}); export const links = z.object({self: z.string().nullable().optional(), first: z.string().nullable().optional(), last: z.string().nullable().optional(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}); export const ruleListData = z.object({meta: pageMeta.optional(), data: z.array(rule), links: links.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 }; /** * Gets all authorized catalog rules * * GET /catalogs/rules */ export const getApiCatalogsRules = async (args: GetApiCatalogsRulesArgs, options?: GetApiCatalogsRulesOptions) => { const {baseUrl = "https://euwest.api.elasticpath.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args['page[limit]'] != null) query.append("page[limit]", String(args['page[limit]'])) if (args['page[offset]'] != null) query.append("page[offset]", String(args['page[offset]'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/catalogs/rules${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(ruleListData, await res.json()) };