import {z} from 'zod' export type ProfileFilter = {key: string, op?: string | undefined, value: string}; export type ProfilesQueryBody = {data_source?: (string | null) | undefined, end_time?: number | undefined, filters?: Array | undefined, group_by?: (string | null) | undefined, group_limit?: (number | null) | undefined, max_nodes?: (number | null) | undefined, profile_type?: (string | null) | undefined, profile_unit?: (string | null) | undefined, service_name?: (string | null) | undefined, start_time?: number | undefined, step?: (string | null) | undefined, timeout?: (number | null) | undefined}; export type CreateApiApiOrgIdStreamNameProfilesMergeArgs = {org_id: string, stream_name: string, body: ProfilesQueryBody}; export type CreateApiApiOrgIdStreamNameProfilesMergeOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const profilesMergeResponse = z.object({merged_total: z.number().int(), profile_type: z.string(), root: z.object({}), took: z.number().int().gte(0), top: z.array(z.object({})), total: z.number().int(), tree_truncated: z.boolean(), truncated: z.boolean(), unit: z.string()}); 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 }; /** * Merge profile stacks into a call tree * * POST /api/{org_id}/{stream_name}/profiles/merge */ export const createApiApiOrgIdStreamNameProfilesMerge = async (args: CreateApiApiOrgIdStreamNameProfilesMergeArgs, options: CreateApiApiOrgIdStreamNameProfilesMergeOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/${encodeURIComponent(String(args.stream_name))}/profiles/merge${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(profilesMergeResponse, await res.json()) };