@pollyjs/core
Advanced tools
Comparing version 0.0.2 to 0.1.0
{ | ||
"name": "@pollyjs/core", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Record, replay, and stub HTTP Interactions", | ||
@@ -21,3 +21,4 @@ "main": "dist/cjs/pollyjs-core.js", | ||
"format": "prettier --config ../../../.prettier.js --write **/*.js", | ||
"lint": "eslint ./*.js src tests" | ||
"lint": "eslint ./*.js src tests", | ||
"prepublishOnly": "NODE_ENV=production yarn build" | ||
}, | ||
@@ -49,3 +50,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@pollyjs/adapter": "^0.0.2", | ||
"@pollyjs/adapter": "^0.1.0", | ||
"@sindresorhus/fnv1a": "^1.0.0", | ||
@@ -52,0 +53,0 @@ "blueimp-md5": "^2.10.0", |
@@ -1,5 +0,4 @@ | ||
<!-- <p align="center"> | ||
<img alt="Polly.JS" width="480px" src="" /> | ||
</p> --> | ||
<h1 align="center">Polly.JS</h1> | ||
<p align="center"> | ||
<img alt="Polly.JS" width="400px" src="https://netflix.github.io/pollyjs/assets/images/wordmark-logo-alt.png" /> | ||
</p> | ||
<h2 align="center">Record, Replay, and Stub HTTP Interactions</h2> | ||
@@ -6,0 +5,0 @@ |
@@ -21,3 +21,3 @@ const ACTIONS = { | ||
.any() | ||
.on('afterResponse', (...args) => this.logRequest(...args)); | ||
.on('response', (...args) => this.logRequest(...args)); | ||
} | ||
@@ -27,3 +27,3 @@ | ||
this.groupEnd(); | ||
this._middleware.off('afterResponse'); | ||
this._middleware.off('response'); | ||
} | ||
@@ -30,0 +30,0 @@ |
@@ -37,5 +37,2 @@ import md5 from 'blueimp-md5'; | ||
// This will be overridden by the route when a method is invoked | ||
this.params = {}; | ||
// Lookup the associated route for this request | ||
@@ -111,4 +108,4 @@ this[ROUTE] = polly.server.lookup(this.method, this.url); | ||
async setup() { | ||
// Trigger the `beforeRequest` event | ||
await this._trigger('beforeRequest'); | ||
// Trigger the `request` event | ||
await this._trigger('request'); | ||
@@ -156,6 +153,6 @@ // Setup the response | ||
// Trigger the `afterResponse` event | ||
await this._trigger('afterResponse', this.response); | ||
freeze(this); | ||
freeze(this); | ||
// Trigger the `response` event | ||
await this._trigger('response', this.response); | ||
} | ||
@@ -171,4 +168,4 @@ | ||
_trigger(methodName, ...args) { | ||
return this[ROUTE].trigger(methodName, this, ...args); | ||
_trigger(eventName, ...args) { | ||
return this[ROUTE].trigger(eventName, this, ...args); | ||
} | ||
@@ -175,0 +172,0 @@ |
import stringify from 'json-stable-stringify'; | ||
import assert from '../utils/assert'; | ||
@@ -9,9 +10,5 @@ const { freeze } = Object; | ||
function status(statusCode) { | ||
return parseInt(statusCode, 10) || null; | ||
} | ||
export default class PollyResponse { | ||
constructor(statusCode, headers, body) { | ||
this.statusCode = status(statusCode); | ||
this.status(statusCode || 200); | ||
this.headers = headers || {}; | ||
@@ -26,4 +23,11 @@ this.body = body; | ||
status(statusCode) { | ||
this.statusCode = status(statusCode); | ||
const status = parseInt(statusCode, 10); | ||
assert( | ||
`[Response] Invalid status code: ${status}`, | ||
status >= 100 && status < 600 | ||
); | ||
this.statusCode = status; | ||
return this; | ||
@@ -30,0 +34,0 @@ } |
@@ -49,3 +49,3 @@ import timestamp from '../utils/timestamp'; | ||
/* | ||
Trigger the `beforeRecord` hook on each new recorded entry. | ||
Trigger the `beforePersist` event on each new recorded entry. | ||
@@ -55,3 +55,3 @@ NOTE: This must be triggered last as this entry can be used to | ||
*/ | ||
await request._trigger('beforeRecord', entry); | ||
await request._trigger('beforePersist', entry); | ||
} | ||
@@ -58,0 +58,0 @@ |
@@ -53,3 +53,3 @@ import stringify from 'json-stable-stringify'; | ||
/** | ||
* 204 - No Content. Polly uses this status code in place of 404 | ||
* 204 - No Content. Polly uses this status code in place of 404 | ||
* when interacting with our Rest server to prevent throwing | ||
@@ -56,0 +56,0 @@ * request errors in consumer's stdout (console.log) |
export default [ | ||
'beforeRequest', | ||
'request', | ||
'beforeReplay', | ||
'beforeRecord', | ||
'beforePersist', | ||
'beforeResponse', | ||
'afterResponse' | ||
'response' | ||
]; |
import assert from '../utils/assert'; | ||
import Events from './events'; | ||
const EVENTS = Symbol(); | ||
function assertEventName(eventName) { | ||
@@ -19,2 +21,7 @@ assert( | ||
export default class Handler extends Map { | ||
constructor() { | ||
super(...arguments); | ||
this.set(EVENTS, new Map()); | ||
} | ||
on(eventName, handler) { | ||
@@ -24,11 +31,13 @@ assertEventName(eventName); | ||
assert( | ||
`Attempted to register ${eventName} but invalid handler provided. Expected function, received: "${typeof handler}".`, | ||
`Attempted to register "${eventName}" but invalid handler provided. Expected function, received: "${typeof handler}".`, | ||
typeof handler === 'function' | ||
); | ||
if (!this.has(eventName)) { | ||
this.set(eventName, []); | ||
const events = this.get(EVENTS); | ||
if (!events.has(eventName)) { | ||
events.set(eventName, []); | ||
} | ||
this.get(eventName).push(handler); | ||
events.get(eventName).push(handler); | ||
@@ -41,7 +50,9 @@ return this; | ||
if (this.hasEvent(eventName)) { | ||
const events = this.get(EVENTS); | ||
if (this._hasEventHandlers(eventName)) { | ||
if (typeof handler === 'function') { | ||
this.set(eventName, this.get(eventName).filter(l => l !== handler)); | ||
events.set(eventName, events.get(eventName).filter(h => h !== handler)); | ||
} else { | ||
this.delete(eventName); | ||
events.delete(eventName); | ||
} | ||
@@ -53,5 +64,11 @@ } | ||
hasEvent(eventName) { | ||
return this.has(eventName) && this.get(eventName).length > 0; | ||
_hasEventHandlers(eventName) { | ||
return this._getEventHandlers(eventName).length > 0; | ||
} | ||
_getEventHandlers(eventName) { | ||
const events = this.get(EVENTS); | ||
return events.has(eventName) ? events.get(eventName) : []; | ||
} | ||
} |
@@ -11,2 +11,3 @@ import RouteRecognizer from 'route-recognizer'; | ||
import removeHostFromUrl from '../utils/remove-host-from-url'; | ||
import castArray from 'lodash-es/castArray'; | ||
@@ -23,3 +24,2 @@ const HOST = Symbol(); | ||
const { keys } = Object; | ||
const { isArray } = Array; | ||
@@ -38,6 +38,2 @@ function parseUrl(url) { | ||
function makeArray(a) { | ||
return isArray(a) ? a : [a]; | ||
} | ||
export default class Server { | ||
@@ -118,3 +114,3 @@ constructor() { | ||
makeArray(routes).forEach(route => { | ||
castArray(routes).forEach(route => { | ||
const { host, path } = parseUrl(this._buildUrl(route)); | ||
@@ -133,3 +129,3 @@ const registry = this._registryForHost(host); | ||
makeArray(routes).forEach(route => { | ||
castArray(routes).forEach(route => { | ||
/* | ||
@@ -136,0 +132,0 @@ If the route is a '*' or '' and there is no host or namespace |
import Handler from './handler'; | ||
async function invoke(handler, route, req, ...args) { | ||
if (typeof handler === 'function') { | ||
const params = req.params; | ||
async function invoke(fn, route, req, ...args) { | ||
if (typeof fn === 'function') { | ||
const proxyReq = new Proxy(req, { | ||
get(source, prop) { | ||
if (prop === 'params') { | ||
// Set the request's params to given route's matched params | ||
return route.params; | ||
} | ||
// Set the request's params to given route's matched params | ||
req.params = route.params || {}; | ||
return source[prop]; | ||
} | ||
}); | ||
const result = await handler(req, ...args); | ||
// Reset the params object to what it was before | ||
req.params = params; | ||
return result; | ||
return await fn(proxyReq, ...args); | ||
} | ||
@@ -20,8 +21,6 @@ } | ||
async function trigger(route, eventName, ...args) { | ||
if (route.handler.hasEvent(eventName)) { | ||
const handlers = route.handler.get(eventName); | ||
const handlers = route.handler._getEventHandlers(eventName); | ||
for (const handler of handlers) { | ||
await invoke(handler, route, ...args); | ||
} | ||
for (const handler of handlers) { | ||
await invoke(handler, route, ...args); | ||
} | ||
@@ -28,0 +27,0 @@ } |
@@ -16,9 +16,9 @@ import { afterEach, beforeEach } from './lib'; | ||
export default function setupMocha(defaults = {}) { | ||
setupMocha.beforeEach(defaults); | ||
setupMocha.afterEach(); | ||
export default function setupMocha(defaults = {}, ctx = self) { | ||
setupMocha.beforeEach(defaults, ctx); | ||
setupMocha.afterEach(ctx); | ||
} | ||
setupMocha.beforeEach = function setupMochaBeforeEach(defaults) { | ||
self.beforeEach(function() { | ||
setupMocha.beforeEach = function setupMochaBeforeEach(defaults, ctx = self) { | ||
ctx.beforeEach(function() { | ||
return beforeEach(this, generateRecordingName(this), defaults); | ||
@@ -28,6 +28,6 @@ }); | ||
setupMocha.afterEach = function setupMochaAfterEach() { | ||
self.afterEach(function() { | ||
setupMocha.afterEach = function setupMochaAfterEach(ctx = self) { | ||
ctx.afterEach(function() { | ||
return afterEach(this, 'mocha'); | ||
}); | ||
}; |
import URL from 'url-parse'; | ||
import isObject from './is-object'; | ||
import isObjectLike from 'lodash-es/isObjectLike'; | ||
@@ -18,3 +18,3 @@ const { keys } = Object; | ||
if (isObject(query)) { | ||
if (isObjectLike(query)) { | ||
const sortedQuery = keys(query) | ||
@@ -35,3 +35,3 @@ .sort() | ||
export function headers(headers, config) { | ||
if (isObject(config) && isObject(headers) && config.exclude) { | ||
if (isObjectLike(config) && isObjectLike(headers) && config.exclude) { | ||
// Filter out excluded headers | ||
@@ -38,0 +38,0 @@ return keys(headers).reduce((h, k) => { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6494913
9219
6
44
106
+ Added@pollyjs/adapter@0.1.0(transitive)
- Removed@pollyjs/adapter@0.0.2(transitive)
Updated@pollyjs/adapter@^0.1.0