import {z} from 'zod' export type RuleSchedule = {valid_from?: (string | null) | undefined, valid_to?: (string | null) | undefined}; export type PrioritizedPricebooks = Array<{id: string, priority: number}>; export type RuleUpdateData = {data: {id: string, attributes?: ({name?: (string | null) | undefined, description?: (string | null) | undefined, account_ids?: (Array | null) | undefined, account_tag_ids?: (Array | null) | undefined, customer_ids?: (Array | null) | undefined, channels?: (Array | null) | undefined, schedules?: (Array | null) | undefined, tags?: (Array | null) | undefined, catalog_id?: (string | null) | undefined, pricebook_ids?: PrioritizedPricebooks | undefined, search_profile_slug?: (string | null) | undefined}) | undefined, type: 'catalog_rule'}}; export type UpdateApiCatalogsRulesCatalogRuleIdArgs = {catalog_rule_id: string, body: RuleUpdateData}; export type UpdateApiCatalogsRulesCatalogRuleIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 ruleData = z.object({data: 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 }; /** * Updates a catalog rule * * PUT /catalogs/rules/{catalog_rule_id} */ export const updateApiCatalogsRulesCatalogRuleId = async (args: UpdateApiCatalogsRulesCatalogRuleIdArgs, options?: UpdateApiCatalogsRulesCatalogRuleIdOptions) => { const {baseUrl = "https://euwest.api.elasticpath.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/catalogs/rules/${encodeURIComponent(String(args.catalog_rule_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ruleData, await res.json()) };