Skip to content

Commit

Permalink
Merge pull request #872 from interval/pre-router-backward-compat
Browse files Browse the repository at this point in the history
Pre router backward compat
  • Loading branch information
jacobmischka authored Oct 5, 2022
2 parents 10f75bb + 22b1e03 commit 95e5b0d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/classes/IntervalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion src/classes/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface RouterConfig {
name: string
description?: string
routes?: IntervalActionDefinitions
actions?: Record<string, IntervalActionDefinition>
groups?: Record<string, Router>
index?: (display: IO['display'], ctx: PageCtx) => Promise<Layout>
}

Expand All @@ -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()

Expand Down
74 changes: 55 additions & 19 deletions src/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,6 +16,7 @@ import type {
IntervalActionDefinitions,
IntervalPageStore,
PageCtx,
IntervalActionDefinition,
} from './types'
import IntervalError from './classes/IntervalError'
import IntervalClient, {
Expand All @@ -35,6 +37,10 @@ export type {
export interface InternalConfig {
apiKey?: string
routes?: IntervalActionDefinitions
// TODO: Mark as deprecated soon, remove soon afterward
actions?: Record<string, IntervalActionDefinition>
// TODO: Mark as deprecated soon, remove soon afterward
groups?: Record<string, ActionGroup>
endpoint?: string
logLevel?: 'prod' | 'debug'
retryIntervalMs?: number
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 95e5b0d

Please sign in to comment.