diff --git a/src/classes/IntervalClient.ts b/src/classes/IntervalClient.ts index 4729e5c..25cd31d 100644 --- a/src/classes/IntervalClient.ts +++ b/src/classes/IntervalClient.ts @@ -174,8 +174,14 @@ export default class IntervalClient { } } - if (this.#config.routes) { - for (const [slug, def] of Object.entries(this.#config.routes)) { + const routes = { + ...this.#config.actions, + ...this.#config.groups, + ...this.#config.routes, + } + + if (routes) { + for (const [slug, def] of Object.entries(routes)) { if (def instanceof Router) { walkRouter(slug, def) } else { diff --git a/src/classes/Router.ts b/src/classes/Router.ts index 8ed6654..a9df8fc 100644 --- a/src/classes/Router.ts +++ b/src/classes/Router.ts @@ -11,6 +11,8 @@ export interface RouterConfig { name: string description?: string routes?: IntervalActionDefinitions + actions?: Record + groups?: Record index?: (display: IO['display'], ctx: PageCtx) => Promise } @@ -26,7 +28,11 @@ export default class Router { constructor(config: RouterConfig) { this.name = config.name this.description = config.description - this.routes = config.routes ?? {} + this.routes = { + ...config.routes, + ...config.actions, + ...config.groups, + } this.index = config.index this.onChange = new Evt() diff --git a/src/experimental.ts b/src/experimental.ts index 1002b27..db774c1 100644 --- a/src/experimental.ts +++ b/src/experimental.ts @@ -36,8 +36,14 @@ class ExperimentalInterval extends Interval { this.apiKey ) - if (this.config.routes) { - for (const group of Object.values(this.config.routes)) { + const routes = { + ...this.config.actions, + ...this.config.groups, + ...this.config.routes, + } + + if (routes) { + for (const group of Object.values(routes)) { if (group instanceof Router) { group.onChange.attach(this.#groupChangeCtx, () => { this.client?.handleActionsChange(this.config) @@ -47,6 +53,21 @@ class ExperimentalInterval extends Interval { } } + // TODO: Mark as deprecated soon, remove soon afterward + get actions(): ExperimentalRoutes { + return this.routes + } + + // TODO: Mark as deprecated soon, remove soon afterward + addGroup(slug: string, group: Router) { + return this.routes.add(slug, group) + } + + // TODO: Mark as deprecated soon, remove soon afterward + removeGroup(slug: string) { + return this.routes.remove(slug) + } + /* * Handle a serverless host endpoint request. Receives the deserialized request body object. */ @@ -169,13 +190,17 @@ class ExperimentalInterval extends Interval { } async #declareHost(httpHostId: string) { - const actions = Object.entries(this.config.routes ?? {}).map( - ([slug, def]) => ({ - slug, - ...('handler' in def ? def : {}), - handler: undefined, - }) - ) + const routes = { + ...this.config.actions, + ...this.config.groups, + ...this.config.routes, + } + + const actions = Object.entries(routes).map(([slug, def]) => ({ + slug, + ...('handler' in def ? def : {}), + handler: undefined, + })) const slugs = actions.map(a => a.slug) if (slugs.length === 0) { @@ -263,22 +288,33 @@ export class ExperimentalRoutes extends Routes { } remove(slug: string) { - const { routes } = this.interval.config + for (const key of ['routes', 'actions', 'groups'] as const) { + const routes = this.interval.config[key] - if (!routes) return - const route = routes[slug] - if (!route) return + if (!routes) continue + const route = routes[slug] + if (!route) continue - if (route instanceof Router) { - route.onChange.detach(this.#groupChangeCtx) - } + if (route instanceof Router) { + route.onChange.detach(this.#groupChangeCtx) + } - delete routes[slug] + delete routes[slug] - this.interval.client?.handleActionsChange(this.interval.config) + this.interval.client?.handleActionsChange(this.interval.config) + return + } } } -export { Router, io, ctx, IntervalError, ExperimentalInterval as Interval } +export { + Router, + // TODO: Mark as deprecated soon, remove soon afterward + Router as ActionGroup, + io, + ctx, + IntervalError, + ExperimentalInterval as Interval, +} export default ExperimentalInterval diff --git a/src/index.ts b/src/index.ts index 8a049c1..194f93b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import fetch from 'node-fetch' import Routes from './classes/Routes' import IOError from './classes/IOError' import Logger from './classes/Logger' +import ActionGroup from './classes/Router' import { NOTIFY } from './internalRpcSchema' import { SerializableRecord } from './ioSchema' import type { @@ -15,6 +16,7 @@ import type { IntervalActionDefinitions, IntervalPageStore, PageCtx, + IntervalActionDefinition, } from './types' import IntervalError from './classes/IntervalError' import IntervalClient, { @@ -35,6 +37,10 @@ export type { export interface InternalConfig { apiKey?: string routes?: IntervalActionDefinitions + // TODO: Mark as deprecated soon, remove soon afterward + actions?: Record + // TODO: Mark as deprecated soon, remove soon afterward + groups?: Record endpoint?: string logLevel?: 'prod' | 'debug' retryIntervalMs?: number @@ -135,6 +141,11 @@ export default class Interval { ) } + // TODO: Mark as deprecated soon, remove soon afterward + get actions(): Routes { + return this.routes + } + protected get apiKey(): string | undefined { return this.#apiKey }