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 CreateApiFulfillmentPlansSellerIdArgs = {sellerId: number, body: PlanWhenPost}; export type CreateApiFulfillmentPlansSellerIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const planThenGet = z.object({order: z.number().int().optional(), sku: z.string().optional(), quantity: z.string().optional(), warehouse: z.string().optional(), customsValue: z.string().optional(), splitId: z.string().optional(), lot: z.string().optional(), expirationDate: z.string().optional(), id: z.number().int().optional()}); export const planWhenGet = z.object({type: z.enum(["RULE", "PRE", "POST"]).optional(), order: z.number().int().optional(), sku: z.string().optional(), deliveryCountry: z.string().optional(), scriptedCondition: z.string().optional(), id: z.number().int().optional(), thens: z.array(planThenGet).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 }; /** * Create a new fulfillment plan entry for the given seller * * POST /fulfillmentPlans/{sellerId} */ export const createApiFulfillmentPlansSellerId = async (args: CreateApiFulfillmentPlansSellerIdArgs, options?: CreateApiFulfillmentPlansSellerIdOptions) => { 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))}${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(planWhenGet, await res.json()) };