import {z} from 'zod' export type GetApiUsersUserIdMetricsArgs = {user_id: number, org_id?: number | undefined, resource_type: 'SOURCE' | 'SINK', from: string, to?: string | undefined, aggregate: number}; export type GetApiUsersUserIdMetricsOptions = {baseUrl?: string | undefined, NexlaSessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiUsersUserIdMetricsResponse = z.object({metrics: z.array(z.object({time: z.string().optional(), records: z.number().int().optional(), size: z.number().int().optional(), errors: z.number().int().optional()})).optional(), status: z.number().int().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 Daily Data Processing Metrics for a User * * GET /users/{user_id}/metrics */ export const getApiUsersUserIdMetrics = async (args: GetApiUsersUserIdMetricsArgs, options?: GetApiUsersUserIdMetricsOptions) => { const {baseUrl = "https://dataops.nexla.io/nexla-api", fetch: fetchFn = fetch, NexlaSessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.org_id != null) query.append("org_id", String(args.org_id)) if (args.resource_type != null) query.append("resource_type", String(args.resource_type)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.aggregate != null) query.append("aggregate", String(args.aggregate)) if (NexlaSessionToken !== undefined) headers.set('authorization', `Bearer ${NexlaSessionToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.user_id))}/metrics${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(getApiUsersUserIdMetricsResponse, await res.json()) };