@@ -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. */
8383export 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 */
149148export 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