import {z} from 'zod' export type ManifestOrdersInputSerializer = {orderIds?: (Array | null) | undefined, externalOrderIds?: (Array | null) | undefined, providerIds?: (Array | null) | undefined}; export type CreateApiV1ManifestArgs = {body: ManifestOrdersInputSerializer}; export type CreateApiV1ManifestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const manifestDocumentSerializer = z.object({id: z.string(), url: z.string().nullable(), contentType: z.string()}); export const manifestResultSerializer = z.object({providerId: z.string(), storeLocationId: z.string().nullable().optional(), orderIds: z.array(z.string()), documents: z.array(manifestDocumentSerializer)}); export const manifestOrdersResponseSerializer = z.object({manifests: z.array(manifestResultSerializer)}); 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 }; /** * Manifest Orders * * POST /v1/manifest */ export const createApiV1Manifest = async (args: CreateApiV1ManifestArgs, options?: CreateApiV1ManifestOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/manifest${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(manifestOrdersResponseSerializer, await res.json()) };