-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'isomorphic-fetch'; | ||
|
||
import { expect } from 'chai'; | ||
import { catchErrors } from '../index'; | ||
|
||
const echo = (input, options) => | ||
Promise.resolve({ input, options }); | ||
|
||
describe('catchErrors', () => { | ||
it('do notthing for normal request', () => { | ||
expect(() => { | ||
catchErrors()(echo, 'url', { | ||
method: 'post', | ||
body: JSON.stringify({ name: 'catchErrors' }), | ||
}); | ||
}).to.not.throw(); | ||
}); | ||
|
||
describe('when the options has a data property', () => { | ||
it('throws an error', () => | ||
expect(() => { | ||
catchErrors()(echo, 'url', { | ||
method: 'post', | ||
data: JSON.stringify({ name: 'catchErrors' }), | ||
}); | ||
}).to.throw(/Did you try to use 'body:/) | ||
); | ||
}); | ||
|
||
describe('when the options has a POJO body property', () => { | ||
it('throws an error', () => | ||
expect(() => { | ||
catchErrors()(echo, 'url', { | ||
method: 'post', | ||
body: { name: 'catchErrors' }, | ||
}); | ||
}).to.throw(/Did you forgot to 'JSON\.stringify/) | ||
); | ||
}); | ||
}); |
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,53 @@ | ||
const OptionsKeyValidators = [ | ||
{ | ||
name: 'data', | ||
validate: (input, options) => { | ||
const verb = (options.method || 'GET').toUpperCase(); | ||
if (verb === 'GET' || verb === 'HEAD') { | ||
return; | ||
} | ||
throw new Error(`Did you try to use 'body: ${JSON.stringify(options.data)}'?`); | ||
}, | ||
}, | ||
{ | ||
name: 'body', | ||
validate: (input, options) => { | ||
const verb = (options.method || 'GET').toUpperCase(); | ||
if (verb === 'GET' || verb === 'HEAD') { | ||
return; | ||
} | ||
function isPojo(obj) { | ||
// Just copy for now. | ||
// https://github.com/nickb1080/is-pojo/blob/master/lib/index.js | ||
if (obj === null || typeof obj !== 'object') { | ||
return false; | ||
} | ||
return Object.getPrototypeOf(obj) === Object.prototype; | ||
} | ||
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch#Parameters | ||
if (isPojo(options.body)) { | ||
throw new Error(`Did you forgot to 'JSON.stringify(${JSON.stringify(options.body)})'`); | ||
} | ||
}, | ||
}, | ||
]; | ||
|
||
function checkOptions(input, options) { | ||
OptionsKeyValidators.forEach(({ name, validate }) => { | ||
if (name in options) { | ||
validate(input, options); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Check agains the final input and options to check if there's missing/misconfigured | ||
* properties. | ||
*/ | ||
export const catchErrors = () => ( | ||
(fetch, input, options = {}) => { | ||
checkOptions(input, options); | ||
|
||
return fetch(input, options); | ||
} | ||
); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './console'; | ||
export * from './catchErrors'; |