import {z} from 'zod' export type MetricType = 'download_count' | 'deployment_succeeded_count' | 'deployment_failed_count'; export type GetApiApiV3OtaDeploymentsDeploymentIdUsageMetricArgs = {deployment_id: string, metric: MetricType, period_from: string | null, period_to: string | null}; export type GetApiApiV3OtaDeploymentsDeploymentIdUsageMetricOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const otaDeploymentUsageSchemasValueSchema = z.object({timestamp: z.string(), value: z.number().int()}); export const otaDeploymentUsageSchemasSeriesSchema = z.object({name: z.string(), metric: z.string(), values: z.array(otaDeploymentUsageSchemasValueSchema)}); export const otaDeploymentUsageSchemasMetaSchema = z.object({period_from: z.string(), period_to: z.string(), interval: z.literal("1d")}); export const otaDeploymentUsageSchemasUsageSchema = z.object({series: z.array(otaDeploymentUsageSchemasSeriesSchema), meta: otaDeploymentUsageSchemasMetaSchema}); export const otaDeploymentUsageSchemasUsageSchemaNoPagination = z.object({data: otaDeploymentUsageSchemasUsageSchema}); 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 usage stats for a deployment channel. * * GET /api/v3/ota/deployments/{deployment_id}/usage/{metric} */ export const getApiApiV3OtaDeploymentsDeploymentIdUsageMetric = async (args: GetApiApiV3OtaDeploymentsDeploymentIdUsageMetricArgs, options?: GetApiApiV3OtaDeploymentsDeploymentIdUsageMetricOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period_from != null) query.append("period_from", String(args.period_from)) if (args.period_to != null) query.append("period_to", String(args.period_to)) if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/ota/deployments/${encodeURIComponent(String(args.deployment_id))}/usage/${encodeURIComponent(String(args.metric))}${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(otaDeploymentUsageSchemasUsageSchemaNoPagination, await res.json()) };