import {z} from 'zod' export type GetApiPlansPricingArgs = {orgid?: number | undefined, planid?: number | undefined, planids?: string | undefined, haszdr?: boolean | undefined, zone?: string | undefined}; export type GetApiPlansPricingOptions = {baseUrl?: string | undefined, apiKeyAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successProperty = z.boolean(); export const planPriceEntry = z.object({plan_id: z.number().int().optional(), pricing_id: z.number().int().optional(), zone: z.string().optional(), account_tier: z.string().optional(), amount: z.number().optional(), sms: z.number().optional(), overage: z.number().optional(), zero_data_rate_flag: z.union([z.literal(0), z.literal(1)]).optional(), zero_data_rate: z.number().nullable().optional()}); export const getPlanPricingResponse = z.object({success: successProperty.optional(), data: z.array(planPriceEntry).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 all data plan pricing * * GET /plans/pricing */ export const getApiPlansPricing = async (args: GetApiPlansPricingArgs, options?: GetApiPlansPricingOptions) => { 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.orgid != null) query.append("orgid", String(args.orgid)) if (args.planid != null) query.append("planid", String(args.planid)) if (args.planids != null) query.append("planids", String(args.planids)) if (args.haszdr != null) query.append("haszdr", String(args.haszdr)) if (args.zone != null) query.append("zone", String(args.zone)) if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans/pricing${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(getPlanPricingResponse, await res.json()) };