bridgex 1.0.0 → 2.0.1

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 (47) hide show
  1. package/README.md +525 -0
  2. package/dist/CircuitBreaker.d.ts +25 -0
  3. package/dist/CircuitBreaker.js +58 -0
  4. package/dist/CredentialProvisioner.d.ts +27 -0
  5. package/dist/CredentialProvisioner.js +43 -0
  6. package/dist/MessageFormatter.d.ts +13 -1
  7. package/dist/MessageFormatter.js +30 -6
  8. package/dist/PluginManager.d.ts +77 -0
  9. package/dist/PluginManager.js +101 -0
  10. package/dist/RetryHandler.d.ts +8 -1
  11. package/dist/RetryHandler.js +28 -6
  12. package/dist/SMSClient.d.ts +65 -0
  13. package/dist/SMSClient.js +236 -0
  14. package/dist/SMSQueue.d.ts +82 -0
  15. package/dist/SMSQueue.js +207 -0
  16. package/dist/SMSScheduler.d.ts +75 -0
  17. package/dist/SMSScheduler.js +196 -0
  18. package/dist/SMSService.d.ts +41 -0
  19. package/dist/SMSService.js +214 -0
  20. package/dist/SendConfig.d.ts +90 -0
  21. package/dist/SendConfig.js +165 -0
  22. package/dist/errors.d.ts +18 -1
  23. package/dist/errors.js +28 -5
  24. package/dist/helpers.d.ts +3 -0
  25. package/dist/helpers.js +23 -0
  26. package/dist/main.d.ts +20 -1
  27. package/dist/main.js +19 -1
  28. package/dist/types.d.ts +43 -0
  29. package/dist/types.js +1 -0
  30. package/package.json +1 -1
  31. package/src/CircuitBreaker.ts +81 -0
  32. package/src/CredentialProvisioner.ts +68 -0
  33. package/src/MessageFormatter.ts +38 -7
  34. package/src/PluginManager.ts +155 -0
  35. package/src/RetryHandler.ts +42 -9
  36. package/src/SMSClient.ts +308 -0
  37. package/src/SMSQueue.ts +281 -0
  38. package/src/SMSScheduler.ts +250 -0
  39. package/src/SMSService.ts +254 -0
  40. package/src/SendConfig.ts +208 -0
  41. package/src/errors.ts +40 -6
  42. package/src/helpers.ts +31 -0
  43. package/src/main.ts +61 -1
  44. package/src/types.ts +33 -0
  45. package/src/client/SMSManager.ts +0 -67
  46. package/src/client/types.ts +0 -24
  47. package/src/help.ts +0 -3
