@pagamio/frontend-commons-lib 0.8.190 → 0.8.191
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/lib/api/client.js
CHANGED
|
@@ -11,7 +11,7 @@ export class ApiClient {
|
|
|
11
11
|
defaultHeaders: {
|
|
12
12
|
'Content-Type': 'application/json',
|
|
13
13
|
},
|
|
14
|
-
credentials:
|
|
14
|
+
credentials: 'include',
|
|
15
15
|
...config,
|
|
16
16
|
};
|
|
17
17
|
}
|
|
@@ -215,7 +215,7 @@ export class ApiClient {
|
|
|
215
215
|
* Makes the actual HTTP request.
|
|
216
216
|
*/
|
|
217
217
|
async makeRequest(endpoint, method, config) {
|
|
218
|
-
const { params, timeout, skipAuth, skipRefresh, requestConfig, overrideDefaultHeaders, signal, overrideDefaultCredentials } = config;
|
|
218
|
+
const { params, timeout, skipAuth, skipRefresh, requestConfig, overrideDefaultHeaders, signal, overrideDefaultCredentials, } = config;
|
|
219
219
|
const url = this.createUrl(endpoint, params);
|
|
220
220
|
// Use provided signal, or create a new AbortController for this request
|
|
221
221
|
let requestSignal;
|
|
@@ -253,10 +253,7 @@ export class ApiClient {
|
|
|
253
253
|
...requestConfig.headers,
|
|
254
254
|
},
|
|
255
255
|
signal,
|
|
256
|
-
credentials: requestConfig.credentials ??
|
|
257
|
-
overrideDefaultCredentials ??
|
|
258
|
-
this.config.credentials ??
|
|
259
|
-
"same-origin",
|
|
256
|
+
credentials: requestConfig.credentials ?? overrideDefaultCredentials ?? this.config.credentials ?? 'same-origin',
|
|
260
257
|
};
|
|
261
258
|
}
|
|
262
259
|
async injectAuthHeader(finalConfig, skipAuth) {
|
package/lib/api/context.d.ts
CHANGED
|
@@ -102,7 +102,7 @@ export declare function useApi<T extends CustomAuthConfig>(): ApiClient<T>;
|
|
|
102
102
|
* });
|
|
103
103
|
* ```
|
|
104
104
|
*/
|
|
105
|
-
export declare function createApiClient<T extends CustomAuthConfig>({ baseURL, tokenManager, defaultHeaders, timeout, retries, credentials }: {
|
|
105
|
+
export declare function createApiClient<T extends CustomAuthConfig>({ baseURL, tokenManager, defaultHeaders, timeout, retries, credentials, }: {
|
|
106
106
|
baseURL: string;
|
|
107
107
|
tokenManager: TokenManager<T>;
|
|
108
108
|
defaultHeaders?: HeadersInit;
|
package/lib/api/context.js
CHANGED
|
@@ -94,13 +94,13 @@ export function useApi() {
|
|
|
94
94
|
* });
|
|
95
95
|
* ```
|
|
96
96
|
*/
|
|
97
|
-
export function createApiClient({ baseURL, tokenManager, defaultHeaders = {}, timeout = 30000, retries = 1, credentials }) {
|
|
97
|
+
export function createApiClient({ baseURL, tokenManager, defaultHeaders = {}, timeout = 30000, retries = 1, credentials, }) {
|
|
98
98
|
return new ApiClient({
|
|
99
99
|
baseURL,
|
|
100
100
|
tokenManager,
|
|
101
101
|
defaultHeaders,
|
|
102
102
|
timeout,
|
|
103
103
|
retries,
|
|
104
|
-
credentials
|
|
104
|
+
credentials,
|
|
105
105
|
});
|
|
106
106
|
}
|
|
@@ -231,14 +231,52 @@ export interface VasAppAuthConfig extends CustomAuthConfig {
|
|
|
231
231
|
password: string;
|
|
232
232
|
};
|
|
233
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Authentication configuration for the Commerce application.
|
|
236
|
+
* Defines the structure of user information, token data, and login credentials
|
|
237
|
+
* specific to the Commerce app authentication flow.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const authService = createAuthService<CommerceAppAuthConfig>({
|
|
242
|
+
* baseUrl: 'https://api.commerce.example.com',
|
|
243
|
+
* endpoints: {
|
|
244
|
+
* login: '/auth/login',
|
|
245
|
+
* register: '/auth/register',
|
|
246
|
+
* }
|
|
247
|
+
* });
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
234
250
|
export interface CommerceAppAuthConfig extends CustomAuthConfig {
|
|
251
|
+
/** User information structure for Commerce app */
|
|
235
252
|
UserInfo: {
|
|
236
|
-
|
|
253
|
+
/** Unique identifier for the user */
|
|
254
|
+
id: string;
|
|
255
|
+
/** Username used for authentication */
|
|
237
256
|
userName: string;
|
|
238
|
-
|
|
239
|
-
|
|
257
|
+
/** Current onboarding step */
|
|
258
|
+
onboardingStep?: string | null;
|
|
259
|
+
/** Secondary user identifier */
|
|
260
|
+
userId: string;
|
|
261
|
+
/** User type (e.g., customer, merchant, admin) */
|
|
240
262
|
userType: string;
|
|
263
|
+
/** User type identifier */
|
|
241
264
|
userTypeId: string;
|
|
242
265
|
};
|
|
266
|
+
/** Token information structure */
|
|
267
|
+
TokenInfo: {
|
|
268
|
+
/** JWT or other authentication token */
|
|
269
|
+
token: string;
|
|
270
|
+
/** Token expiration time in seconds */
|
|
271
|
+
expiresIn: number;
|
|
272
|
+
};
|
|
273
|
+
/** Login credentials structure */
|
|
274
|
+
Credentials: {
|
|
275
|
+
/** Username or email for authentication */
|
|
276
|
+
username?: string;
|
|
277
|
+
email?: string;
|
|
278
|
+
/** User's password */
|
|
279
|
+
password: string;
|
|
280
|
+
};
|
|
243
281
|
}
|
|
244
282
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ApiResponseTransformer, AuthResponse, EventsAppAuthConfig, VasAppAuthConfig } from '../types';
|
|
1
|
+
import type { ApiResponseTransformer, AuthResponse, CommerceAppAuthConfig, EventsAppAuthConfig, VasAppAuthConfig } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Transforms authentication responses from the Events App API format into a standardized AuthResponse.
|
|
4
4
|
* This transformer handles responses that include a single token and role-based user information.
|
|
@@ -115,6 +115,44 @@ export declare class VasAppResponseTransformer implements ApiResponseTransformer
|
|
|
115
115
|
*/
|
|
116
116
|
transform(response: any, remember?: boolean): AuthResponse<VasAppAuthConfig>;
|
|
117
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Transforms authentication responses from the Commerce App API format into a standardized AuthResponse.
|
|
120
|
+
* This transformer handles responses wrapped in a success/data structure.
|
|
121
|
+
*
|
|
122
|
+
* Expected API response format:
|
|
123
|
+
* ```typescript
|
|
124
|
+
* {
|
|
125
|
+
* success: boolean;
|
|
126
|
+
* data: {
|
|
127
|
+
* user: {...};
|
|
128
|
+
* accessToken: string;
|
|
129
|
+
* refreshToken: string;
|
|
130
|
+
* };
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @implements {ApiResponseTransformer<CommerceAppAuthConfig>}
|
|
135
|
+
*/
|
|
136
|
+
export declare class CommerceAppResponseTransformer implements ApiResponseTransformer<CommerceAppAuthConfig> {
|
|
137
|
+
/**
|
|
138
|
+
* Checks if the given response matches the Commerce App API format.
|
|
139
|
+
* Verifies the presence of required fields specific to Commerce App responses.
|
|
140
|
+
*
|
|
141
|
+
* @param response - The raw API response to check
|
|
142
|
+
* @returns True if the response contains 'success' and 'data' fields with user and tokens
|
|
143
|
+
*/
|
|
144
|
+
canHandle(response: any): boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Transforms a Commerce App API response into the standardized AuthResponse format.
|
|
147
|
+
* Maps API-specific fields to the common auth response structure.
|
|
148
|
+
*
|
|
149
|
+
* @param response - The raw API response to transform
|
|
150
|
+
* @param remember - Whether to use extended token expiration
|
|
151
|
+
* @returns Standardized auth response with user and token information
|
|
152
|
+
* @throws Error if required fields are missing from the response
|
|
153
|
+
*/
|
|
154
|
+
transform(response: any, remember?: boolean): AuthResponse<CommerceAppAuthConfig>;
|
|
155
|
+
}
|
|
118
156
|
/**
|
|
119
157
|
* Factory class for creating and managing response transformers.
|
|
120
158
|
* Implements the Factory pattern to dynamically select the appropriate transformer
|
|
@@ -211,6 +211,106 @@ export class VasAppResponseTransformer {
|
|
|
211
211
|
};
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Transforms authentication responses from the Commerce App API format into a standardized AuthResponse.
|
|
216
|
+
* This transformer handles responses wrapped in a success/data structure.
|
|
217
|
+
*
|
|
218
|
+
* Expected API response format:
|
|
219
|
+
* ```typescript
|
|
220
|
+
* {
|
|
221
|
+
* success: boolean;
|
|
222
|
+
* data: {
|
|
223
|
+
* user: {...};
|
|
224
|
+
* accessToken: string;
|
|
225
|
+
* refreshToken: string;
|
|
226
|
+
* };
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @implements {ApiResponseTransformer<CommerceAppAuthConfig>}
|
|
231
|
+
*/
|
|
232
|
+
export class CommerceAppResponseTransformer {
|
|
233
|
+
/**
|
|
234
|
+
* Checks if the given response matches the Commerce App API format.
|
|
235
|
+
* Verifies the presence of required fields specific to Commerce App responses.
|
|
236
|
+
*
|
|
237
|
+
* @param response - The raw API response to check
|
|
238
|
+
* @returns True if the response contains 'success' and 'data' fields with user and tokens
|
|
239
|
+
*/
|
|
240
|
+
canHandle(response) {
|
|
241
|
+
return (response.hasOwnProperty('success') &&
|
|
242
|
+
response.hasOwnProperty('data') &&
|
|
243
|
+
response.data?.hasOwnProperty('user') &&
|
|
244
|
+
response.data?.hasOwnProperty('accessToken'));
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Transforms a Commerce App API response into the standardized AuthResponse format.
|
|
248
|
+
* Maps API-specific fields to the common auth response structure.
|
|
249
|
+
*
|
|
250
|
+
* @param response - The raw API response to transform
|
|
251
|
+
* @param remember - Whether to use extended token expiration
|
|
252
|
+
* @returns Standardized auth response with user and token information
|
|
253
|
+
* @throws Error if required fields are missing from the response
|
|
254
|
+
*/
|
|
255
|
+
transform(response, remember) {
|
|
256
|
+
const { data } = response;
|
|
257
|
+
// Decode access token to get expiration
|
|
258
|
+
let decodedAccessToken;
|
|
259
|
+
try {
|
|
260
|
+
decodedAccessToken = jwtDecode(data.accessToken);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
console.error('Error decoding JWT access token:', error);
|
|
264
|
+
throw new Error('Failed to decode JWT access token. Please check the token format.');
|
|
265
|
+
}
|
|
266
|
+
if (!decodedAccessToken.exp) {
|
|
267
|
+
throw new Error('Access token does not contain an expiry time (exp claim)');
|
|
268
|
+
}
|
|
269
|
+
// Calculate access token expiration in seconds from now
|
|
270
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
271
|
+
const accessExpiresIn = decodedAccessToken.exp - currentTime;
|
|
272
|
+
if (accessExpiresIn <= 0) {
|
|
273
|
+
throw new Error('Access token has already expired');
|
|
274
|
+
}
|
|
275
|
+
// Decode refresh token to get expiration (if present)
|
|
276
|
+
let refreshExpiresIn;
|
|
277
|
+
if (data.refreshToken) {
|
|
278
|
+
try {
|
|
279
|
+
const decodedRefreshToken = jwtDecode(data.refreshToken);
|
|
280
|
+
if (decodedRefreshToken.exp) {
|
|
281
|
+
refreshExpiresIn = decodedRefreshToken.exp - currentTime;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
console.error('Error decoding JWT refresh token:', error);
|
|
286
|
+
// Use fallback if refresh token can't be decoded
|
|
287
|
+
refreshExpiresIn = remember ? 30 * 24 * 60 * 60 : 7 * 24 * 60 * 60; // 30 days or 7 days
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
user: {
|
|
292
|
+
id: data.user.id,
|
|
293
|
+
userName: data.user.userName,
|
|
294
|
+
onboardingStep: data.user.onboardingStep,
|
|
295
|
+
userId: data.user.id,
|
|
296
|
+
userType: data.user.userType || 'customer',
|
|
297
|
+
userTypeId: data.user.userTypeId || data.user.employeeId || data.user.id,
|
|
298
|
+
},
|
|
299
|
+
auth: {
|
|
300
|
+
accessToken: {
|
|
301
|
+
token: data.accessToken,
|
|
302
|
+
expiresIn: accessExpiresIn,
|
|
303
|
+
},
|
|
304
|
+
refreshToken: data.refreshToken && refreshExpiresIn
|
|
305
|
+
? {
|
|
306
|
+
token: data.refreshToken,
|
|
307
|
+
expiresIn: refreshExpiresIn,
|
|
308
|
+
}
|
|
309
|
+
: undefined,
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
214
314
|
/**
|
|
215
315
|
* Factory class for creating and managing response transformers.
|
|
216
316
|
* Implements the Factory pattern to dynamically select the appropriate transformer
|
|
@@ -232,6 +332,7 @@ export class ResponseTransformerFactory {
|
|
|
232
332
|
* @static
|
|
233
333
|
*/
|
|
234
334
|
static transformers = [
|
|
335
|
+
new CommerceAppResponseTransformer(),
|
|
235
336
|
new EventsAppResponseTransformer(),
|
|
236
337
|
new VasAppResponseTransformer(),
|
|
237
338
|
];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pagamio/frontend-commons-lib",
|
|
3
3
|
"description": "Pagamio library for Frontend reusable components like the form engine and table container",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.191",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"provenance": false
|