import {z} from 'zod' export type Scope = 'Private' | 'Shared' | 'Public'; export type EdgeSource = 'Customer' | 'Media' | 'Customs' | 'PrewavePrediction'; export type TargetSuppliersFilter = {query?: (string | null) | undefined, collectionIds: Array, commodityIds: Array, targetTypeIds: Array, tiers: Array, scopes: Array, source?: (EdgeSource | null) | undefined, countries?: Array | undefined, hscodes: Array, lastShipment?: (string | null) | undefined, onlyDirectlySuppliers: boolean, countryIds: Array}; export type GetApiPublicV2SuppliersSystemIdTargetIdSupplierGraphArgs = {systemId: string, targetId: string, tierLevel?: number | undefined, commodityIds?: Array | undefined, filter: TargetSuppliersFilter, commodityTreeId?: number | undefined, fetchParents?: boolean | undefined}; export type GetApiPublicV2SuppliersSystemIdTargetIdSupplierGraphOptions = {baseUrl?: string | undefined, TokenAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const publicTargetInfoDTO = z.object({tier: z.number().int().nullable().optional(), path: z.array(z.number().int()), collectionLevel: z.number().int().nullable().optional(), collectionPath: z.array(z.number().int()), collectionPaths: z.array(z.array(z.number().int())).nullable().optional()}); export const publicHSCode = z.object({code: z.string(), nShipments: z.number().int().nullable().optional(), nshipments: z.number().int().optional()}); export const publicEdgeInfo = z.object({hsCodes: z.array(publicHSCode), probability: z.number().nullable().optional()}); export const publicTreeTargetV2: z.ZodType = z.object({target: z.number().int(), suppliers: z.array(z.lazy(() => publicTreeTargetV2)), targetInfo: publicTargetInfoDTO, edgeSource: z.string(), edgeInfo: publicEdgeInfo.nullable().optional(), merged_targets: z.array(z.number().int()).optional()}); export const getApiPublicV2SuppliersSystemIdTargetIdSupplierGraphResponse = z.array(publicTreeTargetV2); 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 target * * GET /public/v2/suppliers/{systemId}/{targetId}/supplier-graph */ export const getApiPublicV2SuppliersSystemIdTargetIdSupplierGraph = async (args: GetApiPublicV2SuppliersSystemIdTargetIdSupplierGraphArgs, options?: GetApiPublicV2SuppliersSystemIdTargetIdSupplierGraphOptions) => { 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.tierLevel != null) query.append("tierLevel", String(args.tierLevel)) for (const item of args.commodityIds ?? []) query.append("commodityIds", String(item)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.commodityTreeId != null) query.append("commodityTreeId", String(args.commodityTreeId)) if (args.fetchParents != null) query.append("fetchParents", String(args.fetchParents)) if (TokenAuthentication !== undefined) headers.set("X-Auth-Token", TokenAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/public/v2/suppliers/${encodeURIComponent(String(args.systemId))}/${encodeURIComponent(String(args.targetId))}/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(getApiPublicV2SuppliersSystemIdTargetIdSupplierGraphResponse, await res.json()) };