Skip to content

Commit

Permalink
feat: Use status code 204 in place of 404. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit authored Jun 8, 2018
1 parent c603e6d commit 930c492
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
4 changes: 3 additions & 1 deletion packages/@pollyjs/core/src/persisters/rest/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ function handleResponse(xhr, resolve, reject) {
}
}

return xhr.status >= 200 && xhr.status < 300 ? resolve(body) : reject(xhr);
return xhr.status >= 200 && xhr.status < 300
? resolve({ body, xhr })
: reject(xhr);
}
44 changes: 25 additions & 19 deletions packages/@pollyjs/core/src/persisters/rest/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ajax from './ajax';
import Persister from '../persister';
import buildUrl from '../../utils/build-url';
import stringify from 'json-stable-stringify';
import buildUrl from '../../utils/build-url';
import Persister from '../persister';
import ajax from './ajax';

export default class RestPersister extends Persister {
ajax(url, ...args) {
Expand All @@ -10,33 +10,25 @@ export default class RestPersister extends Persister {
return ajax(buildUrl(host, apiNamespace, url), ...args);
}

findRecordingEntry(pollyRequest) {
async findRecordingEntry(pollyRequest) {
const { id, order, recordingId } = pollyRequest;

return this.ajax(
const response = await this.ajax(
`/${encodeURIComponent(recordingId)}/${id}?order=${order}`,
{
Accept: 'application/json; charset=utf-8'
}
).catch(e => {
if (e && e.status === 404) {
return null;
}
);

throw e;
});
return this._normalize(response);
}

findRecording(recordingId) {
return this.ajax(`/${encodeURIComponent(recordingId)}`, {
async findRecording(recordingId) {
const response = await this.ajax(`/${encodeURIComponent(recordingId)}`, {
Accept: 'application/json; charset=utf-8'
}).catch(e => {
if (e && e.status === 404) {
return null;
}

throw e;
});

return this._normalize(response);
}

async saveRecording(recordingId, data) {
Expand All @@ -55,4 +47,18 @@ export default class RestPersister extends Persister {
method: 'DELETE'
});
}

_normalize({ xhr, body }) {
/**
* 204 - No Content. Polly uses this status code in place of 404
* when interacting with our Rest server to prevent throwing
* request errors in consumer's stdout (console.log)
*/
if (xhr.status === 204) {
/* return null when a record was not found */
return null;
}

return body;
}
}
8 changes: 4 additions & 4 deletions packages/@pollyjs/node-server/src/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import fs from 'fs-extra';
import path from 'path';

export default class API {
constructor(recordingsDir) {
Expand All @@ -18,7 +18,7 @@ export default class API {
}
}

return this.respond(404);
return this.respond(204);
}

getRecording(recording) {
Expand All @@ -28,7 +28,7 @@ export default class API {
return this.respond(200, fs.readJsonSync(recordingFilename));
}

return this.respond(404);
return this.respond(204);
}

saveRecording(recording, data) {
Expand All @@ -46,7 +46,7 @@ export default class API {
fs.removeSync(recordingFilename);
}

return this.respond(204);
return this.respond(200);
}

filenameFor(recording) {
Expand Down

0 comments on commit 930c492

Please sign in to comment.