package/src/errors.ts CHANGED
@@ -1,37 +1,71 @@
1
1
  class SMSClientError extends Error {
2
2
  public code: string;
3
3
  public details?: unknown;
4
+ public isClientError: boolean;
5
+ public isServerError: boolean;
4
6
 
5
- constructor(message: string, code: string, details?: unknown) {
7
+ constructor(
8
+ message: string,
9
+ code: string,
10
+ details?: unknown,
11
+ isClientError = false,
12
+ isServerError = false,
13
+ ) {
6
14
  super(message);
7
15
  this.name = this.constructor.name;
8
16
  this.code = code;
9
17
  this.details = details;
10
-
18
+ this.isClientError = isClientError;
19
+ this.isServerError = isServerError;
11
20
  Object.setPrototypeOf(this, new.target.prototype);
12
21
  }
22
+
23
+ toJSON() {
24
+ return {
25
+ name: this.name,
26
+ message: this.message,
27
+ code: this.code,
28
+ details: this.details,
29
+ isClientError: this.isClientError,
30
+ isServerError: this.isServerError,
31
+ };
32
+ }
13
33
  }
14
34
 
15
35
  export class ServerError extends SMSClientError {
16
36
  constructor(message: string, details?: unknown) {
17
- super(message, "SERVER_ERROR", details);
37
+ super(message, "SERVER_ERROR", details, false, true);
18
38
  }
19
39
  }
20
40
 
21
41
  export class ValidationError extends SMSClientError {
22
42
  constructor(message: string, details?: unknown) {
23
- super(message, "VALIDATION_ERROR", details);
43
+ super(message, "VALIDATION_ERROR", details, true, false);
24
44
  }
25
45
  }
26
46
 
27
47
  export class TemplateError extends SMSClientError {
28
48
  constructor(message: string, details?: unknown) {
29
- super(message, "TEMPLATE_ERROR", details);
49
+ super(message, "TEMPLATE_ERROR", details, true, false);
30
50
  }
31
51
  }
32
52
 
33
53
  export class NetworkError extends SMSClientError {
34
54
  constructor(message: string, details?: unknown) {
35
- super(message, "NETWORK_ERROR", details);
55
+ super(message, "NETWORK_ERROR", details, false, false);
56
+ }
57
+ }
58
+
59
+ export class RateLimitError extends SMSClientError {
60
+ public retryAfter?: number;
61
+ constructor(message: string, retryAfter?: number, details?: unknown) {
62
+ super(message, "RATE_LIMIT_ERROR", details, false, true);
63
+ this.retryAfter = retryAfter;
64
+ }
65
+ }
66
+
67
+ export class CircuitOpenError extends SMSClientError {
68
+ constructor(message: string, details?: unknown) {
69
+ super(message, "CIRCUIT_OPEN", details, false, false);
36
70
  }
37
71
  }
package/src/helpers.ts ADDED
@@ -0,0 +1,31 @@
1
+ export function isObject(value: unknown): value is Record<string, unknown> {
2
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3
+ }
4
+
5
+ export function flattenObject(
6
+ obj: Record<string, unknown>,
7
+ prefix = "",
8
+ separator = ".",
9
+ ): Record<string, string> {
10
+ const result: Record<string, string> = {};
11
+
12
+ for (const [key, value] of Object.entries(obj)) {
13
+ const fullKey = prefix ? `${prefix}${separator}${key}` : key;
14
+
15
+ if (isObject(value)) {
16
+ Object.assign(result, flattenObject(value, fullKey, separator));
17
+ } else if (value !== null && value !== undefined) {
18
+ result[fullKey] = String(value);
19
+ }
20
+ }
21
+
22
+ return result;
23
+ }
24
+
25
+ export function chunkArray<T>(array: T[], size: number): T[][] {
26
+ const chunks: T[][] = [];
27
+ for (let i = 0; i < array.length; i += size) {
28
+ chunks.push(array.slice(i, i + size));
29
+ }
30
+ return chunks;
31
+ }
package/src/main.ts CHANGED
@@ -1 +1,61 @@
1
- export { SMSClient } from "./client/SMSManager.js";
1
+ // Core client
2
+ export { default as SMSClient } from "./SMSClient.js";
3
+ export type { SMSClientOptions } from "./SMSClient.js";
4
+
5
+ // Config builder
6
+ export { default as SendConfig } from "./SendConfig.js";
7
+ export type { SendMeta } from "./SendConfig.js";
8
+
9
+ // Microservice
10
+ export { default as SMSService } from "./SMSService.js";
11
+ export type { SMSServiceOptions } from "./SMSService.js";
12
+
13
+ // Queue & Scheduler
14
+ export { default as SMSQueue } from "./SMSQueue.js";
15
+ export type { QueueOptions, QueueStats, QueueJob } from "./SMSQueue.js";
16
+
17
+ export { default as SMSScheduler } from "./SMSScheduler.js";
18
+ export type { ScheduledJob } from "./SMSScheduler.js";
19
+
20
+ // Plugins
21
+ export {
22
+ default as PluginManager,
23
+ LoggerPlugin,
24
+ MetricsPlugin,
25
+ } from "./PluginManager.js";
26
+ export type { Plugin, PluginHooks, MetricsSnapshot } from "./PluginManager.js";
27
+
28
+ // Credential provisioning
29
+ export {
30
+ default as CredentialProvisioner,
31
+ type ProvisionOptions,
32
+ type Credentials,
33
+ } from "./CredentialProvisioner.js";
34
+
35
+ // Lower-level building blocks (advanced use)
36
+ export { default as CircuitBreaker } from "./CircuitBreaker.js";
37
+ export type { CircuitBreakerOptions } from "./CircuitBreaker.js";
38
+
39
+ export { default as RetryHandler } from "./RetryHandler.js";
40
+ export type { RetryOptions } from "./RetryHandler.js";
41
+
42
+ export { default as MessageFormatter } from "./MessageFormatter.js";
43
+
44
+ // Errors
45
+ export {
46
+ ServerError,
47
+ ValidationError,
48
+ TemplateError,
49
+ NetworkError,
50
+ RateLimitError,
51
+ CircuitOpenError,
52
+ } from "./errors.js";
53
+
54
+ // Types
55
+ export type {
56
+ Result,
57
+ ErrorLog,
58
+ SendParams,
59
+ SendObjectParams,
60
+ BatchResult,
61
+ } from "./types.js";
package/src/types.ts ADDED
@@ -0,0 +1,33 @@
1
+ export type Result<T> =
2
+ | { ok: true; data: T; error: null }
3
+ | { ok: false; data: null; error: ErrorLog };
4
+
5
+ export interface ErrorLog {
6
+ name: string;
7
+ message: string;
8
+ code: string;
9
+ isClientError: boolean;
10
+ isServerError: boolean;
11
+ details?: unknown;
12
+ timestamp: string;
13
+ }
14
+
15
+ export interface SendParams {
16
+ to: string;
17
+ template: string;
18
+ variables?: Record<string, unknown>;
19
+ }
20
+
21
+ export interface SendObjectParams<T extends Record<string, unknown>> {
22
+ to: string;
23
+ object: T;
24
+ template?: string;
25
+ }
26
+
27
+ export interface BatchResult<T> {
28
+ succeeded: Array<{ index: number; to: string; data: T }>;
29
+ failed: Array<{ index: number; to: string; error: ErrorLog }>;
30
+ total: number;
31
+ successCount: number;
32
+ failureCount: number;
33
+ }
@@ -1,67 +0,0 @@
1
- import HttpClient from "../HttpClient.js";
2
- import RetryHandler, { type RetryOptions } from "../RetryHandler.js";
3
- import MessageFormatter from "../MessageFormatter.js";
4
- import { ValidationError } from "../errors.js";
5
-
6
- interface SMSClientOptions {
7
- baseUrl: string;
8
- apiKey: string;
9
- projectKey: string;
10
- retry?: RetryOptions;
11
- }
12
-
13
- export class SMSClient {
14
- private http: HttpClient;
15
- private retry: RetryHandler;
16
- private formatter: MessageFormatter;
17
-
18
- constructor(private options: SMSClientOptions) {
19
- this.validateOptions(options);
20
-
21
- this.http = new HttpClient({
22
- baseUrl: options.baseUrl,
23
- apiKey: options.apiKey,
24
- projectKey: options.projectKey,
25
- });
26
-
27
- this.retry = new RetryHandler(options.retry);
28
- this.formatter = new MessageFormatter();
29
- }
30
-
31
- private validateOptions(options: SMSClientOptions): void {
32
- const { baseUrl, apiKey, projectKey } = options;
33
-
34
- if (!baseUrl) {
35
- throw new ValidationError("baseUrl is required");
36
- }
37
-
38
- if (!apiKey) {
39
- throw new ValidationError("apiKey is required");
40
- }
41
-
42
- if (!projectKey) {
43
- throw new ValidationError("projectKey is required");
44
- }
45
- }
46
-
47
- async send(params: {
48
- to: string;
49
- template: string;
50
- variables?: Record<string, unknown>;
51
- }): Promise<any> {
52
- const { to, template, variables = {} } = params;
53
-
54
- if (!to) {
55
- throw new ValidationError("Recipient phone number is required");
56
- }
57
-
58
- const message = this.formatter.format(template, variables);
59
-
60
- const payload = {
61
- to,
62
- message,
63
- };
64
-
65
- return this.retry.execute(() => this.http.post("/sms/send", payload));
66
- }
67
- }
@@ -1,24 +0,0 @@
1
- type SMSClientConfigMode = "BASE" | "FAULT_TOLERANT";
2
-
3
- type RetryOptions = {};
4
-
5
- type CircuitBreakerOptions = {};
6
-
7
- type BatchingOptions = {};
8
-
9
- type QueueOptions = {};
10
-
11
- export default class SMSClientConfig {
12
- constructor(
13
- public baseUrl: string,
14
- public apiKey: string,
15
- public mode: SMSClientConfigMode,
16
- public timeout: number,
17
- public retryOptions: RetryOptions,
18
- public circuitBreakerOptions: CircuitBreakerOptions,
19
- public batchingOptions: BatchingOptions,
20
- public queueOptions: QueueOptions,
21
- ) {}
22
- }
23
-
24
- export type Result = [string, Error | null];
package/src/help.ts DELETED
@@ -1,3 +0,0 @@
1
- export function isObject(value: unknown): value is Record<string, unknown> {
2
- return typeof value === "object" && value !== null && !Array.isArray(value);
3
- }