import {z} from 'zod' export type MetricItemV3CreateSchema = {projectId?: string | undefined, primaryEntityId: string, primaryEntityType?: string | undefined, name: string, aggregationTemporality?: string | undefined, metricPointType?: string | undefined, time: string, startTime?: string | undefined, timeUnixNano: number | string, startTimeUnixNano?: (number | string) | undefined, attributes?: {[key: string]: string} | undefined, attributeKeys?: Array | undefined, entityKeys?: Array | undefined, serviceEntityKey?: string | undefined, hostEntityKey?: string | undefined, k8sPodEntityKey?: string | undefined, k8sNodeEntityKey?: string | undefined, k8sClusterEntityKey?: string | undefined, containerEntityKey?: string | undefined, isMonotonic?: boolean | undefined, count?: number | undefined, sum?: number | undefined, value?: number | undefined, min?: number | undefined, max?: number | undefined, bucketCounts?: Array | undefined, explicitBounds?: Array | undefined, scale?: number | undefined, zeroCount?: number | undefined, positiveOffset?: number | undefined, positiveBucketCounts?: Array | undefined, negativeOffset?: number | undefined, negativeBucketCounts?: Array | undefined, summaryQuantiles?: Array | undefined, summaryValues?: Array | undefined, traceId?: string | undefined, spanId?: string | undefined}; export type CreateApiMetricsArgs = {body: {data: MetricItemV3CreateSchema}}; export type CreateApiMetricsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const metricItemV3ReadSchema = z.object({projectId: z.string(), primaryEntityId: z.string(), primaryEntityType: z.string().optional(), name: z.string(), aggregationTemporality: z.string().optional(), metricPointType: z.string().optional(), time: z.string().optional(), startTime: z.string().optional(), timeUnixNano: z.string().optional(), startTimeUnixNano: z.string().optional(), attributes: z.string().optional(), attributeKeys: z.array(z.string()).optional(), entityKeys: z.array(z.string()).optional(), serviceEntityKey: z.string().optional(), hostEntityKey: z.string().optional(), k8sPodEntityKey: z.string().optional(), k8sNodeEntityKey: z.string().optional(), k8sClusterEntityKey: z.string().optional(), containerEntityKey: z.string().optional(), isMonotonic: z.boolean().optional(), count: z.string().optional(), sum: z.number().optional(), value: z.number().optional(), min: z.number().optional(), max: z.number().optional(), bucketCounts: z.string().optional(), explicitBounds: z.string().optional(), scale: z.number().optional(), zeroCount: z.string().optional(), positiveOffset: z.number().optional(), positiveBucketCounts: z.string().optional(), negativeOffset: z.number().optional(), negativeBucketCounts: z.string().optional(), summaryQuantiles: z.string().optional(), summaryValues: z.string().optional(), traceId: z.string().optional(), spanId: z.string().optional()}); export const createApiMetricsResponse = z.object({data: metricItemV3ReadSchema.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 Metric * * POST /metrics */ export const createApiMetrics = async (args: CreateApiMetricsArgs, options?: CreateApiMetricsOptions) => { 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(/\/+$/, '')}/metrics${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(createApiMetricsResponse, await res.json()) };