import {z} from 'zod' export type SupplyChainAggregateAssetsBody = {count_by_level?: string | undefined, field: string, filter_by_query?: boolean | undefined, number_of_buckets: number, query: string, supplier_ids?: (Array | null) | undefined}; export type CreateApiV1SupplychainsSupplyChainIdAssetsAggregateArgs = {organization_id: string, 'X-Organization-ID'?: string | undefined, supply_chain_id: string, body: SupplyChainAggregateAssetsBody}; export type CreateApiV1SupplychainsSupplyChainIdAssetsAggregateOptions = {baseUrl?: string | undefined, PersonalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const supplyChainAggregateBucket = z.object({count: z.number().int(), key: z.string()}); export const supplyChainAggregateResponse = z.object({buckets: z.array(supplyChainAggregateBucket).nullable(), is_more_than_total_hits: z.boolean(), other_count: z.number().int(), query_duration_millis: z.number().int(), total_count: z.number().int()}); export const responseEnvelopeSupplyChainAggregateResponse = z.object({result: supplyChainAggregateResponse.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 }; /** * Aggregate supply chain assets * * POST /v1/supplychains/{supply_chain_id}/assets/aggregate */ export const createApiV1SupplychainsSupplyChainIdAssetsAggregate = async (args: CreateApiV1SupplychainsSupplyChainIdAssetsAggregateArgs, options?: CreateApiV1SupplychainsSupplyChainIdAssetsAggregateOptions) => { const {baseUrl = "https://api.platform.censys.io", fetch: fetchFn = fetch, PersonalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.organization_id != null) query.append("organization_id", String(args.organization_id)) if (args['X-Organization-ID'] != null) headers.set("X-Organization-ID", String(args['X-Organization-ID'])) if (PersonalAccessToken !== undefined) headers.set('authorization', `Bearer ${PersonalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/supplychains/${encodeURIComponent(String(args.supply_chain_id))}/assets/aggregate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(responseEnvelopeSupplyChainAggregateResponse, await res.json()) };