Comparing version 0.1.89 to 0.1.90
@@ -1,7 +0,2 @@ | ||
import { HTTPMethod, Interceptors, ValueOf } from './types'; | ||
interface RouteAndOptions { | ||
route: string; | ||
options: RequestInit; | ||
} | ||
import { HTTPMethod, Interceptors, ValueOf, RouteAndOptions } from './types'; | ||
export default function makeRouteAndOptions(initialOptions: RequestInit, method: HTTPMethod, controller: AbortController, routeOrBody?: string | BodyInit | object, bodyAs2ndParam?: BodyInit | object, requestInterceptor?: ValueOf<Pick<Interceptors, 'request'>>): Promise<RouteAndOptions>; | ||
export {}; |
@@ -10,2 +10,6 @@ export declare enum HTTPMethod { | ||
} | ||
export interface RouteAndOptions { | ||
route: string; | ||
options: RequestInit; | ||
} | ||
export interface FetchContextTypes { | ||
@@ -70,2 +74,4 @@ url?: string; | ||
interceptors?: Interceptors; | ||
onAbort?: () => void; | ||
onTimeout?: () => void; | ||
} | ||
@@ -72,0 +78,0 @@ export declare type Options = CustomOptions & Omit<RequestInit, 'body'> & { |
@@ -23,3 +23,3 @@ "use strict"; | ||
const { customOptions, requestInit, defaults } = useFetchArgs_1.default(...args); | ||
const { url, onMount, onUpdate, path, interceptors, timeout, retries } = customOptions; | ||
const { url, onMount, onUpdate, path, interceptors, timeout, retries, onTimeout, onAbort, } = customOptions; | ||
const { isServer } = use_ssr_1.default(); | ||
@@ -37,3 +37,5 @@ const controller = react_1.useRef(); | ||
return; // for now, we don't do anything on the server | ||
const theController = controller.current = new AbortController(); | ||
controller.current = new AbortController(); | ||
controller.current.signal.onabort = onAbort; | ||
const theController = controller.current; | ||
setLoading(true); | ||
@@ -45,2 +47,3 @@ setError(undefined); | ||
theController.abort(); | ||
onTimeout(); | ||
}, timeout); | ||
@@ -47,0 +50,0 @@ let theData; |
@@ -11,2 +11,4 @@ import { OptionsMaybeURL, NoUrlOptions, Interceptors } from './types'; | ||
interceptors: Interceptors; | ||
onAbort: () => void; | ||
onTimeout: () => void; | ||
}; | ||
@@ -28,2 +30,4 @@ requestInit: RequestInit; | ||
interceptors: {}; | ||
onAbort: () => void; | ||
onTimeout: () => void; | ||
}; | ||
@@ -30,0 +34,0 @@ requestInit: { |
@@ -19,2 +19,4 @@ "use strict"; | ||
interceptors: {}, | ||
onAbort: () => { }, | ||
onTimeout: () => { } | ||
}, | ||
@@ -48,8 +50,10 @@ requestInit: { | ||
utils_1.invariant(!!url, 'The first argument of useFetch is required unless you have a global url setup like: <Provider url="https://example.com"></Provider>'); | ||
const onMount = useField('onMount', defaults.onMount, urlOrOptions, optionsNoURLs); | ||
const onUpdate = useField('onUpdate', defaults.onUpdate, urlOrOptions, optionsNoURLs); | ||
const data = useField('data', defaults.data, urlOrOptions, optionsNoURLs); | ||
const path = useField('path', defaults.path, urlOrOptions, optionsNoURLs); | ||
const timeout = useField('timeout', defaults.timeout, urlOrOptions, optionsNoURLs); | ||
const retries = useField('retries', defaults.retries, urlOrOptions, optionsNoURLs); | ||
const onMount = useField('onMount', urlOrOptions, optionsNoURLs); | ||
const onUpdate = useField('onUpdate', urlOrOptions, optionsNoURLs); | ||
const data = useField('data', urlOrOptions, optionsNoURLs); | ||
const path = useField('path', urlOrOptions, optionsNoURLs); | ||
const timeout = useField('timeout', urlOrOptions, optionsNoURLs); | ||
const retries = useField('retries', urlOrOptions, optionsNoURLs); | ||
const onAbort = useField('onAbort', urlOrOptions, optionsNoURLs); | ||
const onTimeout = useField('onTimeout', urlOrOptions, optionsNoURLs); | ||
const loading = react_1.useMemo(() => { | ||
@@ -100,2 +104,4 @@ if (isServer) | ||
retries, | ||
onAbort, | ||
onTimeout | ||
}, | ||
@@ -110,3 +116,3 @@ requestInit, | ||
exports.default = useFetchArgs; | ||
const useField = (field, defaultVal, urlOrOptions, optionsNoURLs) => { | ||
const useField = (field, urlOrOptions, optionsNoURLs) => { | ||
const context = react_1.useContext(FetchContext_1.default); | ||
@@ -121,5 +127,5 @@ const contextOptions = context.options || {}; | ||
return contextOptions[field]; | ||
return defaultVal; | ||
return defaults[field]; | ||
}, [urlOrOptions, optionsNoURLs]); | ||
}; | ||
//# sourceMappingURL=useFetchArgs.js.map |
{ | ||
"name": "use-http", | ||
"version": "0.1.89", | ||
"version": "0.1.90", | ||
"homepage": "http://use-http.com", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -515,2 +515,4 @@ <a href="http://use-http.com"> | ||
| `onUpdate` | This is essentially the same as the dependency array for useEffect. Whenever one of the variables in this array is updated, the http request will re-run. | `[]` | | ||
| `onAbort` | Runs when the request is aborted. | empty function | | ||
| `onTimeout` | Called when the request times out. | empty function | | ||
| `retries` | When a request fails or times out, retry the request this many times. By default it will not retry. | `0` | | ||
@@ -529,2 +531,4 @@ | `timeout` | The request will be aborted/cancelled after this amount of time. This is also the interval at which `retries` will be made at. **in milliseconds** | `30000` </br> (30 seconds) | | ||
onUpdate: [] // everytime a variable in this array is updated, it will re-run the request (GET by default) | ||
onTimeout: () => {}, // called when the request times out | ||
onAbort: () => {}, // called when aborting the request | ||
retries: 3, // amount of times it should retry before erroring out | ||
@@ -552,3 +556,5 @@ timeout: 10000, // amount of time before the request (or request(s) for retries) errors out. | ||
------ | ||
- [ ] add browser support to docs (currently does not support ie 11) | ||
- [ ] add browser support to docs [1](https://github.com/godban/browsers-support-badges) [2](https://gist.github.com/danbovey/b468c2f810ae8efe09cb5a6fac3eaee5) (currently does not support ie 11) | ||
- [ ] maybe add contributors [all-contributors](https://github.com/all-contributors/all-contributors) | ||
- [ ] add sponsers [similar to this](https://github.com/carbon-app/carbon) | ||
- [ ] tests | ||
@@ -565,2 +571,3 @@ - [ ] tests for SSR | ||
- [ ] show comparison with Apollo | ||
- [ ] figure out a good way to show side-by-side comparisonsf | ||
- [ ] Dedupe requests done to the same endpoint. Only one request to the same endpoint will be initiated. [ref](https://www.npmjs.com/package/@bjornagh/use-fetch) | ||
@@ -601,4 +608,2 @@ - [ ] Cache responses to improve speed and reduce amount of requests | ||
}, | ||
onTimeout: () => {}, // called when the last `retry` is made and times out | ||
onAbort: () => {}, // called when aborting the request | ||
onServer: true, // potential idea to fetch on server instead of just having `loading` state. Not sure if this is a good idea though | ||
@@ -605,0 +610,0 @@ query: `some graphql query` // if you would prefer to pass the query in the config |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
75637
750
681