Skip to content

Commit

Permalink
fix: Deprecates adapter & persister name in favor of id (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit authored Feb 21, 2020
1 parent 0bb2f5a commit 41dd093
Show file tree
Hide file tree
Showing 24 changed files with 177 additions and 97 deletions.
11 changes: 5 additions & 6 deletions docs/adapters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ yarn add @pollyjs/adapter -D
import Adapter from '@pollyjs/adapter';

class CustomAdapter extends Adapter {
static get name() {
static get id() {
return 'custom';
}

Expand All @@ -47,7 +47,7 @@ class CustomAdapter extends Adapter {

/* optional */
async respondToRequest(pollyRequest) {
const { statusCode, body, headers } = pollyRequest.response
const { statusCode, body, headers } = pollyRequest.response;
/* Deliver the response to the user */
}
}
Expand All @@ -74,10 +74,9 @@ full examples, please refer to the source code for the
& [XHR](https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/adapter-xhr/src/index.js)
adapters.


```js
class FetchAdapter extends Adapter {
static get name() {
static get id() {
return 'fetch';
}

Expand All @@ -89,7 +88,7 @@ class FetchAdapter extends Adapter {
url,
method: options.method,
headers: options.headers,
body: options.body,
body: options.body
});

return new Response(response.body, {
Expand All @@ -100,7 +99,7 @@ class FetchAdapter extends Adapter {
}

onDisconnect() {
window.fetch = this.originalFetch
window.fetch = this.originalFetch;
}

async passthroughRequest(pollyRequest) {
Expand Down
2 changes: 1 addition & 1 deletion docs/persisters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ yarn add @pollyjs/persister -D
import Persister from '@pollyjs/persister';

class CustomPersister extends Persister {
static get name() {
static get id() {
return 'custom';
}

Expand Down
8 changes: 7 additions & 1 deletion packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ const IS_STUBBED = Symbol();
const REQUEST_ARGUMENTS = Symbol();

export default class FetchAdapter extends Adapter {
static get name() {
static get id() {
return 'fetch';
}

static get name() {
// NOTE: deprecated in 4.1.0 but proxying since it's possible "core" is behind
// and therefore still referencing `name`. Remove in 5.0.0
return this.id;
}

get defaultOptions() {
return {
context: global
Expand Down
8 changes: 7 additions & 1 deletion packages/@pollyjs/adapter-node-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ const REQUEST_ARGUMENTS = new WeakMap();
nock.restore();

export default class HttpAdapter extends Adapter {
static get name() {
static get id() {
return 'node-http';
}

static get name() {
// NOTE: deprecated in 4.1.0 but proxying since it's possible "core" is behind
// and therefore still referencing `name`. Remove in 5.0.0
return this.id;
}

onConnect() {
this.assert(
'Running concurrent node-http adapters is unsupported, stop any running Polly instances.',
Expand Down
8 changes: 7 additions & 1 deletion packages/@pollyjs/adapter-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ const PASSTHROUGH_PROMISES = Symbol();
const PASSTHROUGH_REQ_ID_QP = 'pollyjs_passthrough_req_id';

export default class PuppeteerAdapter extends Adapter {
static get name() {
static get id() {
return 'puppeteer';
}

static get name() {
// NOTE: deprecated in 4.1.0 but proxying since it's possible "core" is behind
// and therefore still referencing `name`. Remove in 5.0.0
return this.id;
}

get defaultOptions() {
return {
page: null,
Expand Down
8 changes: 7 additions & 1 deletion packages/@pollyjs/adapter-xhr/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ const SEND = Symbol();
const stubbedXhrs = new WeakSet();

export default class XHRAdapter extends Adapter {
static get name() {
static get id() {
return 'xhr';
}

static get name() {
// NOTE: deprecated in 4.1.0 but proxying since it's possible "core" is behind
// and therefore still referencing `name`. Remove in 5.0.0
return this.id;
}

get defaultOptions() {
return {
context: global
Expand Down
2 changes: 1 addition & 1 deletion packages/@pollyjs/adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ documentation for more details.
import Adapter from '@pollyjs/adapter';

class CustomAdapter extends Adapter {
static get name() {
static get id() {
return 'custom';
}

Expand Down
10 changes: 4 additions & 6 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ export default class Adapter {
}

/* eslint-disable-next-line getter-return */
static get name() {
assert('Must override the static `name` getter.');
static get id() {
assert('Must override the static `id` getter.');
}

get defaultOptions() {
return {};
}

get options() {
const { name } = this.constructor;

return {
...(this.defaultOptions || {}),
...((this.polly.config.adapterOptions || {})[name] || {})
...((this.polly.config.adapterOptions || {})[this.constructor.id] || {})
};
}

Expand Down Expand Up @@ -215,7 +213,7 @@ export default class Adapter {

assert(message, ...args) {
assert(
`[${this.constructor.type}:${this.constructor.name}] ${message}`,
`[${this.constructor.type}:${this.constructor.id}] ${message}`,
...args
);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@pollyjs/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import RESTPersister from '@pollyjs/persister-rest';

/*
Register the adapters and persisters we want to use. This way all future
polly instances can access them by name.
polly instances can access them by name/id.
*/
Polly.register(XHRAdapter);
Polly.register(FetchAdapter);
Expand All @@ -77,6 +77,7 @@ describe('Netflix Homepage', function() {
adapters: ['xhr', 'fetch'],
persister: 'rest'
});

const { server } = polly;

/* Intercept all Google Analytic requests and respond with a 200 */
Expand Down
21 changes: 17 additions & 4 deletions packages/@pollyjs/core/src/-private/container.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { assert } from '@pollyjs/utils';

export function idOfFactory(Factory) {
if (Factory.hasOwnProperty('id')) {
return Factory.id;
}

console.warn(
`[Polly] ${Factory.id}:${Factory.name} "name" is deprecated and has been renamed to "id".`
);

return Factory.name;
}

function keyFor(Factory) {
return `${Factory.type}:${Factory.name}`;
return `${Factory.type}:${idOfFactory(Factory)}`;
}

export default class Container {
export class Container {
constructor() {
this._registry = new Map();
}
Expand All @@ -20,10 +32,11 @@ export default class Container {
typeof Factory === 'function'
);

const { type, name } = Factory;
const { type } = Factory;
const name = idOfFactory(Factory);

assert(
`Invalid registration name provided. Expected string, received: "${typeof name}"`,
`Invalid registration id provided. Expected string, received: "${typeof name}"`,
typeof name === 'string'
);

Expand Down
48 changes: 24 additions & 24 deletions packages/@pollyjs/core/src/polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MODES, assert } from '@pollyjs/utils';
import { version } from '../package.json';

import Logger from './-private/logger';
import Container from './-private/container';
import { Container, idOfFactory } from './-private/container';
import DefaultConfig from './defaults/config';
import PollyRequest from './-private/request';
import guidForRecording from './utils/guid-for-recording';
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class Polly {
if (persister) {
if (typeof persister === 'function') {
container.register(persister);
persister = persister.name;
persister = idOfFactory(persister);
}

assert(
Expand Down Expand Up @@ -221,7 +221,7 @@ export default class Polly {
*/
play() {
if (this[PAUSED_ADAPTERS]) {
this[PAUSED_ADAPTERS].forEach(adapterName => this.connectTo(adapterName));
this[PAUSED_ADAPTERS].forEach(adapterId => this.connectTo(adapterId));
delete this[PAUSED_ADAPTERS];
}
}
Expand Down Expand Up @@ -252,48 +252,48 @@ export default class Polly {
}

/**
* @param {String|Function} nameOrFactory
* @param {String|Function} idOrFactory
* @public
* @memberof Polly
*/
connectTo(nameOrFactory) {
connectTo(idOrFactoryIdGetter) {
const { container, adapters } = this;
let adapterName = nameOrFactory;
let adapterId = idOrFactoryIdGetter;

if (typeof nameOrFactory === 'function') {
container.register(nameOrFactory);
adapterName = nameOrFactory.name;
if (typeof idOrFactoryIdGetter === 'function') {
container.register(idOrFactoryIdGetter);
adapterId = idOfFactory(idOrFactoryIdGetter);
}

assert(
`Adapter matching the name \`${adapterName}\` was not registered.`,
container.has(`adapter:${adapterName}`)
`Adapter matching the name \`${adapterId}\` was not registered.`,
container.has(`adapter:${adapterId}`)
);

this.disconnectFrom(adapterName);
this.disconnectFrom(adapterId);

const adapter = new (container.lookup(`adapter:${adapterName}`))(this);
const adapter = new (container.lookup(`adapter:${adapterId}`))(this);

adapter.connect();
adapters.set(adapterName, adapter);
adapters.set(adapterId, adapter);
}

/**
* @param {String|Function} nameOrFactory
* @param {String|Function} idOrFactoryIdGetter
* @public
* @memberof Polly
*/
disconnectFrom(nameOrFactory) {
disconnectFrom(idOrFactoryIdGetter) {
const { adapters } = this;
let adapterName = nameOrFactory;
let adapterId = idOrFactoryIdGetter;

if (typeof nameOrFactory === 'function') {
adapterName = nameOrFactory.name;
if (typeof idOrFactoryIdGetter === 'function') {
adapterId = idOfFactory(idOrFactoryIdGetter);
}

if (adapters.has(adapterName)) {
adapters.get(adapterName).disconnect();
adapters.delete(adapterName);
if (adapters.has(adapterId)) {
adapters.get(adapterId).disconnect();
adapters.delete(adapterId);
}
}

Expand All @@ -302,8 +302,8 @@ export default class Polly {
* @memberof Polly
*/
disconnect() {
for (const adapterName of this.adapters.keys()) {
this.disconnectFrom(adapterName);
for (const adapterId of this.adapters.keys()) {
this.disconnectFrom(adapterId);
}
}

Expand Down
Loading

0 comments on commit 41dd093

Please sign in to comment.