import {z} from 'zod' export type OptionsGroupInputMixin = {id?: (string | null) | undefined, label: string, allowedProviders?: (Array | null) | undefined, enableAutoDispatch?: (boolean | null) | undefined, providerSelectionStrategy?: ('lowest_price' | 'preferred_order' | null) | undefined, reassignOnFailure?: (boolean | null) | undefined, enableMaxDeliveryFee?: (boolean | null) | undefined, maxDeliveryFeeCents?: (number | null) | undefined, preventReassignmentPriceIncrease?: (boolean | null) | undefined, costSplitStrategy?: ('none' | 'price' | 'percentage' | null) | undefined, costSplitMaxPercentage?: (number | null) | undefined, costSplitMaxFixedPriceCents?: (number | null) | undefined, smartReassignment?: (boolean | null) | undefined}; export type CreateApiV1OptionsGroupArgs = {body: OptionsGroupInputMixin}; export type CreateApiV1OptionsGroupOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const optionsGroupInputMixin = z.object({id: z.string().nullable().optional(), label: z.string().max(80), allowedProviders: z.array(z.string()).nullable().optional(), enableAutoDispatch: z.boolean().nullable().optional(), providerSelectionStrategy: z.enum(["lowest_price", "preferred_order"]).nullable().optional(), reassignOnFailure: z.boolean().nullable().optional(), enableMaxDeliveryFee: z.boolean().nullable().optional(), maxDeliveryFeeCents: z.number().int().nullable().optional(), preventReassignmentPriceIncrease: z.boolean().nullable().optional(), costSplitStrategy: z.enum(["none", "price", "percentage"]).nullable().optional(), costSplitMaxPercentage: z.number().int().nullable().optional(), costSplitMaxFixedPriceCents: z.number().int().nullable().optional(), smartReassignment: z.boolean().nullable().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 dispatch strategy * * POST /v1/options_group */ export const createApiV1OptionsGroup = async (args: CreateApiV1OptionsGroupArgs, options?: CreateApiV1OptionsGroupOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", 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(/\/+$/, '')}/v1/options_group${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(optionsGroupInputMixin, await res.json()) };