import {z} from 'zod' export type GetApiV3GlobalAssetHostHostIdObservationsServicesArgs = {organization_id: string, 'X-Organization-ID'?: string | undefined, start_time?: string | undefined, end_time?: string | undefined, page_size?: number | undefined, page_token?: string | undefined, port?: number | undefined, protocol?: string | undefined, transport_protocol?: ('tcp' | 'udp' | 'quic') | undefined, host_id: unknown, order_by?: (Array<'port ASC' | 'port DESC' | 'protocol ASC' | 'protocol DESC' | 'transport_protocol ASC' | 'transport_protocol DESC'> | null) | undefined}; export type GetApiV3GlobalAssetHostHostIdObservationsServicesOptions = {baseUrl?: string | undefined, PersonalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceOnHostRange = z.object({end_time: z.string(), ip: z.string(), port: z.string(), protocol: z.string(), start_time: z.string(), transport_protocol: z.string()}); export const servicesOnHostResponse = z.object({next_page_token: z.string(), ranges: z.array(serviceOnHostRange).nullable()}); export const responseEnvelopeServicesOnHostResponse = z.object({result: servicesOnHostResponse.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 }; /** * Get service history for a host * * GET /v3/global/asset/host/{host_id}/observations/services */ export const getApiV3GlobalAssetHostHostIdObservationsServices = async (args: GetApiV3GlobalAssetHostHostIdObservationsServicesArgs, options?: GetApiV3GlobalAssetHostHostIdObservationsServicesOptions) => { const {baseUrl = "https://api.platform.censys.io", fetch: fetchFn = fetch, PersonalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.port != null) query.append("port", String(args.port)) if (args.protocol != null) query.append("protocol", String(args.protocol)) if (args.transport_protocol != null) query.append("transport_protocol", String(args.transport_protocol)) if (args.order_by != null) query.append("order_by", args.order_by.join(',')) if (PersonalAccessToken !== undefined) headers.set('authorization', `Bearer ${PersonalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/global/asset/host/${encodeURIComponent(String(args.host_id))}/observations/services${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(responseEnvelopeServicesOnHostResponse, await res.json()) };