@sabuj0338/axios-friday 1.0.0 → 1.2.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
@@ -12,6 +12,7 @@ interface FridayOptions {
12
12
  * @var refreshTokenEndpoint: an optional string property representing the API endpoint for refreshing tokens.
13
13
  * @var enableAccessToken: a optional boolean property indicating whether token access is enabled.
14
14
  * @var enableRefreshToken: a optional boolean property indicating whether token refresh is enabled.
15
+ * @var storage: a required string property representing the storage mechanism for tokens.
15
16
  */
16
17
  interface FridayConfig {
17
18
  baseURL: string;
@@ -20,6 +21,7 @@ interface FridayConfig {
20
21
  refreshTokenEndpoint?: string;
21
22
  enableAccessToken?: boolean;
22
23
  enableRefreshToken?: boolean;
24
+ storage?: 'cookie' | 'local';
23
25
  }
24
26
 
25
27
  declare class Friday {
@@ -32,7 +34,7 @@ declare class Friday {
32
34
  * `FridayConfig`. It is used to configure the Friday instance with settings such as `baseURL` and
33
35
  * `enableRefreshToken`. The `axiosInstance` is created with the base URL specified in the `config`.
34
36
  */
35
- constructor({ baseURL, accessTokenKey, refreshTokenKey, refreshTokenEndpoint, enableAccessToken, enableRefreshToken, }: FridayConfig);
37
+ constructor({ baseURL, accessTokenKey, refreshTokenKey, refreshTokenEndpoint, enableAccessToken, enableRefreshToken, storage, }: FridayConfig);
36
38
  /**
37
39
  * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
38
40
  * request using a refresh token and updating the access token in the app's storage.
@@ -40,6 +42,17 @@ declare class Friday {
40
42
  * refreshing it using the refresh token.
41
43
  */
42
44
  refreshAccessToken(): Promise<string | undefined>;
45
+ /**
46
+ * The setTokens function stores a key-value pair either in a cookie or in local storage based on the
47
+ * configuration.
48
+ * @param {string} key - The `key` parameter is a string that represents the key under which the
49
+ * `value` will be stored in either a cookie or local storage, depending on the configuration set in
50
+ * `this.config.storage`.
51
+ * @param {string} value - The `value` parameter in the `setTokens` function represents the value that
52
+ * you want to store in either a cookie or local storage, based on the configuration set in
53
+ * `this.config.storage`.
54
+ */
55
+ private setTokens;
43
56
  /**
44
57
  * The function `resetTokens` sets the access token and refresh token in cookies based on the
45
58
  * response data from an Axios request.
@@ -87,6 +100,17 @@ declare class Friday {
87
100
  * message you want to associate with the error that will be thrown.
88
101
  */
89
102
  throwError(errorMessage: string): void;
103
+ /**
104
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
105
+ * query string, and returns the cleaned URL.
106
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
107
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
108
+ * URL.
109
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
110
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
111
+ * URL without the query string.
112
+ */
113
+ private cleanUrl;
90
114
  /**
91
115
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
92
116
  * the response data.
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ interface FridayOptions {
12
12
  * @var refreshTokenEndpoint: an optional string property representing the API endpoint for refreshing tokens.
13
13
  * @var enableAccessToken: a optional boolean property indicating whether token access is enabled.
14
14
  * @var enableRefreshToken: a optional boolean property indicating whether token refresh is enabled.
15
+ * @var storage: a required string property representing the storage mechanism for tokens.
15
16
  */
16
17
  interface FridayConfig {
17
18
  baseURL: string;
@@ -20,6 +21,7 @@ interface FridayConfig {
20
21
  refreshTokenEndpoint?: string;
21
22
  enableAccessToken?: boolean;
22
23
  enableRefreshToken?: boolean;
24
+ storage?: 'cookie' | 'local';
23
25
  }
24
26
 
25
27
  declare class Friday {
@@ -32,7 +34,7 @@ declare class Friday {
32
34
  * `FridayConfig`. It is used to configure the Friday instance with settings such as `baseURL` and
33
35
  * `enableRefreshToken`. The `axiosInstance` is created with the base URL specified in the `config`.
34
36
  */
35
- constructor({ baseURL, accessTokenKey, refreshTokenKey, refreshTokenEndpoint, enableAccessToken, enableRefreshToken, }: FridayConfig);
37
+ constructor({ baseURL, accessTokenKey, refreshTokenKey, refreshTokenEndpoint, enableAccessToken, enableRefreshToken, storage, }: FridayConfig);
36
38
  /**
37
39
  * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
38
40
  * request using a refresh token and updating the access token in the app's storage.
@@ -40,6 +42,17 @@ declare class Friday {
40
42
  * refreshing it using the refresh token.
41
43
  */
42
44
  refreshAccessToken(): Promise<string | undefined>;
45
+ /**
46
+ * The setTokens function stores a key-value pair either in a cookie or in local storage based on the
47
+ * configuration.
48
+ * @param {string} key - The `key` parameter is a string that represents the key under which the
49
+ * `value` will be stored in either a cookie or local storage, depending on the configuration set in
50
+ * `this.config.storage`.
51
+ * @param {string} value - The `value` parameter in the `setTokens` function represents the value that
52
+ * you want to store in either a cookie or local storage, based on the configuration set in
53
+ * `this.config.storage`.
54
+ */
55
+ private setTokens;
43
56
  /**
44
57
  * The function `resetTokens` sets the access token and refresh token in cookies based on the
45
58
  * response data from an Axios request.
@@ -87,6 +100,17 @@ declare class Friday {
87
100
  * message you want to associate with the error that will be thrown.
88
101
  */
89
102
  throwError(errorMessage: string): void;
103
+ /**
104
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
105
+ * query string, and returns the cleaned URL.
106
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
107
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
108
+ * URL.
109
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
110
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
111
+ * URL without the query string.
112
+ */
113
+ private cleanUrl;
90
114
  /**
91
115
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
92
116
  * the response data.
package/dist/index.js CHANGED
@@ -60,7 +60,8 @@ var Friday = class {
60
60
  refreshTokenKey = "refresh_token",
61
61
  refreshTokenEndpoint = "/api/refresh",
62
62
  enableAccessToken = true,
63
- enableRefreshToken = false
63
+ enableRefreshToken = false,
64
+ storage = "cookie"
64
65
  }) {
65
66
  this.config = {
66
67
  baseURL,
@@ -68,7 +69,8 @@ var Friday = class {
68
69
  refreshTokenKey,
69
70
  refreshTokenEndpoint,
70
71
  enableAccessToken,
71
- enableRefreshToken
72
+ enableRefreshToken,
73
+ storage
72
74
  };
73
75
  this.axiosInstance = import_axios.default.create({ baseURL: this.config.baseURL });
74
76
  if (this.config.enableRefreshToken && this.config.refreshTokenEndpoint) {
@@ -86,6 +88,19 @@ var Friday = class {
86
88
  }
87
89
  );
88
90
  }
91
+ this.axiosInstance.interceptors.request.use(
92
+ (config) => {
93
+ if (config.url) {
94
+ const baseUrl = config.baseURL || "";
95
+ const fullUrl = new URL(config.url, baseUrl).toString();
96
+ config.url = this.cleanUrl(fullUrl).replace(baseUrl, "");
97
+ }
98
+ return config;
99
+ },
100
+ (error) => {
101
+ return Promise.reject(error);
102
+ }
103
+ );
89
104
  }
90
105
  /**
91
106
  * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
@@ -98,9 +113,12 @@ var Friday = class {
98
113
  if (this.config.refreshTokenEndpoint == void 0) return;
99
114
  const refreshToken = this.getRefreshToken();
100
115
  if (refreshToken === void 0) throw new Error("Unauthorized Attempt!");
101
- const res = await this.axiosInstance.post(this.config.refreshTokenEndpoint, {
102
- refreshToken
103
- });
116
+ const res = await this.axiosInstance.post(
117
+ this.config.refreshTokenEndpoint,
118
+ {
119
+ refreshToken
120
+ }
121
+ );
104
122
  if (res.status != 200)
105
123
  throw new Error("Refresh access token request failed!");
106
124
  this.resetTokens(res);
@@ -109,6 +127,23 @@ var Friday = class {
109
127
  throw error;
110
128
  }
111
129
  }
130
+ /**
131
+ * The setTokens function stores a key-value pair either in a cookie or in local storage based on the
132
+ * configuration.
133
+ * @param {string} key - The `key` parameter is a string that represents the key under which the
134
+ * `value` will be stored in either a cookie or local storage, depending on the configuration set in
135
+ * `this.config.storage`.
136
+ * @param {string} value - The `value` parameter in the `setTokens` function represents the value that
137
+ * you want to store in either a cookie or local storage, based on the configuration set in
138
+ * `this.config.storage`.
139
+ */
140
+ setTokens(key, value) {
141
+ if (this.config.storage == "cookie") {
142
+ import_js_cookie.default.set(key, value);
143
+ } else if (this.config.storage == "local") {
144
+ localStorage.setItem(key, value);
145
+ }
146
+ }
112
147
  /**
113
148
  * The function `resetTokens` sets the access token and refresh token in cookies based on the
114
149
  * response data from an Axios request.
@@ -118,10 +153,10 @@ var Friday = class {
118
153
  */
119
154
  resetTokens(res) {
120
155
  if (this.config.accessTokenKey) {
121
- import_js_cookie.default.set(this.config.accessTokenKey, res.data.access_token);
156
+ this.setTokens(this.config.accessTokenKey, res.data.access_token);
122
157
  }
123
158
  if (this.config.refreshTokenKey) {
124
- import_js_cookie.default.set(this.config.refreshTokenKey, res.data.refresh_token);
159
+ this.setTokens(this.config.refreshTokenKey, res.data.refresh_token);
125
160
  }
126
161
  }
127
162
  /**
@@ -131,7 +166,11 @@ var Friday = class {
131
166
  * access token from a cookie using the `accessTokenKey` specified in the configuration.
132
167
  */
133
168
  getAccessToken() {
134
- return this.config.accessTokenKey && import_js_cookie.default.get(this.config.accessTokenKey);
169
+ if (this.config.storage == "cookie") {
170
+ return this.config.accessTokenKey && import_js_cookie.default.get(this.config.accessTokenKey);
171
+ } else if (this.config.storage == "local") {
172
+ return this.config.accessTokenKey && (localStorage.getItem(this.config.accessTokenKey) || void 0);
173
+ }
135
174
  }
136
175
  /**
137
176
  * Retrieves the refresh token from a cookie using the key specified in the configuration.
@@ -186,6 +225,28 @@ var Friday = class {
186
225
  throwError(errorMessage) {
187
226
  throw new Error(errorMessage);
188
227
  }
228
+ /**
229
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
230
+ * query string, and returns the cleaned URL.
231
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
232
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
233
+ * URL.
234
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
235
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
236
+ * URL without the query string.
237
+ */
238
+ cleanUrl(url) {
239
+ const [base, query] = url.split("?");
240
+ if (!query) return url;
241
+ const searchParams = new URLSearchParams(query);
242
+ for (const [key, value] of searchParams.entries()) {
243
+ if (value === "") {
244
+ searchParams.delete(key);
245
+ }
246
+ }
247
+ const cleanedQuery = searchParams.toString();
248
+ return cleanedQuery ? `${base}?${cleanedQuery}` : base;
249
+ }
189
250
  /**
190
251
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
191
252
  * the response data.
package/dist/index.mjs CHANGED
@@ -24,7 +24,8 @@ var Friday = class {
24
24
  refreshTokenKey = "refresh_token",
25
25
  refreshTokenEndpoint = "/api/refresh",
26
26
  enableAccessToken = true,
27
- enableRefreshToken = false
27
+ enableRefreshToken = false,
28
+ storage = "cookie"
28
29
  }) {
29
30
  this.config = {
30
31
  baseURL,
@@ -32,7 +33,8 @@ var Friday = class {
32
33
  refreshTokenKey,
33
34
  refreshTokenEndpoint,
34
35
  enableAccessToken,
35
- enableRefreshToken
36
+ enableRefreshToken,
37
+ storage
36
38
  };
37
39
  this.axiosInstance = axios.create({ baseURL: this.config.baseURL });
38
40
  if (this.config.enableRefreshToken && this.config.refreshTokenEndpoint) {
@@ -50,6 +52,19 @@ var Friday = class {
50
52
  }
51
53
  );
52
54
  }
55
+ this.axiosInstance.interceptors.request.use(
56
+ (config) => {
57
+ if (config.url) {
58
+ const baseUrl = config.baseURL || "";
59
+ const fullUrl = new URL(config.url, baseUrl).toString();
60
+ config.url = this.cleanUrl(fullUrl).replace(baseUrl, "");
61
+ }
62
+ return config;
63
+ },
64
+ (error) => {
65
+ return Promise.reject(error);
66
+ }
67
+ );
53
68
  }
54
69
  /**
55
70
  * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
@@ -62,9 +77,12 @@ var Friday = class {
62
77
  if (this.config.refreshTokenEndpoint == void 0) return;
63
78
  const refreshToken = this.getRefreshToken();
64
79
  if (refreshToken === void 0) throw new Error("Unauthorized Attempt!");
65
- const res = await this.axiosInstance.post(this.config.refreshTokenEndpoint, {
66
- refreshToken
67
- });
80
+ const res = await this.axiosInstance.post(
81
+ this.config.refreshTokenEndpoint,
82
+ {
83
+ refreshToken
84
+ }
85
+ );
68
86
  if (res.status != 200)
69
87
  throw new Error("Refresh access token request failed!");
70
88
  this.resetTokens(res);
@@ -73,6 +91,23 @@ var Friday = class {
73
91
  throw error;
74
92
  }
75
93
  }
94
+ /**
95
+ * The setTokens function stores a key-value pair either in a cookie or in local storage based on the
96
+ * configuration.
97
+ * @param {string} key - The `key` parameter is a string that represents the key under which the
98
+ * `value` will be stored in either a cookie or local storage, depending on the configuration set in
99
+ * `this.config.storage`.
100
+ * @param {string} value - The `value` parameter in the `setTokens` function represents the value that
101
+ * you want to store in either a cookie or local storage, based on the configuration set in
102
+ * `this.config.storage`.
103
+ */
104
+ setTokens(key, value) {
105
+ if (this.config.storage == "cookie") {
106
+ Cookies.set(key, value);
107
+ } else if (this.config.storage == "local") {
108
+ localStorage.setItem(key, value);
109
+ }
110
+ }
76
111
  /**
77
112
  * The function `resetTokens` sets the access token and refresh token in cookies based on the
78
113
  * response data from an Axios request.
@@ -82,10 +117,10 @@ var Friday = class {
82
117
  */
83
118
  resetTokens(res) {
84
119
  if (this.config.accessTokenKey) {
85
- Cookies.set(this.config.accessTokenKey, res.data.access_token);
120
+ this.setTokens(this.config.accessTokenKey, res.data.access_token);
86
121
  }
87
122
  if (this.config.refreshTokenKey) {
88
- Cookies.set(this.config.refreshTokenKey, res.data.refresh_token);
123
+ this.setTokens(this.config.refreshTokenKey, res.data.refresh_token);
89
124
  }
90
125
  }
91
126
  /**
@@ -95,7 +130,11 @@ var Friday = class {
95
130
  * access token from a cookie using the `accessTokenKey` specified in the configuration.
96
131
  */
97
132
  getAccessToken() {
98
- return this.config.accessTokenKey && Cookies.get(this.config.accessTokenKey);
133
+ if (this.config.storage == "cookie") {
134
+ return this.config.accessTokenKey && Cookies.get(this.config.accessTokenKey);
135
+ } else if (this.config.storage == "local") {
136
+ return this.config.accessTokenKey && (localStorage.getItem(this.config.accessTokenKey) || void 0);
137
+ }
99
138
  }
100
139
  /**
101
140
  * Retrieves the refresh token from a cookie using the key specified in the configuration.
@@ -150,6 +189,28 @@ var Friday = class {
150
189
  throwError(errorMessage) {
151
190
  throw new Error(errorMessage);
152
191
  }
192
+ /**
193
+ * The `cleanUrl` function in TypeScript takes a URL as input, removes any empty parameters from the
194
+ * query string, and returns the cleaned URL.
195
+ * @param {string} url - The `cleanUrl` function takes a URL as a parameter and cleans up the query
196
+ * string by removing any empty parameters. If there are no query parameters, it returns the original
197
+ * URL.
198
+ * @returns The `cleanUrl` function returns a cleaned version of the input URL by removing any empty
199
+ * parameters from the query string. If the query string is empty after cleaning, it returns the base
200
+ * URL without the query string.
201
+ */
202
+ cleanUrl(url) {
203
+ const [base, query] = url.split("?");
204
+ if (!query) return url;
205
+ const searchParams = new URLSearchParams(query);
206
+ for (const [key, value] of searchParams.entries()) {
207
+ if (value === "") {
208
+ searchParams.delete(key);
209
+ }
210
+ }
211
+ const cleanedQuery = searchParams.toString();
212
+ return cleanedQuery ? `${base}?${cleanedQuery}` : base;
213
+ }
153
214
  /**
154
215
  * The `get` function makes an asynchronous GET request using Axios with error handling and returns
155
216
  * 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.2.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",