Skip to content

Commit

Permalink
feat!: Cleanup adapter and persister APIs (#429)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

- Adapter
	- `passthroughRequest` renamed to `onFetchResponse`
	- `respondToRequest` renamed to `onRespond`

- Persister
	- `findRecording` renamed to `onFindRecording`
	- `saveRecording` renamed to `onSaveRecording`
	- `deleteRecording` renamed to `onDeleteRecording`
  • Loading branch information
offirgolan authored Nov 30, 2021
1 parent 3ab0d1d commit 06499fc
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 277 deletions.
12 changes: 6 additions & 6 deletions docs/adapters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class CustomAdapter extends Adapter {
/* Do something when the adapter is disconnected from */
}

async passthroughRequest(pollyRequest) {
async onFetchResponse(pollyRequest) {
/* Do something when the adapter is connect to */
}

/* optional */
async respondToRequest(pollyRequest) {
async onRespond(pollyRequest) {
const { statusCode, body, headers } = pollyRequest.response;
/* Deliver the response to the user */
}
Expand All @@ -57,12 +57,12 @@ The `Adapter` class provides the `handleRequest()` method which can be
called from `onConnect`. It accepts request parameters and returns a
PollyRequest object with a `response` property.

The `passthroughRequest` method takes a PollyRequest object, makes a real HTTP
The `onFetchResponse` method takes a PollyRequest object, makes a real HTTP
request and returns the response as a `{ statusCode, headers, body }` object,
where `body` is a string.

The `respondToRequest()` method makes sure that the response has been delivered
to the user. `pollyjs.flush()` will wait for all `respondToRequests()` calls to
The `onRespond()` method makes sure that the response has been delivered
to the user. `pollyjs.flush()` will wait for all `onResponds()` calls to
finish. You can omit the implementation of this method if no asynchronous
delivery is required.

Expand Down Expand Up @@ -103,7 +103,7 @@ class FetchAdapter extends Adapter {
window.fetch = this.originalFetch;
}

async passthroughRequest(pollyRequest) {
async onFetchResponse(pollyRequest) {
const response = await this.originalFetch([
pollyRequest.url,
{
Expand Down
6 changes: 3 additions & 3 deletions docs/persisters/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class CustomPersister extends Persister {
return 'custom';
}

findRecording() {}
onFindRecording() {}

saveRecording() {}
onSaveRecording() {}

deleteRecording() {}
onDeleteRecording() {}
}
```

Expand Down
10 changes: 2 additions & 8 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ export default class FetchAdapter extends Adapter {
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 Expand Up @@ -153,7 +147,7 @@ export default class FetchAdapter extends Adapter {
}
}

async passthroughRequest(pollyRequest) {
async onFetchResponse(pollyRequest) {
const { context } = this.options;
const { options } = pollyRequest.requestArguments;

Expand Down Expand Up @@ -195,7 +189,7 @@ export default class FetchAdapter extends Adapter {
};
}

respondToRequest(pollyRequest, error) {
onRespond(pollyRequest, error) {
const {
context: { Response }
} = this.options;
Expand Down
10 changes: 2 additions & 8 deletions packages/@pollyjs/adapter-node-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ export default class HttpAdapter extends Adapter {
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 Expand Up @@ -187,7 +181,7 @@ export default class HttpAdapter extends Adapter {
}
}

async passthroughRequest(pollyRequest) {
async onFetchResponse(pollyRequest) {
const { parsedArguments } = pollyRequest.requestArguments;
const { method, headers, body } = pollyRequest;
const { options } = parsedArguments;
Expand Down Expand Up @@ -230,7 +224,7 @@ export default class HttpAdapter extends Adapter {
};
}

async respondToRequest(pollyRequest, error) {
async onRespond(pollyRequest, error) {
const { req, respond } = pollyRequest.requestArguments;
const { statusCode, body, headers, encoding } = pollyRequest.response;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ describe('Integration | Jest | Fetch', function () {
expect((await fetch('http://localhost:4000/api/db/foo')).status).toBe(404);
await persister.persist();

const har = await persister.find(recordingId);
const har = await persister.findRecording(recordingId);

expect(har).toBeDefined();
expect(har.log.entries.length).toBe(1);
expect(har.log.entries[0].request.url.includes('/api/db/foo')).toBe(true);
expect(har.log.entries[0].response.status).toBe(404);

await persister.delete(recordingId);
expect(persister.find(recordingId)).resolves.toBeNull();
await persister.deleteRecording(recordingId);
expect(persister.findRecording(recordingId)).resolves.toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ describe('Integration | Jest | XHR', function () {
expect((await request('/api/db/foo')).status).toBe(404);
await persister.persist();

const har = await persister.find(recordingId);
const har = await persister.findRecording(recordingId);

expect(har).toBeDefined();
expect(har.log.entries.length).toBe(1);
expect(har.log.entries[0].request.url.includes('/api/db/foo')).toBe(true);
expect(har.log.entries[0].response.status).toBe(404);

await persister.delete(recordingId);
expect(persister.find(recordingId)).resolves.toBeNull();
await persister.deleteRecording(recordingId);
expect(persister.findRecording(recordingId)).resolves.toBeNull();
});
});
10 changes: 2 additions & 8 deletions packages/@pollyjs/adapter-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ export default class PuppeteerAdapter extends Adapter {
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 Expand Up @@ -160,7 +154,7 @@ export default class PuppeteerAdapter extends Adapter {
this._requestsMapping.pollyRequests.set(request, pollyRequest);
}

async passthroughRequest(pollyRequest) {
async onFetchResponse(pollyRequest) {
const { page } = this.options;
const { id, order, url, method, headers, body } = pollyRequest;
const requestId = `${this.polly.recordingId}:${id}:${order}`;
Expand Down Expand Up @@ -199,7 +193,7 @@ export default class PuppeteerAdapter extends Adapter {
}
}

async respondToRequest(pollyRequest, error) {
async onRespond(pollyRequest, error) {
const { request } = pollyRequest.requestArguments;
const { response } = pollyRequest;

Expand Down
10 changes: 2 additions & 8 deletions packages/@pollyjs/adapter-xhr/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ export default class XHRAdapter extends Adapter {
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 Expand Up @@ -78,7 +72,7 @@ export default class XHRAdapter extends Adapter {
}
}

async passthroughRequest(pollyRequest) {
async onFetchResponse(pollyRequest) {
const { xhr: fakeXhr } = pollyRequest.requestArguments;
const xhr = new this.NativeXMLHttpRequest();

Expand Down Expand Up @@ -143,7 +137,7 @@ export default class XHRAdapter extends Adapter {
};
}

respondToRequest(pollyRequest, error) {
onRespond(pollyRequest, error) {
const { xhr } = pollyRequest.requestArguments;

if (pollyRequest[ABORT_HANDLER]) {
Expand Down
Loading

0 comments on commit 06499fc

Please sign in to comment.