Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transport-commons): Ability to register routes with custom params (
- Loading branch information
Showing
3 changed files
with
62 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,50 @@ | ||
import { Application, Service } from '@feathersjs/feathers'; | ||
import { Application, Service, ServiceOptions } from '@feathersjs/feathers'; | ||
import { Router } from './router'; | ||
|
||
declare module '@feathersjs/feathers/lib/declarations' { | ||
interface RouteLookup { | ||
service: Service, | ||
params: { [key: string]: string } | ||
params: { [key: string]: any } | ||
} | ||
|
||
interface Application<Services, Settings> { // eslint-disable-line | ||
routes: Router; | ||
routes: Router<{ | ||
service: Service, | ||
params?: { [key: string]: any } | ||
}>; | ||
lookup (path: string): RouteLookup; | ||
} | ||
} | ||
|
||
export * from './router'; | ||
|
||
export const routing = () => (app: Application) => { | ||
if (typeof app.lookup === 'function') { | ||
return; | ||
const lookup = function(this: Application, path: string) { | ||
const result = this.routes.lookup(path); | ||
|
||
if (result === null) { | ||
return null; | ||
} | ||
|
||
const routes = new Router(); | ||
const { params: colonParams, data: { service, params: dataParams } } = result; | ||
|
||
Object.assign(app, { | ||
routes, | ||
lookup (this: Application, path: string) { | ||
const result = this.routes.lookup(path); | ||
const params = dataParams ? { ...dataParams, ...colonParams } : colonParams; | ||
|
||
if (result !== null) { | ||
const { params, data: service } = result; | ||
return { service, params }; | ||
}; | ||
|
||
return { params, service }; | ||
} | ||
export const routing = () => (app: Application) => { | ||
if (typeof app.lookup === 'function') { | ||
return; | ||
} | ||
|
||
return result; | ||
} | ||
}); | ||
app.routes = new Router(); | ||
app.lookup = lookup; | ||
|
||
// Add a mixin that registers a service on the router | ||
app.mixins.push((service: Service, path: string) => { | ||
app.routes.insert(path, service); | ||
app.routes.insert(`${path}/:__id`, service); | ||
app.mixins.push((service: Service, path: string, options: ServiceOptions) => { | ||
const { routeParams: params = {} } = options; | ||
|
||
app.routes.insert(path, { service, params }); | ||
app.routes.insert(`${path}/:__id`, { service, params }); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters