@ventlio/tanstack-query 0.2.26 → 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.
@@ -10,6 +10,8 @@ const useQueryHeaders = () => {
10
10
  queryFn: () => {
11
11
  return queryClient.getQueryData(['config']);
12
12
  },
13
+ staleTime: Infinity,
14
+ cacheTime: Infinity,
13
15
  });
14
16
  };
15
17
  const setQueryHeaders = (newHeaders) => {
@@ -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
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","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.26",
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
+ });
@@ -10,6 +10,8 @@ export const useQueryHeaders = (): IUseQueryHeaders => {
10
10
  queryFn: () => {
11
11
  return queryClient.getQueryData(['config']);
12
12
  },
13
+ staleTime: Infinity,
14
+ cacheTime: Infinity,
13
15
  });
14
16
  };
15
17
 
@@ -19,6 +19,8 @@ export async function makeRequest<TResponse>({
19
19
 
20
20
  // configure request header
21
21
 
22
+ console.log({ isFormData });
23
+
22
24
  if (!isFormData) {
23
25
  headers['Content-Type'] = ContentType.APPLICATION_JSON;
24
26
  }