@ventlio/tanstack-query 0.1.9 → 0.1.10

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/index.mjs CHANGED
@@ -163,7 +163,9 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
163
163
  ...options,
164
164
  });
165
165
  useEffect(() => {
166
- updatePath(path);
166
+ if (path) {
167
+ updatePath(path);
168
+ }
167
169
  }, [path]);
168
170
  const nextPage = () => {
169
171
  if (query.data?.data.pagination) {
@@ -184,24 +186,14 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
184
186
  }
185
187
  };
186
188
  const constructPaginationLink = (link, pageNumber) => {
187
- const oldLink = link;
188
- if (link.includes('?')) {
189
- if (link.includes('?page=')) {
190
- // replace current page number with new number
191
- link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
192
- }
193
- else if (link.includes('&page=')) {
194
- link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
195
- }
196
- else {
197
- link = `${link}&page=${pageNumber}`;
198
- }
199
- }
200
- else {
201
- link = `${link}?page=${pageNumber}`;
202
- }
203
- // only update page when pagination is done
204
- if (oldLink !== link) {
189
+ const oldParams = new URLSearchParams(link);
190
+ const oldPage = Number(oldParams.get('page'));
191
+ const [pathname, queryStrings] = link.split('?', 1);
192
+ const queryParams = new URLSearchParams(queryStrings ?? '');
193
+ queryParams.set('page', pageNumber);
194
+ link = pathname + '?' + queryParams.toString();
195
+ // only update page when pagination number changed
196
+ if (oldPage !== pageNumber) {
205
197
  setPage(pageNumber);
206
198
  }
207
199
  return link;
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import type { UseQueryOptions } from '@tanstack/react-query';
2
3
  import type { IRequestError, IRequestSuccess } from '../request';
3
4
  import type { TanstackQueryOption } from './queries.interface';
@@ -30,7 +30,9 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
30
30
  ...options,
31
31
  });
32
32
  react.useEffect(() => {
33
- updatePath(path);
33
+ if (path) {
34
+ updatePath(path);
35
+ }
34
36
  }, [path]);
35
37
  const nextPage = () => {
36
38
  if (query.data?.data.pagination) {
@@ -51,24 +53,14 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
51
53
  }
52
54
  };
53
55
  const constructPaginationLink = (link, pageNumber) => {
54
- const oldLink = link;
55
- if (link.includes('?')) {
56
- if (link.includes('?page=')) {
57
- // replace current page number with new number
58
- link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
59
- }
60
- else if (link.includes('&page=')) {
61
- link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
62
- }
63
- else {
64
- link = `${link}&page=${pageNumber}`;
65
- }
66
- }
67
- else {
68
- link = `${link}?page=${pageNumber}`;
69
- }
70
- // only update page when pagination is done
71
- if (oldLink !== link) {
56
+ const oldParams = new URLSearchParams(link);
57
+ const oldPage = Number(oldParams.get('page'));
58
+ const [pathname, queryStrings] = link.split('?', 1);
59
+ const queryParams = new URLSearchParams(queryStrings ?? '');
60
+ queryParams.set('page', pageNumber);
61
+ link = pathname + '?' + queryParams.toString();
62
+ // only update page when pagination number changed
63
+ if (oldPage !== pageNumber) {
72
64
  setPage(pageNumber);
73
65
  }
74
66
  return link;
@@ -1 +1 @@
1
- {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -52,7 +52,9 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
52
52
  );
53
53
 
54
54
  useEffect(() => {
55
- updatePath(path);
55
+ if (path) {
56
+ updatePath(path);
57
+ }
56
58
  }, [path]);
57
59
 
58
60
  const nextPage = () => {
@@ -82,22 +84,18 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
82
84
  };
83
85
 
84
86
  const constructPaginationLink = (link: string, pageNumber: number) => {
85
- const oldLink = link;
86
- if (link.includes('?')) {
87
- if (link.includes('?page=')) {
88
- // replace current page number with new number
89
- link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
90
- } else if (link.includes('&page=')) {
91
- link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
92
- } else {
93
- link = `${link}&page=${pageNumber}`;
94
- }
95
- } else {
96
- link = `${link}?page=${pageNumber}`;
97
- }
87
+ const oldParams = new URLSearchParams(link);
88
+ const oldPage = Number(oldParams.get('page'));
89
+
90
+ const [pathname, queryStrings] = link.split('?', 1);
91
+ const queryParams = new URLSearchParams(queryStrings ?? '');
92
+
93
+ queryParams.set('page', pageNumber as any);
94
+
95
+ link = pathname + '?' + queryParams.toString();
98
96
 
99
- // only update page when pagination is done
100
- if (oldLink !== link) {
97
+ // only update page when pagination number changed
98
+ if (oldPage !== pageNumber) {
101
99
  setPage(pageNumber);
102
100
  }
103
101
  return link;