import {z} from 'zod' export type GetApiUsageSmsArgs = {limit?: number | undefined, before?: number | undefined, after?: number | undefined, simcardid?: number | undefined, simcardids?: string | undefined, timestart?: number | undefined, timeend?: number | undefined, aggregate?: ('month' | 'day' | 'hour' | 'billing') | undefined, startafter?: number | undefined, maxperiodsback?: number | undefined, tagid?: number | undefined, tagname?: string | undefined, usagetype?: ('persim' | 'perprofile' | 'simtotal') | undefined}; export type GetApiUsageSmsOptions = {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 sMSUsageRecord = z.object({linkid: z.number().int().optional(), record_id: z.number().int().optional(), timestamp: z.string().optional(), direction: z.string().optional(), other_number: z.string().optional(), sendtime_is_approximate: z.union([z.literal(0), z.literal(1)]).optional(), bootstrap_id: z.number().int().optional()}); export const sMSUsageListResponse = 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(sMSUsageRecord).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 SMS usage records * * GET /usage/sms/ */ export const getApiUsageSms = async (args: GetApiUsageSmsArgs, options?: GetApiUsageSmsOptions) => { 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.simcardid != null) query.append("simcardid", String(args.simcardid)) if (args.simcardids != null) query.append("simcardids", String(args.simcardids)) if (args.timestart != null) query.append("timestart", String(args.timestart)) if (args.timeend != null) query.append("timeend", String(args.timeend)) if (args.aggregate != null) query.append("aggregate", String(args.aggregate)) if (args.startafter != null) query.append("startafter", String(args.startafter)) if (args.maxperiodsback != null) query.append("maxperiodsback", String(args.maxperiodsback)) if (args.tagid != null) query.append("tagid", String(args.tagid)) if (args.tagname != null) query.append("tagname", String(args.tagname)) if (args.usagetype != null) query.append("usagetype", String(args.usagetype)) if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/usage/sms/${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(sMSUsageListResponse, await res.json()) };