import {z} from 'zod' export type GetApiV1PurchaseOrdersArgs = {purchaseOrderNumber: string}; export type GetApiV1PurchaseOrdersOptions = {baseUrl?: string | undefined, httpBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const purchaseOrderStatus = z.enum(["OPEN", "CLOSED", "CANCELED"]); export const purchaseOrderLineItemResponse = z.object({id: z.string(), lineNumber: z.string(), sku: z.string().nullable().optional(), description: z.string().nullable().optional(), orderedQuantity: z.number().int().nullable().optional(), openQuantity: z.number().int().nullable().optional(), unitOfMeasure: z.string().nullable().optional(), unitPrice: z.string().nullable().optional(), currency: z.string(), status: purchaseOrderStatus.optional(), expectedArrivalDate: z.string().nullable().optional(), customData: z.object({}).nullable().optional()}); export const purchaseOrderResponse = z.object({id: z.string(), purchaseOrderNumber: z.string(), internalPurchaseOrderNumber: z.string().nullable().optional(), status: purchaseOrderStatus, purchaseOrderDate: z.string(), customData: z.object({}).nullable().optional(), lineItems: z.array(purchaseOrderLineItemResponse), createdAt: z.string(), updatedAt: 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 }; /** * Get purchase order by number * * GET /v1/purchase-orders */ export const getApiV1PurchaseOrders = async (args: GetApiV1PurchaseOrdersArgs, options?: GetApiV1PurchaseOrdersOptions) => { const {baseUrl = "https://api.prod.goaugment.com", fetch: fetchFn = fetch, httpBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.purchaseOrderNumber != null) query.append("purchaseOrderNumber", String(args.purchaseOrderNumber)) if (httpBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${httpBasic.username}:${httpBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/purchase-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(purchaseOrderResponse, await res.json()) };