import {z} from 'zod' export type ExternalPrepackItemV2 = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, variant_id?: string | undefined, quantity?: number | undefined}; export type ExternalPrepackV2LifecycleState = 'DRAFT' | 'PUBLISHED' | 'UNPUBLISHED'; export type ExternalPrepackV2 = {id?: string | undefined, idempotence_token?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, name?: string | undefined, description?: string | undefined, items?: Array | undefined, lifecycle_state?: ExternalPrepackV2LifecycleState | undefined}; export type CreateApiProductsProductIdPrepacksArgs = {product_id: string, body: ExternalPrepackV2}; export type CreateApiProductsProductIdPrepacksOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, AppCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const externalPrepackItemV2 = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), variant_id: z.string().optional(), quantity: z.number().int().optional()}); export const externalPrepackV2LifecycleState = z.enum(["DRAFT", "PUBLISHED", "UNPUBLISHED"]); export const externalPrepackV2 = z.object({id: z.string().optional(), idempotence_token: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), description: z.string().optional(), items: z.array(externalPrepackItemV2).optional(), lifecycle_state: externalPrepackV2LifecycleState.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/{product_id}/prepacks * * POST /products/{product_id}/prepacks */ export const createApiProductsProductIdPrepacks = async (args: CreateApiProductsProductIdPrepacksArgs, options?: CreateApiProductsProductIdPrepacksOptions) => { 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/${encodeURIComponent(String(args.product_id))}/prepacks${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(externalPrepackV2, await res.json()) };