@djust-b2b/djust-front-sdk 1.15.0 → 1.16.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.
@@ -1,8 +1,17 @@
1
1
  "use strict";
2
2
  /**
3
- * @packageDocumentation
3
+ * # Auth Service
4
4
  *
5
- * @document documents/auth.md
5
+ * This module provides all functionalities related to user authentication and authorization.
6
+ *
7
+ * ## Features:
8
+ * - Validate tokens
9
+ * - Refresh tokens
10
+ * - Reset user passwords
11
+ * - Manage user login and logout
12
+ * - Send password reset emails
13
+ *
14
+ * Each function is described with its input parameters, output responses, and example usages.
6
15
  */
7
16
  Object.defineProperty(exports, "__esModule", { value: true });
8
17
  exports.isTokenValid = isTokenValid;
@@ -14,28 +23,30 @@ exports.logout = logout;
14
23
  const parameters_validation_1 = require("../../helpers/parameters-validation");
15
24
  const fetch_instance_1 = require("../../settings/fetch-instance");
16
25
  /**
17
- * CART ENDPOINT
18
- */
19
- /**
20
- * ##R eturns true if the token is valid and not expired, false otherwise
21
- * ### APICODE(TOK-200)
22
- * @param {IsTokenValidParameters} params - The parameters for the function
23
- * #### token - `string` | <strong style={{ color: 'red' }}>required</strong>
24
- * The token to validate.
25
- *
26
- * @returns {Promise<boolean>} - A promise that resolves to a boolean indicating whether the token is valid or not
27
- *
28
- * @example
29
- * #### input
30
- * ```typescript
31
- * const isValidToken = await isTokenValid({
32
- * token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
26
+ * 🔐 Validates if a token is still active and unexpired.
27
+ *
28
+ * This function checks whether the provided token is valid, ensuring it has not expired or been invalidated.
29
+ *
30
+ * 🛠 **Endpoint**: `GET /auth/is-token-valid [TOK-200]`
31
+ *
32
+ * | Parameter | Type | Required | Description |
33
+ * |-----------|----------|----------|-------------------------------------|
34
+ * | `token` | `string` | ✅ | The token to validate. |
35
+ *
36
+ * 📤 **Returns**:
37
+ * A `Promise<boolean>` resolving to `true` if the token is valid and active, or `false` otherwise.
38
+ *
39
+ * 🛠 **Example usage**:
40
+ * ```ts
41
+ * const isValid = await isTokenValid({
42
+ * token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
33
43
  * });
44
+ * console.log(isValid); // true
34
45
  * ```
35
- * #### output
36
- * ```json
37
- * true
38
- * ```
46
+ *
47
+ * @param {IsTokenValidParameters} params - The parameters for validating the token.
48
+ * @throws {Error} If the required `token` parameter is missing.
49
+ * @returns {Promise<boolean>} A promise that resolves to a boolean indicating whether the token is valid.
39
50
  */
