Skip to content

Commit

Permalink
Rename killswitch to isValidRequest.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 23, 2025
1 parent 586b5bb commit 355f4ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ftch, jsonrpc, replayable } from 'micro-ftch';

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

- [ftch](#ftch)
- [killswitch](#killswitch)
- [isValidRequest](#isValidRequest)
- [log](#log)
- [timeout](#timeout)
- [concurrencyLimit](#concurrencyLimit)
Expand All @@ -50,22 +50,22 @@ await net('https://user:[email protected]/basic-auth/user/pwd');

There are three wrappers over `fetch()`:

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

## ftch

Basic wrapper over `fetch()`.

### killswitch
### isValidRequest

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

```ts
let ENABLED = true;
const f = ftch(fetch, { killswitch: () => ENABLED });
const f = ftch(fetch, { isValidRequest: () => ENABLED });
f('http://localhost'); // ok
ENABLED = false;
f('http://localhost'); // throws
Expand Down
15 changes: 7 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ export type FetchFn = (
arrayBuffer: () => Promise<ArrayBuffer>;
}>;

/** Options for `ftch`. killswitch can disable fetching, while log will receive all requests. */
/** Options for `ftch`. isValidRequest can disable fetching, while log will receive all requests. */
export type FtchOpts = {
// TODO: killswitch => isValidRequest or beforeEach
// maybe also pass options?
isValidRequest?: (url?: string) => boolean;
killswitch?: (url?: string) => boolean;
concurrencyLimit?: number;
timeout?: number;
Expand All @@ -107,15 +106,15 @@ const getRequestInfo = (req: UnPromise<ReturnType<FetchFn>>) => ({
*
* @param fn - The fetch function to be wrapped.
* @param opts - Options to control the behavior of the fetch wrapper.
* @param [opts.killswitch] - Function to determine if the fetch request should be cancelled.
* @param [opts.isValidRequest] - Function to determine if the fetch request should be cancelled.
* @param [opts.concurrencyLimit] - Limit on the number of concurrent fetch requests.
* @param [opts.timeout] - Default timeout for all requests, can be overriden in request opts
* @param [opts.log] - Callback to log all requests
* @returns Wrapped fetch function
* @example
* ```js
* let ENABLED = true;
* const f = ftch(fetch, { killswitch: ()=>ENABLED });
* const f = ftch(fetch, { isValidRequest: () => ENABLED });
* f('http://localhost'); // ok
* ENABLED = false;
* f('http://localhost'); // throws
Expand Down Expand Up @@ -147,9 +146,9 @@ const getRequestInfo = (req: UnPromise<ReturnType<FetchFn>>) => ({
* ```
*/
export function ftch(fetchFunction: FetchFn, opts: FtchOpts = {}): FetchFn {
if (opts.killswitch && typeof opts.killswitch !== 'function')
throw new Error('opts.killswitch must be a function');
const noNetwork = (url: string) => opts.killswitch && !opts.killswitch(url);
const ks = opts.isValidRequest || opts.killswitch;
if (ks && typeof ks !== 'function') throw new Error('opts.killswitch must be a function');
const noNetwork = (url: string) => ks && !ks(url);
const wrappedFetch: FetchFn = async (url, reqOpts = {}) => {
if (opts.log) opts.log(url, reqOpts);
const abort = new AbortController();
Expand Down

0 comments on commit 355f4ad

Please sign in to comment.