import {z} from 'zod' export type GetApiApiV1ExternalSourcesArgs = {status?: ('awaiting_upload' | 'pending' | 'processing' | 'complete' | 'failed') | undefined, carrierId?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiV1ExternalSourcesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const externalSourceSummary = z.object({id: z.string().optional(), filename: z.string().optional(), status: z.enum(["awaiting_upload", "pending", "processing", "complete", "failed"]).optional(), sourceType: z.enum(["contract", "spot", "tariff"]).optional(), carrierId: z.string().optional(), carrierName: z.string().optional(), rateCount: z.number().int().optional(), effectiveDate: z.string().optional(), expiryDate: z.string().optional(), createdAt: z.string().optional(), processedAt: z.string().optional()}); export const getApiApiV1ExternalSourcesResponse = z.object({success: z.boolean().optional(), data: z.object({sources: z.array(externalSourceSummary).optional(), total: z.number().int().optional()}).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 }; /** * List all sources * * GET /api/v1/external/sources */ export const getApiApiV1ExternalSources = async (args: GetApiApiV1ExternalSourcesArgs, options?: GetApiApiV1ExternalSourcesOptions) => { const {baseUrl = "https://api.wove.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.carrierId != null) query.append("carrierId", String(args.carrierId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/external/sources${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(getApiApiV1ExternalSourcesResponse, await res.json()) };