@alertsamurai/sdk-js 0.0.3 → 0.0.6

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.mts CHANGED
@@ -10,6 +10,14 @@ interface AlertSamuraiConfig {
10
10
  timeout?: number;
11
11
  /** Number of retries on network failure (optional) - defaults to 3 */
12
12
  retries?: number;
13
+ /**
14
+ * Silent mode (optional) - defaults to true
15
+ * When true, errors are swallowed silently
16
+ * When false, warnings/errors are logged to console
17
+ */
18
+ silent?: boolean;
19
+ /** Default environment name (e.g., "production", "staging") - used if not specified per-call */
20
+ environment?: string;
13
21
  }
14
22
  /**
15
23
  * Options for sending a metric
@@ -32,12 +40,16 @@ interface SendMetricOptions {
32
40
  * Response from sending a metric
33
41
  */
34
42
  interface SendMetricResponse {
35
- /** ID of the created metric record */
36
- id: string;
43
+ /** ID of the created metric record (present on success) */
44
+ id?: string;
37
45
  /** Whether the metric was successfully processed */
38
46
  success: boolean;
39
47
  /** Whether any threshold-based alerts were triggered */
40
48
  alertTriggered?: boolean;
49
+ /** True if client is disabled (no DSN configured) */
50
+ disabled?: boolean;
51
+ /** Error message if the request failed */
52
+ error?: string;
41
53
  }
42
54
  /**
43
55
  * Alert priority levels
@@ -60,15 +72,23 @@ interface SendAlertOptions {
60
72
  * Response from sending an alert
61
73
  */
62
74
  interface SendAlertResponse {
63
- /** ID of the created alert record */
64
- id: string;
75
+ /** ID of the created alert record (present on success) */
76
+ id?: string;
65
77
  /** Whether the alert was successfully processed */
66
78
  success: boolean;
79
+ /** True if client is disabled (no DSN configured) */
80
+ disabled?: boolean;
81
+ /** Error message if the request failed */
82
+ error?: string;
67
83
  }
68
84
 
69
85
  /**
70
86
  * AlertSamurai SDK client for sending metrics and alerts
71
87
  *
88
+ * The client is designed to be fault-tolerant and will never throw errors
89
+ * that could crash your application. If misconfigured, it operates in
90
+ * "disabled" mode and silently returns empty responses.
91
+ *
72
92
  * @example
73
93
  * ```typescript
74
94
  * const client = new AlertSamuraiClient({
@@ -91,7 +111,14 @@ declare class AlertSamuraiClient {
91
111
  private baseUrl;
92
112
  private timeout;
93
113
  private retries;
94
- constructor(config: AlertSamuraiConfig);
114
+ private enabled;
115
+ private silent;
116
+ private environment?;
117
+ constructor(config?: AlertSamuraiConfig | null);
118
+ /**
119
+ * Check if the client is enabled and configured properly
120
+ */
121
+ isEnabled(): boolean;
95
122
  /**
96
123
  * Send a custom metric for tracking and threshold-based alerting
97
124
  *
package/dist/index.d.ts CHANGED
@@ -10,6 +10,14 @@ interface AlertSamuraiConfig {
10
10
  timeout?: number;
11
11
  /** Number of retries on network failure (optional) - defaults to 3 */
12
12
  retries?: number;
13
+ /**
14
+ * Silent mode (optional) - defaults to true
15
+ * When true, errors are swallowed silently
16
+ * When false, warnings/errors are logged to console
17
+ */
18
+ silent?: boolean;
19
+ /** Default environment name (e.g., "production", "staging") - used if not specified per-call */
20
+ environment?: string;
13
21
  }
14
22
  /**
15
23
  * Options for sending a metric
@@ -32,12 +40,16 @@ interface SendMetricOptions {
32
40
  * Response from sending a metric
33
41
  */
34
42
  interface SendMetricResponse {
35
- /** ID of the created metric record */
36
- id: string;
43
+ /** ID of the created metric record (present on success) */
44
+ id?: string;
37
45
  /** Whether the metric was successfully processed */
38
46
  success: boolean;
39
47
  /** Whether any threshold-based alerts were triggered */
40
48
  alertTriggered?: boolean;
49
+ /** True if client is disabled (no DSN configured) */
50
+ disabled?: boolean;
51
+ /** Error message if the request failed */
52
+ error?: string;
41
53
  }
42
54
  /**
43
55
  * Alert priority levels
@@ -60,15 +72,23 @@ interface SendAlertOptions {
60
72
  * Response from sending an alert
61
73
  */
62
74
  interface SendAlertResponse {
63
- /** ID of the created alert record */
64
- id: string;
75
+ /** ID of the created alert record (present on success) */
76
+ id?: string;
65
77
  /** Whether the alert was successfully processed */
66
78
  success: boolean;
79
+ /** True if client is disabled (no DSN configured) */
80
+ disabled?: boolean;
81
+ /** Error message if the request failed */
82
+ error?: string;
67
83
  }
