import {z} from 'zod' export type NetworkFlowV1CreateSchema = {projectId?: string | undefined, networkDeviceId: string, exporterIp: string, srcIp: string, dstIp: string, srcPort: number, dstPort: number, protocol: number, inputInterfaceIndex: number, outputInterfaceIndex: number, octets: number | string, packets: number | string, flowStartAt: string, flowEndAt: string, ingestedAt: string, flowFormat: string, samplingRate: number, flowCount: number, probeId?: string | undefined, srcName: string, dstName: string}; export type CreateApiNetworkFlowArgs = {body: {data: NetworkFlowV1CreateSchema}}; export type CreateApiNetworkFlowOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const networkFlowV1ReadSchema = z.object({projectId: z.string(), networkDeviceId: z.string(), exporterIp: z.string(), srcIp: z.string(), dstIp: z.string(), srcPort: z.number(), dstPort: z.number(), protocol: z.number(), inputInterfaceIndex: z.number(), outputInterfaceIndex: z.number(), octets: z.string().optional(), packets: z.string().optional(), flowStartAt: z.string().optional(), flowEndAt: z.string().optional(), ingestedAt: z.string().optional(), flowFormat: z.string(), samplingRate: z.number(), flowCount: z.number(), probeId: z.string().optional(), srcName: z.string(), dstName: z.string()}); export const createApiNetworkFlowResponse = z.object({data: networkFlowV1ReadSchema.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 }; /** * Create Network Flow * * POST /network-flow */ export const createApiNetworkFlow = async (args: CreateApiNetworkFlowArgs, options?: CreateApiNetworkFlowOptions) => { const {baseUrl = "https://oneuptime.com/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("APIKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/network-flow${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(createApiNetworkFlowResponse, await res.json()) };