import {z} from 'zod' export type SortDirection = 'ASC' | 'DESC'; export type GetApiInternalV1TracesProjectIdArgs = {project_id: number, time_start: string, time_end: string, query?: Array | undefined, alias?: Array | undefined, system?: Array | undefined, sort_by?: Array | undefined, sort_dir?: Array | undefined, limit?: number | undefined}; export type GetApiInternalV1TracesProjectIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const span = z.object({id: z.string(), parentId: z.string().optional(), traceId: z.string(), standalone: z.boolean().optional(), projectId: z.number().int().optional(), groupId: z.string().optional(), type: z.string().optional(), system: z.string().optional(), kind: z.string().optional(), name: z.string(), eventName: z.string().optional(), displayName: z.string().optional(), time: z.number(), duration: z.number(), statusCode: z.string(), statusMessage: z.string().optional(), attrs: z.record(z.string(), z.unknown()).optional(), events: z.array(z.record(z.string(), z.unknown())).optional(), logs: z.array(z.record(z.string(), z.unknown())).optional(), links: z.array(z.object({traceId: z.string().optional(), spanId: z.string().optional(), attrs: z.record(z.string(), z.unknown()).optional()})).optional()}); export const orderItem = z.object({key: z.string().optional(), order: z.enum(["asc", "desc"]).optional()}); export const getApiInternalV1TracesProjectIdResponse = z.object({spans: z.array(span), count: z.number().int(), query: z.array(z.record(z.string(), z.unknown())).optional(), join: z.array(z.array(z.record(z.string(), z.unknown()))).optional(), sorting: z.array(orderItem).optional(), search: z.array(z.record(z.string(), z.unknown())).optional(), whereAttrs: z.record(z.string(), z.unknown()).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 }; /** * List traces * * GET /internal/v1/traces/{project_id} * * Also accepts auth: authTokenQuery */ export const getApiInternalV1TracesProjectId = async (args: GetApiInternalV1TracesProjectIdArgs, options?: GetApiInternalV1TracesProjectIdOptions) => { const {baseUrl = "https://api.uptrace.dev", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.time_start != null) query.append("time_start", String(args.time_start)) if (args.time_end != null) query.append("time_end", String(args.time_end)) for (const item of args.query ?? []) query.append("query", String(item)) for (const item of args.alias ?? []) query.append("alias", String(item)) for (const item of args.system ?? []) query.append("system", String(item)) for (const item of args.sort_by ?? []) query.append("sort_by", String(item)) for (const item of args.sort_dir ?? []) query.append("sort_dir", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/internal/v1/traces/${encodeURIComponent(String(args.project_id))}${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(getApiInternalV1TracesProjectIdResponse, await res.json()) };