@alter-ai/alter-sdk 0.2.0 → 0.2.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.
package/dist/index.d.cts CHANGED
@@ -1,3 +1,197 @@
1
+ /**
2
+ * Models for Alter SDK.
3
+ *
4
+ * These models provide type-safe data structures for SDK operations.
5
+ *
6
+ * Security Hardening (v0.4.0):
7
+ * - TokenResponse: accessToken stored in module-private WeakMap in client.ts
8
+ * (not readable as property, not importable from this module)
9
+ * - TokenResponse: Object.freeze(this) prevents mutation after creation
10
+ * - toJSON() and toString() exclude access token from serialization
11
+ */
12
+ /**
13
+ * OAuth token response from Alter Vault.
14
+ *
15
+ * This represents an access token retrieved from the backend.
16
+ * The access_token is NOT stored as a readable property — it lives in a
17
+ * module-private WeakMap in client.ts and is only accessible internally.
18
+ *
19
+ * SECURITY:
20
+ * - No accessToken property (stored in WeakMap inside client.ts)
21
+ * - Instance is frozen after construction to prevent mutation
22
+ * - toJSON() and toString() exclude access token
23
+ * - _extractAccessToken() lives in client.ts, NOT in this file,
24
+ * so it cannot be imported by consumers even via deep imports
25
+ */
26
+ declare class TokenResponse {
27
+ /** Token type (usually "Bearer") */
28
+ readonly tokenType: string;
29
+ /** Seconds until token expires */
30
+ readonly expiresIn: number | null;
31
+ /** Absolute expiration time */
32
+ readonly expiresAt: Date | null;
33
+ /** OAuth scopes granted */
34
+ readonly scopes: string[];
35
+ /** Connection ID that provided this token */
36
+ readonly connectionId: string;
37
+ constructor(data: {
38
+ access_token: string;
39
+ token_type?: string;
40
+ expires_in?: number | null;
41
+ expires_at?: string | null;
42
+ scopes?: string[];
43
+ connection_id: string;
44
+ });
45
+ /**
46
+ * Parse expires_at from ISO string.
47
+ */
48
+ private static parseExpiresAt;
49
+ /**
50
+ * Check if token is expired.
51
+ *
52
+ * @param bufferSeconds - Consider token expired N seconds before actual expiry.
53
+ * Useful for preventing race conditions.
54
+ * @returns True if token is expired or will expire within bufferSeconds.
55
+ */
56
+ isExpired(bufferSeconds?: number): boolean;
57
+ /**
58
+ * Check if token should be refreshed soon.
59
+ *
60
+ * @param bufferSeconds - Consider token needing refresh N seconds before expiry.
61
+ * Default 5 minutes (300 seconds).
62
+ * @returns True if token will expire within bufferSeconds.
63
+ */
64
+ needsRefresh(bufferSeconds?: number): boolean;
65
+ /**
66
+ * Custom JSON serialization — EXCLUDES access_token for security.
67
+ */
68
+ toJSON(): Record<string, unknown>;
69
+ /**
70
+ * Custom string representation — EXCLUDES access_token for security.
71
+ */
72
+ toString(): string;
73
+ }
74
+ /**
75
+ * OAuth connection info from Alter Vault.
76
+ *
77
+ * Represents a connected OAuth service (e.g., Google, GitHub).
78
+ * Returned by listConnections(). Contains metadata only — no tokens.
79
+ */
80
+ declare class ConnectionInfo {
81
+ readonly id: string;
82
+ readonly providerId: string;
83
+ readonly attributes: Record<string, unknown>;
84
+ readonly scopes: string[];
85
+ readonly accountIdentifier: string | null;
86
+ readonly accountDisplayName: string | null;
87
+ readonly status: string;
88
+ readonly expiresAt: string | null;
89
+ readonly createdAt: string;
90
+ readonly lastUsedAt: string | null;
91
+ constructor(data: {
92
+ id: string;
93
+ provider_id: string;
94
+ attributes?: Record<string, unknown>;
95
+ scopes?: string[];
96
+ account_identifier?: string | null;
97
+ account_display_name?: string | null;
98
+ status: string;
99
+ expires_at?: string | null;
100
+ created_at: string;
101
+ last_used_at?: string | null;
102
+ });
103
+ toJSON(): Record<string, unknown>;
104
+ toString(): string;
105
+ }
106
+ /**
107
+ * Connect session for initiating OAuth flows.
108
+ *
109
+ * Returned by createConnectSession(). Contains a URL the user
110
+ * opens in their browser to authorize access.
111
+ */
112
+ declare class ConnectSession {
113
+ readonly sessionToken: string;
114
+ readonly connectUrl: string;
115
+ readonly expiresIn: number;
116
+ readonly expiresAt: string;
117
+ constructor(data: {
118
+ session_token: string;
119
+ connect_url: string;
120
+ expires_in: number;
121
+ expires_at: string;
122
+ });
123
+ toJSON(): Record<string, unknown>;
124
+ toString(): string;
125
+ }
126
+ /**
127
+ * Paginated list of connections.
128
+ *
129
+ * Returned by listConnections(). Contains connection array plus pagination metadata.
130
+ */
131
+ declare class ConnectionListResult {
132
+ readonly connections: ConnectionInfo[];
133
+ readonly total: number;
134
+ readonly limit: number;
135
+ readonly offset: number;
136
+ readonly hasMore: boolean;
137
+ constructor(data: {
138
+ connections: ConnectionInfo[];
139
+ total: number;
140
+ limit: number;
141
+ offset: number;
142
+ has_more: boolean;
143
+ });
144
+ }
145
+ /**
146
+ * Audit log entry for an API call to a provider.
147
+ *
148
+ * This is sent to the backend audit endpoint.
149
+ */
150
+ declare class APICallAuditLog {
151
+ readonly connectionId: string;
152
+ readonly providerId: string;
153
+ readonly method: string;
154
+ readonly url: string;
155
+ readonly requestHeaders: Record<string, string> | null;
156
+ readonly requestBody: unknown | null;
157
+ readonly responseStatus: number;
158
+ readonly responseHeaders: Record<string, string> | null;
159
+ readonly responseBody: unknown | null;
160
+ readonly latencyMs: number;
161
+ /** Client-side timestamp (excluded from sanitize() output, like Python SDK) */
162
+ readonly timestamp: Date;
163
+ readonly reason: string | null;
164
+ /** Execution run ID for actor tracking */
165
+ readonly runId: string | null;
166
+ /** Conversation thread ID for actor tracking */
167
+ readonly threadId: string | null;
168
+ /** Tool invocation ID for actor tracking */
169
+ readonly toolCallId: string | null;
170
+ constructor(data: {
171
+ connectionId: string;
172
+ providerId: string;
173
+ method: string;
174
+ url: string;
175
+ requestHeaders?: Record<string, string> | null;
176
+ requestBody?: unknown | null;
177
+ responseStatus: number;
178
+ responseHeaders?: Record<string, string> | null;
179
+ responseBody?: unknown | null;
180
+ latencyMs: number;
181
+ reason?: string | null;
182
+ runId?: string | null;
183
+ threadId?: string | null;
184
+ toolCallId?: string | null;
185
+ });
186
+ /**
187
+ * Sanitize sensitive data before sending.
188
+ *
189
+ * Removes Authorization headers, cookies, etc.
190
+ */
191
+ sanitize(): Record<string, unknown>;
192
+ private filterSensitiveHeaders;
193
+ }
194
+
1
195
  /**
2
196
  * Provider and HttpMethod enums for type-safe SDK usage.
3
197
  *
@@ -60,17 +254,9 @@ declare enum HttpMethod {
60
254
  /**
61
255
  * Main Alter Vault SDK client.
62
256
  *
63
- * This module provides the primary AlterVault class for interacting with
64
- * the Alter Vault OAuth token management system.
65
- *
66
- * Security Hardening (v0.4.0):
67
- * - Layer 1: Prototype frozen to prevent subclass method override attacks
68
- * - Layer 1b: HttpClient.prototype also frozen to prevent request interception
69
- * - Layer 2: Instance frozen after construction (Object.freeze)
70
- * - Layer 3: API key not stored as instance property
71
- * - Layer 4: ES2022 private fields (#) for truly private internal state
72
- * - Layer 5: (see models.ts) TokenResponse access_token in module-private WeakMap
73
- * - Layer 6: Test accessors removed from production builds
257
+ * Provides the AlterVault class for OAuth token management with
258
+ * zero token exposure tokens are retrieved and injected automatically,
259
+ * never returned to the caller.
74
260
  */
