@tinycloud/sdk-services 2.0.1 → 2.0.2-beta.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.
Files changed (139) hide show
  1. package/dist/{types.d.ts → BaseService-D9BFm_rV.d.cts} +179 -27
  2. package/dist/BaseService-D9BFm_rV.d.ts +440 -0
  3. package/dist/index.cjs +3221 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +1843 -0
  6. package/dist/index.d.ts +1826 -41
  7. package/dist/index.js +3136 -58
  8. package/dist/index.js.map +1 -1
  9. package/dist/kv/index.cjs +909 -0
  10. package/dist/kv/index.cjs.map +1 -0
  11. package/dist/kv/index.d.cts +748 -0
  12. package/dist/kv/index.d.ts +745 -7
  13. package/dist/kv/index.js +877 -9
  14. package/dist/kv/index.js.map +1 -1
  15. package/dist/sql/index.cjs +596 -0
  16. package/dist/sql/index.cjs.map +1 -0
  17. package/dist/sql/index.d.cts +228 -0
  18. package/dist/sql/index.d.ts +225 -7
  19. package/dist/sql/index.js +566 -8
  20. package/dist/sql/index.js.map +1 -1
  21. package/package.json +7 -6
  22. package/dist/base/BaseService.d.ts +0 -151
  23. package/dist/base/BaseService.d.ts.map +0 -1
  24. package/dist/base/BaseService.js +0 -221
  25. package/dist/base/BaseService.js.map +0 -1
  26. package/dist/base/index.d.ts +0 -6
  27. package/dist/base/index.d.ts.map +0 -1
  28. package/dist/base/index.js +0 -6
  29. package/dist/base/index.js.map +0 -1
  30. package/dist/base/types.d.ts +0 -36
  31. package/dist/base/types.d.ts.map +0 -1
  32. package/dist/base/types.js +0 -7
  33. package/dist/base/types.js.map +0 -1
  34. package/dist/context.d.ts +0 -142
  35. package/dist/context.d.ts.map +0 -1
  36. package/dist/context.js +0 -218
  37. package/dist/context.js.map +0 -1
  38. package/dist/duckdb/DuckDbDatabaseHandle.d.ts +0 -23
  39. package/dist/duckdb/DuckDbDatabaseHandle.d.ts.map +0 -1
  40. package/dist/duckdb/DuckDbDatabaseHandle.js +0 -36
  41. package/dist/duckdb/DuckDbDatabaseHandle.js.map +0 -1
  42. package/dist/duckdb/DuckDbService.d.ts +0 -50
  43. package/dist/duckdb/DuckDbService.d.ts.map +0 -1
  44. package/dist/duckdb/DuckDbService.js +0 -285
  45. package/dist/duckdb/DuckDbService.js.map +0 -1
  46. package/dist/duckdb/IDuckDbService.d.ts +0 -84
  47. package/dist/duckdb/IDuckDbService.d.ts.map +0 -1
  48. package/dist/duckdb/IDuckDbService.js +0 -7
  49. package/dist/duckdb/IDuckDbService.js.map +0 -1
  50. package/dist/duckdb/index.d.ts +0 -10
  51. package/dist/duckdb/index.d.ts.map +0 -1
  52. package/dist/duckdb/index.js +0 -9
  53. package/dist/duckdb/index.js.map +0 -1
  54. package/dist/duckdb/types.d.ts +0 -148
  55. package/dist/duckdb/types.d.ts.map +0 -1
  56. package/dist/duckdb/types.js +0 -19
  57. package/dist/duckdb/types.js.map +0 -1
  58. package/dist/errors.d.ts +0 -62
  59. package/dist/errors.d.ts.map +0 -1
  60. package/dist/errors.js +0 -149
  61. package/dist/errors.js.map +0 -1
  62. package/dist/index.d.ts.map +0 -1
  63. package/dist/kv/IKVService.d.ts +0 -148
  64. package/dist/kv/IKVService.d.ts.map +0 -1
  65. package/dist/kv/IKVService.js +0 -8
  66. package/dist/kv/IKVService.js.map +0 -1
  67. package/dist/kv/KVService.d.ts +0 -155
  68. package/dist/kv/KVService.d.ts.map +0 -1
  69. package/dist/kv/KVService.js +0 -419
  70. package/dist/kv/KVService.js.map +0 -1
  71. package/dist/kv/PrefixedKVService.d.ts +0 -246
  72. package/dist/kv/PrefixedKVService.d.ts.map +0 -1
  73. package/dist/kv/PrefixedKVService.js +0 -145
  74. package/dist/kv/PrefixedKVService.js.map +0 -1
  75. package/dist/kv/index.d.ts.map +0 -1
  76. package/dist/kv/types.d.ts +0 -204
  77. package/dist/kv/types.d.ts.map +0 -1
  78. package/dist/kv/types.js +0 -16
  79. package/dist/kv/types.js.map +0 -1
  80. package/dist/quota/TinyCloudQuota.d.ts +0 -27
  81. package/dist/quota/TinyCloudQuota.d.ts.map +0 -1
  82. package/dist/quota/TinyCloudQuota.js +0 -31
  83. package/dist/quota/TinyCloudQuota.js.map +0 -1
  84. package/dist/quota/index.d.ts +0 -3
  85. package/dist/quota/index.d.ts.map +0 -1
  86. package/dist/quota/index.js +0 -2
  87. package/dist/quota/index.js.map +0 -1
  88. package/dist/sql/DatabaseHandle.d.ts +0 -20
  89. package/dist/sql/DatabaseHandle.d.ts.map +0 -1
  90. package/dist/sql/DatabaseHandle.js +0 -27
  91. package/dist/sql/DatabaseHandle.js.map +0 -1
  92. package/dist/sql/ISQLService.d.ts +0 -67
  93. package/dist/sql/ISQLService.d.ts.map +0 -1
  94. package/dist/sql/ISQLService.js +0 -7
  95. package/dist/sql/ISQLService.js.map +0 -1
  96. package/dist/sql/SQLService.d.ts +0 -44
  97. package/dist/sql/SQLService.d.ts.map +0 -1
  98. package/dist/sql/SQLService.js +0 -216
  99. package/dist/sql/SQLService.js.map +0 -1
  100. package/dist/sql/index.d.ts.map +0 -1
  101. package/dist/sql/types.d.ts +0 -102
  102. package/dist/sql/types.d.ts.map +0 -1
  103. package/dist/sql/types.js +0 -21
  104. package/dist/sql/types.js.map +0 -1
  105. package/dist/types.d.ts.map +0 -1
  106. package/dist/types.js +0 -94
  107. package/dist/types.js.map +0 -1
  108. package/dist/types.schema.d.ts +0 -712
  109. package/dist/types.schema.d.ts.map +0 -1
  110. package/dist/types.schema.js +0 -342
  111. package/dist/types.schema.js.map +0 -1
  112. package/dist/types.schema.test.d.ts +0 -5
  113. package/dist/types.schema.test.d.ts.map +0 -1
  114. package/dist/types.schema.test.js +0 -677
  115. package/dist/types.schema.test.js.map +0 -1
  116. package/dist/vault/DataVaultService.d.ts +0 -267
  117. package/dist/vault/DataVaultService.d.ts.map +0 -1
  118. package/dist/vault/DataVaultService.js +0 -1040
  119. package/dist/vault/DataVaultService.js.map +0 -1
  120. package/dist/vault/IDataVaultService.d.ts +0 -158
  121. package/dist/vault/IDataVaultService.d.ts.map +0 -1
  122. package/dist/vault/IDataVaultService.js +0 -8
  123. package/dist/vault/IDataVaultService.js.map +0 -1
  124. package/dist/vault/SignatureCache.d.ts +0 -20
  125. package/dist/vault/SignatureCache.d.ts.map +0 -1
  126. package/dist/vault/SignatureCache.js +0 -167
  127. package/dist/vault/SignatureCache.js.map +0 -1
  128. package/dist/vault/createVaultCrypto.d.ts +0 -16
  129. package/dist/vault/createVaultCrypto.d.ts.map +0 -1
  130. package/dist/vault/createVaultCrypto.js +0 -12
  131. package/dist/vault/createVaultCrypto.js.map +0 -1
  132. package/dist/vault/index.d.ts +0 -11
  133. package/dist/vault/index.d.ts.map +0 -1
  134. package/dist/vault/index.js +0 -12
  135. package/dist/vault/index.js.map +0 -1
  136. package/dist/vault/types.d.ts +0 -141
  137. package/dist/vault/types.d.ts.map +0 -1
  138. package/dist/vault/types.js +0 -31
  139. package/dist/vault/types.js.map +0 -1
