moleculer-web
Advanced tools
Comparing version 0.10.1 to 0.10.2
@@ -0,1 +1,44 @@ | ||
<a name="0.10.2"></a> | ||
# 0.10.2 (2021-09-05) | ||
## Named routes | ||
Many developers issued that version 0.10 doesn't support multiple routes with the same path. This version fixes it but you should give a unique name for the routes if they have same path. | ||
**Example** | ||
You can call `/api/hi` without auth, but `/api/hello` only with auth. | ||
```js | ||
const ApiGateway = require("moleculer-web"); | ||
module.exports = { | ||
mixins: [ApiGateway], | ||
settings: { | ||
path: "/api", | ||
routes: [ | ||
{ | ||
name: "no-auth-route", // unique name | ||
path: "/", | ||
aliases: { | ||
hi: "greeter.hello", | ||
} | ||
}, | ||
{ | ||
name: "with-auth-route", // unique name | ||
path: "/", | ||
aliases: { | ||
"hello": "greeter.hello", | ||
}, | ||
authorization: true | ||
} | ||
] | ||
} | ||
}; | ||
``` | ||
## Changes | ||
- add `removeRouteByName(name: string)` method to remove a route by its name. | ||
----------------------------- | ||
<a name="0.10.1"></a> | ||
@@ -2,0 +45,0 @@ # 0.10.1 (2021-09-01) |
{ | ||
"name": "moleculer-web", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"description": "Official API Gateway service for Moleculer framework", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -268,6 +268,10 @@ /* | ||
params: { | ||
path: { type: "string" } | ||
name: { type: "string", optional: true }, | ||
path: { type: "string", optional: true } | ||
}, | ||
visibility: "public", | ||
handler(ctx) { | ||
if (ctx.params.name != null) | ||
return this.removeRouteByName(ctx.params.name); | ||
return this.removeRoute(ctx.params.path); | ||
@@ -1138,3 +1142,3 @@ } | ||
const route = this.createRoute(opts); | ||
const idx = this.routes.findIndex(r => r.path == route.path); | ||
const idx = this.routes.findIndex(r => this.isEqualRoutes(r, route)); | ||
if (idx !== -1) { | ||
@@ -1179,2 +1183,22 @@ // Replace the previous | ||
/** | ||
* Remove a route by name | ||
* @param {String} name | ||
*/ | ||
removeRouteByName(name) { | ||
const idx = this.routes.findIndex(r => r.opts.name == name); | ||
if (idx !== -1) { | ||
const route = this.routes[idx]; | ||
// Clean global aliases for this route | ||
this.aliases = this.aliases.filter(a => a.route != route); | ||
// Remote route | ||
this.routes.splice(idx, 1); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
/** | ||
* Optimize route order by route path depth | ||
@@ -1205,2 +1229,3 @@ */ | ||
let route = { | ||
name: opts.name, | ||
opts, | ||
@@ -1345,3 +1370,3 @@ middlewares: [] | ||
// Clean previous aliases for this route | ||
this.aliases = this.aliases.filter(a => a.route.path != route.path); | ||
this.aliases = this.aliases.filter(a => !this.isEqualRoutes(a.route, route)); | ||
@@ -1363,2 +1388,16 @@ // Process aliases definitions from route settings | ||
/** | ||
* Checks whether the routes are same. | ||
* | ||
* @param {Object} routeA | ||
* @param {Object} routeB | ||
* @returns {Boolean} | ||
*/ | ||
isEqualRoutes(routeA, routeB) { | ||
if (routeA.name != null && routeB.name != null) { | ||
return routeA.name === routeB.name; | ||
} | ||
return routeA.path === routeB.path; | ||
}, | ||
/** | ||
* Generate aliases for REST. | ||
@@ -1365,0 +1404,0 @@ * |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107611
2182