import {z} from 'zod' export type ExternalProductV2SaleState = 'FOR_SALE' | 'SALES_PAUSED'; export type ExternalProductV2LifecycleState = 'DRAFT' | 'PUBLISHED' | 'UNPUBLISHED' | 'DELETED'; export type ExternalImageV2 = {id?: string | undefined, width?: number | undefined, height?: number | undefined, sequence?: number | undefined, url?: string | undefined, original_url?: string | undefined, tags?: Array | undefined}; export type ExternalProductVariantOptionV2 = {name?: string | undefined, value?: string | undefined}; 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 ExternalProductVariantV2VariantPreorderDetails = {expected_ship_date?: string | undefined, expected_ship_window_end_date?: string | undefined, stop_selling_at?: number | undefined}; export type ExternalMassUnitV2 = 'GRAMS' | 'KILOGRAMS' | 'OUNCES' | 'POUNDS'; export type ExternalDistanceUnitV2 = 'CENTIMETERS' | 'INCHES' | 'FEET' | 'MILLIMETERS' | 'METERS' | 'YARDS'; export type ExternalMeasurementsV2 = {mass_unit?: ExternalMassUnitV2 | undefined, weight?: number | undefined, distance_unit?: ExternalDistanceUnitV2 | undefined, length?: number | undefined, width?: number | undefined, height?: number | undefined}; export type ExternalProductVariantV2VariantOrderabilityType = 'IMMEDIATE' | 'PREORDER'; export type ExternalProductVariantV2 = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, product_id?: string | undefined, name?: string | undefined, sale_state?: ExternalProductV2SaleState | undefined, lifecycle_state?: ExternalProductV2LifecycleState | undefined, idempotence_token?: string | undefined, sku?: string | undefined, available_quantity?: number | undefined, backordered_until?: string | undefined, wholesale_price_cents?: number | undefined, retail_price_cents?: number | undefined, tariff_code?: string | undefined, images?: Array | undefined, options?: Array | undefined, prices?: Array | undefined, variant_preorder_details?: ExternalProductVariantV2VariantPreorderDetails | undefined, measurements?: ExternalMeasurementsV2 | undefined, gtin?: string | undefined, orderability_type?: ExternalProductVariantV2VariantOrderabilityType | undefined, case_measurements?: ExternalMeasurementsV2 | undefined}; export type ExternalProductVariantOptionDefinitionV2 = {name?: string | undefined, values?: Array | undefined}; export type ExternalTaxonomyTypeV2 = {id?: string | undefined, name?: string | undefined}; export type ExternalProductV2PreorderDetails = {order_by_date?: string | undefined, keep_active_past_order_by_date?: boolean | undefined, expected_ship_date?: string | undefined, expected_ship_window_end_date?: string | undefined}; export type ExternalProductTaxonomyAttributeV2 = {name?: string | undefined, value?: string | undefined}; export type ExternalPublicationValidationErrorV2 = {error_code?: string | undefined, field_path?: string | undefined, message?: string | undefined}; export type ExternalProductV2 = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, brand_id?: string | undefined, name?: string | undefined, description?: string | undefined, short_description?: string | undefined, sale_state?: ExternalProductV2SaleState | undefined, lifecycle_state?: ExternalProductV2LifecycleState | undefined, variants?: Array | undefined, idempotence_token?: string | undefined, unit_multiplier?: number | undefined, minimum_order_quantity?: number | undefined, per_style_minimum_order_quantity?: number | undefined, allow_sales_when_out_of_stock?: boolean | undefined, images?: Array | undefined, variant_option_sets?: Array | undefined, taxonomy_type?: ExternalTaxonomyTypeV2 | undefined, preorderable?: boolean | undefined, preorder_details?: ExternalProductV2PreorderDetails | undefined, product_attributes?: Array | undefined, made_in_country?: string | undefined, publication_warnings?: Array | undefined}; export type CreateApiProductsArgs = {body: ExternalProductV2}; export type CreateApiProductsOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, AppCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const externalProductV2SaleState = z.enum(["FOR_SALE", "SALES_PAUSED"]); export const externalProductV2LifecycleState = z.enum(["DRAFT", "PUBLISHED", "UNPUBLISHED", "DELETED"]); export const externalImageV2 = z.object({id: z.string().optional(), width: z.number().int().optional(), height: z.number().int().optional(), sequence: z.number().int().optional(), url: z.string().optional(), original_url: z.string().optional(), tags: z.array(z.string()).optional()}); export const externalProductVariantOptionV2 = z.object({name: z.string().optional(), value: z.string().optional()}); 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 externalProductVariantV2VariantPreorderDetails = z.object({expected_ship_date: z.string().optional(), expected_ship_window_end_date: z.string().optional(), stop_selling_at: z.number().int().optional()}); export const externalMassUnitV2 = z.enum(["GRAMS", "KILOGRAMS", "OUNCES", "POUNDS"]); export const externalDistanceUnitV2 = z.enum(["CENTIMETERS", "INCHES", "FEET", "MILLIMETERS", "METERS", "YARDS"]); export const externalMeasurementsV2 = z.object({mass_unit: externalMassUnitV2.optional(), weight: z.number().optional(), distance_unit: externalDistanceUnitV2.optional(), length: z.number().optional(), width: z.number().optional(), height: z.number().optional()}); export const externalProductVariantV2VariantOrderabilityType = z.enum(["IMMEDIATE", "PREORDER"]); export const externalProductVariantV2 = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), product_id: z.string().optional(), name: z.string().optional(), sale_state: externalProductV2SaleState.optional(), lifecycle_state: externalProductV2LifecycleState.optional(), idempotence_token: z.string().optional(), sku: z.string().optional(), available_quantity: z.number().int().optional(), backordered_until: z.string().optional(), wholesale_price_cents: z.number().int().optional(), retail_price_cents: z.number().int().optional(), tariff_code: z.string().optional(), images: z.array(externalImageV2).optional(), options: z.array(externalProductVariantOptionV2).optional(), prices: z.array(externalProductVariantV2Price).optional(), variant_preorder_details: externalProductVariantV2VariantPreorderDetails.optional(), measurements: externalMeasurementsV2.optional(), gtin: z.string().optional(), orderability_type: externalProductVariantV2VariantOrderabilityType.optional(), case_measurements: externalMeasurementsV2.optional()}); export const externalProductVariantOptionDefinitionV2 = z.object({name: z.string().optional(), values: z.array(z.string()).optional()}); export const externalTaxonomyTypeV2 = z.object({id: z.string().optional(), name: z.string().optional()}); export const externalProductV2PreorderDetails = z.object({order_by_date: z.string().optional(), keep_active_past_order_by_date: z.boolean().optional(), expected_ship_date: z.string().optional(), expected_ship_window_end_date: z.string().optional()}); export const externalProductTaxonomyAttributeV2 = z.object({name: z.string().optional(), value: z.string().optional()}); export const externalPublicationValidationErrorV2 = z.object({error_code: z.string().optional(), field_path: z.string().optional(), message: z.string().optional()}); export const externalProductV2 = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), brand_id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), short_description: z.string().optional(), sale_state: externalProductV2SaleState.optional(), lifecycle_state: externalProductV2LifecycleState.optional(), variants: z.array(externalProductVariantV2).optional(), idempotence_token: z.string().optional(), unit_multiplier: z.number().int().optional(), minimum_order_quantity: z.number().int().optional(), per_style_minimum_order_quantity: z.number().int().optional(), allow_sales_when_out_of_stock: z.boolean().optional(), images: z.array(externalImageV2).optional(), variant_option_sets: z.array(externalProductVariantOptionDefinitionV2).optional(), taxonomy_type: externalTaxonomyTypeV2.optional(), preorderable: z.boolean().optional(), preorder_details: externalProductV2PreorderDetails.optional(), product_attributes: z.array(externalProductTaxonomyAttributeV2).optional(), made_in_country: z.string().optional(), publication_warnings: z.array(externalPublicationValidationErrorV2).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 }; /** * POST /products * * POST /products */ export const createApiProducts = async (args: CreateApiProductsArgs, options?: CreateApiProductsOptions) => { 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(/\/+$/, '')}/products${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(externalProductV2, await res.json()) };