@@ -21,7 +21,7 @@
21
21
  * }
22
22
  * ```
23
23
  */
24
- export type Result<T, E = ServiceError> = {
24
+ type Result<T, E = ServiceError> = {
25
25
  ok: true;
26
26
  data: T;
27
27
  } | {
@@ -31,7 +31,7 @@ export type Result<T, E = ServiceError> = {
31
31
  /**
32
32
  * Service error with structured information.
33
33
  */
34
- export interface ServiceError {
34
+ interface ServiceError {
35
35
  /** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
36
36
  code: string;
37
37
  /** Human-readable error message */
@@ -46,7 +46,7 @@ export interface ServiceError {
46
46
  /**
47
47
  * Storage quota information returned with quota-related errors.
48
48
  */
49
- export interface StorageQuotaInfo {
49
+ interface StorageQuotaInfo {
50
50
  usedBytes: number;
51
51
  limitBytes: number;
52
52
  service: string;
@@ -54,7 +54,7 @@ export interface StorageQuotaInfo {
54
54
  /**
55
55
  * Standard error codes used across services.
56
56
  */
57
- export declare const ErrorCodes: {
57
+ declare const ErrorCodes: {
58
58
  readonly NOT_FOUND: "NOT_FOUND";
59
59
  readonly AUTH_EXPIRED: "AUTH_EXPIRED";
60
60
  readonly AUTH_REQUIRED: "AUTH_REQUIRED";
@@ -85,12 +85,12 @@ export declare const ErrorCodes: {
85
85
  readonly DUCKDB_SCHEMA_ERROR: "DUCKDB_SCHEMA_ERROR";
86
86
  readonly DUCKDB_READONLY_VIOLATION: "DUCKDB_READONLY_VIOLATION";
87
87
  };
88
- export type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
88
+ type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
89
89
  /**
90
90
  * Session data required for authenticated service operations.
91
91
  * Both TinyCloudSession and web-sdk Session can be cast to this interface.
92
92
  */
93
- export interface ServiceSession {
93
+ interface ServiceSession {
94
94
  /** The delegation header containing the UCAN */
95
95
  delegationHeader: {
96
96
  Authorization: string;
@@ -107,12 +107,12 @@ export interface ServiceSession {
107
107
  /**
108
108
  * Headers type - compatible with both browser and Node.js.
109
109
  */
110
- export type ServiceHeaders = Record<string, string> | [string, string][];
110
+ type ServiceHeaders = Record<string, string> | [string, string][];
111
111
  /**
112
112
  * A single fact object to include in the UCAN invocation.
113
113
  * Facts are key-value objects that the server reads from the UCAN facts field.
114
114
  */
115
- export interface InvocationFact {
115
+ interface InvocationFact {
116
116
  [key: string]: unknown;
117
117
  }
118
118
  /**
@@ -120,7 +120,7 @@ export interface InvocationFact {
120
120
  * This is an array of fact objects per the UCAN spec.
121
121
  * Used to pass additional parameters that the server reads from the UCAN facts field.
122
122
  */
123
- export type InvocationFacts = InvocationFact[];
123
+ type InvocationFacts = InvocationFact[];
124
124
  /**
125
125
  * Invoke function signature - platform-specific implementation injected via DI.
126
126
  * Both node-sdk-wasm and web-sdk-wasm export this with identical signature.
@@ -132,11 +132,11 @@ export type InvocationFacts = InvocationFact[];
132
132
  * @param facts - Optional facts to include in the UCAN (e.g., for capabilities/read params)
133
133
  * @returns Headers to include in the request
134
134
  */
135
- export type InvokeFunction = (session: ServiceSession, service: string, path: string, action: string, facts?: InvocationFacts) => ServiceHeaders;
135
+ type InvokeFunction = (session: ServiceSession, service: string, path: string, action: string, facts?: InvocationFacts) => ServiceHeaders;
136
136
  /**
137
137
  * Fetch request options - compatible with standard fetch API.
138
138
  */
139
- export interface FetchRequestInit {
139
+ interface FetchRequestInit {
140
140
  method?: string;
141
141
  headers?: ServiceHeaders;
142
142
  body?: Blob | string;
@@ -145,7 +145,7 @@ export interface FetchRequestInit {
145
145
  /**
146
146
  * Fetch response interface - compatible with standard Response.
147
147
  */
148
- export interface FetchResponse {
148
+ interface FetchResponse {
149
149
  ok: boolean;
150
150
  status: number;
151
151
  statusText: string;
@@ -161,11 +161,11 @@ export interface FetchResponse {
161
161
  * Fetch function signature - allows for custom fetch implementations.
162
162
  * Compatible with both browser fetch and Node.js fetch.
163
163
  */
164
- export type FetchFunction = (url: string, init?: FetchRequestInit) => Promise<FetchResponse>;
164
+ type FetchFunction = (url: string, init?: FetchRequestInit) => Promise<FetchResponse>;
165
165
  /**
166
166
  * Configuration for automatic retry of failed requests.
167
167
  */
168
- export interface RetryPolicy {
168
+ interface RetryPolicy {
169
169
  /** Maximum number of attempts (including initial) */
170
170
  maxAttempts: number;
171
171
  /** Backoff strategy between retries */
@@ -180,15 +180,15 @@ export interface RetryPolicy {
180
180
  /**
181
181
  * Default retry policy.
182
182
  */
183
- export declare const defaultRetryPolicy: RetryPolicy;
183
+ declare const defaultRetryPolicy: RetryPolicy;
184
184
  /**
185
185
  * Event handler function type.
186
186
  */
187
- export type EventHandler = (data: unknown) => void;
187
+ type EventHandler = (data: unknown) => void;
188
188
  /**
189
189
  * Service interface - base contract for all services.
190
190
  */
191
- export interface IService {
191
+ interface IService {
192
192
  /** Initialize service with context */
193
193
  initialize(context: IServiceContext): void;
194
194
  /** Called when session changes (sign-in, sign-out, refresh) */
@@ -202,7 +202,7 @@ export interface IService {
202
202
  * Context provided to services for accessing platform dependencies.
203
203
  * The SDK creates this context and passes it to services during initialization.
204
204
  */
205
- export interface IServiceContext {
205
+ interface IServiceContext {
206
206
  /** Current active session, or null if not authenticated */
207
207
  readonly session: ServiceSession | null;
208
208
  /** Whether there is an active authenticated session */
@@ -227,7 +227,7 @@ export interface IServiceContext {
227
227
  /**
228
228
  * Event emitted before a service request.
229
229
  */
230
- export interface ServiceRequestEvent {
230
+ interface ServiceRequestEvent {
231
231
  service: string;
232
232
  action: string;
233
233
  key?: string;
@@ -236,7 +236,7 @@ export interface ServiceRequestEvent {
236
236
  /**
237
237
  * Event emitted after a service response.
238
238
  */
239
- export interface ServiceResponseEvent {
239
+ interface ServiceResponseEvent {
240
240
  service: string;
241
241
  action: string;
242
242
  ok: boolean;
@@ -246,14 +246,14 @@ export interface ServiceResponseEvent {
246
246
  /**
247
247
  * Event emitted on service error.
248
248
  */
249
- export interface ServiceErrorEvent {
249
+ interface ServiceErrorEvent {
250
250
  service: string;
251
251
  error: ServiceError;
252
252
  }
253
253
  /**
254
254
  * Event emitted on retry attempt.
255
255
  */
256
- export interface ServiceRetryEvent {
256
+ interface ServiceRetryEvent {
257
257
  service: string;
258
258
  attempt: number;
259
259
  maxAttempts: number;
@@ -262,7 +262,7 @@ export interface ServiceRetryEvent {
262
262
  /**
263
263
  * Telemetry event names.
264
264
  */
265
- export declare const TelemetryEvents: {
265
+ declare const TelemetryEvents: {
266
266
  readonly SERVICE_REQUEST: "service.request";
267
267
  readonly SERVICE_RESPONSE: "service.response";
268
268
  readonly SERVICE_ERROR: "service.error";
@@ -273,16 +273,168 @@ export declare const TelemetryEvents: {
273
273
  /**
274
274
  * Create a success result.
275
275
  */
276
- export declare function ok<T>(data: T): Result<T>;
276
+ declare function ok<T>(data: T): Result<T>;
277
277
  /**
278
278
  * Create an error result.
279
279
  */
280
- export declare function err<E = ServiceError>(error: E): Result<never, E>;
280
+ declare function err<E = ServiceError>(error: E): Result<never, E>;
281
281
  /**
282
282
  * Create a ServiceError.
283
283
  */
284
- export declare function serviceError(code: string, message: string, service: string, options?: {
284
+ declare function serviceError(code: string, message: string, service: string, options?: {
285
285
  cause?: Error;
286
286
  meta?: Record<string, unknown>;
287
287
  }): ServiceError;
288
- //# sourceMappingURL=types.d.ts.map
288
+
289
+ /**
290
+ * BaseService - Abstract base class for all TinyCloud services.
291
+ *
292
+ * Provides common functionality:
293
+ * - Context management
294
+ * - Session lifecycle hooks
295
+ * - Abort signal handling
296
+ * - Telemetry emission
297
+ */
298
+
299
+ /**
300
+ * Abstract base class for TinyCloud services.
301
+ *
302
+ * Services extend this class to get common functionality like
303
+ * context management, session lifecycle, and abort handling.
304
+ *
305
+ * @example
306
+ * ```typescript
307
+ * class MyService extends BaseService implements IMyService {
308
+ * static readonly serviceName = 'myservice';
309
+ *
310
+ * constructor(config: MyServiceConfig = {}) {
311
+ * super();
312
+ * this._config = config;
313
+ * }
314
+ *
315
+ * async doSomething(): Promise<Result<Data>> {
316
+ * if (!this.requireAuth()) {
317
+ * return err(authRequiredError('myservice'));
318
+ * }
319
+ * // ... implementation
320
+ * }
321
+ * }
322
+ * ```
323
+ */
324
+ declare abstract class BaseService implements IService {
325
+ /**
326
+ * Service identifier used for registration.
327
+ * Must be overridden by subclasses.
328
+ */
329
+ static readonly serviceName: string;
330
+ /**
331
+ * Service context providing access to platform dependencies.
332
+ * Set during initialize().
333
+ */
334
+ protected context: IServiceContext;
335
+ /**
336
+ * Abort controller for this service's operations.
337
+ * Reset on sign-out.
338
+ */
339
+ protected abortController: AbortController;
340
+ /**
341
+ * Service-specific configuration.
342
+ */
343
+ protected _config: Record<string, unknown>;
344
+ /**
345
+ * Get the service configuration.
346
+ */
347
+ get config(): Record<string, unknown>;
348
+ /**
349
+ * Initialize the service with context.
350
+ * Called by the SDK after instantiation.
351
+ *
352
+ * @param context - The service context
353
+ */
354
+ initialize(context: IServiceContext): void;
355
+ /**
356
+ * Called when session changes (sign-in, sign-out, refresh).
357
+ * Override in subclasses to handle session changes.
358
+ *
359
+ * @param session - The new session, or null if signed out
360
+ */
361
+ onSessionChange(session: ServiceSession | null): void;
362
+ /**
363
+ * Called when SDK signs out.
364
+ * Aborts all pending operations.
365
+ */
366
+ onSignOut(): void;
367
+ /**
368
+ * Get the abort signal for this service.
369
+ * Combines the service-level abort with context-level abort.
370
+ */
371
+ protected get abortSignal(): AbortSignal;
372
+ /**
373
+ * Check if the service is authenticated.
374
+ */
375
+ protected get isAuthenticated(): boolean;
376
+ /**
377
+ * Get the current session.
378
+ * Throws if not authenticated.
379
+ */
380
+ protected get session(): ServiceSession;
381
+ /**
382
+ * Check authentication and return error result if not authenticated.
383
+ * Use this at the start of methods that require authentication.
384
+ *
385
+ * @returns true if authenticated, false otherwise
386
+ */
387
+ protected requireAuth(): boolean;
388
+ /**
389
+ * Emit a telemetry event.
390
+ *
391
+ * @param event - Event name
392
+ * @param data - Event data
393
+ */
394
+ protected emit(event: string, data: unknown): void;
395
+ /**
396
+ * Emit a service request event.
397
+ *
398
+ * @param action - The action being performed
399
+ * @param key - Optional key/path being accessed
400
+ */
401
+ protected emitRequest(action: string, key?: string): void;
402
+ /**
403
+ * Emit a service response event.
404
+ *
405
+ * @param action - The action that was performed
406
+ * @param ok - Whether the request was successful
407
+ * @param startTime - Start time for duration calculation
408
+ * @param status - Optional HTTP status code
409
+ */
410
+ protected emitResponse(action: string, ok: boolean, startTime: number, status?: number): void;
411
+ /**
412
+ * Emit a service error event.
413
+ *
414
+ * @param error - The service error
415
+ */
416
+ protected emitError(error: ServiceError): void;
417
+ /**
418
+ * Get the service name from the static property.
419
+ * Subclasses must define static serviceName.
420
+ */
421
+ protected getServiceName(): string;
422
+ /**
423
+ * Create a combined abort signal from multiple sources.
424
+ *
425
+ * @param signals - Additional abort signals to combine
426
+ * @returns A combined abort signal
427
+ */
428
+ protected combineSignals(...signals: (AbortSignal | undefined)[]): AbortSignal;
429
+ /**
430
+ * Wrap an operation with error handling and telemetry.
431
+ *
432
+ * @param action - The action name for telemetry
433
+ * @param key - Optional key for telemetry
434
+ * @param operation - The operation to execute
435
+ * @returns Result of the operation
436
+ */
437
+ protected withTelemetry<T>(action: string, key: string | undefined, operation: () => Promise<Result<T>>): Promise<Result<T>>;
438
+ }
439
+
440
+ export { BaseService as B, type ErrorCode as E, type FetchFunction as F, type IServiceContext as I, type RetryPolicy as R, type ServiceSession as S, TelemetryEvents as T, type InvokeFunction as a, type IService as b, type ServiceError as c, type Result as d, type StorageQuotaInfo as e, ErrorCodes as f, type EventHandler as g, type FetchRequestInit as h, type FetchResponse as i, type InvocationFact as j, type InvocationFacts as k, type ServiceErrorEvent as l, type ServiceHeaders as m, type ServiceRequestEvent as n, type ServiceResponseEvent as o, type ServiceRetryEvent as p, defaultRetryPolicy as q, err as r, ok as s, serviceError as t };