import {z} from 'zod' export type GetApiV1JobsArgs = {limit?: number | undefined, offset?: number | undefined, sortBy?: string | undefined, sortDirection?: string | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined, deliveryStatuses?: (string | null) | undefined, providerNames?: (string | null) | undefined, courierNames?: (string | null) | undefined, tags?: (string | null) | undefined, packageTags?: (string | null) | undefined, pickupStoreLocationIds?: (string | null) | undefined, dropoffStoreLocationIds?: (string | null) | undefined, search?: (string | null) | undefined, shortReferenceCode?: (string | null) | undefined, customerName?: (string | null) | undefined, isBatch?: (boolean | null) | undefined, hasNotes?: (boolean | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined}; export type GetApiV1JobsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jobLocationSummary = z.object({businessName: z.string().nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), address: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional()}); export const jobDeliverySummary = z.object({id: z.string().nullable().optional(), providerName: z.string().nullable().optional(), status: z.string().nullable().optional(), courierName: z.string().nullable().optional(), trackingUrl: z.string().nullable().optional(), priceCents: z.number().int().nullable().optional(), currency: z.string().nullable().optional()}); export const jobListItem = z.object({id: z.string(), externalIdentifier: z.string().nullable().optional(), shortReferenceCode: z.string().nullable().optional(), status: z.string().nullable().optional(), isBatch: z.boolean().nullable().optional(), createdAt: z.string().nullable().optional(), routeId: z.string().nullable().optional(), portalUrl: z.string().nullable().optional(), pickup: jobLocationSummary.nullable().optional(), dropoff: jobLocationSummary.nullable().optional(), delivery: jobDeliverySummary.nullable().optional()}); export const jobsListResponse = z.object({results: z.array(jobListItem), totalCount: z.number().int(), limit: z.number().int(), offset: z.number().int()}); 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 }; /** * List/Search Jobs * * GET /v1/jobs */ export const getApiV1Jobs = async (args: GetApiV1JobsArgs, options?: GetApiV1JobsOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.deliveryStatuses != null) query.append("deliveryStatuses", String(args.deliveryStatuses)) if (args.providerNames != null) query.append("providerNames", String(args.providerNames)) if (args.courierNames != null) query.append("courierNames", String(args.courierNames)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.packageTags != null) query.append("packageTags", String(args.packageTags)) if (args.pickupStoreLocationIds != null) query.append("pickupStoreLocationIds", String(args.pickupStoreLocationIds)) if (args.dropoffStoreLocationIds != null) query.append("dropoffStoreLocationIds", String(args.dropoffStoreLocationIds)) if (args.search != null) query.append("search", String(args.search)) if (args.shortReferenceCode != null) query.append("shortReferenceCode", String(args.shortReferenceCode)) if (args.customerName != null) query.append("customerName", String(args.customerName)) if (args.isBatch != null) query.append("isBatch", String(args.isBatch)) if (args.hasNotes != null) query.append("hasNotes", String(args.hasNotes)) if (args.city != null) query.append("city", String(args.city)) if (args.state != null) query.append("state", String(args.state)) if (args.country != null) query.append("country", String(args.country)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/jobs${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(jobsListResponse, await res.json()) };