@sabuj0338/axios-friday 0.1.2 → 0.1.3

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
@@ -1,5 +1,3 @@
1
- import { AxiosResponse } from 'axios';
2
-
3
1
  interface FridayOptions {
4
2
  enableThrowHttpError?: false;
5
3
  body?: object;
@@ -21,140 +19,5 @@ interface FridayConfig {
21
19
  enableAccessToken?: boolean;
22
20
  enableRefreshToken?: boolean;
23
21
  }
24
- declare class Friday {
25
- private config;
26
- private axiosInstance;
27
- /**
28
- * The constructor function initializes an Axios instance with a base URL and sets up a response
29
- * interceptor to handle token expiration and refresh if configured to do so.
30
- * @param {FridayConfig} config - The `config` parameter in the constructor function is of type
31
- * `FridayConfig`. It is used to configure the Friday instance with settings such as `baseURL` and
32
- * `enableRefreshToken`. The `axiosInstance` is created with the base URL specified in the `config`.
33
- */
34
- constructor({ baseURL, accessTokenKey, refreshTokenKey, refreshTokenEndpoint, enableAccessToken, enableRefreshToken, }: FridayConfig);
35
- /**
36
- * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
37
- * request using a refresh token and updating the access token in the app's storage.
38
- * @returns The `refreshAccessToken` function is returning the access token after successfully
39
- * refreshing it using the refresh token.
40
- */
41
- refreshAccessToken(): Promise<string | undefined>;
42
- /**
43
- * The function `resetTokens` sets the access token and refresh token in cookies based on the
44
- * response data from an Axios request.
45
- * @param res - The `res` parameter in the `resetTokens` function is an AxiosResponse object. It
46
- * represents the response returned from an HTTP request made using Axios. The AxiosResponse object
47
- * contains data such as the response data, status, headers, and more.
48
- */
49
- resetTokens(res: AxiosResponse<any, any>): void;
50
- /**
51
- * The function `getAccessToken` retrieves the access token from a cookie using the key specified in
52
- * the configuration.
53
- * @returns The `getAccessToken` method is returning a string value or `undefined`. It retrieves the
54
- * access token from a cookie using the `accessTokenKey` specified in the configuration.
55
- */
56
- private getAccessToken;
57
- /**
58
- * Retrieves the refresh token from a cookie using the key specified in the configuration.
59
- * @returns A string value or `undefined`. It retrieves the refresh token from a cookie using the
60
- * `refreshTokenKey` specified in the configuration.
61
- */
62
- private getRefreshToken;
63
- /**
64
- * The function `getAuthorizationHeader` returns an object with an Authorization header containing a
65
- * Bearer token if an access token is available.
66
- * @returns If the `accessToken` is not `undefined`, an object with the `Authorization` header
67
- * containing the access token in the format `Bearer ` is being returned. If the
68
- * `accessToken` is `undefined`, then `undefined` is being returned.
69
- */
70
- private getAuthorizationHeader;
71
- /**
72
- * The function `handleError` in TypeScript handles errors by extracting error messages and
73
- * optionally displaying them as a toast.
74
- * @param {unknown} e - The parameter `e` in the `handleError` function is used to represent the
75
- * error that needs to be handled. It can be of type `unknown`, which means it can be any type of
76
- * value. The function checks if `e` is an AxiosError and extracts the error message accordingly.
77
- * @param {FridayOptions} [options] - The `options` parameter in the `handleError` function is of type
78
- * `FridayOptions`. It is an optional parameter that allows you to pass additional options to the
79
- * function. These options can include properties like `showToast`, which is a boolean value
80
- * indicating whether a toast message should be displayed
81
- */
82
- private handleError;
83
- /**
84
- * The throwError function in TypeScript throws an error with a specified error message.
85
- * @param {string} errorMessage - The `errorMessage` parameter is a string that represents the
86
- * message you want to associate with the error that will be thrown.
87
- */
88
- throwError(errorMessage: string): void;
89
- /**
90
- * The `get` function makes an asynchronous GET request using Axios with error handling and returns
91
- * the response data.
92
- * @param {URL} url - The `url` parameter in the `get` function is of type `URL`, which represents a
93
- * Uniform Resource Locator and is used to specify the address of a resource on the internet. It is
94
- * the endpoint from which the data will be fetched in the `get` request.
95
- * @param {FridayOptions} [options] - The `options` parameter in the `get` function is of type
96
- * `FridayOptions`. It is an optional parameter that can be passed to the function to provide
97
- * additional configuration or settings for the HTTP request. This parameter allows for customization
98
- * of the request behavior based on the specific needs of the application
99
- * @returns The `get` function is returning the data from the response if the request is successful.
100
- * If there is an error during the request, it will handle the error using the `handleError` method
101
- * with the provided options.
102
- */
103
- get(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
104
- /**
105
- * This TypeScript function sends a POST request using Axios with optional parameters and handles any
106
- * errors that occur.
107
- * @param {URL} url - The `url` parameter in the `post` function is a URL object that represents the
108
- * endpoint where the POST request will be sent. It specifies the location where the data will be
109
- * posted to.
110
- * @param {FridayOptions} [options] - The `options` parameter in the `post` function is an optional
111
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
112
- * HTTP POST request, such as the request body and headers. If `options` is provided, the function
113
- * will use the `body`
114
- * @returns The `post` function is returning the data from the response if the request is successful.
115
- * If there is an error during the request, it will handle the error using the `handleError` method
116
- * and return the result of that handling.
117
- */
118
- post(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
119
- /**
120
- * This TypeScript function sends a PUT request using Axios with specified options and handles any
121
- * errors that occur.
122
- * @param {URL} url - The `url` parameter in the `put` function is a URL object that represents the
123
- * URL where the PUT request will be sent. It is used to specify the destination of the request.
124
- * @param {FridayOptions} [options] - The `options` parameter in the `put` function is an optional
125
- * object that may contain the following properties:
126
- * @returns The `put` function is returning the data from the response if the request is successful.
127
- * If there is an error during the request, it will handle the error using the `handleError` method
128
- * and return the result of that handling.
129
- */
130
- put(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
131
- /**
132
- * The `delete` function sends a DELETE request to a specified URL with optional headers and body,
133
- * handling errors and returning the response data.
134
- * @param {URL} url - The `url` parameter is a `URL` object that represents the URL of the resource
135
- * you want to delete. It is used to specify the location of the resource that you want to delete.
136
- * @param {FridayOptions} [options] - The `options` parameter in the `delete` function is an optional
137
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
138
- * HTTP request, such as request body and headers. The function uses the `body` property from the
139
- * `options` object as the data
140
- * @returns The `delete` method is returning the data from the response if the request is successful.
141
- * If there is an error during the request, it will handle the error and return the result of the
142
- * `handleError` method with the provided options.
143
- */
144
- delete(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
145
- /**
146
- * The `upload` function makes an asynchronous POST request using Axios with error handling and returns
147
- * the response data.
148
- * @param {URL} url - The `url` parameter in the `upload` function is of type `URL`, representing the
149
- * Uniform Resource Locator where the data will be uploaded. It specifies the endpoint to which the
150
- * data will be sent in the `POST` request.
151
- * @param {FormData} body - The `body` parameter in the `upload` function is of type `FormData`, which
152
- * contains the data to be uploaded in the request. It includes the content that will be sent to the
153
- * server in the upload operation.
154
- * @returns The `upload` function is returning the data from the response if the request is successful.
155
- * If an error occurs during the request, it will handle the error using the `handleError` method.
156
- */
157
- upload(url: URL, body: FormData): Promise<AxiosResponse<any, any> | undefined>;
158
- }
159
22
 
160
- export { type FridayConfig, type FridayOptions, Friday as default };
23
+ export type { FridayConfig, FridayOptions };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { AxiosResponse } from 'axios';
2
-
3
1
  interface FridayOptions {
4
2
  enableThrowHttpError?: false;
5
3
  body?: object;
@@ -21,140 +19,5 @@ interface FridayConfig {
21
19
  enableAccessToken?: boolean;
22
20
  enableRefreshToken?: boolean;
23
21
  }
24
- declare class Friday {
25
- private config;
26
- private axiosInstance;
27
- /**
28
- * The constructor function initializes an Axios instance with a base URL and sets up a response
29
- * interceptor to handle token expiration and refresh if configured to do so.
30
- * @param {FridayConfig} config - The `config` parameter in the constructor function is of type
31
- * `FridayConfig`. It is used to configure the Friday instance with settings such as `baseURL` and
32
- * `enableRefreshToken`. The `axiosInstance` is created with the base URL specified in the `config`.
33
- */
34
- constructor({ baseURL, accessTokenKey, refreshTokenKey, refreshTokenEndpoint, enableAccessToken, enableRefreshToken, }: FridayConfig);
35
- /**
36
- * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
37
- * request using a refresh token and updating the access token in the app's storage.
38
- * @returns The `refreshAccessToken` function is returning the access token after successfully
39
- * refreshing it using the refresh token.
40
- */
41
- refreshAccessToken(): Promise<string | undefined>;
42
- /**
43
- * The function `resetTokens` sets the access token and refresh token in cookies based on the
44
- * response data from an Axios request.
45
- * @param res - The `res` parameter in the `resetTokens` function is an AxiosResponse object. It
46
- * represents the response returned from an HTTP request made using Axios. The AxiosResponse object
47
- * contains data such as the response data, status, headers, and more.
48
- */
49
- resetTokens(res: AxiosResponse<any, any>): void;
50
- /**
51
- * The function `getAccessToken` retrieves the access token from a cookie using the key specified in
52
- * the configuration.
53
- * @returns The `getAccessToken` method is returning a string value or `undefined`. It retrieves the
54
- * access token from a cookie using the `accessTokenKey` specified in the configuration.
55
- */
56
- private getAccessToken;
57
- /**
58
- * Retrieves the refresh token from a cookie using the key specified in the configuration.
59
- * @returns A string value or `undefined`. It retrieves the refresh token from a cookie using the
60
- * `refreshTokenKey` specified in the configuration.
61
- */
62
- private getRefreshToken;
63
- /**
64
- * The function `getAuthorizationHeader` returns an object with an Authorization header containing a
65
- * Bearer token if an access token is available.
66
- * @returns If the `accessToken` is not `undefined`, an object with the `Authorization` header
67
- * containing the access token in the format `Bearer ` is being returned. If the
68
- * `accessToken` is `undefined`, then `undefined` is being returned.
69
- */
70
- private getAuthorizationHeader;
71
- /**
72
- * The function `handleError` in TypeScript handles errors by extracting error messages and
73
- * optionally displaying them as a toast.
74
- * @param {unknown} e - The parameter `e` in the `handleError` function is used to represent the
75
- * error that needs to be handled. It can be of type `unknown`, which means it can be any type of
76
- * value. The function checks if `e` is an AxiosError and extracts the error message accordingly.
77
- * @param {FridayOptions} [options] - The `options` parameter in the `handleError` function is of type
78
- * `FridayOptions`. It is an optional parameter that allows you to pass additional options to the
79
- * function. These options can include properties like `showToast`, which is a boolean value
80
- * indicating whether a toast message should be displayed
81
- */
82
- private handleError;
83
- /**
84
- * The throwError function in TypeScript throws an error with a specified error message.
85
- * @param {string} errorMessage - The `errorMessage` parameter is a string that represents the
86
- * message you want to associate with the error that will be thrown.
87
- */
88
- throwError(errorMessage: string): void;
89
- /**
90
- * The `get` function makes an asynchronous GET request using Axios with error handling and returns
91
- * the response data.
92
- * @param {URL} url - The `url` parameter in the `get` function is of type `URL`, which represents a
93
- * Uniform Resource Locator and is used to specify the address of a resource on the internet. It is
94
- * the endpoint from which the data will be fetched in the `get` request.
95
- * @param {FridayOptions} [options] - The `options` parameter in the `get` function is of type
96
- * `FridayOptions`. It is an optional parameter that can be passed to the function to provide
97
- * additional configuration or settings for the HTTP request. This parameter allows for customization
98
- * of the request behavior based on the specific needs of the application
99
- * @returns The `get` function is returning the data from the response if the request is successful.
100
- * If there is an error during the request, it will handle the error using the `handleError` method
101
- * with the provided options.
102
- */
103
- get(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
104
- /**
105
- * This TypeScript function sends a POST request using Axios with optional parameters and handles any
106
- * errors that occur.
107
- * @param {URL} url - The `url` parameter in the `post` function is a URL object that represents the
108
- * endpoint where the POST request will be sent. It specifies the location where the data will be
109
- * posted to.
110
- * @param {FridayOptions} [options] - The `options` parameter in the `post` function is an optional
111
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
112
- * HTTP POST request, such as the request body and headers. If `options` is provided, the function
113
- * will use the `body`
114
- * @returns The `post` function is returning the data from the response if the request is successful.
115
- * If there is an error during the request, it will handle the error using the `handleError` method
116
- * and return the result of that handling.
117
- */
118
- post(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
119
- /**
120
- * This TypeScript function sends a PUT request using Axios with specified options and handles any
121
- * errors that occur.
122
- * @param {URL} url - The `url` parameter in the `put` function is a URL object that represents the
123
- * URL where the PUT request will be sent. It is used to specify the destination of the request.
124
- * @param {FridayOptions} [options] - The `options` parameter in the `put` function is an optional
125
- * object that may contain the following properties:
126
- * @returns The `put` function is returning the data from the response if the request is successful.
127
- * If there is an error during the request, it will handle the error using the `handleError` method
128
- * and return the result of that handling.
129
- */
130
- put(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
131
- /**
132
- * The `delete` function sends a DELETE request to a specified URL with optional headers and body,
133
- * handling errors and returning the response data.
134
- * @param {URL} url - The `url` parameter is a `URL` object that represents the URL of the resource
135
- * you want to delete. It is used to specify the location of the resource that you want to delete.
136
- * @param {FridayOptions} [options] - The `options` parameter in the `delete` function is an optional
137
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
138
- * HTTP request, such as request body and headers. The function uses the `body` property from the
139
- * `options` object as the data
140
- * @returns The `delete` method is returning the data from the response if the request is successful.
141
- * If there is an error during the request, it will handle the error and return the result of the
142
- * `handleError` method with the provided options.
143
- */
144
- delete(url: URL, options?: FridayOptions): Promise<AxiosResponse<any, any> | undefined>;
145
- /**
146
- * The `upload` function makes an asynchronous POST request using Axios with error handling and returns
147
- * the response data.
148
- * @param {URL} url - The `url` parameter in the `upload` function is of type `URL`, representing the
149
- * Uniform Resource Locator where the data will be uploaded. It specifies the endpoint to which the
150
- * data will be sent in the `POST` request.
151
- * @param {FormData} body - The `body` parameter in the `upload` function is of type `FormData`, which
152
- * contains the data to be uploaded in the request. It includes the content that will be sent to the
153
- * server in the upload operation.
154
- * @returns The `upload` function is returning the data from the response if the request is successful.
155
- * If an error occurs during the request, it will handle the error using the `handleError` method.
156
- */
157
- upload(url: URL, body: FormData): Promise<AxiosResponse<any, any> | undefined>;
158
- }
159
22
 
160
- export { type FridayConfig, type FridayOptions, Friday as default };
23
+ export type { FridayConfig, FridayOptions };
package/dist/index.js CHANGED
@@ -5,10 +5,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
8
  var __copyProps = (to, from, except, desc) => {
13
9
  if (from && typeof from === "object" || typeof from === "function") {
14
10
  for (let key of __getOwnPropNames(from))
@@ -29,285 +25,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
25
 
30
26
  // src/index.ts
31
27
  var src_exports = {};
32
- __export(src_exports, {
33
- default: () => Friday
34
- });
35
28
  module.exports = __toCommonJS(src_exports);
29
+
30
+ // src/friday.ts
36
31
  var import_axios = __toESM(require("axios"));
37
32
  var import_js_cookie = __toESM(require("js-cookie"));
38
- var Friday = class {
39
- /* The above code is declaring a private property `config` of type `FridayConfig` in a TypeScript
40
- class or interface. The `FridayConfig` type is likely a custom type defined elsewhere in the
41
- codebase. This property can only be accessed within the class or interface where it is declared. */
42
- config;
43
- /* The above code is declaring a private property named `axiosInstance` of type `AxiosInstance`. This
44
- property is likely used to store an instance of Axios, which is a popular library for making HTTP
45
- requests in JavaScript and TypeScript. This property can be accessed and used within the class
46
- where it is declared. */
47
- axiosInstance;
48
- /**
49
- * The constructor function initializes an Axios instance with a base URL and sets up a response
50
- * interceptor to handle token expiration and refresh if configured to do so.
51
- * @param {FridayConfig} config - The `config` parameter in the constructor function is of type
52
- * `FridayConfig`. It is used to configure the Friday instance with settings such as `baseURL` and
53
- * `enableRefreshToken`. The `axiosInstance` is created with the base URL specified in the `config`.
54
- */
55
- constructor({
56
- baseURL,
57
- accessTokenKey = "access_token",
58
- refreshTokenKey = "refresh_token",
59
- refreshTokenEndpoint = "/api/refresh",
60
- enableAccessToken = true,
61
- enableRefreshToken = false
62
- }) {
63
- this.config = {
64
- baseURL,
65
- accessTokenKey,
66
- refreshTokenKey,
67
- refreshTokenEndpoint,
68
- enableAccessToken,
69
- enableRefreshToken
70
- };
71
- this.axiosInstance = import_axios.default.create({ baseURL: this.config.baseURL });
72
- if (this.config.enableRefreshToken && this.config.refreshTokenEndpoint) {
73
- this.axiosInstance.interceptors.response.use(
74
- (response) => response,
75
- async (error) => {
76
- const originalRequest = error.config;
77
- if (error.response.status === 401 && !originalRequest._retry) {
78
- originalRequest._retry = true;
79
- const newAccessToken = await this.refreshAccessToken();
80
- originalRequest.headers.Authorization = `Bearer ${newAccessToken}`;
81
- return this.axiosInstance(originalRequest);
82
- }
83
- return Promise.reject(error);
84
- }
85
- );
86
- }
87
- }
88
- /**
89
- * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
90
- * request using a refresh token and updating the access token in the app's storage.
91
- * @returns The `refreshAccessToken` function is returning the access token after successfully
92
- * refreshing it using the refresh token.
93
- */
94
- async refreshAccessToken() {
95
- try {
96
- if (this.config.refreshTokenEndpoint == void 0) return;
97
- const refreshToken = this.getRefreshToken();
98
- if (refreshToken === void 0) throw new Error("Unauthorized Attempt!");
99
- const res = await this.axiosInstance.post(this.config.refreshTokenEndpoint, {
100
- refreshToken
101
- });
102
- if (res.status != 200)
103
- throw new Error("Refresh access token request failed!");
104
- this.resetTokens(res);
105
- return this.getAccessToken();
106
- } catch (error) {
107
- throw error;
108
- }
109
- }
110
- /**
111
- * The function `resetTokens` sets the access token and refresh token in cookies based on the
112
- * response data from an Axios request.
113
- * @param res - The `res` parameter in the `resetTokens` function is an AxiosResponse object. It
114
- * represents the response returned from an HTTP request made using Axios. The AxiosResponse object
115
- * contains data such as the response data, status, headers, and more.
116
- */
117
- resetTokens(res) {
118
- if (this.config.accessTokenKey) {
119
- import_js_cookie.default.set(this.config.accessTokenKey, res.data.access_token);
120
- }
121
- if (this.config.refreshTokenKey) {
122
- import_js_cookie.default.set(this.config.refreshTokenKey, res.data.refresh_token);
123
- }
124
- }
125
- /**
126
- * The function `getAccessToken` retrieves the access token from a cookie using the key specified in
127
- * the configuration.
128
- * @returns The `getAccessToken` method is returning a string value or `undefined`. It retrieves the
129
- * access token from a cookie using the `accessTokenKey` specified in the configuration.
130
- */
131
- getAccessToken() {
132
- return this.config.accessTokenKey && import_js_cookie.default.get(this.config.accessTokenKey);
133
- }
134
- /**
135
- * Retrieves the refresh token from a cookie using the key specified in the configuration.
136
- * @returns A string value or `undefined`. It retrieves the refresh token from a cookie using the
137
- * `refreshTokenKey` specified in the configuration.
138
- */
139
- getRefreshToken() {
140
- return this.config.refreshTokenKey && import_js_cookie.default.get(this.config.refreshTokenKey);
141
- }
142
- /**
143
- * The function `getAuthorizationHeader` returns an object with an Authorization header containing a
144
- * Bearer token if an access token is available.
145
- * @returns If the `accessToken` is not `undefined`, an object with the `Authorization` header
146
- * containing the access token in the format `Bearer ` is being returned. If the
147
- * `accessToken` is `undefined`, then `undefined` is being returned.
148
- */
149
- getAuthorizationHeader() {
150
- if (this.config.enableAccessToken != true) return;
151
- const accessToken = this.getAccessToken();
152
- if (accessToken === void 0) return;
153
- return { Authorization: `Bearer ${accessToken}` };
154
- }
155
- /**
156
- * The function `handleError` in TypeScript handles errors by extracting error messages and
157
- * optionally displaying them as a toast.
158
- * @param {unknown} e - The parameter `e` in the `handleError` function is used to represent the
159
- * error that needs to be handled. It can be of type `unknown`, which means it can be any type of
160
- * value. The function checks if `e` is an AxiosError and extracts the error message accordingly.
161
- * @param {FridayOptions} [options] - The `options` parameter in the `handleError` function is of type
162
- * `FridayOptions`. It is an optional parameter that allows you to pass additional options to the
163
- * function. These options can include properties like `showToast`, which is a boolean value
164
- * indicating whether a toast message should be displayed
165
- */
166
- handleError(e, options) {
167
- if (options?.enableThrowHttpError == false) return;
168
- let errorMessage;
169
- if (e?.response) {
170
- const error = e;
171
- errorMessage = (error.response?.data).message;
172
- } else {
173
- errorMessage = e.message;
174
- }
175
- if (errorMessage) {
176
- this.throwError(errorMessage);
177
- }
178
- }
179
- /**
180
- * The throwError function in TypeScript throws an error with a specified error message.
181
- * @param {string} errorMessage - The `errorMessage` parameter is a string that represents the
182
- * message you want to associate with the error that will be thrown.
183
- */
184
- throwError(errorMessage) {
185
- throw new Error(errorMessage);
186
- }
187
- /**
188
- * The `get` function makes an asynchronous GET request using Axios with error handling and returns
189
- * the response data.
190
- * @param {URL} url - The `url` parameter in the `get` function is of type `URL`, which represents a
191
- * Uniform Resource Locator and is used to specify the address of a resource on the internet. It is
192
- * the endpoint from which the data will be fetched in the `get` request.
193
- * @param {FridayOptions} [options] - The `options` parameter in the `get` function is of type
194
- * `FridayOptions`. It is an optional parameter that can be passed to the function to provide
195
- * additional configuration or settings for the HTTP request. This parameter allows for customization
196
- * of the request behavior based on the specific needs of the application
197
- * @returns The `get` function is returning the data from the response if the request is successful.
198
- * If there is an error during the request, it will handle the error using the `handleError` method
199
- * with the provided options.
200
- */
201
- async get(url, options) {
202
- try {
203
- return await this.axiosInstance.get(url.href, {
204
- headers: {
205
- ...this.getAuthorizationHeader(),
206
- "Content-Type": "application/json"
207
- }
208
- });
209
- } catch (error) {
210
- return this.handleError(error, options);
211
- }
212
- }
213
- /**
214
- * This TypeScript function sends a POST request using Axios with optional parameters and handles any
215
- * errors that occur.
216
- * @param {URL} url - The `url` parameter in the `post` function is a URL object that represents the
217
- * endpoint where the POST request will be sent. It specifies the location where the data will be
218
- * posted to.
219
- * @param {FridayOptions} [options] - The `options` parameter in the `post` function is an optional
220
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
221
- * HTTP POST request, such as the request body and headers. If `options` is provided, the function
222
- * will use the `body`
223
- * @returns The `post` function is returning the data from the response if the request is successful.
224
- * If there is an error during the request, it will handle the error using the `handleError` method
225
- * and return the result of that handling.
226
- */
227
- async post(url, options) {
228
- try {
229
- return await this.axiosInstance.post(url.href, options?.body, {
230
- headers: {
231
- ...options?.headers,
232
- ...this.getAuthorizationHeader(),
233
- "Content-Type": "application/json"
234
- }
235
- });
236
- } catch (error) {
237
- return this.handleError(error, options);
238
- }
239
- }
240
- /**
241
- * This TypeScript function sends a PUT request using Axios with specified options and handles any
242
- * errors that occur.
243
- * @param {URL} url - The `url` parameter in the `put` function is a URL object that represents the
244
- * URL where the PUT request will be sent. It is used to specify the destination of the request.
245
- * @param {FridayOptions} [options] - The `options` parameter in the `put` function is an optional
246
- * object that may contain the following properties:
247
- * @returns The `put` function is returning the data from the response if the request is successful.
248
- * If there is an error during the request, it will handle the error using the `handleError` method
249
- * and return the result of that handling.
250
- */
251
- async put(url, options) {
252
- try {
253
- return await this.axiosInstance.put(url.href, options?.body, {
254
- headers: {
255
- ...options?.headers,
256
- ...this.getAuthorizationHeader(),
257
- "Content-Type": "application/json"
258
- }
259
- });
260
- } catch (error) {
261
- return this.handleError(error, options);
262
- }
263
- }
264
- /**
265
- * The `delete` function sends a DELETE request to a specified URL with optional headers and body,
266
- * handling errors and returning the response data.
267
- * @param {URL} url - The `url` parameter is a `URL` object that represents the URL of the resource
268
- * you want to delete. It is used to specify the location of the resource that you want to delete.
269
- * @param {FridayOptions} [options] - The `options` parameter in the `delete` function is an optional
270
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
271
- * HTTP request, such as request body and headers. The function uses the `body` property from the
272
- * `options` object as the data
273
- * @returns The `delete` method is returning the data from the response if the request is successful.
274
- * If there is an error during the request, it will handle the error and return the result of the
275
- * `handleError` method with the provided options.
276
- */
277
- async delete(url, options) {
278
- try {
279
- return await this.axiosInstance.delete(url.href, {
280
- data: options?.body,
281
- headers: {
282
- ...options?.headers,
283
- ...this.getAuthorizationHeader()
284
- }
285
- });
286
- } catch (error) {
287
- return this.handleError(error, options);
288
- }
289
- }
290
- /**
291
- * The `upload` function makes an asynchronous POST request using Axios with error handling and returns
292
- * the response data.
293
- * @param {URL} url - The `url` parameter in the `upload` function is of type `URL`, representing the
294
- * Uniform Resource Locator where the data will be uploaded. It specifies the endpoint to which the
295
- * data will be sent in the `POST` request.
296
- * @param {FormData} body - The `body` parameter in the `upload` function is of type `FormData`, which
297
- * contains the data to be uploaded in the request. It includes the content that will be sent to the
298
- * server in the upload operation.
299
- * @returns The `upload` function is returning the data from the response if the request is successful.
300
- * If an error occurs during the request, it will handle the error using the `handleError` method.
301
- */
302
- async upload(url, body) {
303
- try {
304
- return await this.axiosInstance.post(url.href, body, {
305
- headers: {
306
- ...this.getAuthorizationHeader()
307
- }
308
- });
309
- } catch (e) {
310
- return this.handleError(e);
311
- }
312
- }
313
- };
package/dist/index.mjs CHANGED
@@ -1,282 +1,3 @@
1
- // src/index.ts
1
+ // src/friday.ts
2
2
  import axios from "axios";
3
3
  import Cookies from "js-cookie";
4
- var Friday = class {
5
- /* The above code is declaring a private property `config` of type `FridayConfig` in a TypeScript
6
- class or interface. The `FridayConfig` type is likely a custom type defined elsewhere in the
7
- codebase. This property can only be accessed within the class or interface where it is declared. */
8
- config;
9
- /* The above code is declaring a private property named `axiosInstance` of type `AxiosInstance`. This
10
- property is likely used to store an instance of Axios, which is a popular library for making HTTP
11
- requests in JavaScript and TypeScript. This property can be accessed and used within the class
12
- where it is declared. */
13
- axiosInstance;
14
- /**
15
- * The constructor function initializes an Axios instance with a base URL and sets up a response
16
- * interceptor to handle token expiration and refresh if configured to do so.
17
- * @param {FridayConfig} config - The `config` parameter in the constructor function is of type
18
- * `FridayConfig`. It is used to configure the Friday instance with settings such as `baseURL` and
19
- * `enableRefreshToken`. The `axiosInstance` is created with the base URL specified in the `config`.
20
- */
21
- constructor({
22
- baseURL,
23
- accessTokenKey = "access_token",
24
- refreshTokenKey = "refresh_token",
25
- refreshTokenEndpoint = "/api/refresh",
26
- enableAccessToken = true,
27
- enableRefreshToken = false
28
- }) {
29
- this.config = {
30
- baseURL,
31
- accessTokenKey,
32
- refreshTokenKey,
33
- refreshTokenEndpoint,
34
- enableAccessToken,
35
- enableRefreshToken
36
- };
37
- this.axiosInstance = axios.create({ baseURL: this.config.baseURL });
38
- if (this.config.enableRefreshToken && this.config.refreshTokenEndpoint) {
39
- this.axiosInstance.interceptors.response.use(
40
- (response) => response,
41
- async (error) => {
42
- const originalRequest = error.config;
43
- if (error.response.status === 401 && !originalRequest._retry) {
44
- originalRequest._retry = true;
45
- const newAccessToken = await this.refreshAccessToken();
46
- originalRequest.headers.Authorization = `Bearer ${newAccessToken}`;
47
- return this.axiosInstance(originalRequest);
48
- }
49
- return Promise.reject(error);
50
- }
51
- );
52
- }
53
- }
54
- /**
55
- * The function `refreshAccessToken` asynchronously refreshes the access token by making an API
56
- * request using a refresh token and updating the access token in the app's storage.
57
- * @returns The `refreshAccessToken` function is returning the access token after successfully
58
- * refreshing it using the refresh token.
59
- */
60
- async refreshAccessToken() {
61
- try {
62
- if (this.config.refreshTokenEndpoint == void 0) return;
63
- const refreshToken = this.getRefreshToken();
64
- if (refreshToken === void 0) throw new Error("Unauthorized Attempt!");
65
- const res = await this.axiosInstance.post(this.config.refreshTokenEndpoint, {
66
- refreshToken
67
- });
68
- if (res.status != 200)
69
- throw new Error("Refresh access token request failed!");
70
- this.resetTokens(res);
71
- return this.getAccessToken();
72
- } catch (error) {
73
- throw error;
74
- }
75
- }
76
- /**
77
- * The function `resetTokens` sets the access token and refresh token in cookies based on the
78
- * response data from an Axios request.
79
- * @param res - The `res` parameter in the `resetTokens` function is an AxiosResponse object. It
80
- * represents the response returned from an HTTP request made using Axios. The AxiosResponse object
81
- * contains data such as the response data, status, headers, and more.
82
- */
83
- resetTokens(res) {
84
- if (this.config.accessTokenKey) {
85
- Cookies.set(this.config.accessTokenKey, res.data.access_token);
86
- }
87
- if (this.config.refreshTokenKey) {
88
- Cookies.set(this.config.refreshTokenKey, res.data.refresh_token);
89
- }
90
- }
91
- /**
92
- * The function `getAccessToken` retrieves the access token from a cookie using the key specified in
93
- * the configuration.
94
- * @returns The `getAccessToken` method is returning a string value or `undefined`. It retrieves the
95
- * access token from a cookie using the `accessTokenKey` specified in the configuration.
96
- */
97
- getAccessToken() {
98
- return this.config.accessTokenKey && Cookies.get(this.config.accessTokenKey);
99
- }
100
- /**
101
- * Retrieves the refresh token from a cookie using the key specified in the configuration.
102
- * @returns A string value or `undefined`. It retrieves the refresh token from a cookie using the
103
- * `refreshTokenKey` specified in the configuration.
104
- */
105
- getRefreshToken() {
106
- return this.config.refreshTokenKey && Cookies.get(this.config.refreshTokenKey);
107
- }
108
- /**
109
- * The function `getAuthorizationHeader` returns an object with an Authorization header containing a
110
- * Bearer token if an access token is available.
111
- * @returns If the `accessToken` is not `undefined`, an object with the `Authorization` header
112
- * containing the access token in the format `Bearer ` is being returned. If the
113
- * `accessToken` is `undefined`, then `undefined` is being returned.
114
- */
115
- getAuthorizationHeader() {
116
- if (this.config.enableAccessToken != true) return;
117
- const accessToken = this.getAccessToken();
118
- if (accessToken === void 0) return;
119
- return { Authorization: `Bearer ${accessToken}` };
120
- }
121
- /**
122
- * The function `handleError` in TypeScript handles errors by extracting error messages and
123
- * optionally displaying them as a toast.
124
- * @param {unknown} e - The parameter `e` in the `handleError` function is used to represent the
125
- * error that needs to be handled. It can be of type `unknown`, which means it can be any type of
126
- * value. The function checks if `e` is an AxiosError and extracts the error message accordingly.
127
- * @param {FridayOptions} [options] - The `options` parameter in the `handleError` function is of type
128
- * `FridayOptions`. It is an optional parameter that allows you to pass additional options to the
129
- * function. These options can include properties like `showToast`, which is a boolean value
130
- * indicating whether a toast message should be displayed
131
- */
132
- handleError(e, options) {
133
- if (options?.enableThrowHttpError == false) return;
134
- let errorMessage;
135
- if (e?.response) {
136
- const error = e;
137
- errorMessage = (error.response?.data).message;
138
- } else {
139
- errorMessage = e.message;
140
- }
141
- if (errorMessage) {
142
- this.throwError(errorMessage);
143
- }
144
- }
145
- /**
146
- * The throwError function in TypeScript throws an error with a specified error message.
147
- * @param {string} errorMessage - The `errorMessage` parameter is a string that represents the
148
- * message you want to associate with the error that will be thrown.
149
- */
150
- throwError(errorMessage) {
151
- throw new Error(errorMessage);
152
- }
153
- /**
154
- * The `get` function makes an asynchronous GET request using Axios with error handling and returns
155
- * the response data.
156
- * @param {URL} url - The `url` parameter in the `get` function is of type `URL`, which represents a
157
- * Uniform Resource Locator and is used to specify the address of a resource on the internet. It is
158
- * the endpoint from which the data will be fetched in the `get` request.
159
- * @param {FridayOptions} [options] - The `options` parameter in the `get` function is of type
160
- * `FridayOptions`. It is an optional parameter that can be passed to the function to provide
161
- * additional configuration or settings for the HTTP request. This parameter allows for customization
162
- * of the request behavior based on the specific needs of the application
163
- * @returns The `get` function is returning the data from the response if the request is successful.
164
- * If there is an error during the request, it will handle the error using the `handleError` method
165
- * with the provided options.
166
- */
167
- async get(url, options) {
168
- try {
169
- return await this.axiosInstance.get(url.href, {
170
- headers: {
171
- ...this.getAuthorizationHeader(),
172
- "Content-Type": "application/json"
173
- }
174
- });
175
- } catch (error) {
176
- return this.handleError(error, options);
177
- }
178
- }
179
- /**
180
- * This TypeScript function sends a POST request using Axios with optional parameters and handles any
181
- * errors that occur.
182
- * @param {URL} url - The `url` parameter in the `post` function is a URL object that represents the
183
- * endpoint where the POST request will be sent. It specifies the location where the data will be
184
- * posted to.
185
- * @param {FridayOptions} [options] - The `options` parameter in the `post` function is an optional
186
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
187
- * HTTP POST request, such as the request body and headers. If `options` is provided, the function
188
- * will use the `body`
189
- * @returns The `post` function is returning the data from the response if the request is successful.
190
- * If there is an error during the request, it will handle the error using the `handleError` method
191
- * and return the result of that handling.
192
- */
193
- async post(url, options) {
194
- try {
195
- return await this.axiosInstance.post(url.href, options?.body, {
196
- headers: {
197
- ...options?.headers,
198
- ...this.getAuthorizationHeader(),
199
- "Content-Type": "application/json"
200
- }
201
- });
202
- } catch (error) {
203
- return this.handleError(error, options);
204
- }
205
- }
206
- /**
207
- * This TypeScript function sends a PUT request using Axios with specified options and handles any
208
- * errors that occur.
209
- * @param {URL} url - The `url` parameter in the `put` function is a URL object that represents the
210
- * URL where the PUT request will be sent. It is used to specify the destination of the request.
211
- * @param {FridayOptions} [options] - The `options` parameter in the `put` function is an optional
212
- * object that may contain the following properties:
213
- * @returns The `put` function is returning the data from the response if the request is successful.
214
- * If there is an error during the request, it will handle the error using the `handleError` method
215
- * and return the result of that handling.
216
- */
217
- async put(url, options) {
218
- try {
219
- return await this.axiosInstance.put(url.href, options?.body, {
220
- headers: {
221
- ...options?.headers,
222
- ...this.getAuthorizationHeader(),
223
- "Content-Type": "application/json"
224
- }
225
- });
226
- } catch (error) {
227
- return this.handleError(error, options);
228
- }
229
- }
230
- /**
231
- * The `delete` function sends a DELETE request to a specified URL with optional headers and body,
232
- * handling errors and returning the response data.
233
- * @param {URL} url - The `url` parameter is a `URL` object that represents the URL of the resource
234
- * you want to delete. It is used to specify the location of the resource that you want to delete.
235
- * @param {FridayOptions} [options] - The `options` parameter in the `delete` function is an optional
236
- * parameter of type `FridayOptions`. It is used to provide additional configuration options for the
237
- * HTTP request, such as request body and headers. The function uses the `body` property from the
238
- * `options` object as the data
239
- * @returns The `delete` method is returning the data from the response if the request is successful.
240
- * If there is an error during the request, it will handle the error and return the result of the
241
- * `handleError` method with the provided options.
242
- */
243
- async delete(url, options) {
244
- try {
245
- return await this.axiosInstance.delete(url.href, {
246
- data: options?.body,
247
- headers: {
248
- ...options?.headers,
249
- ...this.getAuthorizationHeader()
250
- }
251
- });
252
- } catch (error) {
253
- return this.handleError(error, options);
254
- }
255
- }
256
- /**
257
- * The `upload` function makes an asynchronous POST request using Axios with error handling and returns
258
- * the response data.
259
- * @param {URL} url - The `url` parameter in the `upload` function is of type `URL`, representing the
260
- * Uniform Resource Locator where the data will be uploaded. It specifies the endpoint to which the
261
- * data will be sent in the `POST` request.
262
- * @param {FormData} body - The `body` parameter in the `upload` function is of type `FormData`, which
263
- * contains the data to be uploaded in the request. It includes the content that will be sent to the
264
- * server in the upload operation.
265
- * @returns The `upload` function is returning the data from the response if the request is successful.
266
- * If an error occurs during the request, it will handle the error using the `handleError` method.
267
- */
268
- async upload(url, body) {
269
- try {
270
- return await this.axiosInstance.post(url.href, body, {
271
- headers: {
272
- ...this.getAuthorizationHeader()
273
- }
274
- });
275
- } catch (e) {
276
- return this.handleError(e);
277
- }
278
- }
279
- };
280
- export {
281
- Friday as default
282
- };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@sabuj0338/axios-friday",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Friday - a axios based api handling custom class. which has refresh token mechanism configured.",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
8
  "files": [
9
9
  "dist/**/*"
10
10
  ],
@@ -17,10 +17,9 @@
17
17
  },
18
18
  "keywords": [
19
19
  "typescript",
20
- "library",
21
20
  "axios",
22
- "refresh",
23
- "token"
21
+ "refresh token",
22
+ "authorization"
24
23
  ],
25
24
  "author": "Sabuj Islam <sabuj0338@gmail.com>",
26
25
  "license": "MIT",