import {z} from 'zod' export type OrderType = 10 | 20 | 40 | 90; export type GetApiV1OrdersArgs = {OrderType?: OrderType | undefined, ExcludeCompleted?: boolean | undefined, VendorAccountId?: number | undefined, Filter?: string | undefined, Page?: number | undefined, PageSize?: number | undefined, SortColumn?: string | undefined, SortOrder?: string | undefined, Account: unknown}; export type GetApiV1OrdersOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const orderType = z.union([z.literal(10), z.literal(20), z.literal(40), z.literal(90)]); export const orderStatus = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]); export const orderListResponse = z.object({id: z.number().int().optional(), orderNumber: z.string().nullable().optional(), externalOrderNumber: z.string().nullable().optional(), orderType: orderType.optional(), description: z.string().nullable().optional(), orderDate: z.string().optional(), orderStatus: orderStatus.optional(), statusDate: z.string().optional(), buyerAccountId: z.number().int().nullable().optional(), buyerAccountName: z.string().nullable().optional(), vendorAccountId: z.number().int().nullable().optional(), vendorAccountName: z.string().nullable().optional(), isReplenishmentOrder: z.boolean().optional(), createdOn: z.string().nullable().optional(), modifiedOn: z.string().nullable().optional(), locationName: z.string().nullable().optional()}); export const orderListResponsePagedResult = z.object({pageSize: z.number().int().optional(), page: z.number().int().optional(), totalCount: z.number().int().optional(), filteredCount: z.number().int().optional(), data: z.array(orderListResponse).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 }; /** * GET /v1/Orders */ export const getApiV1Orders = async (args: GetApiV1OrdersArgs, options: GetApiV1OrdersOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.OrderType != null) query.append("OrderType", String(args.OrderType)) if (args.ExcludeCompleted != null) query.append("ExcludeCompleted", String(args.ExcludeCompleted)) if (args.VendorAccountId != null) query.append("VendorAccountId", String(args.VendorAccountId)) if (args.Filter != null) query.append("Filter", String(args.Filter)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.SortColumn != null) query.append("SortColumn", String(args.SortColumn)) if (args.SortOrder != null) query.append("SortOrder", String(args.SortOrder)) if (args.Account != null) query.append("Account", String(args.Account)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Orders${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(orderListResponsePagedResult, await res.json()) };