import {z} from 'zod' export type GetApiApiV3OtaTeamIdUsageArgs = {period_from: string | null, period_to: string | null, team_id: string}; export type GetApiApiV3OtaTeamIdUsageOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const otaTeamUsageSchemasValueSchema = z.object({timestamp: z.string(), value: z.number().int()}); export const otaTeamUsageSchemasSeriesSchema = z.object({name: z.string(), metric: z.string(), values: z.array(otaTeamUsageSchemasValueSchema)}); export const summarySchema = z.object({metric: z.string(), value: z.number().int()}); export const otaTeamUsageSchemasMetaSchema = z.object({period_from: z.string(), period_to: z.string(), interval: z.literal("1d")}); export const otaTeamUsageSchemasUsageSchema = z.object({series: z.array(otaTeamUsageSchemasSeriesSchema), summary: z.array(summarySchema), meta: otaTeamUsageSchemasMetaSchema}); export const otaTeamUsageSchemasUsageSchemaNoPagination = z.object({data: otaTeamUsageSchemasUsageSchema}); 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 the team. * * GET /api/v3/ota/{team_id}/usage */ export const getApiApiV3OtaTeamIdUsage = async (args: GetApiApiV3OtaTeamIdUsageArgs, options?: GetApiApiV3OtaTeamIdUsageOptions) => { 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/${encodeURIComponent(String(args.team_id))}/usage${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(otaTeamUsageSchemasUsageSchemaNoPagination, await res.json()) };