import {z} from 'zod' export type GetApiV1AppsAppIdMeasurerMeasurementsArgs = {start_time?: string | undefined, end_time?: string | undefined, parameter_set_id?: string | undefined, dataset_id?: string | undefined, field_name?: string | undefined, metric_name?: string | undefined, group_by?: Array | undefined, 'X-Fields'?: unknown | undefined}; export type GetApiV1AppsAppIdMeasurerMeasurementsOptions = {baseUrl: string, apiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const measurementRecord = z.object({field_name: z.string().optional(), metric_name: z.string().optional(), value: z.number().optional(), created_at: z.string().optional(), dataset_id: z.string().optional(), parameter_set_id: z.string().optional()}); export const measurementResponse = z.object({app_id: z.string().optional(), records: z.array(measurementRecord).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 }; /** * Get measurements for the given app_id * * GET /v1/apps/{app_id}/measurer/measurements */ export const getApiV1AppsAppIdMeasurerMeasurements = async (args: GetApiV1AppsAppIdMeasurerMeasurementsArgs, options: GetApiV1AppsAppIdMeasurerMeasurementsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.parameter_set_id != null) query.append("parameter_set_id", String(args.parameter_set_id)) if (args.dataset_id != null) query.append("dataset_id", String(args.dataset_id)) if (args.field_name != null) query.append("field_name", String(args.field_name)) if (args.metric_name != null) query.append("metric_name", String(args.metric_name)) for (const item of args.group_by ?? []) query.append("group_by", String(item)) if (args['X-Fields'] != null) headers.set("X-Fields", String(args['X-Fields'])) if (apiToken !== undefined) headers.set("API-KEY", apiToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/apps/${encodeURIComponent(String(args.app_id))}/measurer/measurements${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(measurementResponse, await res.json()) };