@sabuj0338/axios-friday 0.1.4 → 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/README.md +45 -2
- package/dist/index.d.mts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +41 -3
- package/dist/index.mjs +41 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,9 +15,9 @@ npm install @sabuj0338/axios-friday
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```python
|
|
18
|
-
import Friday from
|
|
18
|
+
import { Friday } from "@sabuj0338/axios-friday";
|
|
19
19
|
|
|
20
|
-
#
|
|
20
|
+
# Configure and use
|
|
21
21
|
|
|
22
22
|
const baseURL = "https://sabuj0338.github.io/portfolio";
|
|
23
23
|
|
|
@@ -30,6 +30,49 @@ const friday = new Friday({
|
|
|
30
30
|
# enableAccessToken: true,
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
+
# returns 'AxiosResponse'
|
|
34
|
+
const response = await friday.get(new URL(baseURL));
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Example of using a class that Extends `Friday`
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import { Friday } from "@sabuj0338/axios-friday";
|
|
42
|
+
import { AxiosResponse } from "axios";
|
|
43
|
+
import Cookies from "js-cookie";
|
|
44
|
+
import toast from "react-hot-toast";
|
|
45
|
+
|
|
46
|
+
const BASE_API_URL = import.meta.env.VITE_BASE_API_URL;
|
|
47
|
+
const REFRESH_TOKEN_API = import.meta.env.VITE_REFRESH_TOKEN_API;
|
|
48
|
+
const REFRESH_TOKEN_KEY = import.meta.env.VITE_REFRESH_TOKEN_KEY;
|
|
49
|
+
const ACCESS_TOKEN_KEY = import.meta.env.VITE_ACCESS_TOKEN_KEY;
|
|
50
|
+
|
|
51
|
+
class MyFriday extends Friday {
|
|
52
|
+
constructor(baseURL: string = BASE_API_URL) {
|
|
53
|
+
super({
|
|
54
|
+
baseURL: baseURL,
|
|
55
|
+
refreshTokenEndpoint: REFRESH_TOKEN_API,
|
|
56
|
+
accessTokenKey: ACCESS_TOKEN_KEY,
|
|
57
|
+
refreshTokenKey: REFRESH_TOKEN_KEY,
|
|
58
|
+
enableRefreshToken: true,
|
|
59
|
+
enableAccessToken: true,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
override resetTokens(res: AxiosResponse<any, any>) {
|
|
64
|
+
Cookies.set(ACCESS_TOKEN_KEY, res.data.tokens.access.token);
|
|
65
|
+
Cookies.set(REFRESH_TOKEN_KEY, res.data.tokens.refresh.token);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
override throwError(message: string): void {
|
|
69
|
+
toast.error(message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const friday = new MyFriday();
|
|
74
|
+
|
|
75
|
+
|
|
33
76
|
# returns 'AxiosResponse'
|
|
34
77
|
const response = await friday.get(new URL(baseURL));
|
|
35
78
|
|
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(
|
|
102
|
-
|
|
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(
|
|
66
|
-
|
|
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