@sabuj0338/axios-friday 1.1.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.
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.
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) {
@@ -125,6 +127,23 @@ var Friday = class {
125
127
  throw error;
126
128
  }
127
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
+ }
128
147
  /**
129
148
  * The function `resetTokens` sets the access token and refresh token in cookies based on the
130
149
  * response data from an Axios request.
@@ -134,10 +153,10 @@ var Friday = class {
134
153
  */
135
154
  resetTokens(res) {
136
155
  if (this.config.accessTokenKey) {
137
- import_js_cookie.default.set(this.config.accessTokenKey, res.data.access_token);
156
+ this.setTokens(this.config.accessTokenKey, res.data.access_token);
138
157
  }
139
158
  if (this.config.refreshTokenKey) {
140
- import_js_cookie.default.set(this.config.refreshTokenKey, res.data.refresh_token);
159
+ this.setTokens(this.config.refreshTokenKey, res.data.refresh_token);
141
160
  }
142
161
  }
143
162
  /**
@@ -147,7 +166,11 @@ var Friday = class {
147
166
  * access token from a cookie using the `accessTokenKey` specified in the configuration.
148
167
  */
149
168
  getAccessToken() {
150
- 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
+ }
151
174
  }
152
175
  /**
153
176
  * Retrieves the refresh token from a cookie using the key specified in the configuration.
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) {
@@ -89,6 +91,23 @@ var Friday = class {
89
91
  throw error;
90
92
  }
91
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
+ }
92
111
  /**
93
112
  * The function `resetTokens` sets the access token and refresh token in cookies based on the
94
113
  * response data from an Axios request.
@@ -98,10 +117,10 @@ var Friday = class {
98
117
  */
99
118
  resetTokens(res) {
100
119
  if (this.config.accessTokenKey) {
101
- Cookies.set(this.config.accessTokenKey, res.data.access_token);
120
+ this.setTokens(this.config.accessTokenKey, res.data.access_token);
102
121
  }
103
122
  if (this.config.refreshTokenKey) {
104
- Cookies.set(this.config.refreshTokenKey, res.data.refresh_token);
123
+ this.setTokens(this.config.refreshTokenKey, res.data.refresh_token);
105
124
  }
106
125
  }
107
126
  /**
@@ -111,7 +130,11 @@ var Friday = class {
111
130
  * access token from a cookie using the `accessTokenKey` specified in the configuration.
112
131
  */
113
132
  getAccessToken() {
114
- 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
+ }
115
138
  }
116
139
  /**
117
140
  * Retrieves the refresh token from a cookie using the key specified in the configuration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sabuj0338/axios-friday",
3
- "version": "1.1.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",