Skip to content

Commit

Permalink
fix(transport-commons): Allow case insensitive route lookups (#3353)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Nov 27, 2023
1 parent 677c214 commit a4a5ab6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/api/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
10 changes: 9 additions & 1 deletion packages/transport-commons/src/routing/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,18 @@ export class RouteNode<T = any> {
}

export class Router<T = any> {
public caseSensitive = true

constructor(public root: RouteNode<T> = new RouteNode<T>('', 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) {
Expand Down
8 changes: 8 additions & 0 deletions packages/transport-commons/test/routing/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit a4a5ab6

Please sign in to comment.