Skip to content

Commit 10ddfe5

Browse files
committed
🏭 Add deferred callback chain.
1 parent e68764f commit 10ddfe5

File tree

10 files changed

+95
-20
lines changed

10 files changed

+95
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ typings/
6060
# IDE
6161
.vscode
6262

63+
# Rollup typescript cache
64+
.rpt2_cache

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<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>
1212
</h1>
1313
<h4 align="center">
14-
A tiny (&lt; 2.4Kb g-zipped) wrapper built around fetch with an intuitive syntax.
14+
A tiny (&lt; 2.5Kb g-zipped) wrapper built around fetch with an intuitive syntax.
1515
</h4>
1616
<h5 align="center">
1717
<i>f[ETCH] [WR]apper</i>
@@ -427,6 +427,33 @@ reAuthOn401.url("/resource")
427427
.then(callback) // <- Will be called for the original OR the replayed promise result
428428
```
429429

430+
#### defer(callback: (originalRequest: Wretcher, url: string, options: Object) => Wretcher, clear = false)
431+
432+
Defer wretcher methods that will be chained and called just before the request is performed.
433+
434+
```js
435+
/* Small fictional example: deferred authentication */
436+
437+
// If you cannot retrieve the auth token while configuring the wretch object you can use .defer to postpone the call
438+
const api = wretch("...").defer((w, url, options) => {
439+
// If we are hitting the route /user…
440+
if(/\/user/.test(url)) {
441+
const { token } = options.context
442+
return w.auth(token)
443+
}
444+
return w
445+
})
446+
447+
// ... //
448+
449+
const token = await getToken(request.session.user)
450+
451+
// .auth gets called here automatically
452+
api.options({
453+
context: { token }
454+
}).get().res()
455+
```
456+
430457
#### resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise<any>, clear = false)
431458

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

dist/bundle/wretch.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bundle/wretch.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/wretcher.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ConfiguredMiddleware } from "./middleware";
44
export declare type WretcherOptions = RequestInit & {
55
[key: string]: any;
66
};
7+
export declare type DeferredCallback = (wretcher: Wretcher, url: string, options: WretcherOptions) => Wretcher;
78
/**
89
* The Wretcher class used to perform easy fetch requests.
910
*
@@ -15,9 +16,10 @@ export declare class Wretcher {
1516
_catchers: Map<number | string, (error: WretcherError, originalRequest: Wretcher) => void>;
1617
_resolvers: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any>;
1718
_middlewares: ConfiguredMiddleware[];
18-
protected constructor(_url: string, _options: WretcherOptions, _catchers?: Map<number | string, (error: WretcherError, originalRequest: Wretcher) => void>, _resolvers?: Array<(resolver: ResponseChain, originalRequest: Wretcher) => any>, _middlewares?: ConfiguredMiddleware[]);
19+
_deferredChain: DeferredCallback[];
20+
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[]);
1921
static factory(url?: string, opts?: WretcherOptions): Wretcher;
20-
private selfFactory({url, options, catchers, resolvers, middlewares}?);
22+
private selfFactory({url, options, catchers, resolvers, middlewares, deferredChain}?);
2123
/**
2224
* Sets the default fetch options used for every subsequent fetch call.
2325
* @param opts New default options
@@ -110,6 +112,10 @@ export declare class Wretcher {
110112
* @param doResolve : Resolver callback
111113
*/
112114
resolve(doResolve: (chain: ResponseChain, originalRequest: Wretcher) => ResponseChain | Promise<any>, clear?: boolean): Wretcher;
115+
/**
116+
* Defer wretcher methods that will be chained and called just before the request is performed.
117+
*/
118+
defer(callback: DeferredCallback, clear?: boolean): Wretcher;
113119
/**
114120
* Add middlewares to intercept a request before being sent.
115121
*/

0 commit comments

Comments
 (0)