@pollyjs/core
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [2.3.0](https://github.com/netflix/pollyjs/tree/master/packages/@pollyjs/core/compare/v2.2.0...v2.3.0) (2019-02-27) | ||
### Features | ||
* **core:** Filter requests matched by a route handler ([#189](https://github.com/netflix/pollyjs/tree/master/packages/[@pollyjs](https://github.com/pollyjs)/core/issues/189)) ([5d57c32](https://github.com/netflix/pollyjs/tree/master/packages/@pollyjs/core/commit/5d57c32)) | ||
# [2.2.0](https://github.com/netflix/pollyjs/tree/master/packages/@pollyjs/core/compare/v2.1.0...v2.2.0) (2019-02-20) | ||
@@ -8,0 +19,0 @@ |
{ | ||
"name": "@pollyjs/core", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Record, replay, and stub HTTP Interactions", | ||
@@ -57,3 +57,3 @@ "main": "dist/cjs/pollyjs-core.js", | ||
}, | ||
"gitHead": "f2eb09f808eb6ea57d35d933b38c575a30e348f9" | ||
"gitHead": "3c10e74b2f7fb7bdb9d9f63d5038e090a1431ac7" | ||
} |
@@ -60,2 +60,5 @@ import md5 from 'blueimp-md5'; | ||
// Filter all matched route handlers by this request | ||
this[ROUTE].applyFiltersWithArgs(this); | ||
// Handle config overrides defined by the route | ||
@@ -62,0 +65,0 @@ this._configure(this[ROUTE].config()); |
@@ -14,2 +14,3 @@ import { assert } from '@pollyjs/utils'; | ||
this.set('config', {}); | ||
this.set('filters', new Set()); | ||
this._eventEmitter = new EventEmitter({ | ||
@@ -83,2 +84,13 @@ eventNames: [ | ||
} | ||
filter(fn) { | ||
assert( | ||
`Invalid filter callback provided. Expected function, received: "${typeof fn}".`, | ||
typeof fn === 'function' | ||
); | ||
this.get('filters').add(fn); | ||
return this; | ||
} | ||
} |
import mergeConfigs from '../utils/merge-configs'; | ||
async function invoke(fn, route, req, ...args) { | ||
if (typeof fn !== 'function') { | ||
return; | ||
} | ||
const HANDLERS = Symbol(); | ||
const proxyReq = new Proxy(req, { | ||
function requestWithParams(req, { params }) { | ||
return new Proxy(req, { | ||
set(source, prop, value) { | ||
@@ -19,3 +17,3 @@ /* NOTE: IE's `Reflect.set` swallows the read-only assignment error */ | ||
// Set the request's params to given route's matched params | ||
return route.params; | ||
return { ...params }; | ||
} | ||
@@ -26,16 +24,4 @@ | ||
}); | ||
return await fn(proxyReq, ...args); | ||
} | ||
async function emit(route, eventName, ...args) { | ||
for (const handler of route.handlers) { | ||
const listeners = handler._eventEmitter.listeners(eventName); | ||
for (const listener of listeners) { | ||
await invoke(listener, route, ...args); | ||
} | ||
} | ||
} | ||
export default class Route { | ||
@@ -60,2 +46,4 @@ /** | ||
} | ||
this[HANDLERS] = this._orderedHandlers(); | ||
} | ||
@@ -77,6 +65,14 @@ | ||
return mergeConfigs( | ||
...this._orderedHandlers().map(handler => handler.get('config')) | ||
...this[HANDLERS].map(({ handler }) => handler.get('config')) | ||
); | ||
} | ||
applyFiltersWithArgs(req, ...args) { | ||
this[HANDLERS] = this[HANDLERS].filter(({ route, handler }) => | ||
[...handler.get('filters')].every(fn => | ||
fn(requestWithParams(req, route), ...args) | ||
) | ||
); | ||
} | ||
/** | ||
@@ -89,5 +85,9 @@ * Invokes the intercept handlers defined on the routes + middleware. | ||
async intercept(req, res, interceptor) { | ||
for (const handler of this._orderedHandlers()) { | ||
for (const { route, handler } of this[HANDLERS]) { | ||
if (handler.has('intercept') && interceptor.shouldIntercept) { | ||
await invoke(handler.get('intercept'), this, ...arguments); | ||
await handler.get('intercept')( | ||
requestWithParams(req, route), | ||
res, | ||
interceptor | ||
); | ||
} | ||
@@ -103,8 +103,10 @@ } | ||
*/ | ||
async emit() { | ||
for (const m of this.middleware) { | ||
await emit(m, ...arguments); | ||
async emit(eventName, req, ...args) { | ||
for (const { route, handler } of this[HANDLERS]) { | ||
const listeners = handler._eventEmitter.listeners(eventName); | ||
for (const listener of listeners) { | ||
await listener(requestWithParams(req, route), ...args); | ||
} | ||
} | ||
await emit(this, ...arguments); | ||
} | ||
@@ -114,3 +116,3 @@ | ||
return [...this.middleware, this].reduce((handlers, route) => { | ||
handlers.push(...route.handlers); | ||
handlers.push(...route.handlers.map(handler => ({ route, handler }))); | ||
@@ -124,3 +126,3 @@ return handlers; | ||
for (const handler of this._orderedHandlers()) { | ||
for (const { handler } of this[HANDLERS]) { | ||
if (handler.has(key)) { | ||
@@ -127,0 +129,0 @@ value = handler.get(key); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
3787820
24567