@mynitorai/sdk 0.1.3 → 0.1.4

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.ts CHANGED
@@ -10,12 +10,19 @@ export declare class MyNitor {
10
10
  private static instance;
11
11
  private config;
12
12
  private isInstrumented;
13
+ private pendingPromises;
13
14
  private constructor();
14
15
  static init(config: MyNitorConfig): MyNitor;
15
16
  /**
16
17
  * Automatically detect and wrap AI libraries like OpenAI
17
18
  */
18
19
  instrument(): void;
20
+ /**
21
+ * Waits for all pending network requests to complete.
22
+ * Call this before your process exits (e.g. in AWS Lambda or scripts).
23
+ * @param timeoutMs Maximum time to wait in milliseconds (default: 10000)
24
+ */
25
+ flush(timeoutMs?: number): Promise<void>;
19
26
  private getCallSite;
20
27
  private sendEvent;
21
28
  private wrapOpenAI;
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ exports.init = exports.MyNitor = void 0;
8
8
  class MyNitor {
9
9
  constructor(config) {
10
10
  this.isInstrumented = false;
11
+ this.pendingPromises = new Set();
11
12
  this.config = {
12
13
  endpoint: 'https://app.mynitor.ai/api/v1/events',
13
14
  ...config
@@ -29,6 +30,19 @@ class MyNitor {
29
30
  this.isInstrumented = true;
30
31
  console.log('🚀 MyNitor: Auto-instrumentation active.');
31
32
  }
33
+ /**
34
+ * Waits for all pending network requests to complete.
35
+ * Call this before your process exits (e.g. in AWS Lambda or scripts).
36
+ * @param timeoutMs Maximum time to wait in milliseconds (default: 10000)
37
+ */
38
+ async flush(timeoutMs = 10000) {
39
+ if (this.pendingPromises.size === 0)
40
+ return;
41
+ console.log(`🚀 MyNitor: Flushing ${this.pendingPromises.size} pending logs...`);
42
+ const timeoutPromise = new Promise((resolve) => setTimeout(resolve, timeoutMs));
43
+ const allSettledPromise = Promise.allSettled(this.pendingPromises);
44
+ await Promise.race([allSettledPromise, timeoutPromise]);
45
+ }
32
46
  getCallSite() {
33
47
  try {
34
48
  const err = new Error();
@@ -62,7 +76,8 @@ class MyNitor {
62
76
  async sendEvent(payload) {
63
77
  try {
64
78
  // Fire and forget
65
- fetch(this.config.endpoint, {
79
+ // Fire and forget (but track)
80
+ const promise = fetch(this.config.endpoint, {
66
81
  method: 'POST',
67
82
  headers: {
68
83
  'Content-Type': 'application/json',
@@ -72,7 +87,13 @@ class MyNitor {
72
87
  ...payload,
73
88
  eventVersion: '1.0'
74
89
  })
75
- }).catch(() => { });
90
+ })
91
+ .then(() => { })
92
+ .catch(() => { })
93
+ .finally(() => {
94
+ this.pendingPromises.delete(promise);
95
+ });
96
+ this.pendingPromises.add(promise);
76
97
  }
77
98
  catch (e) { }
78
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mynitorai/sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Production safety and observability for AI systems.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",