import {z} from 'zod' export type EdgeSource = 'Customer' | 'Media' | 'Customs' | 'PrewavePrediction'; export type GetApiPublicV1CollectionsCollectionIdTierNDataArgs = {collectionId: number, includeCommodityFilter?: boolean | undefined, includeMediaSources?: boolean | undefined, includeShipmentDetails?: boolean | undefined, tier?: number | undefined, source?: EdgeSource | undefined, lastShipment?: string | undefined, hsCodes?: string | undefined, maxTier?: number | undefined, scopePrivate?: boolean | undefined, scopeShared?: boolean | undefined, scopePublic?: boolean | undefined, sourceCustomer?: boolean | undefined, sourceCustoms?: boolean | undefined, sourceMedia?: boolean | undefined, sourcePrewavePrediction?: boolean | undefined, minShipments?: number | undefined, probability?: number | undefined, shipmentsPeriodFrom?: string | undefined, shipmentsPeriodTo?: string | undefined, suspectedSuppliers?: boolean | undefined, logisticsProvider?: boolean | undefined}; export type GetApiPublicV1CollectionsCollectionIdTierNDataOptions = {baseUrl?: string | undefined, TokenAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const edgeSource = z.enum(["Customer", "Media", "Customs", "PrewavePrediction"]); export const publicTierNCollectionGraphShipment = z.object({hscode: z.number().int(), nShipments: z.number().int().nullable().optional(), lastShipment: z.string().nullable().optional(), nshipments: z.number().int().optional()}); export const publicTierNCollectionGraphEntry = z.object({tier: z.number().int(), supplierTargetId: z.number().int(), customerTargetId: z.number().int(), source: edgeSource.nullable().optional(), probability: z.number().int().nullable().optional(), mediaSources: z.array(z.string()).nullable().optional(), nShipments: z.number().int().nullable().optional(), shipments: publicTierNCollectionGraphShipment.nullable().optional(), nshipments: z.number().int().optional()}); export const getApiPublicV1CollectionsCollectionIdTierNDataResponse = z.array(publicTierNCollectionGraphEntry); 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 tier-n collection supplier graph * * GET /public/v1/collections/{collectionId}/tier-n/data */ export const getApiPublicV1CollectionsCollectionIdTierNData = async (args: GetApiPublicV1CollectionsCollectionIdTierNDataArgs, options?: GetApiPublicV1CollectionsCollectionIdTierNDataOptions) => { 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.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)) if (args.hsCodes != null) query.append("hsCodes", String(args.hsCodes)) if (args.maxTier != null) query.append("maxTier", String(args.maxTier)) if (args.scopePrivate != null) query.append("scopePrivate", String(args.scopePrivate)) if (args.scopeShared != null) query.append("scopeShared", String(args.scopeShared)) if (args.scopePublic != null) query.append("scopePublic", String(args.scopePublic)) if (args.sourceCustomer != null) query.append("sourceCustomer", String(args.sourceCustomer)) if (args.sourceCustoms != null) query.append("sourceCustoms", String(args.sourceCustoms)) if (args.sourceMedia != null) query.append("sourceMedia", String(args.sourceMedia)) if (args.sourcePrewavePrediction != null) query.append("sourcePrewavePrediction", String(args.sourcePrewavePrediction)) if (args.minShipments != null) query.append("minShipments", String(args.minShipments)) if (args.probability != null) query.append("probability", String(args.probability)) if (args.shipmentsPeriodFrom != null) query.append("shipmentsPeriodFrom", String(args.shipmentsPeriodFrom)) if (args.shipmentsPeriodTo != null) query.append("shipmentsPeriodTo", String(args.shipmentsPeriodTo)) if (args.suspectedSuppliers != null) query.append("suspectedSuppliers", String(args.suspectedSuppliers)) if (args.logisticsProvider != null) query.append("logisticsProvider", String(args.logisticsProvider)) if (TokenAuthentication !== undefined) headers.set("X-Auth-Token", TokenAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/collections/${encodeURIComponent(String(args.collectionId))}/tier-n/data${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(getApiPublicV1CollectionsCollectionIdTierNDataResponse, await res.json()) };