75
261
 
76
262
  /**
@@ -81,8 +267,6 @@ interface AlterVaultOptions {
81
267
  apiKey: string;
82
268
  /** Base URL for Alter Vault API */
83
269
  baseUrl?: string;
84
- /** Whether to send audit logs to backend (default: true) */
85
- enableAuditLogging?: boolean;
86
270
  /** HTTP request timeout in milliseconds (default: 30000) */
87
271
  timeout?: number;
88
272
  /** Actor type ("ai_agent" or "mcp_server") for tracking */
@@ -121,6 +305,41 @@ interface RequestOptions {
121
305
  /** Tool invocation ID */
122
306
  toolCallId?: string;
123
307
  }
308
+ /**
309
+ * Options for the listConnections() method.
310
+ */
311
+ interface ListConnectionsOptions {
312
+ /** Filter by provider ID (e.g., "google") */
313
+ providerId?: string;
314
+ /** Maximum number of connections to return (default 100, max 1000) */
315
+ limit?: number;
316
+ /** Offset for pagination (default 0) */
317
+ offset?: number;
318
+ }
319
+ /**
320
+ * Options for the createConnectSession() method.
321
+ */
322
+ interface CreateConnectSessionOptions {
323
+ /** End user to create session for (requires at least id) */
324
+ endUser: {
325
+ id: string;
326
+ email?: string;
327
+ name?: string;
328
+ };
329
+ /** User attributes for connection matching */
330
+ attributes?: Record<string, unknown>;
331
+ /** Restrict to specific providers (e.g., ["google", "github"]) */
332
+ allowedProviders?: string[];
333
+ /** URL to redirect after OAuth completion */
334
+ returnUrl?: string;
335
+ /** Allowed origin for postMessage communication */
336
+ allowedOrigin?: string;
337
+ /** Request metadata for audit */
338
+ metadata?: {
339
+ ipAddress?: string;
340
+ userAgent?: string;
341
+ };
342
+ }
124
343
  /**
125
344
  * Main SDK class for Alter Vault OAuth token management.
126
345
  *
@@ -129,17 +348,7 @@ interface RequestOptions {
129
348
  * - Audit logging of API calls
130
349
  * - Actor tracking for AI agents and MCP servers
131
350
  *
132
- * Zero Token Exposure:
133
- * - Tokens are retrieved internally but NEVER exposed to developers
134
- * - All API calls inject tokens automatically behind the scenes
135
- * - Developers only see API results, never authentication credentials
136
- *
137
- * Security Hardening:
138
- * - Cannot be effectively subclassed (prototype is frozen)
139
- * - Instance is frozen after construction (Object.freeze)
140
- * - API key is NOT stored as an instance property
141
- * - Internal HTTP clients use ES2022 private fields (#)
142
- * - Test accessors are removed (no production leak path)
351
+ * Tokens are retrieved and injected automatically — never exposed to callers.
143
352
  *
144
353
  * @example
145
354
  * ```typescript
@@ -162,7 +371,6 @@ interface RequestOptions {
162
371
  declare class AlterVault {
163
372
  #private;
164
373
  readonly baseUrl: string;
165
- readonly enableAuditLogging: boolean;
166
374
  constructor(options: AlterVaultOptions);
167
375
  /**
168
376
  * Execute an HTTP request to a provider API with automatic token injection.
@@ -171,143 +379,33 @@ declare class AlterVault {
171
379
  * 1. Fetches an OAuth token from Alter backend (never exposed)
172
380
  * 2. Injects the token as a Bearer header
173
381
  * 3. Calls the provider API
174
- * 4. Logs the call for audit (if enabled, fire-and-forget)
382
+ * 4. Logs the call for audit (fire-and-forget)
175
383
  * 5. Returns the raw response
176
384
  */
