Skip to content

Commit

Permalink
feat: Keyed persister & adapter options (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored and jasonmit committed Jul 10, 2018
1 parent 73026d4 commit 29ed8e1
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 94 deletions.
8 changes: 4 additions & 4 deletions docs/adapters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ yarn add @pollyjs/adapter -D
import Adapter from '@pollyjs/adapter';

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

onConnect() {
/* Do something when the adapter is connect to */
}

onDisconnect() {
/* Do something when the adapter is disconnected from */
}

toString() {
return '[Adapter: CustomAdapter]';
}
}
```

Expand Down
38 changes: 27 additions & 11 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ polly.configure({
});
```

## adapterOptions

_Type_: `Object`
_Default_: `{}`

Options to be passed into the adapters keyed by the adapter name.

?> __Note:__ Check out the appropriate documentation pages for each adapter
for more details.

__Example__

```js
polly.configure({
adapterOptions: {
fetch: {
context: win
}
}
});
```

## persister

_Type_: `String`
Expand All @@ -159,17 +181,9 @@ polly.configure({
## persisterOptions

_Type_: `Object`
_Default_: `{}`

_Default_:

```js
{
host: '',
apiNamespace: '/polly'
}
```

Options to be passed into the persister.
Options to be passed into the persister keyed by the persister name.

?> __Note:__ Check out the appropriate documentation pages for each persister
for more details.
Expand All @@ -179,7 +193,9 @@ __Example__
```js
polly.configure({
persisterOptions: {
apiNamespace: '/pollyjs'
rest: {
apiNamespace: '/pollyjs'
}
}
});
```
Expand Down
40 changes: 38 additions & 2 deletions docs/persisters/local-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,42 @@ const polly = new Polly('<Recording Name>', {
});
```

## Options {docsify-ignore}
## Options

_There are currently no available configuration options for this persister._
### context

_Type_: `Object`
_Default_: `global`

The context object of where the `localStorage` reference exists.

__Example__

```js
polly.configure({
persisterOptions: {
'local-storage': {
context: win
}
}
});
```

### key

_Type_: `String`
_Default_: `'pollyjs'`

The localStorage key to store the recordings data under.

__Example__

```js
polly.configure({
persisterOptions: {
'local-storage': {
key: '__pollyjs__'
}
}
});
```
16 changes: 11 additions & 5 deletions docs/persisters/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ as well as a [CLI](cli/overview) to get you up and running.
new Polly('<Recording Name>', {
persister: 'rest',
persisterOptions: {
host: '',
apiNamespace: '/polly'
rest: {
host: '',
apiNamespace: '/polly'
}
}
});
```
Expand All @@ -25,7 +27,7 @@ new Polly('<Recording Name>', {
### host

_Type_: `String`
_Default_: `''`
_Default_: `'http://localhost:3000'`

The host that the API exists on.

Expand All @@ -34,7 +36,9 @@ __Example__
```js
polly.configure({
persisterOptions: {
host: 'http://netflix.com:3000'
rest: {
host: 'http://localhost.com:4000'
}
}
});
```
Expand All @@ -58,7 +62,9 @@ __Example__
```js
polly.configure({
persisterOptions: {
apiNamespace: '/pollyjs'
rest: {
apiNamespace: '/pollyjs'
}
}
});
```
8 changes: 4 additions & 4 deletions packages/@pollyjs/adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ documentation for more details.
import Adapter from '@pollyjs/adapter';

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

onConnect() {
/* Do something when the adapter is connect to */
}

onDisconnect() {
/* Do something when the adapter is disconnected from */
}

toString() {
return '[Adapter: CustomAdapter]';
}
}
```

Expand Down
49 changes: 34 additions & 15 deletions packages/@pollyjs/adapter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ export default class Adapter {
this.isConnected = false;
}

static get type() {
return 'adapter';
}

static get name() {
assert('Must override the static `name` getter.', false);
}

get defaultOptions() {
return {};
}

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

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

get persister() {
return this.polly.persister;
}
Expand Down Expand Up @@ -42,7 +63,7 @@ export default class Adapter {
return false;
}

if (!navigator.onLine) {
if (navigator && !navigator.onLine) {
console.warn(
'[Polly] Recording for the following request has expired but the browser is offline.\n' +
`${recordingEntry.request.method} ${recordingEntry.request.url}\n`,
Expand Down Expand Up @@ -171,24 +192,18 @@ export default class Adapter {
}

assert(message, ...args) {
assert(`${this} ${message}`, ...args);
}

toString() {
/* cannot use this.assert since `this` calls toString */
assert('Must implement the the `toString` hook.', false);
assert(
`[${this.constructor.type}:${this.constructor.name}] ${message}`,
...args
);
}

onConnect() {
this.assert('Must implement the `onConnect` hook.', false);
}

onPassthrough() {
this.assert('Must implement the `onPassthrough` hook.', false);
}

onIntercept() {
this.assert('Must implement the `onIntercept` hook.', false);
onDisconnect() {
this.assert('Must implement the `onDisconnect` hook.', false);
}

onRecord() {
Expand All @@ -199,7 +214,11 @@ export default class Adapter {
this.assert('Must implement the `onReplay` hook.', false);
}

onDisconnect() {
this.assert('Must implement the `onDisconnect` hook.', false);
onIntercept() {
this.assert('Must implement the `onIntercept` hook.', false);
}

onPassthrough() {
this.assert('Must implement the `onPassthrough` hook.', false);
}
}
8 changes: 4 additions & 4 deletions packages/@pollyjs/core/src/adapters/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const nativeFetch = global.fetch;
const { defineProperty } = Object;

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

onConnect() {
this.assert('Fetch global not found.', nativeFetch);
this.assert(
Expand Down Expand Up @@ -82,8 +86,4 @@ export default class FetchAdapter extends Adapter {

return fetchResponse;
}

toString() {
return '[Adapter: Fetch]';
}
}
8 changes: 4 additions & 4 deletions packages/@pollyjs/core/src/adapters/xhr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import serializeResponseHeaders from './utils/serialize-response-headers';
const SEND = Symbol();

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

onConnect() {
this.assert('XHR global not found.', FakeXHR.xhr.supportsXHR);
this.assert(
Expand Down Expand Up @@ -93,8 +97,4 @@ export default class XHRAdapter extends Adapter {
xhr.responseText
);
}

toString() {
return '[Adapter: XHR]';
}
}
7 changes: 3 additions & 4 deletions packages/@pollyjs/core/src/defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Timing from '../utils/timing';

export default {
mode: MODES.REPLAY,

adapters: ['fetch', 'xhr'],
adapterOptions: {},

logging: false,

Expand All @@ -15,10 +17,7 @@ export default {
timing: Timing.fixed(0),

persister: 'rest',
persisterOptions: {
host: 'http://localhost:3000',
apiNamespace: '/polly'
},
persisterOptions: {},

matchRequestsBy: {
method: true,
Expand Down
32 changes: 25 additions & 7 deletions packages/@pollyjs/core/src/persisters/local-storage/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import Persister from '@pollyjs/persister';

const { parse } = JSON;

export default class LocalStoragePersister extends Persister {
constructor(polly, store = global.localStorage) {
super(polly);
this._store = store;
this._namespace = '__pollyjs__';
static get name() {
return 'local-storage';
}

get defaultOptions() {
return {
key: 'pollyjs',
context: global
};
}

get localStorage() {
const { context } = this.options;

this.assert(
`Could not find "localStorage" on the given context "${context}".`,
context && context.localStorage
);

return context.localStorage;
}

get db() {
const items = this._store.getItem(this._namespace);
const items = this.localStorage.getItem(this.options.key);

return items ? JSON.parse(items) : {};
return items ? parse(items) : {};
}

set db(db) {
this._store.setItem(this._namespace, this.stringify(db));
this.localStorage.setItem(this.options.key, this.stringify(db));
}

findRecording(recordingId) {
Expand Down
Loading

0 comments on commit 29ed8e1

Please sign in to comment.