@tinycloud/sdk-services 1.7.0 → 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.
- package/dist/{types.d.ts → BaseService-D9BFm_rV.d.cts} +179 -27
- package/dist/BaseService-D9BFm_rV.d.ts +440 -0
- package/dist/index.cjs +3221 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1843 -0
- package/dist/index.d.ts +1826 -41
- package/dist/index.js +3136 -58
- package/dist/index.js.map +1 -1
- package/dist/kv/index.cjs +909 -0
- package/dist/kv/index.cjs.map +1 -0
- package/dist/kv/index.d.cts +748 -0
- package/dist/kv/index.d.ts +745 -7
- package/dist/kv/index.js +877 -9
- package/dist/kv/index.js.map +1 -1
- package/dist/sql/index.cjs +596 -0
- package/dist/sql/index.cjs.map +1 -0
- package/dist/sql/index.d.cts +228 -0
- package/dist/sql/index.d.ts +225 -7
- package/dist/sql/index.js +566 -8
- package/dist/sql/index.js.map +1 -1
- package/package.json +7 -6
- package/dist/base/BaseService.d.ts +0 -151
- package/dist/base/BaseService.d.ts.map +0 -1
- package/dist/base/BaseService.js +0 -221
- package/dist/base/BaseService.js.map +0 -1
- package/dist/base/index.d.ts +0 -6
- package/dist/base/index.d.ts.map +0 -1
- package/dist/base/index.js +0 -6
- package/dist/base/index.js.map +0 -1
- package/dist/base/types.d.ts +0 -36
- package/dist/base/types.d.ts.map +0 -1
- package/dist/base/types.js +0 -7
- package/dist/base/types.js.map +0 -1
- package/dist/context.d.ts +0 -142
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -218
- package/dist/context.js.map +0 -1
- package/dist/duckdb/DuckDbDatabaseHandle.d.ts +0 -23
- package/dist/duckdb/DuckDbDatabaseHandle.d.ts.map +0 -1
- package/dist/duckdb/DuckDbDatabaseHandle.js +0 -36
- package/dist/duckdb/DuckDbDatabaseHandle.js.map +0 -1
- package/dist/duckdb/DuckDbService.d.ts +0 -50
- package/dist/duckdb/DuckDbService.d.ts.map +0 -1
- package/dist/duckdb/DuckDbService.js +0 -285
- package/dist/duckdb/DuckDbService.js.map +0 -1
- package/dist/duckdb/IDuckDbService.d.ts +0 -84
- package/dist/duckdb/IDuckDbService.d.ts.map +0 -1
- package/dist/duckdb/IDuckDbService.js +0 -7
- package/dist/duckdb/IDuckDbService.js.map +0 -1
- package/dist/duckdb/index.d.ts +0 -10
- package/dist/duckdb/index.d.ts.map +0 -1
- package/dist/duckdb/index.js +0 -9
- package/dist/duckdb/index.js.map +0 -1
- package/dist/duckdb/types.d.ts +0 -148
- package/dist/duckdb/types.d.ts.map +0 -1
- package/dist/duckdb/types.js +0 -19
- package/dist/duckdb/types.js.map +0 -1
- package/dist/errors.d.ts +0 -62
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -149
- package/dist/errors.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/kv/IKVService.d.ts +0 -148
- package/dist/kv/IKVService.d.ts.map +0 -1
- package/dist/kv/IKVService.js +0 -8
- package/dist/kv/IKVService.js.map +0 -1
- package/dist/kv/KVService.d.ts +0 -155
- package/dist/kv/KVService.d.ts.map +0 -1
- package/dist/kv/KVService.js +0 -419
- package/dist/kv/KVService.js.map +0 -1
- package/dist/kv/PrefixedKVService.d.ts +0 -246
- package/dist/kv/PrefixedKVService.d.ts.map +0 -1
- package/dist/kv/PrefixedKVService.js +0 -145
- package/dist/kv/PrefixedKVService.js.map +0 -1
- package/dist/kv/index.d.ts.map +0 -1
- package/dist/kv/types.d.ts +0 -204
- package/dist/kv/types.d.ts.map +0 -1
- package/dist/kv/types.js +0 -16
- package/dist/kv/types.js.map +0 -1
- package/dist/quota/TinyCloudQuota.d.ts +0 -27
- package/dist/quota/TinyCloudQuota.d.ts.map +0 -1
- package/dist/quota/TinyCloudQuota.js +0 -31
- package/dist/quota/TinyCloudQuota.js.map +0 -1
- package/dist/quota/index.d.ts +0 -3
- package/dist/quota/index.d.ts.map +0 -1
- package/dist/quota/index.js +0 -2
- package/dist/quota/index.js.map +0 -1
- package/dist/sql/DatabaseHandle.d.ts +0 -20
- package/dist/sql/DatabaseHandle.d.ts.map +0 -1
- package/dist/sql/DatabaseHandle.js +0 -27
- package/dist/sql/DatabaseHandle.js.map +0 -1
- package/dist/sql/ISQLService.d.ts +0 -67
- package/dist/sql/ISQLService.d.ts.map +0 -1
- package/dist/sql/ISQLService.js +0 -7
- package/dist/sql/ISQLService.js.map +0 -1
- package/dist/sql/SQLService.d.ts +0 -44
- package/dist/sql/SQLService.d.ts.map +0 -1
- package/dist/sql/SQLService.js +0 -216
- package/dist/sql/SQLService.js.map +0 -1
- package/dist/sql/index.d.ts.map +0 -1
- package/dist/sql/types.d.ts +0 -102
- package/dist/sql/types.d.ts.map +0 -1
- package/dist/sql/types.js +0 -21
- package/dist/sql/types.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -94
- package/dist/types.js.map +0 -1
- package/dist/types.schema.d.ts +0 -712
- package/dist/types.schema.d.ts.map +0 -1
- package/dist/types.schema.js +0 -342
- package/dist/types.schema.js.map +0 -1
- package/dist/types.schema.test.d.ts +0 -5
- package/dist/types.schema.test.d.ts.map +0 -1
- package/dist/types.schema.test.js +0 -677
- package/dist/types.schema.test.js.map +0 -1
- package/dist/vault/DataVaultService.d.ts +0 -258
- package/dist/vault/DataVaultService.d.ts.map +0 -1
- package/dist/vault/DataVaultService.js +0 -977
- package/dist/vault/DataVaultService.js.map +0 -1
- package/dist/vault/IDataVaultService.d.ts +0 -150
- package/dist/vault/IDataVaultService.d.ts.map +0 -1
- package/dist/vault/IDataVaultService.js +0 -8
- package/dist/vault/IDataVaultService.js.map +0 -1
- package/dist/vault/createVaultCrypto.d.ts +0 -16
- package/dist/vault/createVaultCrypto.d.ts.map +0 -1
- package/dist/vault/createVaultCrypto.js +0 -12
- package/dist/vault/createVaultCrypto.js.map +0 -1
- package/dist/vault/index.d.ts +0 -10
- package/dist/vault/index.d.ts.map +0 -1
- package/dist/vault/index.js +0 -11
- package/dist/vault/index.js.map +0 -1
- package/dist/vault/types.d.ts +0 -133
- package/dist/vault/types.d.ts.map +0 -1
- package/dist/vault/types.js +0 -23
- package/dist/vault/types.js.map +0 -1
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK Services - Core Types
|
|
3
|
+
*
|
|
4
|
+
* These types define the service architecture for TinyCloud SDK.
|
|
5
|
+
* Services use dependency injection via IServiceContext for platform independence.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Result type for service operations.
|
|
9
|
+
* Services return Result instead of throwing, making error handling explicit.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The success data type
|
|
12
|
+
* @template E - The error type (defaults to ServiceError)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const result = await kv.get('key');
|
|
17
|
+
* if (result.ok) {
|
|
18
|
+
* console.log(result.data);
|
|
19
|
+
* } else {
|
|
20
|
+
* console.error(result.error.code);
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
type Result<T, E = ServiceError> = {
|
|
25
|
+
ok: true;
|
|
26
|
+
data: T;
|
|
27
|
+
} | {
|
|
28
|
+
ok: false;
|
|
29
|
+
error: E;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Service error with structured information.
|
|
33
|
+
*/
|
|
34
|
+
interface ServiceError {
|
|
35
|
+
/** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
|
|
36
|
+
code: string;
|
|
37
|
+
/** Human-readable error message */
|
|
38
|
+
message: string;
|
|
39
|
+
/** Service that produced the error (e.g., 'kv', 'sql') */
|
|
40
|
+
service: string;
|
|
41
|
+
/** Original error if this wraps another error */
|
|
42
|
+
cause?: Error;
|
|
43
|
+
/** Additional metadata about the error */
|
|
44
|
+
meta?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Storage quota information returned with quota-related errors.
|
|
48
|
+
*/
|
|
49
|
+
interface StorageQuotaInfo {
|
|
50
|
+
usedBytes: number;
|
|
51
|
+
limitBytes: number;
|
|
52
|
+
service: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Standard error codes used across services.
|
|
56
|
+
*/
|
|
57
|
+
declare const ErrorCodes: {
|
|
58
|
+
readonly NOT_FOUND: "NOT_FOUND";
|
|
59
|
+
readonly AUTH_EXPIRED: "AUTH_EXPIRED";
|
|
60
|
+
readonly AUTH_REQUIRED: "AUTH_REQUIRED";
|
|
61
|
+
readonly AUTH_UNAUTHORIZED: "AUTH_UNAUTHORIZED";
|
|
62
|
+
readonly NETWORK_ERROR: "NETWORK_ERROR";
|
|
63
|
+
readonly TIMEOUT: "TIMEOUT";
|
|
64
|
+
readonly ABORTED: "ABORTED";
|
|
65
|
+
readonly INVALID_INPUT: "INVALID_INPUT";
|
|
66
|
+
readonly PERMISSION_DENIED: "PERMISSION_DENIED";
|
|
67
|
+
readonly KV_NOT_FOUND: "KV_NOT_FOUND";
|
|
68
|
+
readonly KV_WRITE_FAILED: "KV_WRITE_FAILED";
|
|
69
|
+
readonly SQL_ERROR: "SQL_ERROR";
|
|
70
|
+
readonly SQL_PERMISSION_DENIED: "SQL_PERMISSION_DENIED";
|
|
71
|
+
readonly SQL_DATABASE_NOT_FOUND: "SQL_DATABASE_NOT_FOUND";
|
|
72
|
+
readonly SQL_RESPONSE_TOO_LARGE: "SQL_RESPONSE_TOO_LARGE";
|
|
73
|
+
readonly SQL_QUOTA_EXCEEDED: "SQL_QUOTA_EXCEEDED";
|
|
74
|
+
readonly SQL_INVALID_STATEMENT: "SQL_INVALID_STATEMENT";
|
|
75
|
+
readonly SQL_SCHEMA_ERROR: "SQL_SCHEMA_ERROR";
|
|
76
|
+
readonly SQL_READONLY_VIOLATION: "SQL_READONLY_VIOLATION";
|
|
77
|
+
readonly STORAGE_QUOTA_EXCEEDED: "STORAGE_QUOTA_EXCEEDED";
|
|
78
|
+
readonly STORAGE_LIMIT_REACHED: "STORAGE_LIMIT_REACHED";
|
|
79
|
+
readonly DUCKDB_ERROR: "DUCKDB_ERROR";
|
|
80
|
+
readonly DUCKDB_PERMISSION_DENIED: "DUCKDB_PERMISSION_DENIED";
|
|
81
|
+
readonly DUCKDB_DATABASE_NOT_FOUND: "DUCKDB_DATABASE_NOT_FOUND";
|
|
82
|
+
readonly DUCKDB_RESPONSE_TOO_LARGE: "DUCKDB_RESPONSE_TOO_LARGE";
|
|
83
|
+
readonly DUCKDB_QUOTA_EXCEEDED: "DUCKDB_QUOTA_EXCEEDED";
|
|
84
|
+
readonly DUCKDB_INVALID_STATEMENT: "DUCKDB_INVALID_STATEMENT";
|
|
85
|
+
readonly DUCKDB_SCHEMA_ERROR: "DUCKDB_SCHEMA_ERROR";
|
|
86
|
+
readonly DUCKDB_READONLY_VIOLATION: "DUCKDB_READONLY_VIOLATION";
|
|
87
|
+
};
|
|
88
|
+
type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
89
|
+
/**
|
|
90
|
+
* Session data required for authenticated service operations.
|
|
91
|
+
* Both TinyCloudSession and web-sdk Session can be cast to this interface.
|
|
92
|
+
*/
|
|
93
|
+
interface ServiceSession {
|
|
94
|
+
/** The delegation header containing the UCAN */
|
|
95
|
+
delegationHeader: {
|
|
96
|
+
Authorization: string;
|
|
97
|
+
};
|
|
98
|
+
/** The delegation CID */
|
|
99
|
+
delegationCid: string;
|
|
100
|
+
/** The space ID for this session */
|
|
101
|
+
spaceId: string;
|
|
102
|
+
/** The verification method DID */
|
|
103
|
+
verificationMethod: string;
|
|
104
|
+
/** The session key JWK (required for invoke) */
|
|
105
|
+
jwk: object;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Headers type - compatible with both browser and Node.js.
|
|
109
|
+
*/
|
|
110
|
+
type ServiceHeaders = Record<string, string> | [string, string][];
|
|
111
|
+
/**
|
|
112
|
+
* A single fact object to include in the UCAN invocation.
|
|
113
|
+
* Facts are key-value objects that the server reads from the UCAN facts field.
|
|
114
|
+
*/
|
|
115
|
+
interface InvocationFact {
|
|
116
|
+
[key: string]: unknown;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Facts to include in the UCAN invocation.
|
|
120
|
+
* This is an array of fact objects per the UCAN spec.
|
|
121
|
+
* Used to pass additional parameters that the server reads from the UCAN facts field.
|
|
122
|
+
*/
|
|
123
|
+
type InvocationFacts = InvocationFact[];
|
|
124
|
+
/**
|
|
125
|
+
* Invoke function signature - platform-specific implementation injected via DI.
|
|
126
|
+
* Both node-sdk-wasm and web-sdk-wasm export this with identical signature.
|
|
127
|
+
*
|
|
128
|
+
* @param session - The service session with delegation data
|
|
129
|
+
* @param service - Service name (e.g., "kv")
|
|
130
|
+
* @param path - Resource path or key
|
|
131
|
+
* @param action - Action to perform (e.g., "tinycloud.kv/get")
|
|
132
|
+
* @param facts - Optional facts to include in the UCAN (e.g., for capabilities/read params)
|
|
133
|
+
* @returns Headers to include in the request
|
|
134
|
+
*/
|
|
135
|
+
type InvokeFunction = (session: ServiceSession, service: string, path: string, action: string, facts?: InvocationFacts) => ServiceHeaders;
|
|
136
|
+
/**
|
|
137
|
+
* Fetch request options - compatible with standard fetch API.
|
|
138
|
+
*/
|
|
139
|
+
interface FetchRequestInit {
|
|
140
|
+
method?: string;
|
|
141
|
+
headers?: ServiceHeaders;
|
|
142
|
+
body?: Blob | string;
|
|
143
|
+
signal?: AbortSignal;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Fetch response interface - compatible with standard Response.
|
|
147
|
+
*/
|
|
148
|
+
interface FetchResponse {
|
|
149
|
+
ok: boolean;
|
|
150
|
+
status: number;
|
|
151
|
+
statusText: string;
|
|
152
|
+
headers: {
|
|
153
|
+
get(name: string): string | null;
|
|
154
|
+
};
|
|
155
|
+
json(): Promise<unknown>;
|
|
156
|
+
text(): Promise<string>;
|
|
157
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
158
|
+
blob(): Promise<Blob>;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Fetch function signature - allows for custom fetch implementations.
|
|
162
|
+
* Compatible with both browser fetch and Node.js fetch.
|
|
163
|
+
*/
|
|
164
|
+
type FetchFunction = (url: string, init?: FetchRequestInit) => Promise<FetchResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Configuration for automatic retry of failed requests.
|
|
167
|
+
*/
|
|
168
|
+
interface RetryPolicy {
|
|
169
|
+
/** Maximum number of attempts (including initial) */
|
|
170
|
+
maxAttempts: number;
|
|
171
|
+
/** Backoff strategy between retries */
|
|
172
|
+
backoff: "none" | "linear" | "exponential";
|
|
173
|
+
/** Base delay in milliseconds for backoff calculation */
|
|
174
|
+
baseDelayMs: number;
|
|
175
|
+
/** Maximum delay in milliseconds between retries */
|
|
176
|
+
maxDelayMs: number;
|
|
177
|
+
/** Error codes that should trigger a retry */
|
|
178
|
+
retryableErrors: string[];
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Default retry policy.
|
|
182
|
+
*/
|
|
183
|
+
declare const defaultRetryPolicy: RetryPolicy;
|
|
184
|
+
/**
|
|
185
|
+
* Event handler function type.
|
|
186
|
+
*/
|
|
187
|
+
type EventHandler = (data: unknown) => void;
|
|
188
|
+
/**
|
|
189
|
+
* Service interface - base contract for all services.
|
|
190
|
+
*/
|
|
191
|
+
interface IService {
|
|
192
|
+
/** Initialize service with context */
|
|
193
|
+
initialize(context: IServiceContext): void;
|
|
194
|
+
/** Called when session changes (sign-in, sign-out, refresh) */
|
|
195
|
+
onSessionChange(session: ServiceSession | null): void;
|
|
196
|
+
/** Called when SDK signs out - should abort pending operations */
|
|
197
|
+
onSignOut(): void;
|
|
198
|
+
/** Service-specific configuration */
|
|
199
|
+
readonly config: Record<string, unknown>;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Context provided to services for accessing platform dependencies.
|
|
203
|
+
* The SDK creates this context and passes it to services during initialization.
|
|
204
|
+
*/
|
|
205
|
+
interface IServiceContext {
|
|
206
|
+
/** Current active session, or null if not authenticated */
|
|
207
|
+
readonly session: ServiceSession | null;
|
|
208
|
+
/** Whether there is an active authenticated session */
|
|
209
|
+
readonly isAuthenticated: boolean;
|
|
210
|
+
/** Platform-specific invoke function from WASM binding */
|
|
211
|
+
readonly invoke: InvokeFunction;
|
|
212
|
+
/** Fetch function (defaults to globalThis.fetch) */
|
|
213
|
+
readonly fetch: FetchFunction;
|
|
214
|
+
/** Available TinyCloud host URLs */
|
|
215
|
+
readonly hosts: string[];
|
|
216
|
+
/** Get another registered service by name */
|
|
217
|
+
getService<T extends IService>(name: string): T | undefined;
|
|
218
|
+
/** Emit a telemetry event */
|
|
219
|
+
emit(event: string, data: unknown): void;
|
|
220
|
+
/** Subscribe to events */
|
|
221
|
+
on(event: string, handler: EventHandler): () => void;
|
|
222
|
+
/** Abort signal that fires when SDK signs out */
|
|
223
|
+
readonly abortSignal: AbortSignal;
|
|
224
|
+
/** Retry policy for failed requests */
|
|
225
|
+
readonly retryPolicy: RetryPolicy;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Event emitted before a service request.
|
|
229
|
+
*/
|
|
230
|
+
interface ServiceRequestEvent {
|
|
231
|
+
service: string;
|
|
232
|
+
action: string;
|
|
233
|
+
key?: string;
|
|
234
|
+
timestamp: number;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Event emitted after a service response.
|
|
238
|
+
*/
|
|
239
|
+
interface ServiceResponseEvent {
|
|
240
|
+
service: string;
|
|
241
|
+
action: string;
|
|
242
|
+
ok: boolean;
|
|
243
|
+
duration: number;
|
|
244
|
+
status?: number;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Event emitted on service error.
|
|
248
|
+
*/
|
|
249
|
+
interface ServiceErrorEvent {
|
|
250
|
+
service: string;
|
|
251
|
+
error: ServiceError;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Event emitted on retry attempt.
|
|
255
|
+
*/
|
|
256
|
+
interface ServiceRetryEvent {
|
|
257
|
+
service: string;
|
|
258
|
+
attempt: number;
|
|
259
|
+
maxAttempts: number;
|
|
260
|
+
error: ServiceError;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Telemetry event names.
|
|
264
|
+
*/
|
|
265
|
+
declare const TelemetryEvents: {
|
|
266
|
+
readonly SERVICE_REQUEST: "service.request";
|
|
267
|
+
readonly SERVICE_RESPONSE: "service.response";
|
|
268
|
+
readonly SERVICE_ERROR: "service.error";
|
|
269
|
+
readonly SERVICE_RETRY: "service.retry";
|
|
270
|
+
readonly SESSION_CHANGED: "session.changed";
|
|
271
|
+
readonly SESSION_EXPIRED: "session.expired";
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* Create a success result.
|
|
275
|
+
*/
|
|
276
|
+
declare function ok<T>(data: T): Result<T>;
|
|
277
|
+
/**
|
|
278
|
+
* Create an error result.
|
|
279
|
+
*/
|
|
280
|
+
declare function err<E = ServiceError>(error: E): Result<never, E>;
|
|
281
|
+
/**
|
|
282
|
+
* Create a ServiceError.
|
|
283
|
+
*/
|
|
284
|
+
declare function serviceError(code: string, message: string, service: string, options?: {
|
|
285
|
+
cause?: Error;
|
|
286
|
+
meta?: Record<string, unknown>;
|
|
287
|
+
}): ServiceError;
|
|
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 };
|