68
84
 
69
85
  /**
70
86
  * AlertSamurai SDK client for sending metrics and alerts
71
87
  *
88
+ * The client is designed to be fault-tolerant and will never throw errors
89
+ * that could crash your application. If misconfigured, it operates in
90
+ * "disabled" mode and silently returns empty responses.
91
+ *
72
92
  * @example
73
93
  * ```typescript
74
94
  * const client = new AlertSamuraiClient({
@@ -91,7 +111,14 @@ declare class AlertSamuraiClient {
91
111
  private baseUrl;
92
112
  private timeout;
93
113
  private retries;
94
- constructor(config: AlertSamuraiConfig);
114
+ private enabled;
115
+ private silent;
116
+ private environment?;
117
+ constructor(config?: AlertSamuraiConfig | null);
118
+ /**
119
+ * Check if the client is enabled and configured properly
120
+ */
121
+ isEnabled(): boolean;
95
122
  /**
96
123
  * Send a custom metric for tracking and threshold-based alerting
97
124
  *
package/dist/index.js CHANGED
@@ -83,18 +83,38 @@ function sleep(ms) {
83
83
  }
84
84
 
85
85
  // src/client.ts
86
- var DEFAULT_BASE_URL = "https://api.alertsamurai.com";
86
+ var DEFAULT_BASE_URL = "https://monitor.alertsamurai.com";
87
87
  var DEFAULT_TIMEOUT = 3e4;
88
88
  var DEFAULT_RETRIES = 3;
89
89
  var AlertSamuraiClient = class {
90
90
  constructor(config) {
91
- if (!config.dsn) {
92
- throw new ValidationError("DSN is required");
91
+ if (!config || !config.dsn) {
92
+ this.enabled = false;
93
+ this.dsn = "";
94
+ this.baseUrl = DEFAULT_BASE_URL;
95
+ this.timeout = DEFAULT_TIMEOUT;
96
+ this.retries = DEFAULT_RETRIES;
97
+ this.silent = config?.silent ?? true;
98
+ if (!this.silent) {
99
+ console.warn(
100
+ "[AlertSamurai] Client disabled: DSN is required. Calls will be no-ops."
101
+ );
102
+ }
103
+ return;
93
104
  }
105
+ this.enabled = true;
94
106
  this.dsn = config.dsn;
95
107
  this.baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
96
108
  this.timeout = config.timeout || DEFAULT_TIMEOUT;
97
109
  this.retries = config.retries ?? DEFAULT_RETRIES;
110
+ this.silent = config.silent ?? true;
111
+ this.environment = config.environment;
112
+ }
113
+ /**
114
+ * Check if the client is enabled and configured properly
115
+ */
116
+ isEnabled() {
117
+ return this.enabled;
98
118
  }
99
119
  /**
100
120
  * Send a custom metric for tracking and threshold-based alerting
@@ -114,16 +134,29 @@ var AlertSamuraiClient = class {
114
134
  * ```
115
135
  */
116
136
  async sendMetric(options) {
117
- this.validateMetric(options);
118
- const body = {
119
- metricType: options.metricType,
120
- value: options.value,
121
- unit: options.unit,
122
- environment: options.environment,
123
- metadata: options.metadata,
124
- timestamp: this.normalizeTimestamp(options.timestamp)
125
- };
126
- return this.request("/api/metrics", body);
137
+ if (!this.enabled) {
138
+ return { success: false, disabled: true };
139
+ }
140
+ try {
141
+ this.validateMetric(options);
142
+ const body = {
143
+ metricType: options.metricType,
144
+ value: options.value,
145
+ unit: options.unit,
146
+ environment: options.environment ?? this.environment,
147
+ metadata: options.metadata,
148
+ timestamp: this.normalizeTimestamp(options.timestamp)
149
+ };
150
+ return this.request("/api/metrics", body);
151
+ } catch (error) {
152
+ if (!this.silent) {
153
+ console.error("[AlertSamurai] sendMetric failed:", error);
154
+ }
155
+ return {
156
+ success: false,
157
+ error: error instanceof Error ? error.message : "Unknown error"
158
+ };
159
+ }
127
160
  }
128
161
  /**
129
162
  * Send an application alert with priority level
@@ -142,8 +175,25 @@ var AlertSamuraiClient = class {
142
175
  * ```
143
176
  */
