UNPKG

1.95 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/rest
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.TrieRouter = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const util_1 = require("util");
11const keys_1 = require("../keys");
12const route_entry_1 = require("./route-entry");
13const router_base_1 = require("./router-base");
14const trie_1 = require("./trie");
15const debug = require('debug')('loopback:rest:router:trie');
16/**
17 * Router implementation based on trie
18 */
19let TrieRouter = class TrieRouter extends router_base_1.BaseRouter {
20 constructor(options) {
21 super(options);
22 this.trie = new trie_1.Trie();
23 }
24 addRouteWithPathVars(route) {
25 // Add the route to the trie
26 const key = this.getKeyForRoute(route);
27 this.trie.create(key, route);
28 }
29 findRouteWithPathVars(verb, path) {
30 var _a;
31 const key = this.getKey(verb, path);
32 const found = this.trie.match(key);
33 debug('Route matched: %j', found);
34 if (found) {
35 const route = found.node.value;
36 if (route) {
37 debug('Route found: %s', (0, util_1.inspect)(route, { depth: 5 }));
38 return (0, route_entry_1.createResolvedRoute)(route, (_a = found.params) !== null && _a !== void 0 ? _a : {});
39 }
40 }
41 return undefined;
42 }
43 listRoutesWithPathVars() {
44 return this.trie.list().map(n => n.value);
45 }
46};
47TrieRouter = tslib_1.__decorate([
48 tslib_1.__param(0, (0, core_1.inject)(keys_1.RestBindings.ROUTER_OPTIONS, { optional: true })),
49 tslib_1.__metadata("design:paramtypes", [Object])
50], TrieRouter);
51exports.TrieRouter = TrieRouter;
52//# sourceMappingURL=trie-router.js.map
\No newline at end of file