import {z} from 'zod' export type PrioritizedPricebooks = Array<{id: string, priority: number}>; export type CatalogCreateData = {data: {attributes: {name: string, description?: (string | null) | undefined, hierarchy_ids: Array, pricebook_id?: string | undefined, pricebook_ids?: PrioritizedPricebooks | undefined, locales?: {[key: string]: {[key: string]: string}} | undefined, enable_price_segmentation?: boolean | undefined, include_draft_products?: boolean | undefined, max_published_releases?: (number | null) | undefined, allow_search_overrides?: (boolean | null) | undefined}, type: 'catalog'}}; export type CreateApiCatalogsArgs = {body: CatalogCreateData}; export type CreateApiCatalogsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const prioritizedPricebooks = z.array(z.object({id: z.string(), priority: z.number().int()})); export const relatedLink = z.object({related: z.string()}); export const catalog = z.object({id: z.string(), attributes: z.object({name: z.string(), description: z.string().optional(), hierarchy_ids: z.array(z.string()), pricebook_id: z.string().optional(), pricebook_ids: prioritizedPricebooks.optional(), locales: z.record(z.string(), z.record(z.string(), z.string())).optional(), created_at: z.string(), updated_at: z.string(), owner: z.enum(["store", "organization"]).nullable().optional(), enable_price_segmentation: z.boolean().optional(), include_draft_products: z.boolean().optional(), max_published_releases: z.number().int().nullable().optional(), allow_search_overrides: z.boolean().optional()}), relationships: z.object({rules: z.object({links: relatedLink.optional()}).optional(), releases: z.object({links: relatedLink.optional(), meta: z.object({count: z.number().int().optional()}).optional()}).optional()}).optional(), type: z.literal("catalog")}); 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 catalogData = z.object({data: catalog, 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 }; /** * Creates a new catalog * * POST /catalogs */ export const createApiCatalogs = async (args: CreateApiCatalogsArgs, options?: CreateApiCatalogsOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(catalogData, await res.json()) };