import {z} from 'zod' export type ChangeString = {present?: boolean | undefined, value?: string | undefined}; export type ChangeBoolean = {present?: boolean | undefined, value?: boolean | undefined}; export type MonetaryAmount = {currency?: ({currencyCode?: string | undefined, defaultFractionDigits?: number | undefined, displayName?: string | undefined, numericCode?: number | undefined, numericCodeAsString?: string | undefined, symbol?: string | undefined}) | undefined, value?: number | undefined}; export type ChangeMonetaryAmount = {present?: boolean | undefined, value?: MonetaryAmount | undefined}; export type UpdateProductPricing = {basePrice?: ChangeMonetaryAmount | undefined, onSale?: ChangeBoolean | undefined, salePrice?: ChangeMonetaryAmount | undefined}; export type ChangeUpdateProductPricing = {present?: boolean | undefined, value?: UpdateProductPricing | undefined}; export type ChangeListString = {present?: boolean | undefined, value?: Array | undefined}; export type SeoOptions = {description?: string | undefined, title?: string | undefined}; export type ChangeSeoOptions = {present?: boolean | undefined, value?: SeoOptions | undefined}; export type UpdateProductRequest = {description?: ChangeString | undefined, isVisible?: ChangeBoolean | undefined, name?: ChangeString | undefined, pricing?: ChangeUpdateProductPricing | undefined, productAttributeNames?: ChangeListString | undefined, seoData?: ChangeSeoOptions | undefined, tags?: ChangeListString | undefined, urlSlug?: ChangeString | undefined}; export type CreateApiV2CommerceProductsProductIdArgs = {productId: string, Authorization: string, 'User-Agent': string, body: UpdateProductRequest}; export type CreateApiV2CommerceProductsProductIdOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const productImage = z.object({altText: z.string().optional(), availableFormats: z.array(z.string()).optional(), id: z.string().optional(), orderIndex: z.number().int().optional(), originalSize: z.object({}).optional(), url: z.string().optional()}); export const seoOptions = z.object({description: z.string().optional(), title: z.string().optional()}); export const productTypeV2 = z.enum(["PHYSICAL", "SERVICE", "GIFT_CARD", "DIGITAL"]); export const productV2 = z.object({createdOn: z.string(), description: z.string().optional(), id: z.string(), images: z.array(productImage).optional(), isVisible: z.boolean().optional(), modifiedOn: z.string(), name: z.string().optional(), seoOptions: seoOptions.optional(), storePageId: z.string(), tags: z.array(z.string()).optional(), type: productTypeV2, url: z.string().optional(), urlSlug: z.string().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 }; /** * Update product * * POST /v2/commerce/products/{productId} */ export const createApiV2CommerceProductsProductId = async (args: CreateApiV2CommerceProductsProductIdArgs, options?: CreateApiV2CommerceProductsProductIdOptions) => { const {baseUrl = "https://api.squarespace.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/commerce/products/${encodeURIComponent(String(args.productId))}${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(productV2, await res.json()) };