diff --git a/docs/api/application.md b/docs/api/application.md index 3228b01670..c62a35b89d 100644 --- a/docs/api/application.md +++ b/docs/api/application.md @@ -155,6 +155,12 @@ lookup.service.find({ }) ``` +Case insensitive lookups can be enabled in the `app` file like this: + +```ts +app.routes.caseSensitive = false +``` + ## .hooks(hooks) `app.hooks(hooks) -> app` allows registration of application-level hooks. For more information see the [application hooks section in the hooks chapter](./hooks.md#application-hooks). diff --git a/packages/transport-commons/src/routing/router.ts b/packages/transport-commons/src/routing/router.ts index ad76319b70..c2be1631f0 100644 --- a/packages/transport-commons/src/routing/router.ts +++ b/packages/transport-commons/src/routing/router.ts @@ -115,10 +115,18 @@ export class RouteNode { } export class Router { + public caseSensitive = true + constructor(public root: RouteNode = new RouteNode('', 0)) {} getPath(path: string) { - return stripSlashes(path).split('/') + const result = stripSlashes(path).split('/') + + if (!this.caseSensitive) { + return result.map((p) => (p.startsWith(':') ? p : p.toLowerCase())) + } + + return result } insert(path: string, data: T) { diff --git a/packages/transport-commons/test/routing/index.test.ts b/packages/transport-commons/test/routing/index.test.ts index 0131533b3f..0149262d2d 100644 --- a/packages/transport-commons/test/routing/index.test.ts +++ b/packages/transport-commons/test/routing/index.test.ts @@ -43,6 +43,14 @@ describe('app.routes', () => { assert.strictEqual(result.service, app.service('/my/service/')) }) + it('can look up case insensitive', () => { + app.routes.caseSensitive = false + + const result = app.lookup('/My/ServicE') + + assert.strictEqual(result.service, app.service('my/service')) + }) + it('can look up with id', () => { const result = app.lookup('/my/service/1234')