@explorins/pers-sdk 1.0.0-alpha.1 → 1.1.2

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.
Files changed (77) hide show
  1. package/config/domains.js +22 -0
  2. package/explorins-pers-sdk-1.0.0-alpha.1.tgz +0 -0
  3. package/package.json +116 -23
  4. package/rollup.config.js +50 -54
  5. package/scripts/copy-declarations.js +147 -0
  6. package/src/analytics/api/analytics-api.ts +24 -0
  7. package/src/analytics/index.ts +52 -0
  8. package/src/analytics/models/index.ts +74 -0
  9. package/src/analytics/services/analytics-service.ts +28 -0
  10. package/src/auth-admin/api/auth-admin-api.ts +42 -0
  11. package/src/auth-admin/index.ts +47 -0
  12. package/src/auth-admin/services/auth-admin-service.ts +36 -0
  13. package/src/business/api/business-api.ts +181 -19
  14. package/src/business/index.ts +4 -3
  15. package/src/business/models/index.ts +4 -4
  16. package/src/business/services/business-service.ts +1 -1
  17. package/src/campaign/api/campaign-api.ts +376 -0
  18. package/src/campaign/index.ts +67 -0
  19. package/src/campaign/services/campaign-service.ts +164 -0
  20. package/src/core/abstractions/http-client.ts +1 -0
  21. package/src/core/auth/auth-provider.interface.ts +2 -2
  22. package/src/core/auth/create-auth-provider.ts +6 -6
  23. package/src/core/index.ts +33 -0
  24. package/src/core/pers-api-client.ts +211 -19
  25. package/src/core/pers-config.ts +34 -7
  26. package/src/core/utils/jwt.function.ts +24 -0
  27. package/src/donation/api/donation-api.ts +24 -0
  28. package/src/donation/index.ts +47 -0
  29. package/src/donation/models/index.ts +11 -0
  30. package/src/donation/services/donation-service.ts +25 -0
  31. package/src/index.ts +40 -1
  32. package/src/payment/api/payment-api.ts +185 -0
  33. package/src/payment/index.ts +64 -0
  34. package/src/payment/models/index.ts +29 -0
  35. package/src/payment/services/payment-service.ts +70 -0
  36. package/src/redemption/api/redemption-api.ts +241 -0
  37. package/src/redemption/index.ts +60 -0
  38. package/src/redemption/models/index.ts +17 -0
  39. package/src/redemption/services/redemption-service.ts +103 -0
  40. package/src/shared/interfaces/pers-shared-lib.interfaces.ts +99 -0
  41. package/src/tenant/api/tenant-api.ts +92 -0
  42. package/src/tenant/index.ts +61 -0
  43. package/src/tenant/models/index.ts +20 -0
  44. package/src/tenant/services/tenant-service.ts +78 -0
  45. package/src/token/api/token-api.ts +129 -0
  46. package/src/token/base/base-token-service.ts +167 -0
  47. package/src/token/index.ts +38 -0
  48. package/src/token/models/index.ts +30 -0
  49. package/src/token/services/token-service.ts +125 -0
  50. package/src/token/token-sdk.ts +231 -0
  51. package/src/transaction/api/transaction-api.ts +296 -0
  52. package/src/transaction/index.ts +65 -0
  53. package/src/transaction/models/index.ts +60 -0
  54. package/src/transaction/services/transaction-service.ts +104 -0
  55. package/src/user/api/user-api.ts +98 -0
  56. package/src/user/index.ts +62 -0
  57. package/src/user/models/index.ts +10 -0
  58. package/src/user/services/user-service.ts +75 -0
  59. package/src/user-status/api/user-status-api.ts +78 -0
  60. package/src/user-status/index.ts +55 -0
  61. package/src/user-status/models/index.ts +11 -0
  62. package/src/user-status/services/user-status-service.ts +51 -0
  63. package/src/web3/api/web3-api.ts +68 -0
  64. package/src/web3/index.ts +38 -0
  65. package/src/web3/models/index.ts +150 -0
  66. package/src/web3/services/web3-service.ts +338 -0
  67. package/src/web3-chain/api/web3-chain-api.ts +42 -0
  68. package/src/web3-chain/index.ts +27 -0
  69. package/src/web3-chain/models/index.ts +45 -0
  70. package/src/web3-chain/services/getWeb3FCD.service.ts +47 -0
  71. package/src/web3-chain/services/provider.service.ts +123 -0
  72. package/src/web3-chain/services/public-http-provider.service.ts +26 -0
  73. package/src/web3-chain/services/web3-chain-service.ts +131 -0
  74. package/src/business/business/tsconfig.json +0 -18
  75. package/src/core/abstractions/core-interfaces.ts +0 -56
  76. package/src/core/core.ts +0 -30
  77. package/src/core.ts +0 -30
