import {z} from 'zod' export type GetApiV1OrgsOrgIdMetricsV2GraphArgs = {org_id: string, query?: string | undefined, lookback?: string | undefined, from?: string | undefined, to?: string | undefined, order?: string | undefined, sort?: string | undefined, limit?: number | undefined, time_range_adjustment?: string | undefined, save?: string | undefined}; export type GetApiV1OrgsOrgIdMetricsV2GraphOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreCommonTimeseriesAggregate = z.object({value: z.number().optional()}); export const coreCommonTimeseriesRecord = z.object({aggregate: coreCommonTimeseriesAggregate.optional(), timeseries: z.array(z.unknown()).optional(), values: z.array(z.string()).optional()}); export const coreCommonTimeseriesResponse = z.object({from: z.string().optional(), keys: z.array(z.string()).optional(), records: z.array(coreCommonTimeseriesRecord).optional(), to: z.string().optional(), window: z.string().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 }; /** * Returns metrics with common time series response for the given query. * * GET /v1/orgs/{org_id}/metrics_v2/graph */ export const getApiV1OrgsOrgIdMetricsV2Graph = async (args: GetApiV1OrgsOrgIdMetricsV2GraphArgs, options?: GetApiV1OrgsOrgIdMetricsV2GraphOptions) => { const {baseUrl = "https://api.edgedelta.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.lookback != null) query.append("lookback", String(args.lookback)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.order != null) query.append("order", String(args.order)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.time_range_adjustment != null) query.append("time_range_adjustment", String(args.time_range_adjustment)) if (args.save != null) query.append("save", String(args.save)) if (ApiKeyAuth !== undefined) headers.set("X-ED-API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orgs/${encodeURIComponent(String(args.org_id))}/metrics_v2/graph${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(coreCommonTimeseriesResponse, await res.json()) };