@explorins/pers-sdk 1.2.5 → 1.3.1
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/README.md +416 -0
- package/dist/analytics.cjs +6 -0
- package/dist/analytics.cjs.map +1 -1
- package/dist/analytics.js +2 -0
- package/dist/analytics.js.map +1 -1
- package/dist/business.cjs +6 -0
- package/dist/business.cjs.map +1 -1
- package/dist/business.js +2 -0
- package/dist/business.js.map +1 -1
- package/dist/campaign/api/campaign-api.d.ts +19 -65
- package/dist/campaign/api/campaign-api.d.ts.map +1 -1
- package/dist/campaign.cjs +55 -103
- package/dist/campaign.cjs.map +1 -1
- package/dist/campaign.js +51 -103
- package/dist/campaign.js.map +1 -1
- package/dist/chunks/base-token-service-BA81_Ouq.js +532 -0
- package/dist/chunks/base-token-service-BA81_Ouq.js.map +1 -0
- package/dist/chunks/base-token-service-BQ6uFoki.cjs +537 -0
- package/dist/chunks/base-token-service-BQ6uFoki.cjs.map +1 -0
- package/dist/chunks/jwt.function-BYiyl-z_.cjs +25 -0
- package/dist/chunks/jwt.function-BYiyl-z_.cjs.map +1 -0
- package/dist/chunks/jwt.function-d6jPtBqI.js +23 -0
- package/dist/chunks/jwt.function-d6jPtBqI.js.map +1 -0
- package/dist/chunks/pers-sdk-JC-hSYUd.js +1377 -0
- package/dist/chunks/pers-sdk-JC-hSYUd.js.map +1 -0
- package/dist/chunks/pers-sdk-_1sTi9x9.cjs +1384 -0
- package/dist/chunks/pers-sdk-_1sTi9x9.cjs.map +1 -0
- package/dist/core/auth/api/auth-api.d.ts +30 -0
- package/dist/core/auth/api/auth-api.d.ts.map +1 -0
- package/dist/core/auth/auth-constants.d.ts +33 -0
- package/dist/core/auth/auth-constants.d.ts.map +1 -0
- package/dist/core/auth/auth-errors.d.ts +8 -0
- package/dist/core/auth/auth-errors.d.ts.map +1 -0
- package/dist/core/auth/auth-provider.interface.d.ts +72 -2
- package/dist/core/auth/auth-provider.interface.d.ts.map +1 -1
- package/dist/core/auth/create-auth-provider.d.ts +3 -3
- package/dist/core/auth/create-auth-provider.d.ts.map +1 -1
- package/dist/core/auth/default-auth-provider.d.ts +71 -0
- package/dist/core/auth/default-auth-provider.d.ts.map +1 -0
- package/dist/core/auth/index.d.ts +17 -0
- package/dist/core/auth/index.d.ts.map +1 -0
- package/dist/core/auth/services/auth-service.d.ts +49 -0
- package/dist/core/auth/services/auth-service.d.ts.map +1 -0
- package/dist/core/auth/token-refresh.d.ts +91 -0
- package/dist/core/auth/token-refresh.d.ts.map +1 -0
- package/dist/core/auth/token-storage.d.ts +74 -0
- package/dist/core/auth/token-storage.d.ts.map +1 -0
- package/dist/core/errors/index.d.ts +80 -0
- package/dist/core/errors/index.d.ts.map +1 -0
- package/dist/core/index.d.ts +1 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/pers-api-client.d.ts +172 -12
- package/dist/core/pers-api-client.d.ts.map +1 -1
- package/dist/core/pers-config.d.ts +36 -1
- package/dist/core/pers-config.d.ts.map +1 -1
- package/dist/core/utils/jwt.function.d.ts.map +1 -1
- package/dist/core.cjs +8 -500
- package/dist/core.cjs.map +1 -1
- package/dist/core.js +2 -496
- package/dist/core.js.map +1 -1
- package/dist/donation.cjs +6 -0
- package/dist/donation.cjs.map +1 -1
- package/dist/donation.js +2 -0
- package/dist/donation.js.map +1 -1
- package/dist/index.cjs +79 -4751
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -4699
- package/dist/index.js.map +1 -1
- package/dist/package.json +129 -129
- package/dist/payment.cjs +6 -0
- package/dist/payment.cjs.map +1 -1
- package/dist/payment.js +2 -0
- package/dist/payment.js.map +1 -1
- package/dist/pers-sdk.d.ts +49 -7
- package/dist/pers-sdk.d.ts.map +1 -1
- package/dist/redemption/api/redemption-api.d.ts +38 -77
- package/dist/redemption/api/redemption-api.d.ts.map +1 -1
- package/dist/redemption/services/redemption-service.d.ts +21 -3
- package/dist/redemption/services/redemption-service.d.ts.map +1 -1
- package/dist/redemption.cjs +84 -117
- package/dist/redemption.cjs.map +1 -1
- package/dist/redemption.js +80 -117
- package/dist/redemption.js.map +1 -1
- package/dist/shared/interfaces/pers-shared-lib.interfaces.d.ts +2 -1
- package/dist/shared/interfaces/pers-shared-lib.interfaces.d.ts.map +1 -1
- package/dist/tenant/api/tenant-api.d.ts +28 -10
- package/dist/tenant/api/tenant-api.d.ts.map +1 -1
- package/dist/tenant/index.d.ts +4 -4
- package/dist/tenant.cjs +46 -11
- package/dist/tenant.cjs.map +1 -1
- package/dist/tenant.js +42 -11
- package/dist/tenant.js.map +1 -1
- package/dist/token.cjs +10 -531
- package/dist/token.cjs.map +1 -1
- package/dist/token.js +2 -531
- package/dist/token.js.map +1 -1
- package/dist/transaction.cjs +4 -0
- package/dist/transaction.cjs.map +1 -1
- package/dist/transaction.js +1 -0
- package/dist/transaction.js.map +1 -1
- package/dist/user-status.cjs +6 -0
- package/dist/user-status.cjs.map +1 -1
- package/dist/user-status.js +2 -0
- package/dist/user-status.js.map +1 -1
- package/dist/user.cjs +6 -0
- package/dist/user.cjs.map +1 -1
- package/dist/user.js +2 -0
- package/dist/user.js.map +1 -1
- package/dist/web3-chain.cjs +12 -148
- package/dist/web3-chain.cjs.map +1 -1
- package/dist/web3-chain.js +7 -147
- package/dist/web3-chain.js.map +1 -1
- package/dist/web3.cjs +8 -537
- package/dist/web3.cjs.map +1 -1
- package/dist/web3.js +6 -535
- package/dist/web3.js.map +1 -1
- package/package.json +129 -129
- package/dist/auth-admin/api/auth-admin-api.d.ts +0 -29
- package/dist/auth-admin/api/auth-admin-api.d.ts.map +0 -1
- package/dist/auth-admin/index.d.ts +0 -27
- package/dist/auth-admin/index.d.ts.map +0 -1
- package/dist/auth-admin/services/auth-admin-service.d.ts +0 -27
- package/dist/auth-admin/services/auth-admin-service.d.ts.map +0 -1
- package/dist/auth-admin.cjs +0 -115
- package/dist/auth-admin.cjs.map +0 -1
- package/dist/auth-admin.js +0 -111
- package/dist/auth-admin.js.map +0 -1
- package/dist/core/auth/simple-auth-config.interface.d.ts +0 -15
- package/dist/core/auth/simple-auth-config.interface.d.ts.map +0 -1
package/dist/core.cjs
CHANGED
|
@@ -1,506 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Default configuration values
|
|
8
|
-
*/
|
|
9
|
-
const DEFAULT_PERS_CONFIG = {
|
|
10
|
-
environment: 'production',
|
|
11
|
-
apiVersion: 'v2',
|
|
12
|
-
timeout: 30000,
|
|
13
|
-
retries: 3
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Internal function to construct API root from environment
|
|
17
|
-
* Now defaults to production and v2
|
|
18
|
-
*/
|
|
19
|
-
function buildApiRoot(environment = 'production', version = 'v2') {
|
|
20
|
-
const baseUrls = {
|
|
21
|
-
development: 'https://explorins-loyalty.ngrok.io',
|
|
22
|
-
staging: `https://dev.api.pers.ninja/${version}`,
|
|
23
|
-
production: `https://api.pers.ninja/${version}`
|
|
24
|
-
};
|
|
25
|
-
return `${baseUrls[environment]}`;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Merge user config with defaults
|
|
29
|
-
*/
|
|
30
|
-
function mergeWithDefaults(config) {
|
|
31
|
-
return {
|
|
32
|
-
...DEFAULT_PERS_CONFIG,
|
|
33
|
-
...config,
|
|
34
|
-
environment: config.environment ?? DEFAULT_PERS_CONFIG.environment,
|
|
35
|
-
apiVersion: config.apiVersion ?? DEFAULT_PERS_CONFIG.apiVersion,
|
|
36
|
-
timeout: config.timeout ?? DEFAULT_PERS_CONFIG.timeout,
|
|
37
|
-
retries: config.retries ?? DEFAULT_PERS_CONFIG.retries
|
|
38
|
-
};
|
|
39
|
-
}
|
|
3
|
+
var persSdk = require('./chunks/pers-sdk-_1sTi9x9.cjs');
|
|
4
|
+
require('@explorins/pers-shared');
|
|
40
5
|
|
|
41
|
-
// packages/pers-sdk/src/core/pers-api-client.ts
|
|
42
|
-
/**
|
|
43
|
-
* PERS API Client - Core platform-agnostic client for PERS backend
|
|
44
|
-
*/
|
|
45
|
-
class PersApiClient {
|
|
46
|
-
constructor(httpClient, config) {
|
|
47
|
-
this.httpClient = httpClient;
|
|
48
|
-
this.config = config;
|
|
49
|
-
// Merge user config with defaults (production + v2)
|
|
50
|
-
this.mergedConfig = mergeWithDefaults(config);
|
|
51
|
-
// Build API root from merged environment and version
|
|
52
|
-
this.apiRoot = buildApiRoot(this.mergedConfig.environment, this.mergedConfig.apiVersion);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Get request headers including auth token and project key
|
|
56
|
-
*/
|
|
57
|
-
async getHeaders() {
|
|
58
|
-
const headers = {
|
|
59
|
-
'Content-Type': 'application/json',
|
|
60
|
-
};
|
|
61
|
-
// Add authentication token
|
|
62
|
-
if (this.mergedConfig.authProvider) {
|
|
63
|
-
const token = await this.mergedConfig.authProvider.getToken();
|
|
64
|
-
if (token) {
|
|
65
|
-
headers['Authorization'] = `Bearer ${token}`;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
// Add project key
|
|
69
|
-
if (this.mergedConfig.authProvider) {
|
|
70
|
-
const projectKey = await this.mergedConfig.authProvider.getProjectKey();
|
|
71
|
-
if (projectKey) {
|
|
72
|
-
headers['x-project-key'] = projectKey;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
else if (this.mergedConfig.apiProjectKey) {
|
|
76
|
-
// Fallback to config project key if no auth provider
|
|
77
|
-
headers['x-project-key'] = this.mergedConfig.apiProjectKey;
|
|
78
|
-
}
|
|
79
|
-
return headers;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Make a request with proper headers, auth, and error handling
|
|
83
|
-
*/
|
|
84
|
-
async request(method, endpoint, body, options) {
|
|
85
|
-
const { retryCount = 0, responseType = 'json' } = options || {};
|
|
86
|
-
const url = `${this.apiRoot}${endpoint}`;
|
|
87
|
-
// ✅ DEBUGGING: Add extensive logging for CSV endpoint
|
|
88
|
-
const isCSVEndpoint = endpoint.includes('/export/csv');
|
|
89
|
-
const requestOptions = {
|
|
90
|
-
headers: await this.getHeaders(),
|
|
91
|
-
timeout: this.mergedConfig.timeout,
|
|
92
|
-
responseType
|
|
93
|
-
};
|
|
94
|
-
try {
|
|
95
|
-
let result;
|
|
96
|
-
switch (method) {
|
|
97
|
-
case 'GET':
|
|
98
|
-
result = await this.httpClient.get(url, requestOptions);
|
|
99
|
-
break;
|
|
100
|
-
case 'POST':
|
|
101
|
-
result = await this.httpClient.post(url, body, requestOptions);
|
|
102
|
-
break;
|
|
103
|
-
case 'PUT':
|
|
104
|
-
result = await this.httpClient.put(url, body, requestOptions);
|
|
105
|
-
break;
|
|
106
|
-
case 'DELETE':
|
|
107
|
-
result = await this.httpClient.delete(url, requestOptions);
|
|
108
|
-
break;
|
|
109
|
-
default:
|
|
110
|
-
throw new Error(`Unsupported HTTP method: ${method}`);
|
|
111
|
-
}
|
|
112
|
-
return result;
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
if (isCSVEndpoint) {
|
|
116
|
-
console.error('❌ [PERS API CLIENT] CSV Request failed:', error);
|
|
117
|
-
}
|
|
118
|
-
// Handle 401 errors with automatic token refresh
|
|
119
|
-
const apiError = error;
|
|
120
|
-
if (apiError.status === 401 && retryCount === 0 && this.mergedConfig.authProvider?.onTokenExpired) {
|
|
121
|
-
try {
|
|
122
|
-
await this.mergedConfig.authProvider.onTokenExpired();
|
|
123
|
-
// Retry once with refreshed token
|
|
124
|
-
return this.request(method, endpoint, body, { ...options, retryCount: 1 });
|
|
125
|
-
}
|
|
126
|
-
catch (refreshError) {
|
|
127
|
-
throw new PersApiError(`Authentication refresh failed: ${refreshError}`, endpoint, method, 401);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
throw new PersApiError(`PERS API request failed: ${apiError.message || error}`, endpoint, method, apiError.status);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Generic GET request
|
|
135
|
-
*/
|
|
136
|
-
async get(endpoint, responseType) {
|
|
137
|
-
return this.request('GET', endpoint, undefined, { responseType });
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Generic POST request
|
|
141
|
-
*/
|
|
142
|
-
async post(endpoint, body) {
|
|
143
|
-
return this.request('POST', endpoint, body);
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Generic PUT request
|
|
147
|
-
*/
|
|
148
|
-
async put(endpoint, body) {
|
|
149
|
-
return this.request('PUT', endpoint, body);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Generic DELETE request
|
|
153
|
-
*/
|
|
154
|
-
async delete(endpoint) {
|
|
155
|
-
return this.request('DELETE', endpoint);
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Get current configuration (returns merged config)
|
|
159
|
-
*/
|
|
160
|
-
getConfig() {
|
|
161
|
-
return this.mergedConfig;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Get original user configuration
|
|
165
|
-
*/
|
|
166
|
-
getOriginalConfig() {
|
|
167
|
-
return this.config;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
class PersApiError extends Error {
|
|
171
|
-
constructor(message, endpoint, method, statusCode) {
|
|
172
|
-
super(message);
|
|
173
|
-
this.endpoint = endpoint;
|
|
174
|
-
this.method = method;
|
|
175
|
-
this.statusCode = statusCode;
|
|
176
|
-
this.name = 'PersApiError';
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* PERS API Client - Core platform-agnostic client for PERS backend
|
|
181
|
-
*/
|
|
182
|
-
/*import { HttpClient, RequestOptions } from './abstractions/http-client';
|
|
183
|
-
import { PersConfig, buildApiRoot, mergeWithDefaults } from './pers-config';
|
|
184
6
|
|
|
185
|
-
export class PersApiClient {
|
|
186
|
-
private readonly apiRoot: string;
|
|
187
|
-
private readonly mergedConfig: ReturnType<typeof mergeWithDefaults>;
|
|
188
7
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
// Build API root from merged environment and version
|
|
197
|
-
this.apiRoot = buildApiRoot(this.mergedConfig.environment, this.mergedConfig.apiVersion);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Get request headers including auth token and project key
|
|
202
|
-
*/
|
|
203
|
-
/*private async getHeaders(): Promise<Record<string, string>> {
|
|
204
|
-
const headers: Record<string, string> = {
|
|
205
|
-
'Content-Type': 'application/json',
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
// Add authentication token
|
|
209
|
-
if (this.mergedConfig.authProvider) {
|
|
210
|
-
const token = await this.mergedConfig.authProvider.getToken();
|
|
211
|
-
if (token) {
|
|
212
|
-
headers['Authorization'] = `Bearer ${token}`;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Add project key
|
|
217
|
-
if (this.mergedConfig.authProvider) {
|
|
218
|
-
const projectKey = await this.mergedConfig.authProvider.getProjectKey();
|
|
219
|
-
if (projectKey) {
|
|
220
|
-
headers['x-project-key'] = projectKey;
|
|
221
|
-
}
|
|
222
|
-
} else if(this.mergedConfig.apiProjectKey) {
|
|
223
|
-
// Fallback to config project key if no auth provider
|
|
224
|
-
headers['x-project-key'] = this.mergedConfig.apiProjectKey;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return headers;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Make a request with proper headers, auth, and error handling
|
|
232
|
-
*/
|
|
233
|
-
/*private async request<T>(
|
|
234
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
|
235
|
-
endpoint: string,
|
|
236
|
-
body?: any,
|
|
237
|
-
options?: { retryCount?: number }
|
|
238
|
-
): Promise<T> {
|
|
239
|
-
const { retryCount = 0 } = options || {};
|
|
240
|
-
const url = `${this.apiRoot}${endpoint}`;
|
|
241
|
-
|
|
242
|
-
const requestOptions: RequestOptions = {
|
|
243
|
-
headers: await this.getHeaders(),
|
|
244
|
-
timeout: this.mergedConfig.timeout,
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
try {
|
|
248
|
-
switch (method) {
|
|
249
|
-
case 'GET':
|
|
250
|
-
return await this.httpClient.get<T>(url, requestOptions);
|
|
251
|
-
case 'POST':
|
|
252
|
-
return await this.httpClient.post<T>(url, body, requestOptions);
|
|
253
|
-
case 'PUT':
|
|
254
|
-
return await this.httpClient.put<T>(url, body, requestOptions);
|
|
255
|
-
case 'DELETE':
|
|
256
|
-
return await this.httpClient.delete<T>(url, requestOptions);
|
|
257
|
-
default:
|
|
258
|
-
throw new Error(`Unsupported HTTP method: ${method}`);
|
|
259
|
-
}
|
|
260
|
-
} catch (error: any) {
|
|
261
|
-
// Handle 401 errors with automatic token refresh
|
|
262
|
-
if (error.status === 401 && retryCount === 0 && this.mergedConfig.authProvider?.onTokenExpired) {
|
|
263
|
-
try {
|
|
264
|
-
await this.mergedConfig.authProvider.onTokenExpired();
|
|
265
|
-
// Retry once with refreshed token
|
|
266
|
-
return this.request<T>(method, endpoint, body, { ...options, retryCount: 1 });
|
|
267
|
-
} catch (refreshError) {
|
|
268
|
-
throw new PersApiError(
|
|
269
|
-
`Authentication refresh failed: ${refreshError}`,
|
|
270
|
-
endpoint,
|
|
271
|
-
method,
|
|
272
|
-
401
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
throw new PersApiError(
|
|
278
|
-
`PERS API request failed: ${error.message || error}`,
|
|
279
|
-
endpoint,
|
|
280
|
-
method,
|
|
281
|
-
error.status
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Generic GET request
|
|
288
|
-
*/
|
|
289
|
-
/*async get<T>(endpoint: string): Promise<T> {
|
|
290
|
-
return this.request<T>('GET', endpoint);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Generic POST request
|
|
295
|
-
*/
|
|
296
|
-
/*async post<T>(endpoint: string, body?: any): Promise<T> {
|
|
297
|
-
return this.request<T>('POST', endpoint, body);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Generic PUT request
|
|
302
|
-
*/
|
|
303
|
-
/*async put<T>(endpoint: string, body?: any): Promise<T> {
|
|
304
|
-
return this.request<T>('PUT', endpoint, body);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Generic DELETE request
|
|
309
|
-
*/
|
|
310
|
-
/*async delete<T>(endpoint: string): Promise<T> {
|
|
311
|
-
return this.request<T>('DELETE', endpoint);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Get current configuration (returns merged config)
|
|
316
|
-
*/
|
|
317
|
-
/*getConfig(): ReturnType<typeof mergeWithDefaults> {
|
|
318
|
-
return this.mergedConfig;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Get original user configuration
|
|
323
|
-
*/
|
|
324
|
-
/*getOriginalConfig(): PersConfig {
|
|
325
|
-
return this.config;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
export class PersApiError extends Error {
|
|
330
|
-
constructor(
|
|
331
|
-
message: string,
|
|
332
|
-
public endpoint: string,
|
|
333
|
-
public method: string,
|
|
334
|
-
public statusCode?: number
|
|
335
|
-
) {
|
|
336
|
-
super(message);
|
|
337
|
-
this.name = 'PersApiError';
|
|
338
|
-
}
|
|
339
|
-
}*/
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Creates a platform-agnostic AuthProvider from simple configuration
|
|
343
|
-
*
|
|
344
|
-
* This factory function is completely platform-agnostic and can be used
|
|
345
|
-
* across Angular, React, Vue, Node.js, or any other JavaScript environment.
|
|
346
|
-
*
|
|
347
|
-
* Features:
|
|
348
|
-
* - Token caching with refresh support
|
|
349
|
-
* - Automatic token refresh on expiration
|
|
350
|
-
* - Configurable token providers
|
|
351
|
-
* - Platform-independent (no localStorage assumptions)
|
|
352
|
-
*
|
|
353
|
-
* @param config - Simple auth configuration
|
|
354
|
-
* @returns AuthProvider implementation
|
|
355
|
-
*/
|
|
356
|
-
function createAuthProvider(config) {
|
|
357
|
-
// Store current token for refresh scenarios and caching
|
|
358
|
-
let currentToken = config.token || null;
|
|
359
|
-
let isRefreshing = false; // Prevent concurrent refresh attempts
|
|
360
|
-
let refreshPromise = null;
|
|
361
|
-
return {
|
|
362
|
-
authType: config.authType || 'user',
|
|
363
|
-
async getToken() {
|
|
364
|
-
// If currently refreshing, wait for it to complete
|
|
365
|
-
if (isRefreshing && refreshPromise) {
|
|
366
|
-
await refreshPromise;
|
|
367
|
-
return currentToken;
|
|
368
|
-
}
|
|
369
|
-
// Use cached current token (updated after refresh)
|
|
370
|
-
if (currentToken) {
|
|
371
|
-
return currentToken;
|
|
372
|
-
}
|
|
373
|
-
// Custom token provider function (always fresh)
|
|
374
|
-
if (config.tokenProvider) {
|
|
375
|
-
const token = await config.tokenProvider();
|
|
376
|
-
currentToken = token; // Cache for future calls
|
|
377
|
-
return token;
|
|
378
|
-
}
|
|
379
|
-
// No token available
|
|
380
|
-
return null;
|
|
381
|
-
},
|
|
382
|
-
async getProjectKey() {
|
|
383
|
-
return config.projectKey || null;
|
|
384
|
-
},
|
|
385
|
-
async onTokenExpired() {
|
|
386
|
-
// Prevent concurrent refresh attempts
|
|
387
|
-
if (isRefreshing) {
|
|
388
|
-
if (refreshPromise) {
|
|
389
|
-
await refreshPromise;
|
|
390
|
-
}
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
// No refresh logic provided
|
|
394
|
-
if (!config.onTokenExpired) {
|
|
395
|
-
console.warn('Token expired but no refresh logic provided');
|
|
396
|
-
currentToken = null; // Clear expired token
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
// Start refresh process
|
|
400
|
-
isRefreshing = true;
|
|
401
|
-
refreshPromise = (async () => {
|
|
402
|
-
try {
|
|
403
|
-
// Execute refresh logic (should update token source)
|
|
404
|
-
await config.onTokenExpired();
|
|
405
|
-
// After refresh, get the new token
|
|
406
|
-
if (config.tokenProvider) {
|
|
407
|
-
const newToken = await config.tokenProvider();
|
|
408
|
-
if (newToken && newToken !== currentToken) {
|
|
409
|
-
currentToken = newToken;
|
|
410
|
-
// Notify about successful token refresh
|
|
411
|
-
if (config.onTokenRefreshed) {
|
|
412
|
-
config.onTokenRefreshed(newToken);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
else {
|
|
416
|
-
console.warn('Token refresh completed but no new token received');
|
|
417
|
-
currentToken = null;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
421
|
-
// For static token configs, clear the token since we can't refresh
|
|
422
|
-
console.warn('Token expired for static token config - clearing token');
|
|
423
|
-
currentToken = null;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
catch (error) {
|
|
427
|
-
console.error('Token refresh failed:', error);
|
|
428
|
-
currentToken = null; // Clear token on refresh failure
|
|
429
|
-
throw error; // Re-throw to let SDK handle the error
|
|
430
|
-
}
|
|
431
|
-
finally {
|
|
432
|
-
isRefreshing = false;
|
|
433
|
-
refreshPromise = null;
|
|
434
|
-
}
|
|
435
|
-
})();
|
|
436
|
-
await refreshPromise;
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Platform-specific localStorage token provider for browsers
|
|
442
|
-
* This is a convenience function for browser environments
|
|
443
|
-
*/
|
|
444
|
-
/* export function createBrowserTokenProvider(tokenKey: string = 'userJwt'): () => Promise<string | null> {
|
|
445
|
-
return async () => {
|
|
446
|
-
if (typeof localStorage !== 'undefined') {
|
|
447
|
-
return localStorage.getItem(tokenKey);
|
|
448
|
-
}
|
|
449
|
-
return null;
|
|
450
|
-
};
|
|
451
|
-
} */
|
|
452
|
-
/**
|
|
453
|
-
* Platform-specific environment variable token provider for Node.js
|
|
454
|
-
* This is a convenience function for Node.js environments
|
|
455
|
-
*/
|
|
456
|
-
/* export function createNodeTokenProvider(envVar: string = 'JWT_TOKEN'): () => Promise<string | null> {
|
|
457
|
-
return async () => {
|
|
458
|
-
if (typeof process !== 'undefined' && process.env) {
|
|
459
|
-
return process.env[envVar] || null;
|
|
460
|
-
}
|
|
461
|
-
return null;
|
|
462
|
-
};
|
|
463
|
-
} */
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* PERS SDK - Minimal platform-agnostic client with built-in authentication
|
|
467
|
-
* Authentication is now handled at the SDK core level for better scalability
|
|
468
|
-
*/
|
|
469
|
-
/**
|
|
470
|
-
* Minimal PERS SDK - API client with authentication built-in
|
|
471
|
-
* Platform adapters provide auth providers and HTTP clients
|
|
472
|
-
*/
|
|
473
|
-
class PersSDK {
|
|
474
|
-
constructor(httpClient, config) {
|
|
475
|
-
this.apiClient = new PersApiClient(httpClient, config);
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Get the API client for direct PERS API calls
|
|
479
|
-
* This is the main interface - keep it simple!
|
|
480
|
-
*/
|
|
481
|
-
api() {
|
|
482
|
-
return this.apiClient;
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* Quick config check
|
|
486
|
-
*/
|
|
487
|
-
isProduction() {
|
|
488
|
-
return this.apiClient.getConfig().environment === 'production';
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Simple factory function
|
|
493
|
-
*/
|
|
494
|
-
function createPersSDK(httpClient, config) {
|
|
495
|
-
return new PersSDK(httpClient, config);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
exports.DEFAULT_PERS_CONFIG = DEFAULT_PERS_CONFIG;
|
|
499
|
-
exports.PersApiClient = PersApiClient;
|
|
500
|
-
exports.PersApiError = PersApiError;
|
|
501
|
-
exports.PersSDK = PersSDK;
|
|
502
|
-
exports.buildApiRoot = buildApiRoot;
|
|
503
|
-
exports.createAuthProvider = createAuthProvider;
|
|
504
|
-
exports.createPersSDK = createPersSDK;
|
|
505
|
-
exports.mergeWithDefaults = mergeWithDefaults;
|
|
8
|
+
exports.DEFAULT_PERS_CONFIG = persSdk.DEFAULT_PERS_CONFIG;
|
|
9
|
+
exports.PersApiClient = persSdk.PersApiClient;
|
|
10
|
+
exports.PersSDK = persSdk.PersSDK;
|
|
11
|
+
exports.buildApiRoot = persSdk.buildApiRoot;
|
|
12
|
+
exports.createPersSDK = persSdk.createPersSDK;
|
|
13
|
+
exports.mergeWithDefaults = persSdk.mergeWithDefaults;
|
|
506
14
|
//# sourceMappingURL=core.cjs.map
|
package/dist/core.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.cjs","sources":[
|
|
1
|
+
{"version":3,"file":"core.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
|