Skip to content

Commit

Permalink
🎨 Moves polyfills to the config object
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 4, 2017
1 parent 112b9cc commit 2820e34
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 60 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,25 @@ For older environment without fetch support, you should get a [polyfill](https:/

## Node.js

Works with [FormData](https:/form-data/form-data) and [fetch](https://www.npmjs.com/package/node-fetch) polyfills.
Works with any [FormData](https:/form-data/form-data) or [fetch](https://www.npmjs.com/package/node-fetch) polyfills.

```javascript
// The global way :

global.fetch = require("node-fetch")
global.FormData = require("form-data")
global.URLSearchParams = require("url").URLSearchParams

// Or the non-global way :

const fetch = require("node-fetch")
const FormData = require("form-data")

wretch().polyfills({
fetch: fetch,
FormData: FormData,
URLSearchParams: require("url").URLSearchParams
})
```

# Usage
Expand Down Expand Up @@ -254,7 +268,6 @@ wretch().defaults({ encoding: "same-origin", headers: { "X-Custom": "Header" }},
*/
```


#### errorType(method: "text" | "json" = "text")

Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.
Expand All @@ -273,6 +286,21 @@ wretch("http://server/which/returns/an/error/with/a/json/body")
}
```
#### polyfills(polyfills: Object)
Sets the non-global polyfills which will be used for every subsequent calls.
```javascript
const fetch = require("node-fetch")
const FormData = require("form-data")

wretch().polyfills({
fetch: fetch,
FormData: FormData,
URLSearchParams: require("url").URLSearchParams
})
```
#### catcher(code: number, catcher: (error: WretcherError) => void)
Adds a [catcher](https:/elbywan/wretch#catchers) which will be called on every subsequent request error.
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle/wretch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/bundle/wretch.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
declare const _default: {
defaults: {};
errorType: any;
polyfills: {
fetch: any;
FormData: any;
URLSearchParams: any;
};
};
export default _default;
8 changes: 7 additions & 1 deletion dist/config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/config.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/resolver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export declare type WretcherError = Error & {
text?: string;
json?: any;
};
export declare const resolver: (url: any, fetchF: (input: RequestInfo, init?: RequestInit) => Promise<Response>) => (catchers?: Map<number, (error: WretcherError) => void>) => (opts?: {}) => {
export declare const resolver: (url: any) => (catchers?: Map<number, (error: WretcherError) => void>) => (opts?: {}) => {
res: <Result = Response>(cb?: (type: void) => Result) => Promise<Result>;
json: <Result = {
[key: string]: any;
Expand Down
4 changes: 2 additions & 2 deletions dist/resolver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/resolver.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2820e34

Please sign in to comment.