144
177
  async sendAlert(options) {
145
- this.validateAlert(options);
146
- return this.request("/api/alerts", options);
178
+ if (!this.enabled) {
179
+ return { success: false, disabled: true };
180
+ }
181
+ try {
182
+ this.validateAlert(options);
183
+ const body = {
184
+ ...options,
185
+ environment: options.environment ?? this.environment
186
+ };
187
+ return this.request("/api/alerts", body);
188
+ } catch (error) {
189
+ if (!this.silent) {
190
+ console.error("[AlertSamurai] sendAlert failed:", error);
191
+ }
192
+ return {
193
+ success: false,
194
+ error: error instanceof Error ? error.message : "Unknown error"
195
+ };
196
+ }
147
197
  }
148
198
  /**
149
199
  * Send a critical priority alert
package/dist/index.mjs CHANGED
@@ -53,18 +53,38 @@ function sleep(ms) {
53
53
  }
54
54
 
55
55
  // src/client.ts
56
- var DEFAULT_BASE_URL = "https://api.alertsamurai.com";
56
+ var DEFAULT_BASE_URL = "https://monitor.alertsamurai.com";
57
57
  var DEFAULT_TIMEOUT = 3e4;
58
58
  var DEFAULT_RETRIES = 3;
59
59
  var AlertSamuraiClient = class {
60
60
  constructor(config) {
61
- if (!config.dsn) {
62
- throw new ValidationError("DSN is required");
61
+ if (!config || !config.dsn) {
62
+ this.enabled = false;
63
+ this.dsn = "";
64
+ this.baseUrl = DEFAULT_BASE_URL;
65
+ this.timeout = DEFAULT_TIMEOUT;
66
+ this.retries = DEFAULT_RETRIES;
67
+ this.silent = config?.silent ?? true;
68
+ if (!this.silent) {
69
+ console.warn(
70
+ "[AlertSamurai] Client disabled: DSN is required. Calls will be no-ops."
71
+ );
72
+ }
73
+ return;
63
74
  }
75
+ this.enabled = true;
64
76
  this.dsn = config.dsn;
65
77
  this.baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
66
78
  this.timeout = config.timeout || DEFAULT_TIMEOUT;
67
79
  this.retries = config.retries ?? DEFAULT_RETRIES;
80
+ this.silent = config.silent ?? true;
81
+ this.environment = config.environment;
82
+ }
83
+ /**
84
+ * Check if the client is enabled and configured properly
85
+ */
86
+ isEnabled() {
87
+ return this.enabled;
68
88
  }
69
89
  /**
70
90
  * Send a custom metric for tracking and threshold-based alerting
@@ -84,16 +104,29 @@ var AlertSamuraiClient = class {
84
104
  * ```
85
105
  */
86
106
  async sendMetric(options) {
87
- this.validateMetric(options);
88
- const body = {
89
- metricType: options.metricType,
90
- value: options.value,
91
- unit: options.unit,
92
- environment: options.environment,
93
- metadata: options.metadata,
94
- timestamp: this.normalizeTimestamp(options.timestamp)
95
- };
96
- return this.request("/api/metrics", body);
107
+ if (!this.enabled) {
108
+ return { success: false, disabled: true };
109
+ }
110
+ try {
111
+ this.validateMetric(options);
112
+ const body = {
113
+ metricType: options.metricType,
114
+ value: options.value,
115
+ unit: options.unit,
116
+ environment: options.environment ?? this.environment,
117
+ metadata: options.metadata,
118
+ timestamp: this.normalizeTimestamp(options.timestamp)
119
+ };
120
+ return this.request("/api/metrics", body);
121
+ } catch (error) {
122
+ if (!this.silent) {
123
+ console.error("[AlertSamurai] sendMetric failed:", error);
124
+ }
125
+ return {
126
+ success: false,
127
+ error: error instanceof Error ? error.message : "Unknown error"
128
+ };
129
+ }
97
130
  }
98
131
  /**
99
132
  * Send an application alert with priority level
@@ -112,8 +145,25 @@ var AlertSamuraiClient = class {
112
145
  * ```
113
146
  */
114
147
  async sendAlert(options) {
115
- this.validateAlert(options);
116
- return this.request("/api/alerts", options);
148
+ if (!this.enabled) {
149
+ return { success: false, disabled: true };
150
+ }
151
+ try {
152
+ this.validateAlert(options);
153
+ const body = {
154
+ ...options,
155
+ environment: options.environment ?? this.environment
156
+ };
157
+ return this.request("/api/alerts", body);
158
+ } catch (error) {
159
+ if (!this.silent) {
160
+ console.error("[AlertSamurai] sendAlert failed:", error);
161
+ }
162
+ return {
163
+ success: false,
164
+ error: error instanceof Error ? error.message : "Unknown error"
165
+ };
166
+ }
117
167
  }
118
168
  /**
119
169
  * Send a critical priority alert
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alertsamurai/sdk-js",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
4
4
  "description": "TypeScript SDK for AlertSamurai metrics and alerting",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",