@@ -0,0 +1,131 @@
1
+ import { Web3ChainApi } from '../api/web3-chain-api';
2
+ import { ChainData, CachedChainInstance } from '../models';
3
+ import type { Web3 } from 'web3';
4
+ import { isTokenExpired } from '../../core/utils/jwt.function';
5
+ import { Web3ProviderService } from './provider.service';
6
+
7
+ export class Web3ChainService {
8
+
9
+ private web3InstanceCache = new Map<number, CachedChainInstance>();
10
+
11
+ constructor(
12
+ private web3ChainApi: Web3ChainApi,
13
+ private providerService: Web3ProviderService
14
+ ) {}
15
+
16
+ async getWeb3ByChainId(chainId: number): Promise<Web3> {
17
+ const cached = this.web3InstanceCache.get(chainId);
18
+
19
+ // ✅ SMART CACHE: Check if cache is valid based on token expiration
20
+ if (cached && !this.checkIsTokenExpired(cached.chainData.authHeader)) {
21
+ console.log(`♻️ [Web3ChainService] Using cached Web3 instance for chain ${chainId}`);
22
+ return cached.web3Instance;
23
+ }
24
+
25
+ // ✅ CREATE WITH ERROR RECOVERY: Handle auth errors gracefully
26
+ try {
27
+ console.log(`🔧 [Web3ChainService] Creating new Web3 instance for chain ${chainId}`);
28
+ const { web3, chainData } = await this.createWeb3Instance(chainId);
29
+
30
+ // ✅ CACHE NEW INSTANCE
31
+ this.web3InstanceCache.set(chainId, {
32
+ web3Instance: web3,
33
+ chainData: chainData,
34
+ createdAt: Date.now(),
35
+ chainId
36
+ });
37
+
38
+ return web3;
39
+ } catch (error) {
40
+ // ✅ AUTH ERROR RECOVERY: Clear cache and retry once
41
+ if (this.isAuthError(error)) {
42
+ console.warn(`🔄 [Web3ChainService] Auth error detected, clearing cache and retrying...`);
43
+ this.web3InstanceCache.delete(chainId);
44
+
45
+ // Retry once with fresh instance
46
+ const { web3, chainData } = await this.createWeb3Instance(chainId);
47
+
48
+ this.web3InstanceCache.set(chainId, {
49
+ web3Instance: web3,
50
+ chainData: chainData,
51
+ createdAt: Date.now(),
52
+ chainId
53
+ });
54
+
55
+ return web3;
56
+ }
57
+
58
+ throw error;
59
+ }
60
+ }
61
+
62
+ async getChainDataWithCache(chainId: number): Promise<ChainData> {
63
+ const cached = this.web3InstanceCache.get(chainId);
64
+ //const now = Date.now();
65
+
66
+ if (cached && !this.checkIsTokenExpired(cached.chainData.authHeader)) {
67
+ console.log(`♻️ [Web3ChainService] Using cached ChainData for chain ${chainId}`);
68
+ return cached.chainData;
69
+ }
70
+
71
+ // If not cached, fetch fresh data
72
+ const chainData = await this.getChainDataById(chainId);
73
+ if (!chainData) {
74
+ throw new Error(`Chain data not found for chainId: ${chainId}`);
75
+ }
76
+
77
+ return chainData;
78
+ }
79
+
80
+ private checkIsTokenExpired(authHeader?: string): boolean {
81
+ // ✅ NULL CHECK: Handle undefined case
82
+ if (!authHeader) {
83
+ return false; // No token means no expiration (public chains)
84
+ }
85
+
86
+ // ✅ FUNCTION CALL: Use imported function, not method
87
+ return isTokenExpired(authHeader);
88
+ }
89
+
90
+ private async createWeb3Instance(chainId: number): Promise<{ web3: Web3; chainData: ChainData }> {
91
+ const chainData = await this.getChainDataById(chainId);
92
+
93
+ if (!chainData) {
94
+ throw new Error(`Chain data not found for chainId: ${chainId}`);
95
+ }
96
+
97
+ // ✅ CRITICAL CHECK: Ensure authHeader is present for private chains
98
+ if (chainData.chainType === 'PRIVATE' && !chainData.authHeader) {
99
+ console.error('❌ [Web3ChainService] CRITICAL: Private chain missing authHeader!');
100
+ throw new Error(`Private chain ${chainId} missing authentication header`);
101
+ }
102
+
103
+ const web3 = await this.providerService.getWeb3(
104
+ chainId,
105
+ chainData.chainType || 'PUBLIC',
106
+ chainData
107
+ );
108
+
109
+ return { web3, chainData };
110
+ }
111
+
112
+ // ✅ HELPER: Type-safe error checking
113
+ private isAuthError(error: unknown): boolean {
114
+ const message = error instanceof Error ? error.message.toLowerCase() : String(error).toLowerCase();
115
+ return (
116
+ message.includes('unauthorized') ||
117
+ message.includes('401') ||
118
+ message.includes('token expired') ||
119
+ message.includes('invalid token')
120
+ );
121
+ }
122
+
123
+ async getChainDataById(chainId: number): Promise<ChainData | null> {
124
+ try {
125
+ return await this.web3ChainApi.getChainData(chainId);
126
+ } catch (error) {
127
+ console.error('❌ [SDK Web3ChainService] Error getting chain data for chainId:', chainId, error);
128
+ return null;
129
+ }
130
+ }
131
+ }
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ESNext",
5
- "moduleResolution": "node",
6
- "esModuleInterop": true,
7
- "allowSyntheticDefaultImports": true,
8
- "strict": true,
9
- "declaration": true,
10
- "outDir": "dist",
11
- "rootDir": "src",
12
- "skipLibCheck": true,
13
- "forceConsistentCasingInFileNames": true,
14
- "lib": ["ES2022", "DOM"]
15
- },
16
- "include": ["src/**/*"],
17
- "exclude": ["node_modules", "dist", "**/*.spec.ts", "**/*.test.ts"]
18
- }
@@ -1,56 +0,0 @@
1
- /**
2
- * Core SDK interfaces based on actual PERS Tourism Loyalty App patterns
3
- * These interfaces reflect the real DDD architecture with Lazy Facade pattern
4
- */
5
-
6
- // Core HTTP abstractions matching the app's ApiService patterns
7
- export interface CoreHttpClient {
8
- get<T>(url: string, options?: any): Observable<T>;
9
- post<T>(url: string, body: any, options?: any): Observable<T>;
10
- put<T>(url: string, body: any, options?: any): Observable<T>;
11
- delete<T>(url: string, options?: any): Observable<T>;
12
- }
13
-
14
- // Base facade interface reflecting the app's LazyClass pattern
15
- export interface CoreFacade {
16
- [key: string]: any;
17
- }
18
-
19
- // Environment configuration interface based on LoyaltyEnvironment
20
- export interface CoreEnvironment {
21
- production: boolean;
22
- version: string;
23
- apiVersion: string
24
- apiRoot: string;
25
- apiProjectKey?: string;
26
- }
27
-
28
- // State management interface reflecting the app's Signal-based patterns
29
- export interface CoreStateManager<T> {
30
- state$: Observable<T>;
31
- updateState(partialState: Partial<T>): void;
32
- resetState(): void;
33
- }
34
-
35
- // Event system interface for cross-platform communication
36
- export interface CoreEventEmitter<T = any> {
37
- emit(eventName: string, data?: T): void;
38
- on(eventName: string, callback: (data?: T) => void): void;
39
- off(eventName: string, callback?: (data?: T) => void): void;
40
- }
41
-
42
- // Service factory interface for platform-specific implementations
43
- export interface CoreServiceFactory {
44
- createHttpClient(): CoreHttpClient;
45
- createStateManager<T>(initialState: T): CoreStateManager<T>;
46
- createEventEmitter<T>(): CoreEventEmitter<T>;
47
- }
48
-
49
- // Observable type for platform-agnostic reactive programming
50
- export interface Observable<T> {
51
- subscribe(observer: {
52
- next?: (value: T) => void;
53
- error?: (error: any) => void;
54
- complete?: () => void;
55
- }): { unsubscribe(): void };
56
- }
package/src/core/core.ts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * @explorins/pers-sdk/core - Core SDK Entry Point
3
- *
4
- * Platform-agnostic PERS API client and core functionality
5
- */
6
-
7
- // Core PERS API client
8
- export * from './pers-api-client';
9
-
10
- // Configuration interfaces
11
- export * from './pers-config';
12
-
13
- // Authentication interfaces
14
- export {
15
- AuthProvider,
16
- SimpleAuthConfig,
17
- createAuthProvider,
18
- createBrowserTokenProvider,
19
- createNodeTokenProvider
20
- } from './auth/auth-provider.interface';
21
- export type { RequestOptions as AuthRequestOptions } from './auth/auth-provider.interface';
22
-
23
- // Platform abstractions
24
- export * from './abstractions/http-client';
25
-
26
- // Main SDK class
27
- export * from '../pers-sdk';
28
-
29
- // Version
30
- export const PERS_SDK_VERSION = '1.0.0-alpha.1';
package/src/core.ts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * @explorins/pers-sdk/core - Core SDK Entry Point
3
- *
4
- * Platform-agnostic PERS API client and core functionality
5
- */
6
-
7
- // Core PERS API client
8
- export * from './core/pers-api-client';
9
-
10
- // Configuration interfaces
11
- export * from './core/pers-config';
12
-
13
- // Authentication interfaces
14
- export {
15
- AuthProvider,
16
- SimpleAuthConfig,
17
- createAuthProvider,
18
- createBrowserTokenProvider,
19
- createNodeTokenProvider
20
- } from './core/auth/auth-provider.interface';
21
- export type { RequestOptions as AuthRequestOptions } from './core/auth/auth-provider.interface';
22
-
23
- // Platform abstractions
24
- export * from './core/abstractions/http-client';
25
-
26
- // Main SDK class
27
- export * from './pers-sdk';
28
-
29
- // Version
30
- export const PERS_SDK_VERSION = '1.0.0-alpha.1';