Skip to content

Commit

Permalink
🏭 Add early resolvers with the resolve function
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 13, 2017
1 parent 1694396 commit 481803b
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 348 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ Creates a new Wretcher object with an url and [vanilla fetch options](https://de

*Helper methods are optional and can be chained.*

| [url](#urlurl-string-replace-boolean--false) | [query](#queryqp-object) | [options](#optionsoptions-object-mixin-boolean--true) | [headers](#headersheadervalues-object) | [accept](#acceptheadervalue-string) | [content](#contentheadervalue-string) | [auth](#authheadervalue-string) | [catcher](#catchererrorid-number--string-catcher-error-wretchererror--void) | [defaults](#defaultsopts-object-mixin-boolean--false) | [errorType](#errortypemethod-text--json--text) | [polyfills](#polyfillspolyfills-object) |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| [url](#urlurl-string-replace-boolean--false) | [query](#queryqp-object) | [options](#optionsoptions-object-mixin-boolean--true) | [headers](#headersheadervalues-object) | [accept](#acceptheadervalue-string) | [content](#contentheadervalue-string) | [auth](#authheadervalue-string) | [catcher](#catchererrorid-number--string-catcher-error-wretchererror--void) | [resolve](#resolvedoresolve-chain-responsechain--responsechain--promise-clear--false) | [defaults](#defaultsopts-object-mixin-boolean--false) | [errorType](#errortypemethod-text--json--text) | [polyfills](#polyfillspolyfills-object) |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|

#### url(url: string, replace: boolean = false)

Expand Down Expand Up @@ -389,6 +389,30 @@ w.url("http://myapi.com/get/something").get().json(json => /* ... */)
w.url("...").notFound(err => /* overrides the default 'redirect' catcher */)
```

#### resolve(doResolve: (chain: ResponseChain) => ResponseChain | Promise<any>, clear = false)

Programs a resolver which will automatically be injected to perform response chain tasks.

Very useful when you need to perform repetitive actions on the wretch response.

*The clear argument, if set to true, removes previously defined resolvers.*

```js
// Program "response" chain actions early on
const w = wretch()
.resolve(resolver => resolver
.perfs(_ => /* monitor every request */)
.json(_ => _ /* automatically parse and return json */))

const myJson = await w.url("http://a.com").get()
// Equivalent to wretch()
// .url("http://a.com")
// .get()
// <- the resolver chain is automatically injected here !
// .perfs(_ => /* ... */)
// .json(_ => _)
```

#### defaults(opts: Object, mixin: boolean = false)

Sets default fetch options which will be used for every subsequent requests.
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.

25 changes: 13 additions & 12 deletions 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) => (catchers?: Map<string | number, (error: WretcherError) => void>) => (opts?: {}) => {
export declare type ResponseChain = {
res: <Result = Response>(cb?: (type: Response) => Result) => Promise<Result>;
json: <Result = {
[key: string]: any;
Expand All @@ -15,15 +15,16 @@ export declare const resolver: (url: any) => (catchers?: Map<string | number, (e
formData: <Result = FormData>(cb?: (type: FormData) => Result) => Promise<Result>;
arrayBuffer: <Result = ArrayBuffer>(cb?: (type: ArrayBuffer) => Result) => Promise<Result>;
text: <Result = string>(cb?: (type: string) => Result) => Promise<Result>;
perfs: (cb?: (type: any) => void) => any;
setTimeout: (time: number, controller: any) => any;
controller: () => [any, any];
error: (code: string | number, cb: any) => any;
badRequest: (cb: (error: WretcherError) => void) => any;
unauthorized: (cb: (error: WretcherError) => void) => any;
forbidden: (cb: (error: WretcherError) => void) => any;
notFound: (cb: (error: WretcherError) => void) => any;
timeout: (cb: (error: WretcherError) => void) => any;
internalError: (cb: (error: WretcherError) => void) => any;
onAbort: (cb: (error: Error) => void) => any;
perfs: (cb?: (type: any) => void) => ResponseChain;
setTimeout: (time: number, controller: any) => ResponseChain;
controller: () => [any, ResponseChain];
error: (code: (number | string), cb: any) => ResponseChain;
badRequest: (cb: (error: WretcherError) => void) => ResponseChain;
unauthorized: (cb: (error: WretcherError) => void) => ResponseChain;
forbidden: (cb: (error: WretcherError) => void) => ResponseChain;
notFound: (cb: (error: WretcherError) => void) => ResponseChain;
timeout: (cb: (error: WretcherError) => void) => ResponseChain;
internalError: (cb: (error: WretcherError) => void) => ResponseChain;
onAbort: (cb: (error: Error) => void) => ResponseChain;
};
export declare const resolver: (url: any) => (catchers?: Map<string | number, (error: WretcherError) => void>) => (resolvers: ((chain: ResponseChain) => Promise<any> | ResponseChain)[]) => (opts?: {}) => Promise<any> | ResponseChain;
Loading

0 comments on commit 481803b

Please sign in to comment.