@debros/network-ts-sdk 0.4.2 → 0.6.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 +1 -1
- package/README.md +1 -0
- package/dist/index.d.ts +60 -16
- package/dist/index.js +82 -11
- package/dist/index.js.map +1 -1
- package/package.json +26 -3
- package/src/auth/index.ts +3 -0
- package/src/cache/index.ts +14 -0
- package/src/core/http.ts +101 -6
- package/src/core/index.ts +10 -0
- package/src/core/interfaces/IAuthStrategy.ts +28 -0
- package/src/core/interfaces/IHttpTransport.ts +73 -0
- package/src/core/interfaces/IRetryPolicy.ts +20 -0
- package/src/core/interfaces/IWebSocketClient.ts +60 -0
- package/src/core/interfaces/index.ts +4 -0
- package/src/core/transport/AuthHeaderStrategy.ts +108 -0
- package/src/core/transport/RequestLogger.ts +116 -0
- package/src/core/transport/RequestRetryPolicy.ts +53 -0
- package/src/core/transport/TLSConfiguration.ts +53 -0
- package/src/core/transport/index.ts +4 -0
- package/src/core/ws.ts +39 -3
- package/src/db/index.ts +13 -0
- package/src/functions/index.ts +2 -0
- package/src/index.ts +10 -1
- package/src/network/index.ts +7 -0
- package/src/pubsub/index.ts +12 -0
- package/src/storage/index.ts +7 -0
- package/src/utils/codec.ts +68 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/platform.ts +44 -0
- package/src/utils/retry.ts +58 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -367,6 +367,7 @@ interface ClientConfig {
|
|
|
367
367
|
timeout?: number; // Request timeout in ms (default: 30000)
|
|
368
368
|
maxRetries?: number; // Max retry attempts (default: 3)
|
|
369
369
|
retryDelayMs?: number; // Delay between retries (default: 1000)
|
|
370
|
+
debug?: boolean; // Enable debug logging with full SQL queries (default: false)
|
|
370
371
|
storage?: StorageAdapter; // For persisting JWT/API key (default: MemoryStorage)
|
|
371
372
|
wsConfig?: Partial<WSClientConfig>; // WebSocket configuration
|
|
372
373
|
fetch?: typeof fetch; // Custom fetch implementation
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,49 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
2
|
|
|
3
|
+
declare class SDKError extends Error {
|
|
4
|
+
readonly httpStatus: number;
|
|
5
|
+
readonly code: string;
|
|
6
|
+
readonly details: Record<string, any>;
|
|
7
|
+
constructor(message: string, httpStatus?: number, code?: string, details?: Record<string, any>);
|
|
8
|
+
static fromResponse(status: number, body: any, message?: string): SDKError;
|
|
9
|
+
toJSON(): {
|
|
10
|
+
name: string;
|
|
11
|
+
message: string;
|
|
12
|
+
httpStatus: number;
|
|
13
|
+
code: string;
|
|
14
|
+
details: Record<string, any>;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Context provided to the onNetworkError callback
|
|
20
|
+
*/
|
|
21
|
+
interface NetworkErrorContext {
|
|
22
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "WS";
|
|
23
|
+
path: string;
|
|
24
|
+
isRetry: boolean;
|
|
25
|
+
attempt: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Callback invoked when a network error occurs.
|
|
29
|
+
* Use this to trigger gateway failover or other error handling.
|
|
30
|
+
*/
|
|
31
|
+
type NetworkErrorCallback = (error: SDKError, context: NetworkErrorContext) => void;
|
|
3
32
|
interface HttpClientConfig {
|
|
4
33
|
baseURL: string;
|
|
5
34
|
timeout?: number;
|
|
6
35
|
maxRetries?: number;
|
|
7
36
|
retryDelayMs?: number;
|
|
8
37
|
fetch?: typeof fetch;
|
|
38
|
+
/**
|
|
39
|
+
* Enable debug logging (includes full SQL queries and args). Default: false
|
|
40
|
+
*/
|
|
41
|
+
debug?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Callback invoked on network errors (after all retries exhausted).
|
|
44
|
+
* Use this to trigger gateway failover at the application layer.
|
|
45
|
+
*/
|
|
46
|
+
onNetworkError?: NetworkErrorCallback;
|
|
9
47
|
}
|
|
10
48
|
declare class HttpClient {
|
|
11
49
|
private baseURL;
|
|
@@ -15,7 +53,13 @@ declare class HttpClient {
|
|
|
15
53
|
private fetch;
|
|
16
54
|
private apiKey?;
|
|
17
55
|
private jwt?;
|
|
56
|
+
private debug;
|
|
57
|
+
private onNetworkError?;
|
|
18
58
|
constructor(config: HttpClientConfig);
|
|
59
|
+
/**
|
|
60
|
+
* Set the network error callback
|
|
61
|
+
*/
|
|
62
|
+
setOnNetworkError(callback: NetworkErrorCallback | undefined): void;
|
|
19
63
|
setApiKey(apiKey?: string): void;
|
|
20
64
|
setJwt(jwt?: string): void;
|
|
21
65
|
private getAuthHeaders;
|
|
@@ -287,6 +331,11 @@ interface WSClientConfig {
|
|
|
287
331
|
timeout?: number;
|
|
288
332
|
authToken?: string;
|
|
289
333
|
WebSocket?: typeof WebSocket;
|
|
334
|
+
/**
|
|
335
|
+
* Callback invoked on WebSocket errors.
|
|
336
|
+
* Use this to trigger gateway failover at the application layer.
|
|
337
|
+
*/
|
|
338
|
+
onNetworkError?: NetworkErrorCallback;
|
|
290
339
|
}
|
|
291
340
|
type WSMessageHandler = (data: string) => void;
|
|
292
341
|
type WSErrorHandler = (error: Error) => void;
|
|
@@ -302,6 +351,7 @@ declare class WSClient {
|
|
|
302
351
|
private timeout;
|
|
303
352
|
private authToken?;
|
|
304
353
|
private WebSocketClass;
|
|
354
|
+
private onNetworkError?;
|
|
305
355
|
private ws?;
|
|
306
356
|
private messageHandlers;
|
|
307
357
|
private errorHandlers;
|
|
@@ -309,6 +359,10 @@ declare class WSClient {
|
|
|
309
359
|
private openHandlers;
|
|
310
360
|
private isClosed;
|
|
311
361
|
constructor(config: WSClientConfig);
|
|
362
|
+
/**
|
|
363
|
+
* Set the network error callback
|
|
364
|
+
*/
|
|
365
|
+
setOnNetworkError(callback: NetworkErrorCallback | undefined): void;
|
|
312
366
|
/**
|
|
313
367
|
* Get the current WebSocket URL
|
|
314
368
|
*/
|
|
@@ -769,21 +823,6 @@ declare class FunctionsClient {
|
|
|
769
823
|
invoke<TInput = any, TOutput = any>(functionName: string, input: TInput): Promise<TOutput>;
|
|
770
824
|
}
|
|
771
825
|
|
|
772
|
-
declare class SDKError extends Error {
|
|
773
|
-
readonly httpStatus: number;
|
|
774
|
-
readonly code: string;
|
|
775
|
-
readonly details: Record<string, any>;
|
|
776
|
-
constructor(message: string, httpStatus?: number, code?: string, details?: Record<string, any>);
|
|
777
|
-
static fromResponse(status: number, body: any, message?: string): SDKError;
|
|
778
|
-
toJSON(): {
|
|
779
|
-
name: string;
|
|
780
|
-
message: string;
|
|
781
|
-
httpStatus: number;
|
|
782
|
-
code: string;
|
|
783
|
-
details: Record<string, any>;
|
|
784
|
-
};
|
|
785
|
-
}
|
|
786
|
-
|
|
787
826
|
/**
|
|
788
827
|
* Serverless Functions Types
|
|
789
828
|
* Type definitions for calling serverless functions on the Orama Network
|
|
@@ -811,6 +850,11 @@ interface ClientConfig extends Omit<HttpClientConfig, "fetch"> {
|
|
|
811
850
|
wsConfig?: Partial<Omit<WSClientConfig, "wsURL">>;
|
|
812
851
|
functionsConfig?: FunctionsClientConfig;
|
|
813
852
|
fetch?: typeof fetch;
|
|
853
|
+
/**
|
|
854
|
+
* Callback invoked on network errors (HTTP and WebSocket).
|
|
855
|
+
* Use this to trigger gateway failover at the application layer.
|
|
856
|
+
*/
|
|
857
|
+
onNetworkError?: NetworkErrorCallback;
|
|
814
858
|
}
|
|
815
859
|
interface Client {
|
|
816
860
|
auth: AuthClient;
|
|
@@ -823,4 +867,4 @@ interface Client {
|
|
|
823
867
|
}
|
|
824
868
|
declare function createClient(config: ClientConfig): Client;
|
|
825
869
|
|
|
826
|
-
export { AuthClient, type AuthConfig, CacheClient, type CacheDeleteRequest, type CacheDeleteResponse, type CacheGetRequest, type CacheGetResponse, type CacheHealthResponse, type CacheMultiGetRequest, type CacheMultiGetResponse, type CachePutRequest, type CachePutResponse, type CacheScanRequest, type CacheScanResponse, type Client, type ClientConfig, type CloseHandler, type ColumnDefinition, DBClient, type Entity, type ErrorHandler, type FindOptions, type FunctionResponse, FunctionsClient, type FunctionsClientConfig, HttpClient, LocalStorageAdapter, MemoryStorage, type MessageHandler, NetworkClient, type NetworkStatus, type PeerInfo, type PresenceMember, type PresenceOptions, type PresenceResponse, type ProxyRequest, type ProxyResponse, PubSubClient, type PubSubMessage, QueryBuilder, type QueryResponse, Repository, SDKError, type SelectOptions, type StorageAdapter, StorageClient, type StoragePinRequest, type StoragePinResponse, type StorageStatus, type StorageUploadResponse, type SubscribeOptions, Subscription, type SuccessResponse, type TransactionOp, type TransactionRequest, WSClient, type WhoAmI, createClient, extractPrimaryKey, extractTableName };
|
|
870
|
+
export { AuthClient, type AuthConfig, CacheClient, type CacheDeleteRequest, type CacheDeleteResponse, type CacheGetRequest, type CacheGetResponse, type CacheHealthResponse, type CacheMultiGetRequest, type CacheMultiGetResponse, type CachePutRequest, type CachePutResponse, type CacheScanRequest, type CacheScanResponse, type Client, type ClientConfig, type CloseHandler, type ColumnDefinition, DBClient, type Entity, type ErrorHandler, type FindOptions, type FunctionResponse, FunctionsClient, type FunctionsClientConfig, HttpClient, LocalStorageAdapter, MemoryStorage, type MessageHandler, NetworkClient, type NetworkErrorCallback, type NetworkErrorContext, type NetworkStatus, type PeerInfo, type PresenceMember, type PresenceOptions, type PresenceResponse, type ProxyRequest, type ProxyResponse, PubSubClient, type PubSubMessage, QueryBuilder, type QueryResponse, Repository, SDKError, type SelectOptions, type StorageAdapter, StorageClient, type StoragePinRequest, type StoragePinResponse, type StorageStatus, type StorageUploadResponse, type SubscribeOptions, Subscription, type SuccessResponse, type TransactionOp, type TransactionRequest, WSClient, type WhoAmI, createClient, extractPrimaryKey, extractTableName };
|
package/dist/index.js
CHANGED
|
@@ -40,6 +40,14 @@ var HttpClient = class {
|
|
|
40
40
|
this.maxRetries = config.maxRetries ?? 3;
|
|
41
41
|
this.retryDelayMs = config.retryDelayMs ?? 1e3;
|
|
42
42
|
this.fetch = config.fetch ?? createFetchWithTLSConfig();
|
|
43
|
+
this.debug = config.debug ?? false;
|
|
44
|
+
this.onNetworkError = config.onNetworkError;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Set the network error callback
|
|
48
|
+
*/
|
|
49
|
+
setOnNetworkError(callback) {
|
|
50
|
+
this.onNetworkError = callback;
|
|
43
51
|
}
|
|
44
52
|
setApiKey(apiKey) {
|
|
45
53
|
this.apiKey = apiKey;
|
|
@@ -167,7 +175,7 @@ var HttpClient = class {
|
|
|
167
175
|
const logMessage = `[HttpClient] ${method} ${path} completed in ${duration.toFixed(
|
|
168
176
|
2
|
|
169
177
|
)}ms`;
|
|
170
|
-
if (queryDetails) {
|
|
178
|
+
if (queryDetails && this.debug) {
|
|
171
179
|
console.log(logMessage);
|
|
172
180
|
console.log(`[HttpClient] ${queryDetails}`);
|
|
173
181
|
} else {
|
|
@@ -190,11 +198,26 @@ var HttpClient = class {
|
|
|
190
198
|
2
|
|
191
199
|
)}ms:`;
|
|
192
200
|
console.error(errorMessage, error);
|
|
193
|
-
if (queryDetails) {
|
|
201
|
+
if (queryDetails && this.debug) {
|
|
194
202
|
console.error(`[HttpClient] ${queryDetails}`);
|
|
195
203
|
}
|
|
196
204
|
}
|
|
197
205
|
}
|
|
206
|
+
if (this.onNetworkError) {
|
|
207
|
+
const sdkError = error instanceof SDKError ? error : new SDKError(
|
|
208
|
+
error instanceof Error ? error.message : String(error),
|
|
209
|
+
0,
|
|
210
|
+
// httpStatus 0 indicates network-level failure
|
|
211
|
+
"NETWORK_ERROR"
|
|
212
|
+
);
|
|
213
|
+
this.onNetworkError(sdkError, {
|
|
214
|
+
method,
|
|
215
|
+
path,
|
|
216
|
+
isRetry: false,
|
|
217
|
+
attempt: this.maxRetries
|
|
218
|
+
// All retries exhausted
|
|
219
|
+
});
|
|
220
|
+
}
|
|
198
221
|
throw error;
|
|
199
222
|
} finally {
|
|
200
223
|
clearTimeout(timeoutId);
|
|
@@ -291,6 +314,19 @@ var HttpClient = class {
|
|
|
291
314
|
error
|
|
292
315
|
);
|
|
293
316
|
}
|
|
317
|
+
if (this.onNetworkError) {
|
|
318
|
+
const sdkError = error instanceof SDKError ? error : new SDKError(
|
|
319
|
+
error instanceof Error ? error.message : String(error),
|
|
320
|
+
0,
|
|
321
|
+
"NETWORK_ERROR"
|
|
322
|
+
);
|
|
323
|
+
this.onNetworkError(sdkError, {
|
|
324
|
+
method: "POST",
|
|
325
|
+
path,
|
|
326
|
+
isRetry: false,
|
|
327
|
+
attempt: this.maxRetries
|
|
328
|
+
});
|
|
329
|
+
}
|
|
294
330
|
throw error;
|
|
295
331
|
} finally {
|
|
296
332
|
clearTimeout(timeoutId);
|
|
@@ -315,16 +351,26 @@ var HttpClient = class {
|
|
|
315
351
|
const response = await this.fetch(url.toString(), fetchOptions);
|
|
316
352
|
if (!response.ok) {
|
|
317
353
|
clearTimeout(timeoutId);
|
|
318
|
-
const
|
|
354
|
+
const errorBody = await response.json().catch(() => ({
|
|
319
355
|
error: response.statusText
|
|
320
356
|
}));
|
|
321
|
-
throw SDKError.fromResponse(response.status,
|
|
357
|
+
throw SDKError.fromResponse(response.status, errorBody);
|
|
322
358
|
}
|
|
323
359
|
return response;
|
|
324
360
|
} catch (error) {
|
|
325
361
|
clearTimeout(timeoutId);
|
|
326
|
-
if (
|
|
327
|
-
|
|
362
|
+
if (this.onNetworkError) {
|
|
363
|
+
const sdkError = error instanceof SDKError ? error : new SDKError(
|
|
364
|
+
error instanceof Error ? error.message : String(error),
|
|
365
|
+
0,
|
|
366
|
+
"NETWORK_ERROR"
|
|
367
|
+
);
|
|
368
|
+
this.onNetworkError(sdkError, {
|
|
369
|
+
method: "GET",
|
|
370
|
+
path,
|
|
371
|
+
isRetry: false,
|
|
372
|
+
attempt: 0
|
|
373
|
+
});
|
|
328
374
|
}
|
|
329
375
|
throw error;
|
|
330
376
|
}
|
|
@@ -814,6 +860,13 @@ var WSClient = class {
|
|
|
814
860
|
this.timeout = config.timeout ?? 3e4;
|
|
815
861
|
this.authToken = config.authToken;
|
|
816
862
|
this.WebSocketClass = config.WebSocket ?? WebSocket;
|
|
863
|
+
this.onNetworkError = config.onNetworkError;
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Set the network error callback
|
|
867
|
+
*/
|
|
868
|
+
setOnNetworkError(callback) {
|
|
869
|
+
this.onNetworkError = callback;
|
|
817
870
|
}
|
|
818
871
|
/**
|
|
819
872
|
* Get the current WebSocket URL
|
|
@@ -832,9 +885,16 @@ var WSClient = class {
|
|
|
832
885
|
this.isClosed = false;
|
|
833
886
|
const timeout = setTimeout(() => {
|
|
834
887
|
this.ws?.close();
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
888
|
+
const error = new SDKError("WebSocket connection timeout", 408, "WS_TIMEOUT");
|
|
889
|
+
if (this.onNetworkError) {
|
|
890
|
+
this.onNetworkError(error, {
|
|
891
|
+
method: "WS",
|
|
892
|
+
path: this.wsURL,
|
|
893
|
+
isRetry: false,
|
|
894
|
+
attempt: 0
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
reject(error);
|
|
838
898
|
}, this.timeout);
|
|
839
899
|
this.ws.addEventListener("open", () => {
|
|
840
900
|
clearTimeout(timeout);
|
|
@@ -850,6 +910,14 @@ var WSClient = class {
|
|
|
850
910
|
console.error("[WSClient] WebSocket error:", event);
|
|
851
911
|
clearTimeout(timeout);
|
|
852
912
|
const error = new SDKError("WebSocket error", 500, "WS_ERROR", event);
|
|
913
|
+
if (this.onNetworkError) {
|
|
914
|
+
this.onNetworkError(error, {
|
|
915
|
+
method: "WS",
|
|
916
|
+
path: this.wsURL,
|
|
917
|
+
isRetry: false,
|
|
918
|
+
attempt: 0
|
|
919
|
+
});
|
|
920
|
+
}
|
|
853
921
|
this.errorHandlers.forEach((handler) => handler(error));
|
|
854
922
|
reject(error);
|
|
855
923
|
});
|
|
@@ -1627,7 +1695,9 @@ function createClient(config) {
|
|
|
1627
1695
|
timeout: config.timeout,
|
|
1628
1696
|
maxRetries: config.maxRetries,
|
|
1629
1697
|
retryDelayMs: config.retryDelayMs,
|
|
1630
|
-
|
|
1698
|
+
debug: config.debug,
|
|
1699
|
+
fetch: config.fetch,
|
|
1700
|
+
onNetworkError: config.onNetworkError
|
|
1631
1701
|
});
|
|
1632
1702
|
const auth = new AuthClient({
|
|
1633
1703
|
httpClient,
|
|
@@ -1639,7 +1709,8 @@ function createClient(config) {
|
|
|
1639
1709
|
const db = new DBClient(httpClient);
|
|
1640
1710
|
const pubsub = new PubSubClient(httpClient, {
|
|
1641
1711
|
...config.wsConfig,
|
|
1642
|
-
wsURL
|
|
1712
|
+
wsURL,
|
|
1713
|
+
onNetworkError: config.onNetworkError
|
|
1643
1714
|
});
|
|
1644
1715
|
const network = new NetworkClient(httpClient);
|
|
1645
1716
|
const cache = new CacheClient(httpClient);
|