@ventlio/tanstack-query 0.2.27 → 0.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/useQueryHeaders.js +2 -0
- package/dist/config/useQueryHeaders.js.map +1 -1
- package/dist/index.mjs +3 -4
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useDeleteRequest.js +0 -1
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.js +0 -1
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.js +0 -1
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.js +0 -1
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/request/make-request.js +1 -0
- package/dist/request/make-request.js.map +1 -1
- package/package.json +9 -2
- package/src/__tests__/queries/usePostRequest.spec.ts +85 -0
- package/src/config/useQueryHeaders.ts +2 -0
- package/src/queries/useDeleteRequest.ts +0 -2
- package/src/queries/useGetRequest.ts +0 -2
- package/src/queries/usePatchRequest.ts +0 -2
- package/src/queries/usePostRequest.ts +0 -2
- package/src/request/make-request.ts +2 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -25,6 +25,8 @@ const useQueryHeaders = () => {
|
|
|
25
25
|
queryFn: () => {
|
|
26
26
|
return queryClient.getQueryData(['config']);
|
|
27
27
|
},
|
|
28
|
+
staleTime: Infinity,
|
|
29
|
+
cacheTime: Infinity,
|
|
28
30
|
});
|
|
29
31
|
};
|
|
30
32
|
const setQueryHeaders = (newHeaders) => {
|
|
@@ -138,6 +140,7 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
138
140
|
// configure body
|
|
139
141
|
body = isFormData ? buildFormData(body) : body;
|
|
140
142
|
// configure request header
|
|
143
|
+
console.log({ isFormData });
|
|
141
144
|
if (!isFormData) {
|
|
142
145
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
143
146
|
}
|
|
@@ -184,7 +187,6 @@ const useDeleteRequest = () => {
|
|
|
184
187
|
// get request headers
|
|
185
188
|
const headers = (await getHeadersAsync())
|
|
186
189
|
.headers;
|
|
187
|
-
console.log({ headers });
|
|
188
190
|
const postResponse = await makeRequest({
|
|
189
191
|
path: requestPath,
|
|
190
192
|
headers,
|
|
@@ -232,7 +234,6 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
|
232
234
|
const sendRequest = async (res, rej) => {
|
|
233
235
|
// get request headers
|
|
234
236
|
const headers = (await getHeadersAsync()).headers;
|
|
235
|
-
console.log({ headers });
|
|
236
237
|
const postResponse = await makeRequest({
|
|
237
238
|
path: requestPath,
|
|
238
239
|
headers,
|
|
@@ -332,7 +333,6 @@ const usePatchRequest = ({ path }) => {
|
|
|
332
333
|
// get request headers
|
|
333
334
|
const headers = (await getHeadersAsync())
|
|
334
335
|
.headers;
|
|
335
|
-
console.log({ headers });
|
|
336
336
|
makeRequest({
|
|
337
337
|
path: path,
|
|
338
338
|
body: postData,
|
|
@@ -372,7 +372,6 @@ const usePostRequest = ({ path, isFormData = false, }) => {
|
|
|
372
372
|
// get request headers
|
|
373
373
|
const headers = (await getHeadersAsync())
|
|
374
374
|
.headers;
|
|
375
|
-
console.log({ headers });
|
|
376
375
|
makeRequest({
|
|
377
376
|
path: path,
|
|
378
377
|
body: postData,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -20,7 +20,6 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
|
20
20
|
const sendRequest = async (res, rej) => {
|
|
21
21
|
// get request headers
|
|
22
22
|
const headers = (await getHeadersAsync()).headers;
|
|
23
|
-
console.log({ headers });
|
|
24
23
|
const postResponse = await makeRequest.makeRequest({
|
|
25
24
|
path: requestPath,
|
|
26
25
|
headers,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -9,6 +9,7 @@ async function makeRequest({ body, method = request_enum.HttpMethod.GET, path, i
|
|
|
9
9
|
// configure body
|
|
10
10
|
body = isFormData ? buildFormData.buildFormData(body) : body;
|
|
11
11
|
// configure request header
|
|
12
|
+
console.log({ isFormData });
|
|
12
13
|
if (!isFormData) {
|
|
13
14
|
headers['Content-Type'] = request_enum.ContentType.APPLICATION_JSON;
|
|
14
15
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ventlio/tanstack-query",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.28",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"contributors": [
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "rimraf dist && rollup --config rollup.config.ts --configPlugin rollup-plugin-typescript2",
|
|
25
|
-
"build:watch": "yarn build -w"
|
|
25
|
+
"build:watch": "yarn build -w",
|
|
26
|
+
"test": "jest"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
29
|
"@tanstack/react-query": "^4.26.1",
|
|
@@ -47,13 +48,16 @@
|
|
|
47
48
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
48
49
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
49
50
|
"@tanstack/react-query": "^4.26.1",
|
|
51
|
+
"@testing-library/react-hooks": "^8.0.1",
|
|
50
52
|
"@types/axios": "^0.14.0",
|
|
53
|
+
"@types/jest": "^29.5.1",
|
|
51
54
|
"@types/node": "*",
|
|
52
55
|
"@types/react": "^18.2.0",
|
|
53
56
|
"@types/react-dom": "^18.2.0",
|
|
54
57
|
"@typescript-eslint/eslint-plugin": "^5.54.1",
|
|
55
58
|
"@typescript-eslint/parser": "^5.54.1",
|
|
56
59
|
"axios": "^1.3.4",
|
|
60
|
+
"axios-mock-adapter": "^1.21.4",
|
|
57
61
|
"eslint": "^8.36.0",
|
|
58
62
|
"eslint-config-google": "^0.14.0",
|
|
59
63
|
"eslint-config-prettier": "^8.7.0",
|
|
@@ -62,12 +66,15 @@
|
|
|
62
66
|
"eslint-plugin-prettier": "^4.2.1",
|
|
63
67
|
"eslint-plugin-react": "^7.32.2",
|
|
64
68
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
69
|
+
"jest": "^29.5.0",
|
|
70
|
+
"jest-environment-jsdom": "^29.5.0",
|
|
65
71
|
"prettier": "^2.8.4",
|
|
66
72
|
"react": "^18.2.0",
|
|
67
73
|
"react-dom": "^18.2.0",
|
|
68
74
|
"rimraf": "^4.4.0",
|
|
69
75
|
"rollup": "^3.19.1",
|
|
70
76
|
"rollup-plugin-typescript2": "^0.34.1",
|
|
77
|
+
"ts-jest": "^29.1.0",
|
|
71
78
|
"ts-node": "^10.9.1",
|
|
72
79
|
"tslib": "^2.5.0",
|
|
73
80
|
"typescript": "^4.9.5"
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import MockAdapter from 'axios-mock-adapter';
|
|
4
|
+
import { usePostRequest } from '../../queries';
|
|
5
|
+
|
|
6
|
+
const mockAxios = new MockAdapter(axios);
|
|
7
|
+
|
|
8
|
+
describe('usePostRequest', () => {
|
|
9
|
+
const path = '/test';
|
|
10
|
+
const postData = { name: 'John Doe' };
|
|
11
|
+
const response = { id: 123, name: 'John Doe' };
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
mockAxios.reset();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should return post function and mutation object', () => {
|
|
18
|
+
const { result } = renderHook(() =>
|
|
19
|
+
usePostRequest<{ id: number; name: string }>({ path })
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
expect(result.current.post).toBeInstanceOf(Function);
|
|
23
|
+
expect(result.current.isLoading).toBe(false);
|
|
24
|
+
expect(result.current.isSuccess).toBe(false);
|
|
25
|
+
expect(result.current.isError).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should make post request and return response data', async () => {
|
|
29
|
+
mockAxios.onPost(path).reply(200, response);
|
|
30
|
+
|
|
31
|
+
const { result, waitForNextUpdate } = renderHook(() =>
|
|
32
|
+
usePostRequest<{ id: number; name: string }>({ path })
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const responsePromise = result.current.post(postData);
|
|
36
|
+
|
|
37
|
+
expect(result.current.isLoading).toBe(true);
|
|
38
|
+
|
|
39
|
+
await waitForNextUpdate();
|
|
40
|
+
|
|
41
|
+
expect(result.current.isLoading).toBe(false);
|
|
42
|
+
expect(result.current.isSuccess).toBe(true);
|
|
43
|
+
expect(result.current.data).toEqual(response);
|
|
44
|
+
expect(await responsePromise).toEqual(response);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should make post request and return error', async () => {
|
|
48
|
+
const errorMessage = 'Request failed with status code 500';
|
|
49
|
+
mockAxios.onPost(path).reply(500, { message: errorMessage });
|
|
50
|
+
|
|
51
|
+
const { result, waitForNextUpdate } = renderHook(() =>
|
|
52
|
+
usePostRequest<{ id: number; name: string }>({ path })
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const responsePromise = result.current.post(postData);
|
|
56
|
+
|
|
57
|
+
expect(result.current.isLoading).toBe(true);
|
|
58
|
+
|
|
59
|
+
await waitForNextUpdate();
|
|
60
|
+
|
|
61
|
+
expect(result.current.isLoading).toBe(false);
|
|
62
|
+
expect(result.current.isError).toBe(true);
|
|
63
|
+
expect(result.current.error?.message).toBe(errorMessage);
|
|
64
|
+
|
|
65
|
+
await expect(responsePromise).rejects.toEqual({
|
|
66
|
+
message: errorMessage,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should make post request with FormData', async () => {
|
|
71
|
+
const formData = new FormData();
|
|
72
|
+
formData.append('name', 'John Doe');
|
|
73
|
+
mockAxios.onPost(path).reply(200, response);
|
|
74
|
+
|
|
75
|
+
const { result } = renderHook(() =>
|
|
76
|
+
usePostRequest<{ id: number; name: string }>({ path, isFormData: true })
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const responsePromise = result.current.post(formData);
|
|
80
|
+
|
|
81
|
+
expect(result.current.isLoading).toBe(true);
|
|
82
|
+
|
|
83
|
+
await expect(responsePromise).resolves.toEqual(response);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -42,8 +42,6 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
42
42
|
// get request headers
|
|
43
43
|
const headers: RawAxiosRequestHeaders = (await getHeadersAsync()).headers;
|
|
44
44
|
|
|
45
|
-
console.log({ headers });
|
|
46
|
-
|
|
47
45
|
const postResponse = await makeRequest<TResponse>({
|
|
48
46
|
path: requestPath,
|
|
49
47
|
headers,
|