40
51
  async function isTokenValid({ token, }) {
41
52
  (0, parameters_validation_1.required)({ token });
@@ -49,32 +60,29 @@ async function isTokenValid({ token, }) {
49
60
  return response === null || response === void 0 ? void 0 : response.data;
50
61
  }
51
62
  /**
52
- * ## Ask for a new token from a refresh token
53
- * ### APICODE(AUTH-102)
54
- * @param {RefreshTokenParameters} params - The parameters for the function, including:
55
- * #### refreshToken - `string` | <strong style={{ color: 'red' }}>required</strong>
56
- * The refresh token to request a new access token (required)
57
- *
58
- * @returns {Promise<RefreshTokenResponse>} - A promise that resolves to the response containing the new token
59
- *
60
- * @example
61
- * #### input
62
- * ```typescript
63
- * const response = await refreshToken({ refreshToken: 'string' });
64
- * ```
65
- * #### output
66
- * ```json
67
- * {
68
- * "token": {
69
- * "accessToken": "string",
70
- * "expireAt": 0,
71
- * "refreshToken": "string"
72
- * },
73
- * "user": {
74
- * "id": "userID"
75
- * }
76
- * }
63
+ * 🔄 Requests a new access token using a refresh token.
64
+ *
65
+ * This function allows you to obtain a new access token by providing a valid refresh token.
66
+ *
67
+ * 🛠 **Endpoint**: `POST /auth/refresh-token [AUTH-102]`
68
+ *
69
+ * | Parameter | Type | Required | Description |
70
+ * |-----------------|----------|----------|--------------------------------------------------|
71
+ * | `refreshToken` | `string` | ✅ | The refresh token used to request a new access token. |
72
+ *
73
+ * 📤 **Returns**:
74
+ * A `Promise<RefreshTokenResponse>` containing the new access token, its expiry time, and a refreshed token.
75
+ *
76
+ * 🛠 **Example usage**:
77
+ * ```ts
78
+ * const response = await refreshToken({
79
+ * refreshToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
80
+ * });
77
81
  * ```
82
+ *
83
+ * @param {RefreshTokenParameters} params - The parameters for requesting a new token.
84
+ * @throws {Error} If the required `refreshToken` parameter is missing.
85
+ * @returns {Promise<RefreshTokenResponse>} A promise that resolves to the response containing the new token and user details.
78
86
  */
79
87
  async function refreshToken({ refreshToken, }) {
80
88
  (0, parameters_validation_1.required)({ refreshToken });
@@ -88,25 +96,31 @@ async function refreshToken({ refreshToken, }) {
88
96
  return data;
89
97
  }
90
98
  /**
91
- * ## Reset the password of a user
92
- * ### APICODE(PWD-102)
93
- * @param {ResetPasswordParameters} params - The parameters for the function
94
- * #### newPassword - `string` | <strong style={{ color: 'red' }}>required</strong>
95
- * The new password to set for the user.
96
- * #### resetPasswordToken - `string` | <strong style={{ color: 'red' }}>required</strong>
97
- * The token required to authenticate the password reset process.
98
- *
99
- * @returns {Promise<void>} - Resolves to a success message `"OK"` when the password is successfully reset
100
- *
101
- * @example
102
- * #### input
103
- * ```typescript
104
- * await resetPassword({ newPassword: 'string', resetPasswordToken: 'string' });
105
- * ```
106
- * #### output
107
- * ```json
108
- * "OK" (No output, resolves on success)
99
+ * 🔑 Resets the password of a user.
100
+ *
101
+ * This function allows a user to reset their password by providing a new password along with a valid reset token.
102
+ *
103
+ * 🛠 **Endpoint**: `POST /auth/reset-password [PWD-102]`
104
+ *
105
+ * | Parameter | Type | Required | Description |
106
+ * |-----------------------|----------|----------|--------------------------------------------------------------|
107
+ * | `newPassword` | `string` | ✅ | The new password to set for the user. |
108
+ * | `resetPasswordToken` | `string` | ✅ | The token required to authenticate the password reset process. |
109
+ *
110
+ * 📤 **Returns**:
111
+ * A `Promise<void>` that resolves with a success message `"OK"` if the password is successfully reset.
112
+ *
113
+ * 🛠 **Example usage**:
114
+ * ```ts
115
+ * await resetPassword({
116
+ * newPassword: "mySecurePassword2025!",
117
+ * resetPasswordToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
118
+ * });
109
119
  * ```
120
+ *
121
+ * @param {ResetPasswordParameters} params - The parameters required for resetting the password.
122
+ * @throws {Error} If the required parameters `newPassword` or `resetPasswordToken` are missing.
123
+ * @returns {Promise<void>} Resolves to `"OK"` if the password reset is successful.
110
124
  */
111
125
  async function resetPassword({ newPassword, resetPasswordToken, }) {
112
126
  (0, parameters_validation_1.required)({ newPassword, resetPasswordToken });
@@ -120,23 +134,29 @@ async function resetPassword({ newPassword, resetPasswordToken, }) {
120
134
  });
121
135
  }
122
136
  /**
123
- * ## Send an email to the user with a reset password link
124
- * ### APICODE(PWD-101)
125
- * @param {SendResetPasswordEmailParameters} params - The parameters for the function, including:
126
- * #### email - `string` | <strong style={{ color: 'red' }}>required</strong>
127
- * The email address to send the reset link to.
128
- *
129
- * @returns {Promise<void>} - A promise that resolves when the email is sent
130
- *
131
- * @example
132
- * #### input
133
- * ```typescript
134
- * await sendResetPasswordEmail({ email: 'user@example.com' });
135
- * ```
136
- * #### output
137
- * ```json
138
- * "OK" (No output, resolves on success)
137
+ * 📧 Sends a reset password email to a user.
138
+ *
139
+ * This function sends an email containing a reset password link to the specified email address.
140
+ *
141
+ * 🛠 **Endpoint**: `POST /auth/send-reset-password-email [PWD-101]`
142
+ *
143
+ * | Parameter | Type | Required | Description |
144
+ * |------------|----------|----------|----------------------------------------------------|
145
+ * | `email` | `string` | ✅ | The email address to which the reset link will be sent. |
146
+ *
147
+ * 📤 **Returns**:
148
+ * A `Promise<void>` that resolves when the reset password email is successfully sent.
149
+ *
150
+ * 🛠 **Example usage**:
151
+ * ```ts
152
+ * await sendResetPasswordEmail({
153
+ * email: "user@example.com",
154
+ * });
139
155
  * ```
156
+ *
157
+ * @param {SendResetPasswordEmailParameters} params - The parameters required to send the reset password email.
158
+ * @throws {Error} If the required parameter `email` is missing.
159
+ * @returns {Promise<void>} Resolves when the reset email is successfully sent.
140
160
  */
141
161
  async function sendResetPasswordEmail({ email, }) {
142
162
  (0, parameters_validation_1.required)({ email });
@@ -149,34 +169,33 @@ async function sendResetPasswordEmail({ email, }) {
149
169
  });
150
170
  }
