import {z} from 'zod' export type StrategyAddressCreate = {street: string, number: string, complement?: (string | null) | undefined, district: string, city: string, state: string, country: string, zip_code: string}; export type StrategyContactCreate = {name?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined}; export type StrategyOperatingHourCreate = {day: string, is_24h?: boolean | undefined, opens_at?: (string | null) | undefined, closes_at?: (string | null) | undefined}; export type StrategyHolidayHoursCreate = {is_24h?: boolean | undefined, opens_at?: (string | null) | undefined, closes_at?: (string | null) | undefined}; export type StrategyCreate = {code: string, name: string, description?: (string | null) | undefined, type: 'STORE' | 'DC' | 'DARK_STORE' | 'SELLER' | 'FULFILLMENT_NODE', category: 'SALES' | 'OPERATIONS' | 'SALES_AND_OPERATIONS', active: boolean, daily_capacity: number, sfs?: boolean | undefined, endless_aisle?: boolean | undefined, operates_on_holidays?: boolean | undefined, cluster_id?: (string | null) | undefined, state_machine_id?: (string | null) | undefined, color?: string | undefined, address?: StrategyAddressCreate | undefined, contact?: StrategyContactCreate | undefined, operating_hours?: Array | undefined, holiday_operating_hours?: (StrategyHolidayHoursCreate | null) | undefined, tags?: Array | undefined, metadata?: Record | undefined}; export type CreateApiApiV1StrategiesArgs = {body: StrategyCreate}; export type CreateApiApiV1StrategiesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const strategyCreateResponse = z.object({id: z.string(), code: z.string(), name: z.string(), description: z.string().nullable(), type: z.enum(["STORE", "DC", "DARK_STORE", "SELLER", "FULFILLMENT_NODE"]), category: z.enum(["SALES", "OPERATIONS", "SALES_AND_OPERATIONS"]), active: z.boolean(), sfs: z.boolean(), endless_aisle: z.boolean(), operates_on_holidays: z.boolean(), cluster_id: z.string().nullable(), state_machine_id: z.string().nullable(), color: z.string().nullable(), daily_capacity: z.number(), address: z.object({street: z.string(), number: z.string(), complement: z.string().nullable(), district: z.string(), city: z.string(), state: z.string(), country: z.string(), zip_code: z.string()}), contact: z.object({name: z.string().nullable(), email: z.string().nullable(), phone: z.string().nullable()}), operating_hours: z.array(z.object({day: z.string(), is_24h: z.boolean(), opens_at: z.string().nullable(), closes_at: z.string().nullable()})), holiday_operating_hours: z.object({is_24h: z.boolean(), opens_at: z.string().nullable(), closes_at: z.string().nullable()}), tags: z.array(z.string()).nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: 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 }; /** * Create strategy * * POST /api/v1/strategies * * Also accepts auth: bearer */ export const createApiApiV1Strategies = async (args: CreateApiApiV1StrategiesArgs, options?: CreateApiApiV1StrategiesOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/strategies${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(strategyCreateResponse, await res.json()) };