import {z} from 'zod' export type GetApiOrderStatusArgs = {search?: string | undefined, isSystemDefined?: boolean | undefined, sortBy?: ('status' | 'createdAt' | 'isSystemDefined') | undefined, sortType?: ('ASC' | 'DESC') | undefined, page: number, size: number}; export type GetApiOrderStatusOptions = {baseUrl?: string | undefined, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const orderStatusResponseDto = z.object({id: z.number(), status: z.string(), backgroundColor: z.string().optional(), textColor: z.string().optional(), borderColor: z.string().optional(), isSystemDefined: z.boolean(), createdByName: z.string().optional()}); export const orderStatusListResponseDto = z.object({totalItems: z.number(), items: z.array(orderStatusResponseDto), page: z.number(), size: z.number()}); 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 }; /** * Get order statuses with filters * * GET /order-status */ export const getApiOrderStatus = async (args: GetApiOrderStatusArgs, options?: GetApiOrderStatusOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.isSystemDefined != null) query.append("isSystemDefined", String(args.isSystemDefined)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortType != null) query.append("sortType", String(args.sortType)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/order-status${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(orderStatusListResponseDto, await res.json()) };