Comparing version 0.1.90 to 0.1.91
@@ -49,3 +49,3 @@ export declare enum HTTPMethod { | ||
export interface Res<TData> extends Response { | ||
data: TData | undefined; | ||
data?: TData | undefined; | ||
} | ||
@@ -61,4 +61,4 @@ export declare type Req<TData = any> = ReqMethods & ReqBase<TData>; | ||
export declare type Interceptors = { | ||
request?: (options: Options) => Options; | ||
response?: (response: Res<any>) => any; | ||
request?: (options: Options) => Promise<Options> | Options; | ||
response?: (response: Res<any>) => Res<any>; | ||
}; | ||
@@ -65,0 +65,0 @@ export interface CustomOptions { |
@@ -26,3 +26,3 @@ "use strict"; | ||
const controller = react_1.useRef(); | ||
const res = react_1.useRef(); | ||
const res = react_1.useRef({}); | ||
const data = react_1.useRef(defaults.data); | ||
@@ -49,10 +49,16 @@ const timedout = react_1.useRef(false); | ||
let theData; | ||
let theRes; | ||
try { | ||
res.current = yield fetch(`${url}${path}${route}`, options); | ||
theRes = ((yield fetch(`${url}${path}${route}`, options)) || {}); | ||
try { | ||
theData = yield res.current.json(); | ||
theData = yield theRes.json(); | ||
} | ||
catch (err) { | ||
theData = (yield res.current.text()); // FIXME: should not be `any` type | ||
theData = (yield theRes.text()); // FIXME: should not be `any` type | ||
} | ||
theData = (defaults.data && utils_1.isEmpty(theData)) ? defaults.data : theData; | ||
theRes.data = theData; | ||
res.current = interceptors.response ? interceptors.response(theRes) : theRes; | ||
utils_1.invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`'); | ||
data.current = res.current.data; | ||
} | ||
@@ -73,3 +79,2 @@ catch (err) { | ||
clearTimeout(timer); | ||
data.current = (defaults.data && utils_1.isEmpty(theData)) ? defaults.data : theData; | ||
controller.current = undefined; | ||
@@ -98,4 +103,2 @@ setLoading(false); | ||
}; | ||
const responseObj = Object.assign({ data: data.current }, res.current); | ||
const response = interceptors.response ? interceptors.response(responseObj) : responseObj; | ||
const executeRequest = react_1.useCallback(() => { | ||
@@ -129,3 +132,3 @@ const methodName = requestInit.method || types_1.HTTPMethod.GET; | ||
}, [onMount, executeRequest]); | ||
return Object.assign([request, response, loading, error], Object.assign({ request, response: response }, request)); | ||
return Object.assign([request, res.current, loading, error], Object.assign({ request, response: res.current }, request)); | ||
} | ||
@@ -132,0 +135,0 @@ exports.useFetch = useFetch; |
{ | ||
"name": "use-http", | ||
"version": "0.1.90", | ||
"version": "0.1.91", | ||
"homepage": "http://use-http.com", | ||
@@ -28,2 +28,3 @@ "main": "dist/index.js", | ||
"@typescript-eslint/parser": "^2.0.0", | ||
"convert-keys": "^1.3.4", | ||
"eslint": "^6.0.1", | ||
@@ -30,0 +31,0 @@ "eslint-config-prettier": "6.2.0", |
@@ -84,2 +84,3 @@ <a href="http://use-http.com"> | ||
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/embed/km04k9k9x5'>useFetch - create-react-app</a></li> | ||
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-with-provider-c78w2'>useFetch + Provider</a></li> | ||
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/graphql-usequery-provider-uhdmj'>useQuery - GraphQL</a></li> | ||
@@ -187,2 +188,5 @@ </ul> | ||
``` | ||
[](https://codesandbox.io/s/usefetch-with-provider-c78w2) | ||
</details> | ||
@@ -370,2 +374,3 @@ | ||
``` | ||
[](https://codesandbox.io/s/graphql-usequery-provider-uhdmj) | ||
@@ -438,3 +443,3 @@ ##### Add a new todo | ||
import { Provider } from 'use-http' | ||
import camelCase from 'camelcase-keys-recursive' | ||
import { toCamel } from 'convert-keys' | ||
@@ -453,3 +458,9 @@ function App() { | ||
// every time we make an http request, before getting the response back, this will run | ||
response: (response) => camelCase(response) | ||
response: (response) => { | ||
// unfortunately, because this is a JS Response object, we have to modify it directly. | ||
// It shouldn't have any negative affect since this is getting reset on each request. | ||
// use "eslint-disable-next-line" if you're getting linting errors. | ||
if (response.data) response.data = toCamel(response.data) | ||
return response | ||
} | ||
} | ||
@@ -552,3 +563,10 @@ } | ||
Sponsors | ||
-------- | ||
Does your company use use-http? Consider sponsoring the project to fund new features, bug fixes, and more. | ||
<a href="https://ava.inc" style="margin-right: 2rem;" target="_blank"><img width="280px" src="https://ava.inc/ava-logo-green.png" /></a> | ||
Feature Requests/Ideas | ||
@@ -555,0 +573,0 @@ ---------------------- |
Sorry, the diff of this file is not supported yet
77016
753
699
30