import {z} from 'zod' export type GetApiApiV1StatusesArgs = {entityType: string, page?: number | undefined, limit?: number | undefined, sortBy?: string | undefined, sortOrder?: string | undefined}; export type GetApiApiV1StatusesOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const statusListItemResponse = z.object({id: z.string(), entity_type: z.enum(["ORDER", "FULFILLMENT_ORDER"]), code: z.string(), label: z.string(), description: z.string(), nature: z.enum(["INITIAL", "PROCESSING", "FINAL", "ERROR"]), color: z.string(), origin: z.enum(["KRUZER", "CUSTOM"]), active: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const paginationMetaResponse = z.object({total: z.number(), pages: z.number(), current_page: z.number(), next_page: z.number().nullable(), previous_page: z.number().nullable(), limit: z.number()}); export const statusListResponse = z.object({data: z.array(statusListItemResponse), meta: paginationMetaResponse}); 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 }; /** * List statuses * * GET /api/v1/statuses * * Also accepts auth: bearer */ export const getApiApiV1Statuses = async (args: GetApiApiV1StatusesArgs, options?: GetApiApiV1StatusesOptions) => { 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 (args.entityType != null) query.append("entityType", String(args.entityType)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/statuses${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(statusListResponse, await res.json()) };