import {z} from 'zod' export type UpdateProductStateV1Request = {state: 'PUBLIC' | 'HIDDEN'}; export type UpdateApiOpenApiV10ProductsProductIdStateArgs = {productId: string, body: UpdateProductStateV1Request}; export type UpdateApiOpenApiV10ProductsProductIdStateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const comFourthwallOpenapiModelOfferAbstractV1OfferStateV1Available = z.object({type: z.literal("AVAILABLE")}); export const comFourthwallOpenapiModelOfferAbstractV1OfferStateV1SoldOut = z.object({type: z.literal("SOLD_OUT")}); export const comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Archived = z.object({type: z.literal("ARCHIVED")}); export const comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Hidden = z.object({type: z.literal("HIDDEN")}); export const comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Private = z.object({type: z.literal("PRIVATE")}); export const comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Public = z.object({type: z.literal("PUBLIC")}); export const comFourthwallOpenapiModelImage = z.object({id: z.string(), url: z.string(), width: z.number().int(), height: z.number().int(), transformedUrl: z.string()}); export const money = z.object({value: z.number().gte(0), currency: z.string()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1AttributesColor = z.object({name: z.string(), swatch: z.string()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1AttributesSize = z.object({name: z.string()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1Attributes = z.object({description: z.string(), color: comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1AttributesColor.optional(), size: comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1AttributesSize.optional()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1StockLimited = z.object({type: z.literal("LIMITED"), inStock: z.number().int()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1StockUnlimited = z.object({type: z.literal("UNLIMITED")}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1Weight = z.object({value: z.number(), unit: z.string()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1Dimensions = z.object({length: z.number(), width: z.number(), height: z.number(), unit: z.string()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1OfferVariantV1 = z.object({id: z.string(), name: z.string(), sku: z.string(), unitPrice: money, compareAtPrice: money.optional(), unitCost: money.optional(), attributes: comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1Attributes, creatorDeclaredCost: money.optional(), stock: z.union([comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1StockLimited, comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1StockUnlimited]), weight: comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1Weight, dimensions: comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1Dimensions, images: z.array(comFourthwallOpenapiModelImage), thumbnailImage: comFourthwallOpenapiModelImage.optional()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferAdditionalInformationV1 = z.object({sizeAndFit: z.string().optional(), guaranteeAndReturns: z.string().optional(), moreDetails: z.string().optional()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferSizeGuideV1 = z.object({previewUrl: z.string().optional(), fileUrl: z.string().optional(), description: z.string().optional(), fitGuideDescription: z.string().optional(), fitGuideUrls: z.array(z.string())}); export const comFourthwallOpenapiModelOfferDigitalFileV1 = z.object({uri: z.string(), name: z.string(), fileSize: z.string().optional(), mediaType: z.string().optional()}); export const comFourthwallOpenapiModelOfferAbstractV1OfferFullV1 = z.object({id: z.string(), name: z.string(), slug: z.string(), description: z.string(), type: z.enum(["STANDARD", "GIFT_CARD", "UNKNOWN"]), state: z.union([comFourthwallOpenapiModelOfferAbstractV1OfferStateV1Available, comFourthwallOpenapiModelOfferAbstractV1OfferStateV1SoldOut]), access: z.union([comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Archived, comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Hidden, comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Private, comFourthwallOpenapiModelOfferAbstractV1OfferAccessV1Public]), thumbnailImage: comFourthwallOpenapiModelImage.optional(), images: z.array(comFourthwallOpenapiModelImage), variants: z.array(comFourthwallOpenapiModelOfferAbstractV1OfferVariantAbstractV1OfferVariantV1), additionalInformation: comFourthwallOpenapiModelOfferAbstractV1OfferAdditionalInformationV1.optional(), sizeGuide: comFourthwallOpenapiModelOfferAbstractV1OfferSizeGuideV1.optional(), digitalFiles: z.array(comFourthwallOpenapiModelOfferDigitalFileV1).optional(), createdAt: z.string(), updatedAt: z.string()}); 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 (offer) lifecycle state * * PUT /open-api/v1.0/products/{productId}/state * * Also accepts auth: basicAuth */ export const updateApiOpenApiV10ProductsProductIdState = async (args: UpdateApiOpenApiV10ProductsProductIdStateArgs, options?: UpdateApiOpenApiV10ProductsProductIdStateOptions) => { const {baseUrl = "https://api.fourthwall.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(/\/+$/, '')}/open-api/v1.0/products/${encodeURIComponent(String(args.productId))}/state${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(comFourthwallOpenapiModelOfferAbstractV1OfferFullV1, await res.json()) };