import {z} from 'zod' export type GetApiApiV1StrategiesIdArgs = {id: string}; export type GetApiApiV1StrategiesIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const strategyFindResponse = 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(), cluster: z.object({name: z.string(), color: z.string().nullable()}), state_machine_id: z.string().nullable(), state_machine: z.object({id: z.string(), code: z.string(), name: z.string(), entity_type: z.enum(["ORDER", "FULFILLMENT_ORDER"]), is_default: z.boolean(), active: z.boolean(), transitions: z.array(z.object({from: z.string(), to: z.string()})).optional(), states: z.array(z.object({status: z.string(), actions: z.array(z.string())})).optional()}), 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 }; /** * Fetch strategy * * GET /api/v1/strategies/{id} * * Also accepts auth: bearer */ export const getApiApiV1StrategiesId = async (args: GetApiApiV1StrategiesIdArgs, options?: GetApiApiV1StrategiesIdOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/strategies/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(strategyFindResponse, await res.json()) };