@observa/sdk 2.3.0 → 2.4.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.
Files changed (58) hide show
  1. package/README.md +33 -14
  2. package/dist/index.d.ts +507 -21
  3. package/dist/index.js +582 -18
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/apis/ingestApi.d.ts +22 -0
  6. package/dist/src/apis/ingestApi.js +167 -0
  7. package/dist/src/apis/ingestApi.js.map +1 -0
  8. package/dist/src/apis/uptimeApi.d.ts +11 -0
  9. package/dist/src/apis/uptimeApi.js +39 -0
  10. package/dist/src/apis/uptimeApi.js.map +1 -0
  11. package/dist/src/domain/ingest.d.ts +47 -0
  12. package/dist/src/domain/ingest.js +2 -0
  13. package/dist/src/domain/ingest.js.map +1 -0
  14. package/dist/src/domain/uptime.d.ts +23 -0
  15. package/dist/src/domain/uptime.js +2 -0
  16. package/dist/src/domain/uptime.js.map +1 -0
  17. package/dist/src/http/errors.d.ts +33 -0
  18. package/dist/src/http/errors.js +54 -0
  19. package/dist/src/http/errors.js.map +1 -0
  20. package/dist/src/http/httpClient.d.ts +45 -0
  21. package/dist/src/http/httpClient.js +165 -0
  22. package/dist/src/http/httpClient.js.map +1 -0
  23. package/dist/src/index.d.ts +12 -0
  24. package/dist/src/index.js +7 -0
  25. package/dist/src/index.js.map +1 -0
  26. package/dist/src/sdk.d.ts +23 -0
  27. package/dist/src/sdk.js +40 -0
  28. package/dist/src/sdk.js.map +1 -0
  29. package/dist/src/utils/processContext.d.ts +34 -0
  30. package/dist/src/utils/processContext.js +47 -0
  31. package/dist/src/utils/processContext.js.map +1 -0
  32. package/dist/src/utils/validate.d.ts +2 -0
  33. package/dist/src/utils/validate.js +12 -0
  34. package/dist/src/utils/validate.js.map +1 -0
  35. package/dist/tests/httpClient.test.d.ts +1 -0
  36. package/dist/tests/httpClient.test.js +47 -0
  37. package/dist/tests/httpClient.test.js.map +1 -0
  38. package/dist/tests/ingestApi.test.d.ts +1 -0
  39. package/dist/tests/ingestApi.test.js +65 -0
  40. package/dist/tests/ingestApi.test.js.map +1 -0
  41. package/dist/tests/observaSdk.integration.test.d.ts +1 -0
  42. package/dist/tests/observaSdk.integration.test.js +51 -0
  43. package/dist/tests/observaSdk.integration.test.js.map +1 -0
  44. package/dist/tests/sdk.test.d.ts +1 -0
  45. package/dist/tests/sdk.test.js +104 -0
  46. package/dist/tests/sdk.test.js.map +1 -0
  47. package/dist/tsconfig.build.tsbuildinfo +1 -0
  48. package/package.json +5 -3
  49. package/src/apis/ingestApi.ts +199 -0
  50. package/src/apis/uptimeApi.ts +58 -0
  51. package/src/domain/ingest.ts +93 -0
  52. package/src/domain/uptime.ts +86 -0
  53. package/src/http/errors.ts +88 -0
  54. package/src/http/httpClient.ts +284 -0
  55. package/src/index.ts +68 -0
  56. package/src/sdk.ts +107 -0
  57. package/src/utils/processContext.ts +84 -0
  58. package/src/utils/validate.ts +19 -0
