-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Custom persister support * Create a @pollyjs/persister package * Move out shared utils into their own @pollyjs/utils package * Add support to register a custom persister (same way as an adapter) * Add more tests * docs: Custom adapter & persister docs * test: Add custom persister test
- Loading branch information
1 parent
46bc3a2
commit 8bb313c
Showing
50 changed files
with
3,193 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Custom Adapter | ||
|
||
If you need to create your own adapter or modify an pre-existing one, you've come | ||
to the right page! | ||
|
||
## Creating a Custom Adapter | ||
|
||
The `@pollyjs/adapter` package provides an extendable base adapter class that | ||
contains core logic dependent on by the [Fetch](adapters/fetch) | ||
& [XHR](adapters/xhr) adapters. | ||
|
||
### Installation | ||
|
||
_Note that you must have node (and npm) installed._ | ||
|
||
```bash | ||
npm install @pollyjs/adapter -D | ||
``` | ||
|
||
If you want to install it with [yarn](https://yarnpkg.com): | ||
|
||
```bash | ||
yarn add @pollyjs/adapter -D | ||
``` | ||
|
||
### Usage | ||
|
||
```js | ||
import Adapter from '@pollyjs/adapter'; | ||
|
||
class CustomAdapter extends Adapter { | ||
onConnect() { | ||
/* Do something when the adapter is connect to */ | ||
} | ||
|
||
onDisconnect() { | ||
/* Do something when the adapter is disconnected from */ | ||
} | ||
|
||
toString() { | ||
return '[Adapter: CustomAdapter]'; | ||
} | ||
} | ||
``` | ||
|
||
For better usage examples, please refer to the source code for | ||
the [Fetch](https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/core/src/adapters/fetch/index.js) & [XHR](https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/core/src/adapters/xhr/index.js) adapters. | ||
|
||
## Extending from an Existing Adapter | ||
|
||
The `@pollyjs/core` package exports the `XHRAdapter` and `FetchAdapter` classes, | ||
allowing you to modify them as needed. | ||
|
||
```js | ||
import { XHRAdapter, FetchAdapter } from '@pollyjs/core'; | ||
|
||
class CustomXHRAdapter extends XHRAdapter {} | ||
class CustomFetchAdapter extends FetchAdapter {} | ||
``` | ||
|
||
## Registering & Connecting to a Custom Adapter | ||
|
||
You can register and connect to a custom adapter by passing an array to the `adapters` | ||
config where the first element is the name of your adapter and the second is the | ||
adapter class. | ||
|
||
```js | ||
// Register and connect to a custom adapter: | ||
new Polly('Custom Adapter', { | ||
adapters: [ | ||
['my-custom-adapter', MyCustomAdapterClass] | ||
] | ||
}); | ||
|
||
// Register and connect to the default adapters + a custom adapter: | ||
new Polly('Defaults + Custom Adapter', { | ||
adapters: [ | ||
'fetch', | ||
'xhr', | ||
['my-custom-adapter', MyCustomAdapterClass] | ||
] | ||
}); | ||
|
||
// Register and connect to a custom fetch adapter: | ||
new Polly('Custom Fetch Adapter', { | ||
adapters: [ | ||
['fetch', MyCustomFetchAdapterClass] | ||
] | ||
}); | ||
|
||
// Register and connect to a custom adapter via .configure(): | ||
const polly = new Polly('Custom Adapter'); | ||
|
||
polly.configure({ | ||
adapters: [ | ||
['my-custom-adapter', MyCustomAdapterClass] | ||
] | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Custom Persister | ||
|
||
If you need to create your own persister or modify an pre-existing one, you've come | ||
to the right page! | ||
|
||
## Creating a Custom Persister | ||
|
||
The `@pollyjs/persister` package provides an extendable base persister class that | ||
contains core logic dependent on by the [REST](persisters/rest) | ||
& [Local Storage](persisters/local-storage) persisters. | ||
|
||
### Installation | ||
|
||
_Note that you must have node (and npm) installed._ | ||
|
||
```bash | ||
npm install @pollyjs/persister -D | ||
``` | ||
|
||
If you want to install it with [yarn](https://yarnpkg.com): | ||
|
||
```bash | ||
yarn add @pollyjs/persister -D | ||
``` | ||
|
||
### Usage | ||
|
||
```js | ||
import Persister from '@pollyjs/persister'; | ||
|
||
class CustomPersister extends Persister { | ||
findRecordingEntry() {} | ||
|
||
findRecording() {} | ||
|
||
saveRecording() {} | ||
|
||
deleteRecording() {} | ||
} | ||
``` | ||
|
||
For better usage examples, please refer to the source code for | ||
the [REST](https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/core/src/persisters/rest/index.js) & [Local Storage](https://github.com/Netflix/pollyjs/blob/master/packages/%40pollyjs/core/src/persisters/local-storage/index.js) persisters. | ||
|
||
## Extending from an Existing Persister | ||
|
||
The `@pollyjs/core` package exports the `RESTPersister` and `LocalStoragePersister` classes, | ||
allowing you to modify them as needed. | ||
|
||
```js | ||
import { RESTPersister, LocalStoragePersister } from '@pollyjs/core'; | ||
|
||
class CustomRESTPersister extends RESTPersister {} | ||
class CustomLocalStoragePersister extends LocalStoragePersister {} | ||
``` | ||
|
||
## Registering & Connecting to a Custom Persister | ||
|
||
You can register and connect to a custom persister by passing an array to the `persister` | ||
config where the first element is the name of your persister and the second is the | ||
persister class. | ||
|
||
```js | ||
// Register and connect to a custom persister: | ||
new Polly('Custom Persister', { | ||
persister: ['my-custom-persister', MyCustomPersisterClass] | ||
}); | ||
|
||
// Register and connect to a custom persister via .configure(): | ||
const polly = new Polly('Custom Persister'); | ||
|
||
polly.configure({ | ||
persister: ['my-custom-persister', MyCustomPersisterClass] | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Adapter from '../../src'; | ||
|
||
describe('Unit | Adapter', function() { | ||
it('should exist', function() { | ||
expect(Adapter).to.be.a('function'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.