177
385
  request(provider: Provider | string, method: HttpMethod | string, url: string, options: RequestOptions): Promise<Response>;
178
386
  /**
179
- * Close HTTP clients and release resources.
180
- * Waits for any pending audit tasks before closing.
181
- */
182
- close(): Promise<void>;
183
- /**
184
- * Async dispose support for `await using vault = new AlterVault(...)`.
185
- * Requires TypeScript 5.2+ and Node.js 18+.
186
- */
187
- [Symbol.asyncDispose](): Promise<void>;
188
- }
189
-
190
- /**
191
- * Models for Alter SDK.
192
- *
193
- * These models provide type-safe data structures for SDK operations.
194
- *
195
- * Security Hardening (v0.4.0):
196
- * - TokenResponse: accessToken stored in module-private WeakMap in client.ts
197
- * (not readable as property, not importable from this module)
198
- * - TokenResponse: Object.freeze(this) prevents mutation after creation
199
- * - toJSON() and toString() exclude access token from serialization
200
- */
201
- /**
202
- * OAuth token response from Alter Vault.
203
- *
204
- * This represents an access token retrieved from the backend.
205
- * The access_token is NOT stored as a readable property — it lives in a
206
- * module-private WeakMap in client.ts and is only accessible internally.
207
- *
208
- * SECURITY:
209
- * - No accessToken property (stored in WeakMap inside client.ts)
210
- * - Instance is frozen after construction to prevent mutation
211
- * - toJSON() and toString() exclude access token
212
- * - _extractAccessToken() lives in client.ts, NOT in this file,
213
- * so it cannot be imported by consumers even via deep imports
214
- */
215
- declare class TokenResponse {
216
- /** Token type (usually "Bearer") */
217
- readonly tokenType: string;
218
- /** Seconds until token expires */
219
- readonly expiresIn: number | null;
220
- /** Absolute expiration time */
221
- readonly expiresAt: Date | null;
222
- /** OAuth scopes granted */
223
- readonly scopes: string[];
224
- /** Connection ID that provided this token */
225
- readonly connectionId: string;
226
- constructor(data: {
227
- access_token: string;
228
- token_type?: string;
229
- expires_in?: number | null;
230
- expires_at?: string | null;
231
- scopes?: string[];
232
- connection_id: string;
233
- });
234
- /**
235
- * Parse expires_at from ISO string.
236
- */
237
- private static parseExpiresAt;
238
- /**
239
- * Check if token is expired.
387
+ * List OAuth connections for this app.
240
388
  *
241
- * @param bufferSeconds - Consider token expired N seconds before actual expiry.
242
- * Useful for preventing race conditions.
243
- * @returns True if token is expired or will expire within bufferSeconds.
389
+ * Returns paginated connection metadata (no tokens).
390
+ * Useful for discovering which services a user has connected.
244
391
  */
245
- isExpired(bufferSeconds?: number): boolean;
392
+ listConnections(options?: ListConnectionsOptions): Promise<ConnectionListResult>;
246
393
  /**
247
- * Check if token should be refreshed soon.
394
+ * Create a Connect session for initiating OAuth flows.
248
395
  *
249
- * @param bufferSeconds - Consider token needing refresh N seconds before expiry.
250
- * Default 5 minutes (300 seconds).
251
- * @returns True if token will expire within bufferSeconds.
396
+ * Returns a URL the user can open in their browser to authorize access.
252
397
  */
253
- needsRefresh(bufferSeconds?: number): boolean;
398
+ createConnectSession(options: CreateConnectSessionOptions): Promise<ConnectSession>;
254
399
  /**
255
- * Custom JSON serialization EXCLUDES access_token for security.
256
- */
257
- toJSON(): Record<string, unknown>;
258
- /**
259
- * Custom string representation — EXCLUDES access_token for security.
400
+ * Close HTTP clients and release resources.
401
+ * Waits for any pending audit tasks before closing.
260
402
  */
261
- toString(): string;
262
- }
263
- /**
264
- * Audit log entry for an API call to a provider.
265
- *
266
- * This is sent to the backend audit endpoint.
267
- */
268
- declare class APICallAuditLog {
269
- readonly connectionId: string;
270
- readonly providerId: string;
271
- readonly method: string;
272
- readonly url: string;
273
- readonly requestHeaders: Record<string, string> | null;
274
- readonly requestBody: unknown | null;
275
- readonly responseStatus: number;
276
- readonly responseHeaders: Record<string, string> | null;
277
- readonly responseBody: unknown | null;
278
- readonly latencyMs: number;
279
- /** Client-side timestamp (excluded from sanitize() output, like Python SDK) */
280
- readonly timestamp: Date;
281
- readonly reason: string | null;
282
- /** Execution run ID for actor tracking */
283
- readonly runId: string | null;
284
- /** Conversation thread ID for actor tracking */
285
- readonly threadId: string | null;
286
- /** Tool invocation ID for actor tracking */
287
- readonly toolCallId: string | null;
288
- constructor(data: {
289
- connectionId: string;
290
- providerId: string;
291
- method: string;
292
- url: string;
293
- requestHeaders?: Record<string, string> | null;
294
- requestBody?: unknown | null;
295
- responseStatus: number;
296
- responseHeaders?: Record<string, string> | null;
297
- responseBody?: unknown | null;
298
- latencyMs: number;
299
- reason?: string | null;
300
- runId?: string | null;
301
- threadId?: string | null;
302
- toolCallId?: string | null;
303
- });
403
+ close(): Promise<void>;
304
404
  /**
305
- * Sanitize sensitive data before sending.
306
- *
307
- * Removes Authorization headers, cookies, etc.
405
+ * Async dispose support for `await using vault = new AlterVault(...)`.
406
+ * Requires TypeScript 5.2+ and Node.js 18+.
308
407
  */
309
- sanitize(): Record<string, unknown>;
310
- private filterSensitiveHeaders;
408
+ [Symbol.asyncDispose](): Promise<void>;
311
409
  }
312
410
 
313
411
  /**
@@ -389,4 +487,4 @@ declare class TimeoutError extends NetworkError {
389
487
  constructor(message: string, details?: Record<string, unknown>);
390
488
  }
391
489
 
392
- export { APICallAuditLog, AlterSDKError, AlterVault, type AlterVaultOptions, ConnectionNotFoundError, HttpMethod, NetworkError, PolicyViolationError, Provider, ProviderAPIError, type RequestOptions, TimeoutError, TokenExpiredError, TokenResponse, TokenRetrievalError };
490
+ export { APICallAuditLog, AlterSDKError, AlterVault, type AlterVaultOptions, ConnectSession, ConnectionInfo, ConnectionListResult, ConnectionNotFoundError, type CreateConnectSessionOptions, HttpMethod, type ListConnectionsOptions, NetworkError, PolicyViolationError, Provider, ProviderAPIError, type RequestOptions, TimeoutError, TokenExpiredError, TokenResponse, TokenRetrievalError };