@ventlio/tanstack-query 0.1.8 → 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
@@ -1,5 +1,5 @@
1
1
  import { useQuery, useMutation } from '@tanstack/react-query';
2
- import { useState, startTransition } from 'react';
2
+ import { useState, useEffect, startTransition } from 'react';
3
3
  import axios from 'axios';
4
4
 
5
5
  const API_BASE_URL = 'https://pokeapi.co/api/v2';
@@ -162,6 +162,11 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
162
162
  enabled: load,
163
163
  ...options,
164
164
  });
165
+ useEffect(() => {
166
+ if (path) {
167
+ updatePath(path);
168
+ }
169
+ }, [path]);
165
170
  const nextPage = () => {
166
171
  if (query.data?.data.pagination) {
167
172
  const pagination = query.data.data.pagination;
@@ -181,24 +186,14 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
181
186
  }
182
187
  };
183
188
  const constructPaginationLink = (link, pageNumber) => {
184
- const oldLink = link;
185
- if (link.includes('?')) {
186
- if (link.includes('?page=')) {
187
- // replace current page number with new number
188
- link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
189
- }
190
- else if (link.includes('&page=')) {
191
- link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
192
- }
193
- else {
194
- link = `${link}&page=${pageNumber}`;
195
- }
196
- }
197
- else {
198
- link = `${link}?page=${pageNumber}`;
199
- }
200
- // only update page when pagination is done
201
- 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) {
202
197
  setPage(pageNumber);
203
198
  }
204
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';
@@ -29,6 +29,11 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
29
29
  enabled: load,
30
30
  ...options,
31
31
  });
32
+ react.useEffect(() => {
33
+ if (path) {
34
+ updatePath(path);
35
+ }
36
+ }, [path]);
32
37
  const nextPage = () => {
33
38
  if (query.data?.data.pagination) {
34
39
  const pagination = query.data.data.pagination;
@@ -48,24 +53,14 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
48
53
  }
49
54
  };
50
55
  const constructPaginationLink = (link, pageNumber) => {
51
- const oldLink = link;
52
- if (link.includes('?')) {
53
- if (link.includes('?page=')) {
54
- // replace current page number with new number
55
- link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
56
- }
57
- else if (link.includes('&page=')) {
58
- link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
59
- }
60
- else {
61
- link = `${link}&page=${pageNumber}`;
62
- }
63
- }
64
- else {
65
- link = `${link}?page=${pageNumber}`;
66
- }
67
- // only update page when pagination is done
68
- 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) {
69
64
  setPage(pageNumber);
70
65
  }
71
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.8",
3
+ "version": "0.1.10",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
2
  import { useQuery } from '@tanstack/react-query';
3
- import { startTransition, useState } from 'react';
3
+ import { startTransition, useEffect, useState } from 'react';
4
4
  import type { IRequestError, IRequestSuccess } from '../request';
5
5
  import { makeRequest } from '../request';
6
6
  import type { IPagination, TanstackQueryOption } from './queries.interface';
@@ -51,6 +51,12 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
51
51
  }
52
52
  );
53
53
 
54
+ useEffect(() => {
55
+ if (path) {
56
+ updatePath(path);
57
+ }
58
+ }, [path]);
59
+
54
60
  const nextPage = () => {
55
61
  if (query.data?.data.pagination) {
56
62
  const pagination: IPagination = query.data.data.pagination;
@@ -78,22 +84,18 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
78
84
  };
79
85
 
80
86
  const constructPaginationLink = (link: string, pageNumber: number) => {
81
- const oldLink = link;
82
- if (link.includes('?')) {
83
- if (link.includes('?page=')) {
84
- // replace current page number with new number
85
- link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
86
- } else if (link.includes('&page=')) {
87
- link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
88
- } else {
89
- link = `${link}&page=${pageNumber}`;
90
- }
91
- } else {
92
- link = `${link}?page=${pageNumber}`;
93
- }
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();
94
96
 
95
- // only update page when pagination is done
96
- if (oldLink !== link) {
97
+ // only update page when pagination number changed
98
+ if (oldPage !== pageNumber) {
97
99
  setPage(pageNumber);
98
100
  }
99
101
  return link;