Skip to content

Commit 355f4ad

Browse files
committed
Rename killswitch to isValidRequest.
1 parent 586b5bb commit 355f4ad

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ftch, jsonrpc, replayable } from 'micro-ftch';
1818

1919
let enabled = false;
2020
const net = ftch(fetch, {
21-
killswitch: () => enabled,
21+
isValidRequest: () => enabled,
2222
log: (url, options) => console.log(url, options),
2323
timeout: 5000,
2424
concurrencyLimit: 10,
@@ -38,7 +38,7 @@ await net('https://user:[email protected]/basic-auth/user/pwd');
3838
```
3939

4040
- [ftch](#ftch)
41-
- [killswitch](#killswitch)
41+
- [isValidRequest](#isValidRequest)
4242
- [log](#log)
4343
- [timeout](#timeout)
4444
- [concurrencyLimit](#concurrencyLimit)
@@ -50,22 +50,22 @@ await net('https://user:[email protected]/basic-auth/user/pwd');
5050

5151
There are three wrappers over `fetch()`:
5252

53-
1. `ftch(fetch)` - killswitch, logging, timeouts, concurrency limits, basic auth
53+
1. `ftch(fetch)` - isValidRequest, logging, timeouts, concurrency limits, basic auth
5454
2. `jsonrpc(fetch)` - batched JSON-RPC functionality
5555
3. `replayable(fetch)` - log & replay network requests without actually calling network code.
5656

5757
## ftch
5858

5959
Basic wrapper over `fetch()`.
6060

61-
### killswitch
61+
### isValidRequest
6262

63-
When kill-switch is enabled, all requests will throw an error.
63+
When isValidRequest killswitch is enabled, all requests will throw an error.
6464
You can dynamically enable and disable it any any time.
6565

6666
```ts
6767
let ENABLED = true;
68-
const f = ftch(fetch, { killswitch: () => ENABLED });
68+
const f = ftch(fetch, { isValidRequest: () => ENABLED });
6969
f('http://localhost'); // ok
7070
ENABLED = false;
7171
f('http://localhost'); // throws

index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ export type FetchFn = (
7979
arrayBuffer: () => Promise<ArrayBuffer>;
8080
}>;
8181

82-
/** Options for `ftch`. killswitch can disable fetching, while log will receive all requests. */
82+
/** Options for `ftch`. isValidRequest can disable fetching, while log will receive all requests. */
8383
export type FtchOpts = {
84-
// TODO: killswitch => isValidRequest or beforeEach
85-
// maybe also pass options?
84+
isValidRequest?: (url?: string) => boolean;
8685
killswitch?: (url?: string) => boolean;
8786
concurrencyLimit?: number;
8887
timeout?: number;
@@ -107,15 +106,15 @@ const getRequestInfo = (req: UnPromise<ReturnType<FetchFn>>) => ({
107106
*
108107
* @param fn - The fetch function to be wrapped.
109108
* @param opts - Options to control the behavior of the fetch wrapper.
110-
* @param [opts.killswitch] - Function to determine if the fetch request should be cancelled.
109+
* @param [opts.isValidRequest] - Function to determine if the fetch request should be cancelled.
111110
* @param [opts.concurrencyLimit] - Limit on the number of concurrent fetch requests.
112111
* @param [opts.timeout] - Default timeout for all requests, can be overriden in request opts
113112
* @param [opts.log] - Callback to log all requests
114113
* @returns Wrapped fetch function
115114
* @example
116115
* ```js
117116
* let ENABLED = true;
118-
* const f = ftch(fetch, { killswitch: ()=>ENABLED });
117+
* const f = ftch(fetch, { isValidRequest: () => ENABLED });
119118
* f('http://localhost'); // ok
120119
* ENABLED = false;
121120
* f('http://localhost'); // throws
@@ -147,9 +146,9 @@ const getRequestInfo = (req: UnPromise<ReturnType<FetchFn>>) => ({
147146
* ```
148147
*/
149148
export function ftch(fetchFunction: FetchFn, opts: FtchOpts = {}): FetchFn {
150-
if (opts.killswitch && typeof opts.killswitch !== 'function')
151-
throw new Error('opts.killswitch must be a function');
152-
const noNetwork = (url: string) => opts.killswitch && !opts.killswitch(url);
149+
const ks = opts.isValidRequest || opts.killswitch;
150+
if (ks && typeof ks !== 'function') throw new Error('opts.killswitch must be a function');
151+
const noNetwork = (url: string) => ks && !ks(url);
153152
const wrappedFetch: FetchFn = async (url, reqOpts = {}) => {
154153
if (opts.log) opts.log(url, reqOpts);
155154
const abort = new AbortController();

0 commit comments

Comments
 (0)