@product-intelligence-hub/sdk-core 0.1.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.
- package/LICENSE +21 -0
- package/README.md +143 -0
- package/dist/client.d.ts +91 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +278 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +47 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +74 -0
- package/dist/errors.js.map +1 -0
- package/dist/identity.d.ts +47 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +140 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/queue.d.ts +75 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +225 -0
- package/dist/queue.js.map +1 -0
- package/dist/session.d.ts +72 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +194 -0
- package/dist/session.js.map +1 -0
- package/dist/transport.d.ts +45 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +146 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +182 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +66 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +117 -0
- package/dist/utils.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Product Intelligence Hub
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# @product-intelligence-hub/sdk-core
|
|
2
|
+
|
|
3
|
+
Core SDK internals for Product Intelligence Hub. This package provides the base implementation used by platform-specific SDKs.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package is not intended for direct use. Instead, use one of the platform-specific SDKs:
|
|
8
|
+
|
|
9
|
+
- [@product-intelligence-hub/sdk-web](../sdk-web/README.md) - Browser SDK
|
|
10
|
+
- [@product-intelligence-hub/sdk-react-native](../sdk-react-native/README.md) - React Native SDK
|
|
11
|
+
|
|
12
|
+
## Architecture
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
sdk-core/
|
|
16
|
+
├── client.ts # PIHClient base class
|
|
17
|
+
├── identity.ts # IdentityManager - user/anonymous ID management
|
|
18
|
+
├── session.ts # SessionManager - session tracking
|
|
19
|
+
├── transport.ts # Transport - HTTP communication
|
|
20
|
+
├── queue.ts # EventQueue - batching and retry logic
|
|
21
|
+
├── errors.ts # PIHError - error handling
|
|
22
|
+
├── types.ts # TypeScript interfaces
|
|
23
|
+
└── utils.ts # Utility functions
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Core Components
|
|
27
|
+
|
|
28
|
+
### PIHClient
|
|
29
|
+
|
|
30
|
+
Base client class that platform SDKs extend:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { PIHClient, StorageAdapter } from "@product-intelligence-hub/sdk-core";
|
|
34
|
+
|
|
35
|
+
class MyPlatformClient extends PIHClient {
|
|
36
|
+
constructor(config: PIHConfig, storage: StorageAdapter) {
|
|
37
|
+
super(config, storage);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async initialize(): Promise<void> {
|
|
41
|
+
await super.initialize();
|
|
42
|
+
// Platform-specific initialization
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### StorageAdapter
|
|
48
|
+
|
|
49
|
+
Interface for platform-specific storage:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
interface StorageAdapter {
|
|
53
|
+
getItem(key: string): Promise<string | null>;
|
|
54
|
+
setItem(key: string, value: string): Promise<void>;
|
|
55
|
+
removeItem(key: string): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### IdentityManager
|
|
60
|
+
|
|
61
|
+
Manages user identity:
|
|
62
|
+
|
|
63
|
+
- Anonymous ID generation and persistence
|
|
64
|
+
- User ID tracking after identify()
|
|
65
|
+
- User traits storage
|
|
66
|
+
|
|
67
|
+
### SessionManager
|
|
68
|
+
|
|
69
|
+
Manages session lifecycle:
|
|
70
|
+
|
|
71
|
+
- Session ID generation
|
|
72
|
+
- Automatic session rotation on timeout
|
|
73
|
+
- Session start/end callbacks
|
|
74
|
+
|
|
75
|
+
### EventQueue
|
|
76
|
+
|
|
77
|
+
Handles event batching:
|
|
78
|
+
|
|
79
|
+
- Configurable flush interval and batch size
|
|
80
|
+
- Automatic retry with exponential backoff
|
|
81
|
+
- Persistent queue storage
|
|
82
|
+
|
|
83
|
+
### Transport
|
|
84
|
+
|
|
85
|
+
HTTP transport layer:
|
|
86
|
+
|
|
87
|
+
- Track and identify endpoints
|
|
88
|
+
- Retry logic
|
|
89
|
+
- Error handling
|
|
90
|
+
|
|
91
|
+
## Exports
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
// Types
|
|
95
|
+
export type {
|
|
96
|
+
PIHConfig,
|
|
97
|
+
AutocaptureConfig,
|
|
98
|
+
TrackEvent,
|
|
99
|
+
TrackOptions,
|
|
100
|
+
IdentifyPayload,
|
|
101
|
+
TrackResponse,
|
|
102
|
+
IdentifyResponse,
|
|
103
|
+
QueuedEvent,
|
|
104
|
+
StorageAdapter,
|
|
105
|
+
TransportOptions,
|
|
106
|
+
ClientState,
|
|
107
|
+
} from "./types.js";
|
|
108
|
+
|
|
109
|
+
// Errors
|
|
110
|
+
export { PIHError } from "./errors.js";
|
|
111
|
+
export type { PIHErrorCode } from "./errors.js";
|
|
112
|
+
|
|
113
|
+
// Core classes
|
|
114
|
+
export { PIHClient } from "./client.js";
|
|
115
|
+
export { IdentityManager } from "./identity.js";
|
|
116
|
+
export { SessionManager } from "./session.js";
|
|
117
|
+
export { Transport } from "./transport.js";
|
|
118
|
+
export { EventQueue } from "./queue.js";
|
|
119
|
+
|
|
120
|
+
// Utilities
|
|
121
|
+
export {
|
|
122
|
+
generateUUID,
|
|
123
|
+
getTimestamp,
|
|
124
|
+
dateToTimestamp,
|
|
125
|
+
getBackoffDelay,
|
|
126
|
+
STORAGE_PREFIX,
|
|
127
|
+
STORAGE_KEYS,
|
|
128
|
+
DEFAULTS,
|
|
129
|
+
} from "./utils.js";
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Extending
|
|
133
|
+
|
|
134
|
+
To create a new platform SDK:
|
|
135
|
+
|
|
136
|
+
1. Create a storage adapter implementing `StorageAdapter`
|
|
137
|
+
2. Extend `PIHClient` with platform-specific features
|
|
138
|
+
3. Override `initialize()` for platform setup
|
|
139
|
+
4. Add platform-specific tracking methods
|
|
140
|
+
|
|
141
|
+
## Related
|
|
142
|
+
|
|
143
|
+
- [SDK Spec](/docs/06_SDK_SPEC.md) - Full SDK specification
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { PIHConfig, TrackEvent, TrackOptions, StorageAdapter, TransportOptions } from "./types.js";
|
|
2
|
+
import { IdentityManager } from "./identity.js";
|
|
3
|
+
import { SessionManager } from "./session.js";
|
|
4
|
+
import { Transport } from "./transport.js";
|
|
5
|
+
import { EventQueue } from "./queue.js";
|
|
6
|
+
/**
|
|
7
|
+
* Base PIH client class
|
|
8
|
+
* Platform-specific SDKs extend this class
|
|
9
|
+
*/
|
|
10
|
+
export declare class PIHClient {
|
|
11
|
+
protected config: PIHConfig;
|
|
12
|
+
protected storage: StorageAdapter | null;
|
|
13
|
+
protected identity: IdentityManager;
|
|
14
|
+
protected session: SessionManager;
|
|
15
|
+
protected transport: Transport;
|
|
16
|
+
protected queue: EventQueue;
|
|
17
|
+
protected initialized: boolean;
|
|
18
|
+
protected tenantId: string;
|
|
19
|
+
constructor(config: PIHConfig, storage?: StorageAdapter | null);
|
|
20
|
+
/**
|
|
21
|
+
* Initialize the client (load persisted state)
|
|
22
|
+
*/
|
|
23
|
+
initialize(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Set the tenant ID
|
|
26
|
+
*/
|
|
27
|
+
setTenant(tenantId: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Identify the user
|
|
30
|
+
*/
|
|
31
|
+
identify(userId: string, traits?: Record<string, unknown>): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Set user traits without identifying
|
|
34
|
+
*/
|
|
35
|
+
setUserTraits(traits: Record<string, unknown>): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Track an event
|
|
38
|
+
*/
|
|
39
|
+
track(eventName: string, properties?: Record<string, unknown>, options?: TrackOptions): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Flush the event queue
|
|
42
|
+
*/
|
|
43
|
+
flush(options?: TransportOptions): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Reset identity (logout)
|
|
46
|
+
*/
|
|
47
|
+
reset(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Enable/disable debug mode
|
|
50
|
+
*/
|
|
51
|
+
setDebug(enabled: boolean): void;
|
|
52
|
+
/**
|
|
53
|
+
* Get the anonymous ID
|
|
54
|
+
*/
|
|
55
|
+
getAnonymousId(): string;
|
|
56
|
+
/**
|
|
57
|
+
* Get the user ID
|
|
58
|
+
*/
|
|
59
|
+
getUserId(): string | null;
|
|
60
|
+
/**
|
|
61
|
+
* Get the session ID
|
|
62
|
+
*/
|
|
63
|
+
getSessionId(): string | null;
|
|
64
|
+
/**
|
|
65
|
+
* Get the queue length
|
|
66
|
+
*/
|
|
67
|
+
getQueueLength(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Destroy the client (cleanup)
|
|
70
|
+
*/
|
|
71
|
+
destroy(): void;
|
|
72
|
+
/**
|
|
73
|
+
* Internal track method that doesn't require initialization
|
|
74
|
+
* Used for session events during initialization
|
|
75
|
+
*/
|
|
76
|
+
protected trackInternal(eventName: string, properties?: Record<string, unknown>): void;
|
|
77
|
+
/**
|
|
78
|
+
* Create a track event
|
|
79
|
+
*/
|
|
80
|
+
protected createEvent(eventName: string, properties?: Record<string, unknown>, options?: TrackOptions): TrackEvent;
|
|
81
|
+
/**
|
|
82
|
+
* Ensure client is initialized
|
|
83
|
+
*/
|
|
84
|
+
protected ensureInitialized(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Validate configuration
|
|
87
|
+
*/
|
|
88
|
+
protected validateConfig(config: PIHConfig): void;
|
|
89
|
+
protected log(...args: unknown[]): void;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=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G,KAAK,CACT,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpC,OAAO,CAAC,EAAE,YAAY,GACrB,OAAO,CAAC,IAAI,CAAC;IAuBhB;;OAEG;IACG,KAAK,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAKtD;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAY5B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAQhC;;OAEG;IACH,cAAc,IAAI,MAAM;IAIxB;;OAEG;IACH,SAAS,IAAI,MAAM,GAAG,IAAI;IAI1B;;OAEG;IACH,YAAY,IAAI,MAAM,GAAG,IAAI;IAI7B;;OAEG;IACH,cAAc,IAAI,MAAM;IAIxB;;OAEG;IACH,OAAO,IAAI,IAAI;IAKf;;;OAGG;IACH,SAAS,CAAC,aAAa,CACrB,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACnC,IAAI;IAQP;;OAEG;IACH,SAAS,CAAC,WAAW,CACnB,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpC,OAAO,CAAC,EAAE,YAAY,GACrB,UAAU;IAsBb;;OAEG;cACa,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAMlD;;OAEG;IACH,SAAS,CAAC,cAAc,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI;IAkBjD,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI;CAKxC"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { PIHError } from "./errors.js";
|
|
2
|
+
import { IdentityManager } from "./identity.js";
|
|
3
|
+
import { SessionManager } from "./session.js";
|
|
4
|
+
import { Transport } from "./transport.js";
|
|
5
|
+
import { EventQueue } from "./queue.js";
|
|
6
|
+
import { generateUUID, getTimestamp, dateToTimestamp, DEFAULTS } from "./utils.js";
|
|
7
|
+
/**
|
|
8
|
+
* Base PIH client class
|
|
9
|
+
* Platform-specific SDKs extend this class
|
|
10
|
+
*/
|
|
11
|
+
export class PIHClient {
|
|
12
|
+
config;
|
|
13
|
+
storage = null;
|
|
14
|
+
identity;
|
|
15
|
+
session;
|
|
16
|
+
transport;
|
|
17
|
+
queue;
|
|
18
|
+
initialized = false;
|
|
19
|
+
tenantId;
|
|
20
|
+
constructor(config, storage = null) {
|
|
21
|
+
this.validateConfig(config);
|
|
22
|
+
this.config = {
|
|
23
|
+
debug: false,
|
|
24
|
+
flushInterval: DEFAULTS.FLUSH_INTERVAL,
|
|
25
|
+
flushAt: DEFAULTS.FLUSH_AT,
|
|
26
|
+
maxQueueSize: DEFAULTS.MAX_QUEUE_SIZE,
|
|
27
|
+
sessionTimeout: DEFAULTS.SESSION_TIMEOUT,
|
|
28
|
+
...config,
|
|
29
|
+
};
|
|
30
|
+
this.storage = storage;
|
|
31
|
+
this.tenantId = config.tenantId ?? "";
|
|
32
|
+
// Initialize identity manager
|
|
33
|
+
this.identity = new IdentityManager(storage, this.config.debug);
|
|
34
|
+
// Initialize session manager
|
|
35
|
+
this.session = new SessionManager(storage, this.config.sessionTimeout, {
|
|
36
|
+
onSessionStart: (sessionId) => {
|
|
37
|
+
this.log("Session started:", sessionId);
|
|
38
|
+
// Track session_started event
|
|
39
|
+
this.trackInternal("session_started", { session_id: sessionId });
|
|
40
|
+
},
|
|
41
|
+
onSessionEnd: (sessionId, duration) => {
|
|
42
|
+
this.log("Session ended:", sessionId, "duration:", duration);
|
|
43
|
+
// Track session_ended event
|
|
44
|
+
this.trackInternal("session_ended", {
|
|
45
|
+
session_id: sessionId,
|
|
46
|
+
duration_ms: duration,
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
}, this.config.debug);
|
|
50
|
+
// Initialize transport
|
|
51
|
+
this.transport = new Transport({
|
|
52
|
+
apiUrl: this.config.apiUrl,
|
|
53
|
+
apiKey: this.config.apiKey,
|
|
54
|
+
tenantId: this.tenantId,
|
|
55
|
+
debug: this.config.debug,
|
|
56
|
+
});
|
|
57
|
+
// Initialize queue
|
|
58
|
+
this.queue = new EventQueue({
|
|
59
|
+
flushAt: this.config.flushAt,
|
|
60
|
+
flushInterval: this.config.flushInterval,
|
|
61
|
+
maxQueueSize: this.config.maxQueueSize,
|
|
62
|
+
storage,
|
|
63
|
+
transport: this.transport,
|
|
64
|
+
onError: (error, events) => {
|
|
65
|
+
this.config.onError?.(error);
|
|
66
|
+
this.log("Queue error:", error.message, "events:", events.length);
|
|
67
|
+
},
|
|
68
|
+
debug: this.config.debug,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Initialize the client (load persisted state)
|
|
73
|
+
*/
|
|
74
|
+
async initialize() {
|
|
75
|
+
if (this.initialized)
|
|
76
|
+
return;
|
|
77
|
+
await Promise.all([
|
|
78
|
+
this.identity.initialize(),
|
|
79
|
+
this.session.initialize(),
|
|
80
|
+
this.queue.initialize(),
|
|
81
|
+
]);
|
|
82
|
+
this.initialized = true;
|
|
83
|
+
this.log("Client initialized");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Set the tenant ID
|
|
87
|
+
*/
|
|
88
|
+
setTenant(tenantId) {
|
|
89
|
+
this.tenantId = tenantId;
|
|
90
|
+
this.transport.setTenantId(tenantId);
|
|
91
|
+
this.log("Tenant set:", tenantId);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Identify the user
|
|
95
|
+
*/
|
|
96
|
+
async identify(userId, traits) {
|
|
97
|
+
await this.ensureInitialized();
|
|
98
|
+
await this.identity.setUserId(userId);
|
|
99
|
+
if (traits) {
|
|
100
|
+
await this.identity.setUserTraits(traits);
|
|
101
|
+
}
|
|
102
|
+
// Send identify call to server
|
|
103
|
+
try {
|
|
104
|
+
await this.transport.sendIdentify({
|
|
105
|
+
project_id: this.config.projectId,
|
|
106
|
+
environment: this.config.environment,
|
|
107
|
+
tenant_id: this.tenantId,
|
|
108
|
+
anonymous_id: this.identity.getAnonymousId(),
|
|
109
|
+
user_id: userId,
|
|
110
|
+
traits,
|
|
111
|
+
timestamp: getTimestamp(),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
const pihError = PIHError.fromUnknown(error);
|
|
116
|
+
this.config.onError?.(pihError);
|
|
117
|
+
this.log("Identify error:", pihError.message);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Set user traits without identifying
|
|
122
|
+
*/
|
|
123
|
+
async setUserTraits(traits) {
|
|
124
|
+
await this.ensureInitialized();
|
|
125
|
+
await this.identity.setUserTraits(traits);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Track an event
|
|
129
|
+
*/
|
|
130
|
+
async track(eventName, properties, options) {
|
|
131
|
+
await this.ensureInitialized();
|
|
132
|
+
const event = this.createEvent(eventName, properties, options);
|
|
133
|
+
// Touch session to update last activity
|
|
134
|
+
this.session.touch();
|
|
135
|
+
if (options?.immediate) {
|
|
136
|
+
// Send immediately, bypassing queue
|
|
137
|
+
try {
|
|
138
|
+
await this.transport.sendEvents([event]);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
const pihError = PIHError.fromUnknown(error);
|
|
142
|
+
this.config.onError?.(pihError);
|
|
143
|
+
this.log("Immediate track error:", pihError.message);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
// Add to queue
|
|
148
|
+
await this.queue.enqueue(event);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Flush the event queue
|
|
153
|
+
*/
|
|
154
|
+
async flush(options) {
|
|
155
|
+
await this.ensureInitialized();
|
|
156
|
+
await this.queue.flush(options);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Reset identity (logout)
|
|
160
|
+
*/
|
|
161
|
+
async reset() {
|
|
162
|
+
await this.ensureInitialized();
|
|
163
|
+
// End current session
|
|
164
|
+
this.session.forceEndSession();
|
|
165
|
+
// Reset identity
|
|
166
|
+
await this.identity.reset();
|
|
167
|
+
this.log("Client reset");
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Enable/disable debug mode
|
|
171
|
+
*/
|
|
172
|
+
setDebug(enabled) {
|
|
173
|
+
this.config.debug = enabled;
|
|
174
|
+
this.identity.setDebug(enabled);
|
|
175
|
+
this.session.setDebug(enabled);
|
|
176
|
+
this.transport.setDebug(enabled);
|
|
177
|
+
this.queue.setDebug(enabled);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get the anonymous ID
|
|
181
|
+
*/
|
|
182
|
+
getAnonymousId() {
|
|
183
|
+
return this.identity.getAnonymousId();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Get the user ID
|
|
187
|
+
*/
|
|
188
|
+
getUserId() {
|
|
189
|
+
return this.identity.getUserId();
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Get the session ID
|
|
193
|
+
*/
|
|
194
|
+
getSessionId() {
|
|
195
|
+
return this.session.getCurrentSessionId();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get the queue length
|
|
199
|
+
*/
|
|
200
|
+
getQueueLength() {
|
|
201
|
+
return this.queue.getQueueLength();
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Destroy the client (cleanup)
|
|
205
|
+
*/
|
|
206
|
+
destroy() {
|
|
207
|
+
this.queue.destroy();
|
|
208
|
+
this.log("Client destroyed");
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Internal track method that doesn't require initialization
|
|
212
|
+
* Used for session events during initialization
|
|
213
|
+
*/
|
|
214
|
+
trackInternal(eventName, properties) {
|
|
215
|
+
const event = this.createEvent(eventName, properties);
|
|
216
|
+
// Fire and forget - don't await
|
|
217
|
+
this.queue.enqueue(event).catch((error) => {
|
|
218
|
+
this.log("Internal track error:", error);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Create a track event
|
|
223
|
+
*/
|
|
224
|
+
createEvent(eventName, properties, options) {
|
|
225
|
+
const timestamp = options?.timestamp
|
|
226
|
+
? dateToTimestamp(options.timestamp)
|
|
227
|
+
: getTimestamp();
|
|
228
|
+
return {
|
|
229
|
+
event_id: generateUUID(),
|
|
230
|
+
timestamp,
|
|
231
|
+
project_id: this.config.projectId,
|
|
232
|
+
environment: this.config.environment,
|
|
233
|
+
tenant_id: this.tenantId,
|
|
234
|
+
event_name: eventName,
|
|
235
|
+
anonymous_id: this.identity.getAnonymousId(),
|
|
236
|
+
user_id: this.identity.getUserId(),
|
|
237
|
+
session_id: this.session.getSessionId(),
|
|
238
|
+
platform: this.config.platform,
|
|
239
|
+
app_version: this.config.appVersion ?? null,
|
|
240
|
+
properties: properties ?? {},
|
|
241
|
+
user_traits: this.identity.getUserTraits(),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Ensure client is initialized
|
|
246
|
+
*/
|
|
247
|
+
async ensureInitialized() {
|
|
248
|
+
if (!this.initialized) {
|
|
249
|
+
await this.initialize();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Validate configuration
|
|
254
|
+
*/
|
|
255
|
+
validateConfig(config) {
|
|
256
|
+
if (!config.apiUrl) {
|
|
257
|
+
throw PIHError.invalidConfig("apiUrl is required");
|
|
258
|
+
}
|
|
259
|
+
if (!config.apiKey) {
|
|
260
|
+
throw PIHError.invalidConfig("apiKey is required");
|
|
261
|
+
}
|
|
262
|
+
if (!config.projectId) {
|
|
263
|
+
throw PIHError.invalidConfig("projectId is required");
|
|
264
|
+
}
|
|
265
|
+
if (!config.environment) {
|
|
266
|
+
throw PIHError.invalidConfig("environment is required");
|
|
267
|
+
}
|
|
268
|
+
if (!config.platform) {
|
|
269
|
+
throw PIHError.invalidConfig("platform is required");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
log(...args) {
|
|
273
|
+
if (this.config.debug) {
|
|
274
|
+
console.log("[PIH Client]", ...args);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEnF;;;GAGG;AACH,MAAM,OAAO,SAAS;IACV,MAAM,CAAY;IAClB,OAAO,GAA0B,IAAI,CAAC;IACtC,QAAQ,CAAkB;IAC1B,OAAO,CAAiB;IACxB,SAAS,CAAY;IACrB,KAAK,CAAa;IAClB,WAAW,GAAG,KAAK,CAAC;IACpB,QAAQ,CAAS;IAE3B,YAAY,MAAiB,EAAE,UAAiC,IAAI;QAClE,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAE5B,IAAI,CAAC,MAAM,GAAG;YACZ,KAAK,EAAE,KAAK;YACZ,aAAa,EAAE,QAAQ,CAAC,cAAc;YACtC,OAAO,EAAE,QAAQ,CAAC,QAAQ;YAC1B,YAAY,EAAE,QAAQ,CAAC,cAAc;YACrC,cAAc,EAAE,QAAQ,CAAC,eAAe;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,CAAC,QAAQ,EAAE,CAAC;YACrB,MAAM,QAAQ,CAAC,aAAa,CAAC,sBAAsB,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAES,GAAG,CAAC,GAAG,IAAe;QAC9B,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YACtB,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,IAAI,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;CACF"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error codes for PIH SDK errors
|
|
3
|
+
*/
|
|
4
|
+
export type PIHErrorCode = "NETWORK_ERROR" | "INVALID_CONFIG" | "QUEUE_FULL" | "RATE_LIMITED" | "INVALID_PAYLOAD" | "STORAGE_ERROR" | "SESSION_ERROR" | "UNKNOWN_ERROR";
|
|
5
|
+
/**
|
|
6
|
+
* Custom error class for PIH SDK
|
|
7
|
+
*/
|
|
8
|
+
export declare class PIHError extends Error {
|
|
9
|
+
/** Error code for programmatic handling */
|
|
10
|
+
readonly code: PIHErrorCode;
|
|
11
|
+
/** Additional error details */
|
|
12
|
+
readonly details?: unknown;
|
|
13
|
+
constructor(message: string, code: PIHErrorCode, details?: unknown);
|
|
14
|
+
/**
|
|
15
|
+
* Create a network error
|
|
16
|
+
*/
|
|
17
|
+
static networkError(message: string, details?: unknown): PIHError;
|
|
18
|
+
/**
|
|
19
|
+
* Create an invalid config error
|
|
20
|
+
*/
|
|
21
|
+
static invalidConfig(message: string, details?: unknown): PIHError;
|
|
22
|
+
/**
|
|
23
|
+
* Create a queue full error
|
|
24
|
+
*/
|
|
25
|
+
static queueFull(message: string, details?: unknown): PIHError;
|
|
26
|
+
/**
|
|
27
|
+
* Create a rate limited error
|
|
28
|
+
*/
|
|
29
|
+
static rateLimited(message: string, details?: unknown): PIHError;
|
|
30
|
+
/**
|
|
31
|
+
* Create an invalid payload error
|
|
32
|
+
*/
|
|
33
|
+
static invalidPayload(message: string, details?: unknown): PIHError;
|
|
34
|
+
/**
|
|
35
|
+
* Create a storage error
|
|
36
|
+
*/
|
|
37
|
+
static storageError(message: string, details?: unknown): PIHError;
|
|
38
|
+
/**
|
|
39
|
+
* Create a session error
|
|
40
|
+
*/
|
|
41
|
+
static sessionError(message: string, details?: unknown): PIHError;
|
|
42
|
+
/**
|
|
43
|
+
* Convert unknown error to PIHError
|
|
44
|
+
*/
|
|
45
|
+
static fromUnknown(error: unknown): PIHError;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,YAAY,GACpB,eAAe,GACf,gBAAgB,GAChB,YAAY,GACZ,cAAc,GACd,iBAAiB,GACjB,eAAe,GACf,eAAe,GACf,eAAe,CAAC;AAEpB;;GAEG;AACH,qBAAa,QAAS,SAAQ,KAAK;IACjC,2CAA2C;IAC3C,SAAgB,IAAI,EAAE,YAAY,CAAC;IACnC,+BAA+B;IAC/B,SAAgB,OAAO,CAAC,EAAE,OAAO,CAAC;gBAEtB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,OAAO;IAYlE;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAIjE;;OAEG;IACH,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAIlE;;OAEG;IACH,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAI9D;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAIhE;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAInE;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAIjE;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ;IAIjE;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,QAAQ;CAS7C"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for PIH SDK
|
|
3
|
+
*/
|
|
4
|
+
export class PIHError extends Error {
|
|
5
|
+
/** Error code for programmatic handling */
|
|
6
|
+
code;
|
|
7
|
+
/** Additional error details */
|
|
8
|
+
details;
|
|
9
|
+
constructor(message, code, details) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "PIHError";
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.details = details;
|
|
14
|
+
// Maintains proper stack trace for where error was thrown (V8 only)
|
|
15
|
+
if (Error.captureStackTrace) {
|
|
16
|
+
Error.captureStackTrace(this, PIHError);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a network error
|
|
21
|
+
*/
|
|
22
|
+
static networkError(message, details) {
|
|
23
|
+
return new PIHError(message, "NETWORK_ERROR", details);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create an invalid config error
|
|
27
|
+
*/
|
|
28
|
+
static invalidConfig(message, details) {
|
|
29
|
+
return new PIHError(message, "INVALID_CONFIG", details);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a queue full error
|
|
33
|
+
*/
|
|
34
|
+
static queueFull(message, details) {
|
|
35
|
+
return new PIHError(message, "QUEUE_FULL", details);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a rate limited error
|
|
39
|
+
*/
|
|
40
|
+
static rateLimited(message, details) {
|
|
41
|
+
return new PIHError(message, "RATE_LIMITED", details);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create an invalid payload error
|
|
45
|
+
*/
|
|
46
|
+
static invalidPayload(message, details) {
|
|
47
|
+
return new PIHError(message, "INVALID_PAYLOAD", details);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create a storage error
|
|
51
|
+
*/
|
|
52
|
+
static storageError(message, details) {
|
|
53
|
+
return new PIHError(message, "STORAGE_ERROR", details);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Create a session error
|
|
57
|
+
*/
|
|
58
|
+
static sessionError(message, details) {
|
|
59
|
+
return new PIHError(message, "SESSION_ERROR", details);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convert unknown error to PIHError
|
|
63
|
+
*/
|
|
64
|
+
static fromUnknown(error) {
|
|
65
|
+
if (error instanceof PIHError) {
|
|
66
|
+
return error;
|
|
67
|
+
}
|
|
68
|
+
if (error instanceof Error) {
|
|
69
|
+
return new PIHError(error.message, "UNKNOWN_ERROR", error);
|
|
70
|
+
}
|
|
71
|
+
return new PIHError(String(error), "UNKNOWN_ERROR", error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,2CAA2C;IAC3B,IAAI,CAAe;IACnC,+BAA+B;IACf,OAAO,CAAW;IAElC,YAAY,OAAe,EAAE,IAAkB,EAAE,OAAiB;QAChE,KAAK,CAAC,OAAO,CAAC,C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