Skip to content

Commit

Permalink
feat(core): Allow to unregister services at runtime (#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Sep 17, 2022
1 parent d734931 commit d16601f
Show file tree
Hide file tree
Showing 33 changed files with 316 additions and 215 deletions.
345 changes: 165 additions & 180 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"lerna": "^5.5.1",
"npm-check-updates": "^16.1.2",
"npm-check-updates": "^16.1.3",
"prettier": "^2.7.1",
"typescript": "^4.8.3"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/mongodb": "^4.0.6",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"mongodb": "^4.9.1",
"shx": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@feathersjs/socketio": "^5.0.0-pre.29",
"@feathersjs/socketio-client": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"axios": "^0.27.2",
"mocha": "^10.0.0",
"shx": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/lodash": "^4.14.185",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication-oauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
"devDependencies": {
"@feathersjs/memory": "^5.0.0-pre.29",
"@types/cookie-session": "^2.0.44",
"@types/express": "^4.17.13",
"@types/express": "^4.17.14",
"@types/koa-session": "^5.10.6",
"@types/lodash": "^4.14.185",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"@types/tough-cookie": "^4.0.2",
"axios": "^0.27.2",
"mocha": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@feathersjs/memory": "^5.0.0-pre.29",
"@types/lodash": "^4.14.185",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"@types/uuid": "^8.3.4",
"mocha": "^10.0.0",
"shx": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@feathersjs/socketio": "^5.0.0-pre.29",
"@feathersjs/transport-commons": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"@types/prettier": "^2.7.0",
"axios": "^0.27.2",
"mocha": "^10.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"@feathersjs/socketio-client": "^5.0.0-pre.29"
},
"devDependencies": {
"@babel/core": "^7.19.0",
"@babel/preset-env": "^7.19.0",
"@babel/core": "^7.19.1",
"@babel/preset-env": "^7.19.1",
"@feathersjs/express": "^5.0.0-pre.29",
"@feathersjs/memory": "^5.0.0-pre.29",
"@feathersjs/socketio": "^5.0.0-pre.29",
Expand Down
2 changes: 1 addition & 1 deletion packages/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"devDependencies": {
"@feathersjs/feathers": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
"@feathersjs/errors": "^5.0.0-pre.29",
"@feathersjs/feathers": "^5.0.0-pre.29",
"@feathersjs/transport-commons": "^5.0.0-pre.29",
"@types/express": "^4.17.13",
"@types/express-serve-static-core": "^4.17.30",
"@types/express": "^4.17.14",
"@types/express-serve-static-core": "^4.17.31",
"cors": "^2.8.5",
"express": "^4.18.1"
},
Expand All @@ -67,7 +67,7 @@
"@feathersjs/tests": "^5.0.0-pre.29",
"@types/lodash": "^4.14.185",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"axios": "^0.27.2",
"lodash": "^4.17.21",
"mocha": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/feathers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
15 changes: 15 additions & 0 deletions packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ export class Feathers<Services, Settings>
return this
}

async unuse<L extends keyof Services & string>(
location: L
): Promise<FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>> {
const path = (stripSlashes(location) || '/') as L
const service = this.services[path] as Service

if (service && typeof service.teardown === 'function') {
await service.teardown(this as any, path)
}

delete this.services[path]

return service as any
}

hooks(hookMap: ApplicationHookOptions<this>) {
const untypedMap = hookMap as any

Expand Down
9 changes: 9 additions & 0 deletions packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ export interface FeathersApplication<Services = any, Settings = any> {
options?: ServiceOptions
): this

/**
* Unregister an existing service.
*
* @param path The name of the service to unregister
*/
unuse<L extends keyof Services & string>(
path: L
): Promise<FeathersService<this, keyof any extends keyof Services ? Service : Services[L]>>

/**
* Get the Feathers service instance for a path. This will
* be the service originally registered with Feathers functionality
Expand Down
11 changes: 9 additions & 2 deletions packages/feathers/test/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('Feathers application', () => {
})
})

it('registers and wraps a new service', async () => {
it('registers and wraps a new service and can unregister (#2035)', async () => {
const dummyService = {
async setup(this: any, _app: any, path: string) {
this.path = path
Expand All @@ -100,7 +100,7 @@ describe('Feathers application', () => {
}
}

const app = feathers().use('/dummy', dummyService)
const app = feathers<{ dummy: typeof dummyService }>().use('dummy', dummyService)
const wrappedService = app.service('dummy')

assert.strictEqual(
Expand All @@ -114,6 +114,13 @@ describe('Feathers application', () => {
})

assert.strictEqual(data.message, 'Test message')

await app.unuse('dummy')

assert.strictEqual(Object.keys(app.services).length, 0)
assert.throws(() => app.service('dummy'), {
message: "Can not find service 'dummy'"
})
})

it('can not register custom methods on a protected methods', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/knex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
"devDependencies": {
"@feathersjs/adapter-tests": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"knex": "^2.3.0",
"mocha": "^10.0.0",
"pg": "^8.8.0",
"shx": "^0.3.4",
"sqlite3": "^5.0.11",
"sqlite3": "^5.1.1",
"typescript": "^4.8.3"
},
"gitHead": "4314dc89a41a8bbaabf00b47697bf7887861d17d"
Expand Down
2 changes: 1 addition & 1 deletion packages/koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@feathersjs/tests": "^5.0.0-pre.29",
"@types/koa-compose": "^3.2.5",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"axios": "^0.27.2",
"mocha": "^10.0.0",
"shx": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/memory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@feathersjs/adapter-tests": "^5.0.0-pre.29",
"@feathersjs/feathers": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
"devDependencies": {
"@feathersjs/adapter-tests": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"mongodb-memory-server": "^8.9.1",
"mongodb-memory-server": "^8.9.2",
"shx": "^0.3.4",
"typescript": "^4.8.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@feathersjs/memory": "^5.0.0-pre.29",
"@feathersjs/tests": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"@types/node-fetch": "^2.6.2",
"@types/qs": "^6.9.7",
"axios": "^0.27.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"devDependencies": {
"@feathersjs/memory": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"ajv-formats": "^2.1.1",
"mocha": "^10.0.0",
"shx": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/socketio-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@feathersjs/socketio": "^5.0.0-pre.29",
"@feathersjs/tests": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"socket.io-client": "^4.5.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/socketio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@feathersjs/memory": "^5.0.0-pre.29",
"@feathersjs/tests": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"lodash": "^4.17.21",
"mocha": "^10.0.0",
"shx": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"devDependencies": {
"@feathersjs/feathers": "^5.0.0-pre.29",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@types/encodeurl": "^1.0.0",
"@types/lodash": "^4.14.185",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.17",
"@types/node": "^18.7.18",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
Expand Down
18 changes: 16 additions & 2 deletions packages/transport-commons/src/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Application, Service, ServiceOptions } from '@feathersjs/feathers'
import { Application, FeathersService, Service, ServiceOptions } from '@feathersjs/feathers'
import { Router } from './router'

declare module '@feathersjs/feathers/lib/declarations' {
Expand Down Expand Up @@ -45,8 +45,22 @@ export const routing = () => (app: Application) => {
app.routes = new Router()
app.lookup = lookup

// This mixin allows us to unregister a service. It needs to run
// first so that `teardown` hooks still get registered properly
app.mixins.unshift((service: Service) => {
const { teardown } = service

service.teardown = async function (app: Application, path: string) {
if (typeof teardown === 'function') {
await teardown.call(this, app, path)
}
app.routes.remove(path)
app.routes.remove(`${path}/:__id`)
}
})

// Add a mixin that registers a service on the router
app.mixins.push((service: Service, path: string, options: ServiceOptions) => {
app.mixins.push((service: FeathersService, path: string, options: ServiceOptions) => {
const { routeParams: params = {} } = options

app.routes.insert(path, { service, params })
Expand Down
Loading

0 comments on commit d16601f

Please sign in to comment.