import {z} from 'zod' export type GetApiUsageDataDailyArgs = {limit?: number | undefined, before?: number | undefined, after?: number | undefined, linkid?: number | undefined, linkids?: string | undefined, tagids?: string | undefined, timestart?: number | undefined, timeend?: number | undefined, islive?: boolean | undefined, aggregateall?: boolean | undefined, startat?: string | undefined, tagid?: number | undefined, tagname?: string | undefined, dataplansubscriptionid?: number | undefined}; export type GetApiUsageDataDailyOptions = {baseUrl?: string | undefined, apiKeyAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successProperty = z.boolean(); export const paginationLinks = z.object({path: z.string().optional(), base: z.string().optional(), next: z.string().optional()}); export const usageDailyRecord = z.object({linkid: z.number().int().optional(), record_id: z.number().int().optional(), date: z.string().optional(), bytes: z.number().int().optional(), session_count: z.number().int().optional(), reporting_windows_with_usage: z.number().int().optional()}); export const usageDailyListResponse = z.object({success: successProperty.optional(), limit: z.number().int().optional(), size: z.number().int().optional(), continues: z.boolean().optional(), lastid: z.number().int().optional(), links: paginationLinks.optional(), data: z.array(usageDailyRecord).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 }; /** * List daily aggregated data usage * * GET /usage/data/daily */ export const getApiUsageDataDaily = async (args: GetApiUsageDataDailyArgs, options?: GetApiUsageDataDailyOptions) => { const {baseUrl = "https://dashboard.hologram.io/api/1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.linkid != null) query.append("linkid", String(args.linkid)) if (args.linkids != null) query.append("linkids", String(args.linkids)) if (args.tagids != null) query.append("tagids", String(args.tagids)) if (args.timestart != null) query.append("timestart", String(args.timestart)) if (args.timeend != null) query.append("timeend", String(args.timeend)) if (args.islive != null) query.append("islive", String(args.islive)) if (args.aggregateall != null) query.append("aggregateall", String(args.aggregateall)) if (args.startat != null) query.append("startat", String(args.startat)) if (args.tagid != null) query.append("tagid", String(args.tagid)) if (args.tagname != null) query.append("tagname", String(args.tagname)) if (args.dataplansubscriptionid != null) query.append("dataplansubscriptionid", String(args.dataplansubscriptionid)) if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/usage/data/daily${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(usageDailyListResponse, await res.json()) };