import {z} from 'zod' export type PublicOutputFormat = 'JSON' | 'CSV'; export type EdgeSource = 'Customer' | 'Media' | 'Customs' | 'PrewavePrediction'; export type GetApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraphArgs = {collectionId: number, format?: PublicOutputFormat | undefined, includeHeaderMetadata?: boolean | undefined, includeCommodityFilter?: boolean | undefined, includeMediaSources?: boolean | undefined, includeShipmentDetails?: boolean | undefined, tier?: number | undefined, source?: EdgeSource | undefined, lastShipment?: string | undefined, hsCodes?: Array | undefined}; export type GetApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraphOptions = {baseUrl?: string | undefined, TokenAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const edgeSource = z.enum(["Customer", "Media", "Customs", "PrewavePrediction"]); export const publicSupplierGraphShipment = z.object({hscode: z.number().int(), nShipments: z.number().int().nullable().optional(), lastShipment: z.string().nullable().optional(), nshipments: z.number().int().optional()}); export const publicSupplierGraphEntry = z.object({tier: z.number().int(), supplierId: z.number().int(), customerId: z.number().int(), source: edgeSource, probability: z.number().int().nullable().optional(), mediaSources: z.array(z.string()).nullable().optional(), nShipments: z.number().int().nullable().optional(), shipments: z.array(publicSupplierGraphShipment).nullable().optional(), nshipments: z.number().int().optional()}); export const getApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraphResponse = z.array(publicSupplierGraphEntry); 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 a tree-structure of suppliers of the given collection * * GET /public/v3/alpha/suppliers/collection/{collectionId}/supplier-graph */ export const getApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraph = async (args: GetApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraphArgs, options?: GetApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraphOptions) => { const {baseUrl = "https://api.prewave.com", fetch: fetchFn = fetch, TokenAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) if (args.includeHeaderMetadata != null) query.append("includeHeaderMetadata", String(args.includeHeaderMetadata)) if (args.includeCommodityFilter != null) query.append("includeCommodityFilter", String(args.includeCommodityFilter)) if (args.includeMediaSources != null) query.append("includeMediaSources", String(args.includeMediaSources)) if (args.includeShipmentDetails != null) query.append("includeShipmentDetails", String(args.includeShipmentDetails)) if (args.tier != null) query.append("tier", String(args.tier)) if (args.source != null) query.append("source", String(args.source)) if (args.lastShipment != null) query.append("lastShipment", String(args.lastShipment)) for (const item of args.hsCodes ?? []) query.append("hsCodes", String(item)) if (TokenAuthentication !== undefined) headers.set("X-Auth-Token", TokenAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/public/v3/alpha/suppliers/collection/${encodeURIComponent(String(args.collectionId))}/supplier-graph${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(getApiPublicV3AlphaSuppliersCollectionCollectionIdSupplierGraphResponse, await res.json()) };