import {z} from 'zod' export type ExternalProductVariantV2PricePriceGeoConstraint = {country?: string | undefined, country_group?: string | undefined}; export type ExternalMoneyV2 = {amount_minor?: number | undefined, currency?: string | undefined}; export type ExternalProductVariantV2Price = {geo_constraint?: ExternalProductVariantV2PricePriceGeoConstraint | undefined, wholesale_price?: ExternalMoneyV2 | undefined, retail_price?: ExternalMoneyV2 | undefined}; export type UpdateProductPricesBySkusRequestV2ProductVariantPriceBySku = {sku?: string | undefined, prices?: Array | undefined}; export type UpdateProductPricesBySkusRequestV2 = {prices?: Array | undefined}; export type PatchApiProductPricesBySkusArgs = {body: UpdateProductPricesBySkusRequestV2}; export type PatchApiProductPricesBySkusOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, AppCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const externalProductVariantV2PricePriceGeoConstraint = z.object({country: z.string().optional(), country_group: z.string().optional()}); export const externalMoneyV2 = z.object({amount_minor: z.number().int().optional(), currency: z.string().optional()}); export const externalProductVariantV2Price = z.object({geo_constraint: externalProductVariantV2PricePriceGeoConstraint.optional(), wholesale_price: externalMoneyV2.optional(), retail_price: externalMoneyV2.optional()}); export const updateProductPricesResponseV2PriceUpdateResult = z.object({prices: z.array(externalProductVariantV2Price).optional()}); export const updateProductPricesResponseV2 = z.object({results: z.record(z.string(), updateProductPricesResponseV2PriceUpdateResult).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 }; /** * PATCH /product-prices/by-skus * * PATCH /product-prices/by-skus */ export const patchApiProductPricesBySkus = async (args: PatchApiProductPricesBySkusArgs, options?: PatchApiProductPricesBySkusOptions) => { const {baseUrl = "https://www.faire.com/external-api/v2", fetch: fetchFn = fetch, OAuth, AppCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth !== undefined) headers.set("X-FAIRE-OAUTH-ACCESS-TOKEN", OAuth) if (AppCredentials !== undefined) headers.set("X-FAIRE-APP-CREDENTIALS", AppCredentials) const url = `${baseUrl.replace(/\/+$/, '')}/product-prices/by-skus${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(updateProductPricesResponseV2, await res.json()) };