import {z} from 'zod' export type PlanThenPost = {order?: number | undefined, sku?: string | undefined, quantity?: string | undefined, warehouse?: string | undefined, customsValue?: string | undefined, splitId?: string | undefined, lot?: string | undefined, expirationDate?: string | undefined}; export type PlanWhenPost = {type?: ('RULE' | 'PRE' | 'POST') | undefined, order?: number | undefined, sku?: string | undefined, deliveryCountry?: string | undefined, scriptedCondition?: string | undefined, thens?: Array | undefined}; export type PreviewRequest = {orderId?: number | undefined, excludeWhenIds?: Array | undefined, when?: PlanWhenPost | undefined}; export type CreateApiFulfillmentPlansSellerIdPreviewArgs = {sellerId: number, body: PreviewRequest}; export type CreateApiFulfillmentPlansSellerIdPreviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const previewFulfillmentOrderPosition = z.object({sku: z.string().optional(), warehouseSku: z.string().optional(), quantity: z.number().int().optional(), lot: z.string().optional(), customsValue: z.number().optional(), expirationDate: z.string().optional()}); export const previewFulfillmentOrder = z.object({warehouse: z.string().optional(), positions: z.array(previewFulfillmentOrderPosition).optional()}); export const createApiFulfillmentPlansSellerIdPreviewResponse = z.array(previewFulfillmentOrder); 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 }; /** * Previews the results of a fulfillment plan entry for the given seller * * POST /fulfillmentPlans/{sellerId}/preview */ export const createApiFulfillmentPlansSellerIdPreview = async (args: CreateApiFulfillmentPlansSellerIdPreviewArgs, options?: CreateApiFulfillmentPlansSellerIdPreviewOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/fulfillmentPlans/${encodeURIComponent(String(args.sellerId))}/preview${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(createApiFulfillmentPlansSellerIdPreviewResponse, await res.json()) };