package/src/sdk.ts ADDED
@@ -0,0 +1,107 @@
1
+ import { IngestApi, type IngestNormalizationOptions } from './apis/ingestApi'
2
+ import { UptimeApi } from './apis/uptimeApi'
3
+ import { HttpClient, type RetryPolicy } from './http/httpClient'
4
+ import {
5
+ getProcessContext,
6
+ getProcessContextDynamic,
7
+ getProcessContextStatic,
8
+ type ProcessContext,
9
+ type ProcessContextDynamic,
10
+ type ProcessContextDynamicOptions,
11
+ type ProcessContextOptions,
12
+ type ProcessContextStatic,
13
+ type ProcessContextStaticOptions,
14
+ } from './utils/processContext'
15
+
16
+ /**
17
+ * SDK configuration options.
18
+ */
19
+ export type ObservaSDKOptions = {
20
+ /**
21
+ * Organization API key used to authenticate SDK requests.
22
+ */
23
+ apiKey?: string
24
+ /**
25
+ * Project DSN used to identify the destination of events and heartbeats.
26
+ */
27
+ dsnKey: string
28
+ /**
29
+ * Public key for frontend/mobile projects.
30
+ */
31
+ publicKey?: string
32
+ baseUrl?: string
33
+ /**
34
+ * HTTP request timeout in milliseconds.
35
+ */
36
+ timeoutMs?: number
37
+ /**
38
+ * Retry policy for transient errors.
39
+ */
40
+ retry?: RetryPolicy
41
+ /**
42
+ * Additional headers sent with every request.
43
+ */
44
+ headers?: Record<string, string>
45
+ ingest?: IngestNormalizationOptions
46
+ }
47
+
48
+ /**
49
+ * Fixed backend target for the SDK.
50
+ */
51
+ const DEFAULT_BASE_URL = 'https://backend-observa-production.up.railway.app/v1'
52
+
53
+ /**
54
+ * Main SDK for error ingestion and uptime heartbeats.
55
+ */
56
+ export class ObservaSDK {
57
+ /**
58
+ * Uptime API (heartbeats and public reads).
59
+ */
60
+ readonly uptime: UptimeApi
61
+ /**
62
+ * Event ingestion API.
63
+ */
64
+ readonly ingest: IngestApi
65
+
66
+ private readonly http: HttpClient
67
+
68
+ /**
69
+ * Creates an SDK instance with required dsnKey and either apiKey or publicKey.
70
+ */
71
+ constructor(options: ObservaSDKOptions) {
72
+ if (!options || (!options.apiKey && !options.publicKey) || !options.dsnKey) {
73
+ throw new Error('ObservaSDK requires dsnKey and either apiKey or publicKey')
74
+ }
75
+ const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '')
76
+ const normalizedBaseUrl = baseUrl.endsWith('/v1') ? baseUrl : `${baseUrl}/v1`
77
+ this.http = new HttpClient({
78
+ baseUrl: normalizedBaseUrl,
79
+ apiKey: options.apiKey,
80
+ timeoutMs: options.timeoutMs,
81
+ retry: options.retry,
82
+ headers: options.headers,
83
+ })
84
+ this.ingest = new IngestApi(this.http, options.dsnKey, options.ingest, options.publicKey)
85
+ this.uptime = new UptimeApi(this.http, options.dsnKey)
86
+ this.http.startHealthCheck(() => this.ingest.health(options.dsnKey, options.publicKey))
87
+ }
88
+
89
+ /**
90
+ * Updates the API key used by the SDK at runtime.
91
+ */
92
+ setApiKey(apiKey: string) {
93
+ this.http.setApiKey(apiKey)
94
+ }
95
+
96
+ getProcessContext(options?: ProcessContextOptions): ProcessContext {
97
+ return getProcessContext(options)
98
+ }
99
+
100
+ getProcessContextStatic(options?: ProcessContextStaticOptions): ProcessContextStatic {
101
+ return getProcessContextStatic(options)
102
+ }
103
+
104
+ getProcessContextDynamic(options?: ProcessContextDynamicOptions): ProcessContextDynamic {
105
+ return getProcessContextDynamic(options)
106
+ }
107
+ }
@@ -0,0 +1,84 @@
1
+ export type ProcessContextStatic = {
2
+ versions?: NodeJS.ProcessVersions
3
+ node?: string
4
+ platform?: NodeJS.Platform
5
+ arch?: string
6
+ releaseName?: string
7
+ }
8
+
9
+ export type ProcessContextDynamic = {
10
+ pid?: number
11
+ uptimeSeconds?: number
12
+ memory?: NodeJS.MemoryUsage
13
+ }
14
+
15
+ export type ProcessContext = ProcessContextStatic & ProcessContextDynamic
16
+
17
+ export type ProcessContextOptions = {
18
+ includeStatic?: boolean
19
+ includeDynamic?: boolean
20
+ includeVersions?: boolean
21
+ includeRuntime?: boolean
22
+ includePid?: boolean
23
+ includeUptime?: boolean
24
+ includeMemory?: boolean
25
+ }
26
+
27
+ export type ProcessContextStaticOptions = {
28
+ includeVersions?: boolean
29
+ includeRuntime?: boolean
30
+ }
31
+
32
+ export type ProcessContextDynamicOptions = {
33
+ includePid?: boolean
34
+ includeUptime?: boolean
35
+ includeMemory?: boolean
36
+ }
37
+
38
+ export function getProcessContext(options?: ProcessContextOptions): ProcessContext {
39
+ const includeStatic = options?.includeStatic ?? true
40
+ const includeDynamic = options?.includeDynamic ?? true
41
+ const includeVersions = options?.includeVersions ?? true
42
+ const includeRuntime = options?.includeRuntime ?? true
43
+ const includePid = options?.includePid ?? true
44
+ const includeUptime = options?.includeUptime ?? true
45
+ const includeMemory = options?.includeMemory ?? true
46
+ const context: ProcessContext = {}
47
+
48
+ if (includeDynamic) {
49
+ if (includePid) context.pid = process.pid
50
+ if (includeUptime) context.uptimeSeconds = Math.round(process.uptime())
51
+ if (includeMemory) context.memory = process.memoryUsage()
52
+ }
53
+
54
+ if (includeStatic) {
55
+ if (includeVersions) context.versions = process.versions
56
+ if (includeRuntime) {
57
+ context.node = process.versions.node
58
+ context.platform = process.platform
59
+ context.arch = process.arch
60
+ context.releaseName = process.release?.name
61
+ }
62
+ }
63
+
64
+ return context
65
+ }
66
+
67
+ export function getProcessContextStatic(options?: ProcessContextStaticOptions): ProcessContextStatic {
68
+ return getProcessContext({
69
+ includeDynamic: false,
70
+ includeStatic: true,
71
+ includeVersions: options?.includeVersions,
72
+ includeRuntime: options?.includeRuntime,
73
+ })
74
+ }
75
+
76
+ export function getProcessContextDynamic(options?: ProcessContextDynamicOptions): ProcessContextDynamic {
77
+ return getProcessContext({
78
+ includeStatic: false,
79
+ includeDynamic: true,
80
+ includePid: options?.includePid,
81
+ includeUptime: options?.includeUptime,
82
+ includeMemory: options?.includeMemory,
83
+ })
84
+ }
@@ -0,0 +1,19 @@
1
+ import { ValidationError } from '../http/errors'
2
+
3
+ /**
4
+ * Ensures a string is present and non-empty.
5
+ */
6
+ export function ensureNonEmpty(value: string, name: string) {
7
+ if (!value || value.trim().length === 0) {
8
+ throw new ValidationError(`${name} is required`)
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Ensures a value is neither null nor undefined.
14
+ */
15
+ export function ensureDefined<T>(value: T | null | undefined, name: string): asserts value is T {
16
+ if (value === null || value === undefined) {
17
+ throw new ValidationError(`${name} is required`)
18
+ }
19
+ }