import {z} from 'zod' export type GetApiSubscriptionOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSubscriptionResponse = z.object({id: z.number().int(), resource_type: z.literal("Subscription"), created_at: z.string(), updated_at: z.string(), name: z.string(), status: z.enum(["incomplete", "incomplete_expired", "trialing", "active", "past_due", "canceled", "unpaid", "null"]).nullable(), reseller: z.boolean(), reseller_contact_details: z.string().nullable(), resources: z.object({access_keys: z.object({count: z.number().int(), quota: z.number().int().nullable()}), apple_passes: z.object({count: z.number().int(), quota: z.number().int().nullable()}), cameras: z.object({count: z.number().int(), quota: z.number().int().nullable()}), custom_roles: z.object({count: z.number().int(), quota: z.number().int().nullable()}), elevator_stops: z.object({count: z.number().int(), quota: z.number().int().nullable()}), group_links: z.object({count: z.number().int(), quota: z.number().int().nullable()}), incident_policies: z.object({count: z.number().int(), quota: z.number().int().nullable()}), integrations: z.object({count: z.number().int(), quota: z.number().int().nullable()}), locks: z.object({count: z.number().int(), quota: z.number().int().nullable()}), monthly_email_access_keys: z.object({count: z.number().int(), quota: z.number().int().nullable()}), monthly_email_group_links: z.object({count: z.number().int(), quota: z.number().int().nullable()}), monthly_sms_access_keys: z.object({count: z.number().int(), quota: z.number().int().nullable()}), monthly_sms_group_links: z.object({count: z.number().int(), quota: z.number().int().nullable()}), places: z.object({count: z.number().int(), quota: z.number().int().nullable()}), terminals: z.object({count: z.number().int(), quota: z.number().int().nullable()}), users: z.object({count: z.number().int(), quota: z.number().int().nullable()}), zones: z.object({count: z.number().int(), quota: z.number().int().nullable()})}), addons: z.array(z.union([z.object({id: z.number().int(), resource_type: z.literal("SubscriptionAddon"), status: z.enum(["active", "inactive"]), plan: z.string().min(1).max(100), message: z.string().nullable(), created_at: z.string(), updated_at: z.string(), subscription_id: z.number().int(), type: z.literal("feature"), feature: z.enum(["support_portal", "segment", "tailgating", "monitoring"])}), z.object({id: z.number().int(), resource_type: z.literal("SubscriptionAddon"), status: z.enum(["active", "inactive"]), plan: z.string().min(1).max(100), message: z.string().nullable(), created_at: z.string(), updated_at: z.string(), subscription_id: z.number().int(), type: z.literal("resource"), applies_to_id: z.number().int(), applies_to_type: z.enum(["MarketplaceApp", "VisualizationPage"])})])), organization_id: z.number().int(), organization: z.object({id: z.number().int(), resource_type: z.literal("Organization"), name: z.string(), domain: z.string()}), place_id: z.number().int().nullable()}); 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 }; /** * Fetch subscription * * GET /subscription * * Also accepts auth: OAuth2 */ export const getApiSubscription = async (options?: GetApiSubscriptionOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/subscription${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(getApiSubscriptionResponse, await res.json()) };