-
Notifications
You must be signed in to change notification settings - Fork 821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Makes it easier to use custom Routes within WorkboxSW. #639
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,36 @@ export function isArrayOfClass(object, expectedClass) { | |
} | ||
} | ||
|
||
export function isInterface(object, expectedProperties) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an object that has | ||
'${expectedProperties}' properties.`; | ||
|
||
for (let property of expectedProperties) { | ||
if (!(property in object[parameter])) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
|
||
export function isArrayOfInterface(object, expectedProperties) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
objects that each have '${expectedProperties}' properties.`; | ||
|
||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
|
||
for (let item of object[parameter]) { | ||
for (let property of expectedProperties) { | ||
if (!(property in item)) { | ||
throwError(message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, the message should be updated to handle the missing property |
||
} | ||
} | ||
} | ||
} | ||
|
||
export function isValue(object, expectedValue) { | ||
const parameter = Object.keys(object).pop(); | ||
const actualValue = object[parameter]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,12 @@ | |
limitations under the License. | ||
*/ | ||
|
||
import Route from './route'; | ||
import {isArrayOfClass, isInstance} from '../../../../lib/assert'; | ||
import {isArrayOfInterface, isInterface} from '../../../../lib/assert'; | ||
import logHelper from '../../../../lib/log-helper.js'; | ||
import normalizeHandler from './normalize-handler'; | ||
|
||
const ROUTE_INTERFACE = ['match', 'handler', 'method']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a huge fan of having this manually defined and it being defined away from the class it represents. |
||
|
||
/** | ||
* The Router takes one or more [Routes]{@link Route} and registers a [`fetch` | ||
* event listener](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent) | ||
|
@@ -220,7 +221,10 @@ class Router { | |
* routes to register. | ||
*/ | ||
registerRoutes({routes} = {}) { | ||
isArrayOfClass({routes}, Route); | ||
// Rather than explicitly checking for Route subclasses, check to ensure | ||
// that the object exposes the interface we need. | ||
// See https://github.com/GoogleChrome/workbox/issues/385 | ||
isArrayOfInterface({routes}, ROUTE_INTERFACE); | ||
|
||
for (let route of routes) { | ||
if (!this._routes.has(route.method)) { | ||
|
@@ -244,7 +248,7 @@ class Router { | |
* @param {module:workbox-routing.Route} input.route The route to register. | ||
*/ | ||
registerRoute({route} = {}) { | ||
isInstance({route}, Route); | ||
isInterface({route}, ROUTE_INTERFACE); | ||
|
||
this.registerRoutes({routes: [route]}); | ||
} | ||
|
@@ -265,7 +269,7 @@ class Router { | |
* routes to unregister. | ||
*/ | ||
unregisterRoutes({routes} = {}) { | ||
isArrayOfClass({routes}, Route); | ||
isArrayOfInterface({routes}, ROUTE_INTERFACE); | ||
|
||
for (let route of routes) { | ||
if (!this._routes.has(route.method)) { | ||
|
@@ -305,7 +309,7 @@ class Router { | |
* @param {module:workbox-routing.Route} input.route The route to unregister. | ||
*/ | ||
unregisterRoute({route} = {}) { | ||
isInstance({route}, Route); | ||
isInterface({route}, ROUTE_INTERFACE); | ||
|
||
this.unregisterRoutes({routes: [route]}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,10 +82,12 @@ class Router extends SWRoutingRouter { | |
* @param {function|module:workbox-runtime-caching.Handler} handler The | ||
* handler to use to provide a response if the route matches. The handler | ||
* argument is ignored if you pass in a Route object, otherwise it's required. | ||
* @param {String} [method] Only match requests that use this HTTP method. | ||
* Defaults to `'GET'`. | ||
* @return {module:workbox-routing.Route} The Route object that was | ||
* registered. | ||
*/ | ||
registerRoute(capture, handler) { | ||
registerRoute(capture, handler, method = 'GET') { | ||
if (typeof handler === 'function') { | ||
handler = { | ||
handle: handler, | ||
|
@@ -97,9 +99,9 @@ class Router extends SWRoutingRouter { | |
if (capture.length === 0) { | ||
throw ErrorFactory.createError('empty-express-string'); | ||
} | ||
route = new ExpressRoute({path: capture, handler}); | ||
route = new ExpressRoute({path: capture, handler, method}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you add the method? |
||
} else if (capture instanceof RegExp) { | ||
route = new RegExpRoute({regExp: capture, handler}); | ||
route = new RegExpRoute({regExp: capture, handler, method}); | ||
} else if (capture instanceof Route) { | ||
route = capture; | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth adding the missing parameter to the message?