import {z} from 'zod' export type GetApiV2ManifestsManifestIdOptions = {baseUrl?: string | undefined, apiKeys?: string | undefined, fetch?: typeof fetch | undefined}; export const seId = z.string().min(1).max(25); export const url = z.string().min(1); export const manifestDownload = z.object({href: url.optional()}); export const getManifestByIdResponseBody = z.object({manifest_id: seId.optional(), form_id: seId.optional(), created_at: z.string().min(1).optional(), ship_date: z.string().min(1).optional(), shipments: z.number().int().gte(1).optional(), label_ids: z.array(seId).optional(), warehouse_id: seId.optional(), submission_id: z.string().min(1).optional(), carrier_id: seId.optional(), manifest_download: manifestDownload.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 manifest by ID * * GET /v2/manifests/{manifest_id} */ export const getApiV2ManifestsManifestId = async (options?: GetApiV2ManifestsManifestIdOptions) => { const {baseUrl = "https://api.shipstation.com", fetch: fetchFn = fetch, apiKeys} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeys !== undefined) headers.set("api-key", apiKeys) const url = `${baseUrl.replace(/\/+$/, '')}/v2/manifests/${encodeURIComponent(String(args.manifest_id))}${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(getManifestByIdResponseBody, await res.json()) };