import {z} from 'zod' export type ProfileItemV3CreateSchema = {projectId?: string | undefined, primaryEntityId: string, primaryEntityType?: string | undefined, profileId: string, traceId?: string | undefined, spanId?: string | undefined, startTime: string, endTime: string, startTimeUnixNano: number | string, endTimeUnixNano: number | string, durationNano: number | string, profileType: string, unit: string, periodType?: string | undefined, period?: (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, sampleCount: number, originalPayloadFormat?: string | undefined}; export type CreateApiProfileArgs = {body: {data: ProfileItemV3CreateSchema}}; export type CreateApiProfileOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const profileItemV3ReadSchema = z.object({projectId: z.string(), primaryEntityId: z.string(), primaryEntityType: z.string().optional(), profileId: z.string(), traceId: z.string().optional(), spanId: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), startTimeUnixNano: z.string().optional(), endTimeUnixNano: z.string().optional(), durationNano: z.string().optional(), profileType: z.string(), unit: z.string(), periodType: z.string().optional(), period: 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(), sampleCount: z.number(), originalPayloadFormat: z.string().optional()}); export const createApiProfileResponse = z.object({data: profileItemV3ReadSchema.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 Profile * * POST /profile */ export const createApiProfile = async (args: CreateApiProfileArgs, options?: CreateApiProfileOptions) => { 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(/\/+$/, '')}/profile${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(createApiProfileResponse, await res.json()) };