@sabuj0338/axios-friday 1.0.0 → 1.1.0

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.d.mts CHANGED
@@ -87,6 +87,17 @@ declare class Friday {
87
87
  * message you want to associate with the error that will be thrown.
88
88
  */
89
89
  throwError(errorMessage: string): void;
90
+ /**
91
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
92
+ * query string, and returns the cleaned URL.
93
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
94
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
95
+ * URL.
96
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
97
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
98
+ * URL without the query string.
99
+ */
100
+ private cleanUrl;
90
101
  /**
91
102
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
92
103
  * the response data.
package/dist/index.d.ts CHANGED
@@ -87,6 +87,17 @@ declare class Friday {
87
87
  * message you want to associate with the error that will be thrown.
88
88
  */
89
89
  throwError(errorMessage: string): void;
90
+ /**
91
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
92
+ * query string, and returns the cleaned URL.
93
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
94
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
95
+ * URL.
96
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
97
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
98
+ * URL without the query string.
99
+ */
100
+ private cleanUrl;
90
101
  /**
91
102
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
92
103
  * the response data.
package/dist/index.js CHANGED
@@ -86,6 +86,19 @@ var Friday = class {
86
86
  }
87
87
  );
88
88
  }
89
+ this.axiosInstance.interceptors.request.use(
90
+ (config) => {
91
+ if (config.url) {
92
+ const baseUrl = config.baseURL || "";
93
+ const fullUrl = new URL(config.url, baseUrl).toString();
94
+ config.url = this.cleanUrl(fullUrl).replace(baseUrl, "");
95
+ }
96
+ return config;
97
+ },
98
+ (error) => {
99
+ return Promise.reject(error);
100
+ }
101
+ );
89
102
  }
90
103
  /**
91
104
  * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
@@ -98,9 +111,12 @@ var Friday = class {
98
111
  if (this.config.refreshTokenEndpoint == void 0) return;
99
112
  const refreshToken = this.getRefreshToken();
100
113
  if (refreshToken === void 0) throw new Error("Unauthorized Attempt!");
101
- const res = await this.axiosInstance.post(this.config.refreshTokenEndpoint, {
102
- refreshToken
103
- });
114
+ const res = await this.axiosInstance.post(
115
+ this.config.refreshTokenEndpoint,
116
+ {
117
+ refreshToken
118
+ }
119
+ );
104
120
  if (res.status != 200)
105
121
  throw new Error("Refresh access token request failed!");
106
122
  this.resetTokens(res);
@@ -186,6 +202,28 @@ var Friday = class {
186
202
  throwError(errorMessage) {
187
203
  throw new Error(errorMessage);
188
204
  }
205
+ /**
206
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
207
+ * query string, and returns the cleaned URL.
208
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
209
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
210
+ * URL.
211
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
212
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
213
+ * URL without the query string.
214
+ */
215
+ cleanUrl(url) {
216
+ const [base, query] = url.split("?");
217
+ if (!query) return url;
218
+ const searchParams = new URLSearchParams(query);
219
+ for (const [key, value] of searchParams.entries()) {
220
+ if (value === "") {
221
+ searchParams.delete(key);
222
+ }
223
+ }
224
+ const cleanedQuery = searchParams.toString();
225
+ return cleanedQuery ? `${base}?${cleanedQuery}` : base;
226
+ }
189
227
  /**
190
228
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
191
229
  * the response data.
package/dist/index.mjs CHANGED
@@ -50,6 +50,19 @@ var Friday = class {
50
50
  }
51
51
  );
52
52
  }
53
+ this.axiosInstance.interceptors.request.use(
54
+ (config) => {
55
+ if (config.url) {
56
+ const baseUrl = config.baseURL || "";
57
+ const fullUrl = new URL(config.url, baseUrl).toString();
58
+ config.url = this.cleanUrl(fullUrl).replace(baseUrl, "");
59
+ }
60
+ return config;
61
+ },
62
+ (error) => {
63
+ return Promise.reject(error);
64
+ }
65
+ );
53
66
  }
54
67
  /**
55
68
  * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
@@ -62,9 +75,12 @@ var Friday = class {
62
75
  if (this.config.refreshTokenEndpoint == void 0) return;
63
76
  const refreshToken = this.getRefreshToken();
64
77
  if (refreshToken === void 0) throw new Error("Unauthorized Attempt!");
65
- const res = await this.axiosInstance.post(this.config.refreshTokenEndpoint, {
66
- refreshToken
67
- });
78
+ const res = await this.axiosInstance.post(
79
+ this.config.refreshTokenEndpoint,
80
+ {
81
+ refreshToken
82
+ }
83
+ );
68
84
  if (res.status != 200)
69
85
  throw new Error("Refresh access token request failed!");
70
86
  this.resetTokens(res);
@@ -150,6 +166,28 @@ var Friday = class {
150
166
  throwError(errorMessage) {
151
167
  throw new Error(errorMessage);
152
168
  }
169
+ /**
170
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
171
+ * query string, and returns the cleaned URL.
172
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
173
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
174
+ * URL.
175
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
176
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
177
+ * URL without the query string.
178
+ */
179
+ cleanUrl(url) {
180
+ const [base, query] = url.split("?");
181
+ if (!query) return url;
182
+ const searchParams = new URLSearchParams(query);
183
+ for (const [key, value] of searchParams.entries()) {
184
+ if (value === "") {
185
+ searchParams.delete(key);
186
+ }
187
+ }
188
+ const cleanedQuery = searchParams.toString();
189
+ return cleanedQuery ? `${base}?${cleanedQuery}` : base;
190
+ }
153
191
  /**
154
192
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
155
193
  * the response data.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sabuj0338/axios-friday",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Friday - a axios based api handling custom class. which has refresh token mechanism configured.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",