Skip to content

Commit

Permalink
🏭 Add deferred callback chain.
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Jun 6, 2018
1 parent e68764f commit 10ddfe5
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -60,3 +60,5 @@ typings/
# IDE
.vscode

# Rollup typescript cache
.rpt2_cache
29 changes: 28 additions & 1 deletion README.md
Expand Up @@ -11,7 +11,7 @@
<a href="https://github.com/elbywan/wretch/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="license-badge" height="20"></a>
</h1>
<h4 align="center">
A tiny (&lt; 2.4Kb g-zipped) wrapper built around fetch with an intuitive syntax.
A tiny (&lt; 2.5Kb g-zipped) wrapper built around fetch with an intuitive syntax.
</h4>
<h5 align="center">
<i>f[ETCH] [WR]apper</i>
Expand Down Expand Up @@ -427,6 +427,33 @@ reAuthOn401.url("/resource")
.then(callback) // <- Will be called for the original OR the replayed promise result
```

#### defer(callback: (originalRequest: Wretcher, url: string, options: Object) => Wretcher, clear = false)

Defer wretcher methods that will be chained and called just before the request is performed.

```js
/* Small fictional example: deferred authentication */

// If you cannot retrieve the auth token while configuring the wretch object you can use .defer to postpone the call
const api = wretch("...").defer((w, url, options) => {
// If we are hitting the route /user…
if(/\/user/.test(url)) {
const { token } = options.context
return w.auth(token)
}
return w
})

// ... //

const token = await getToken(request.session.user)

// .auth gets called here automatically
api.options({
context: { token }
}).get().res()
```

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

Programs a resolver which will automatically be injected to perform response chain tasks.
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.

10 changes: 8 additions & 2 deletions dist/wretcher.d.ts
Expand Up @@ -4,6 +4,7 @@ import { ConfiguredMiddleware } from "./middleware";
export declare type WretcherOptions = RequestInit & {
[key: string]: any;
};
export declare type DeferredCallback = (wretcher: Wretcher, url: string, options: WretcherOptions) => Wretcher;
/**
* The Wretcher class used to perform easy fetch requests.
*
Expand All @@ -15,9 +16,10 @@ export declare class Wretcher {
_catchers: Map<number | string, (error: WretcherError, originalRequest: Wretcher) => void>;
_resolvers: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any>;
_middlewares: ConfiguredMiddleware[];
protected constructor(_url: string, _options: WretcherOptions, _catchers?: Map<number | string, (error: WretcherError, originalRequest: Wretcher) => void>, _resolvers?: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any>, _middlewares?: ConfiguredMiddleware[]);
_deferredChain: DeferredCallback[];
protected constructor(_url: string, _options: WretcherOptions, _catchers?: Map<number | string, (error: WretcherError, originalRequest: Wretcher) => void>, _resolvers?: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any>, _middlewares?: ConfiguredMiddleware[], _deferredChain?: DeferredCallback[]);
static factory(url?: string, opts?: WretcherOptions): Wretcher;
private selfFactory({url, options, catchers, resolvers, middlewares}?);
private selfFactory({url, options, catchers, resolvers, middlewares, deferredChain}?);
/**
* Sets the default fetch options used for every subsequent fetch call.
* @param opts New default options
Expand Down Expand Up @@ -110,6 +112,10 @@ export declare class Wretcher {
* @param doResolve : Resolver callback
*/
resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise<any>, clear?: boolean): Wretcher;
/**
* Defer wretcher methods that will be chained and called just before the request is performed.
*/
defer(callback: DeferredCallback, clear?: boolean): Wretcher;
/**
* Add middlewares to intercept a request before being sent.
*/
Expand Down

0 comments on commit 10ddfe5

Please sign in to comment.