151
171
  /**
152
- * ##Login a user
153
- * ### APICODE(AUTH-101)
154
- * @param {LoginParameters} params - The parameters for the function, including:
155
- * #### username - `string` | <strong style={{ color: 'red' }}>required</strong>
156
- * The username of the user to log in.
157
- * #### password - `string` | <strong style={{ color: 'red' }}>required</strong>
158
- * The password of the user to log in.
159
- *
160
- * @returns {Promise<LoginResponse>} - A promise that resolves to the response containing the user's login information
161
- *
162
- * @example
163
- * #### input
164
- * ```typescript
165
- * const loginResponse = await login({ username: 'user@example.com', password: 'password123' });
166
- * ```
167
- * #### output
168
- * ```json
169
- * {
170
- * "token": {
171
- * "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
172
- * "expireAt": 1679022000,
173
- * "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
174
- * },
175
- * "user": {
176
- * "id": "userID"
177
- * }
178
- * }
172
+ * 🔐 Logs in a user.
173
+ *
174
+ * This function allows a user to log in by providing their username and password, and returns their login information, including an access token and user details.
175
+ *
176
+ * 🛠 **Endpoint**: `POST /auth/token?withUser=true [AUTH-101]`
177
+ *
178
+ * | Parameter | Type | Required | Description |
179
+ * |-------------|----------|----------|--------------------------------------|
180
+ * | `username` | `string` | ✅ | The username of the user to log in. |
181
+ * | `password` | `string` | ✅ | The password of the user to log in. |
182
+ *
183
+ * 📤 **Returns**:
184
+ * A `Promise<LoginResponse>` that resolves to the user's login information, including:
185
+ * - `token` (object): Contains the `accessToken`, `refreshToken`, and `expireAt` timestamp.
186
+ * - `user` (object): Contains the user's `id`.
187
+ *
188
+ * 🛠 **Example usage**:
189
+ * ```ts
190
+ * const loginResponse = await login({
191
+ * username: "user@example.com",
192
+ * password: "password123",
193
+ * });
179
194
  * ```
195
+ *
196
+ * @param {LoginParameters} params - The parameters required for logging in the user.
197
+ * @throws {Error} If the required parameters `username` or `password` are missing.
198
+ * @returns {Promise<LoginResponse>} Resolves to an object containing the user's login information.
180
199
  */
181
200
  async function login({ username, password, }) {
182
201
  (0, parameters_validation_1.required)({ username, password });
@@ -191,20 +210,26 @@ async function login({ username, password, }) {
191
210
  return data;
192
211
  }
193
212
  /**
194
- * ### Logout the user
195
- * ### APICODE(AUTH-103)
213
+ * 🚪 Logs out the user.
196
214
  *
197
- * @returns {Promise<void>} - A promise that resolves when the user is logged out
215
+ * This function logs out the user by revoking their authentication token.
198
216
  *
199
- * @example
200
- * #### input
201
- * ```typescript
202
- * await logout();
203
- * ```
204
- * #### output
205
- * ```json
206
- * "OK" (No output, resolves on success)
217
+ * 🛠 **Endpoint**: `POST /auth/revoke-token [AUTH-103]`
218
+ *
219
+ * | Parameter | Type | Required | Description |
220
+ * |-----------|------|----------|-------------|
221
+ * | *(none)* | - | - | This function does not require any parameters. |
222
+ *
223
+ * 📤 **Returns**:
224
+ * A `Promise<void>` that resolves when the user is successfully logged out. No additional data is returned.
225
+ *
226
+ * 🛠 **Example usage**:
227
+ * ```ts
228
+ * await logout();
207
229
  * ```
230
+ *
231
+ * @throws {Error} If there is an issue with the logout process (e.g., network error).
232
+ * @returns {Promise<void>} Resolves when the logout is successful.
208
233
  */
209
234
  async function logout() {
210
235
  await (0, fetch_instance_1.enhancedFetch)({