@observa/sdk 2.3.0 → 2.3.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 (57) hide show
  1. package/dist/index.d.ts +494 -21
  2. package/dist/index.js +568 -18
  3. package/dist/index.js.map +1 -1
  4. package/dist/src/apis/ingestApi.d.ts +22 -0
  5. package/dist/src/apis/ingestApi.js +167 -0
  6. package/dist/src/apis/ingestApi.js.map +1 -0
  7. package/dist/src/apis/uptimeApi.d.ts +11 -0
  8. package/dist/src/apis/uptimeApi.js +39 -0
  9. package/dist/src/apis/uptimeApi.js.map +1 -0
  10. package/dist/src/domain/ingest.d.ts +47 -0
  11. package/dist/src/domain/ingest.js +2 -0
  12. package/dist/src/domain/ingest.js.map +1 -0
  13. package/dist/src/domain/uptime.d.ts +23 -0
  14. package/dist/src/domain/uptime.js +2 -0
  15. package/dist/src/domain/uptime.js.map +1 -0
  16. package/dist/src/http/errors.d.ts +33 -0
  17. package/dist/src/http/errors.js +54 -0
  18. package/dist/src/http/errors.js.map +1 -0
  19. package/dist/src/http/httpClient.d.ts +45 -0
  20. package/dist/src/http/httpClient.js +165 -0
  21. package/dist/src/http/httpClient.js.map +1 -0
  22. package/dist/src/index.d.ts +12 -0
  23. package/dist/src/index.js +7 -0
  24. package/dist/src/index.js.map +1 -0
  25. package/dist/src/sdk.d.ts +23 -0
  26. package/dist/src/sdk.js +40 -0
  27. package/dist/src/sdk.js.map +1 -0
  28. package/dist/src/utils/processContext.d.ts +34 -0
  29. package/dist/src/utils/processContext.js +47 -0
  30. package/dist/src/utils/processContext.js.map +1 -0
  31. package/dist/src/utils/validate.d.ts +2 -0
  32. package/dist/src/utils/validate.js +12 -0
  33. package/dist/src/utils/validate.js.map +1 -0
  34. package/dist/tests/httpClient.test.d.ts +1 -0
  35. package/dist/tests/httpClient.test.js +47 -0
  36. package/dist/tests/httpClient.test.js.map +1 -0
  37. package/dist/tests/ingestApi.test.d.ts +1 -0
  38. package/dist/tests/ingestApi.test.js +65 -0
  39. package/dist/tests/ingestApi.test.js.map +1 -0
  40. package/dist/tests/observaSdk.integration.test.d.ts +1 -0
  41. package/dist/tests/observaSdk.integration.test.js +51 -0
  42. package/dist/tests/observaSdk.integration.test.js.map +1 -0
  43. package/dist/tests/sdk.test.d.ts +1 -0
  44. package/dist/tests/sdk.test.js +104 -0
  45. package/dist/tests/sdk.test.js.map +1 -0
  46. package/dist/tsconfig.build.tsbuildinfo +1 -0
  47. package/package.json +5 -3
  48. package/src/apis/ingestApi.ts +185 -0
  49. package/src/apis/uptimeApi.ts +58 -0
  50. package/src/domain/ingest.ts +89 -0
  51. package/src/domain/uptime.ts +86 -0
  52. package/src/http/errors.ts +88 -0
  53. package/src/http/httpClient.ts +277 -0
  54. package/src/index.ts +68 -0
  55. package/src/sdk.ts +103 -0
  56. package/src/utils/processContext.ts +84 -0
  57. package/src/utils/validate.ts +19 -0
@@ -0,0 +1,22 @@
1
+ import type { IngestRequest, IngestResponse } from '../domain/ingest';
2
+ import type { HttpClient } from '../http/httpClient';
3
+ export type IngestNormalizationOptions = {
4
+ schemaVersion?: number;
5
+ includeContext?: boolean;
6
+ includeSystemContext?: boolean;
7
+ includeRuntimeContext?: boolean;
8
+ maxEventBytes?: number;
9
+ maxFrames?: number;
10
+ maxMessageLength?: number;
11
+ maxExceptionValueLength?: number;
12
+ };
13
+ export declare class IngestApi {
14
+ private readonly http;
15
+ private readonly defaultDsnKey?;
16
+ private readonly normalization?;
17
+ constructor(http: HttpClient, defaultDsnKey?: string | undefined, normalization?: IngestNormalizationOptions | undefined);
18
+ event(input: IngestRequest): Promise<IngestResponse>;
19
+ health(dsnKey?: string): Promise<{
20
+ ok: boolean;
21
+ }>;
22
+ }
@@ -0,0 +1,167 @@
1
+ import { randomUUID } from 'crypto';
2
+ import { ValidationError } from '../http/errors.js';
3
+ import { getProcessContextDynamic, getProcessContextStatic } from '../utils/processContext.js';
4
+ import { ensureDefined, ensureNonEmpty } from '../utils/validate.js';
5
+ const DEFAULT_NORMALIZATION = {
6
+ schemaVersion: 1,
7
+ includeContext: true,
8
+ includeSystemContext: true,
9
+ includeRuntimeContext: true,
10
+ maxEventBytes: 64 * 1024,
11
+ maxFrames: 60,
12
+ maxMessageLength: 4000,
13
+ maxExceptionValueLength: 4000,
14
+ };
15
+ export class IngestApi {
16
+ http;
17
+ defaultDsnKey;
18
+ normalization;
19
+ constructor(http, defaultDsnKey, normalization) {
20
+ this.http = http;
21
+ this.defaultDsnKey = defaultDsnKey;
22
+ this.normalization = normalization;
23
+ }
24
+ async event(input) {
25
+ ensureDefined(input, 'input');
26
+ const dsnKey = input.dsnKey ?? this.defaultDsnKey;
27
+ ensureDefined(dsnKey, 'dsnKey');
28
+ ensureNonEmpty(dsnKey, 'dsnKey');
29
+ ensureDefined(input.event, 'event');
30
+ if (input.idempotencyKey && input.idempotencyKey.length > 128) {
31
+ throw new ValidationError('idempotencyKey must be at most 128 characters');
32
+ }
33
+ const headers = {};
34
+ if (input.idempotencyKey)
35
+ headers['x-idempotency-key'] = input.idempotencyKey;
36
+ if (input.sdkVersion)
37
+ headers['x-sdk-version'] = input.sdkVersion;
38
+ const { idempotencyKey, sdkVersion, event, ...body } = input;
39
+ const normalizedEvent = normalizeEvent(event, this.normalization);
40
+ return this.http.post('/ingest/events', { ...body, dsnKey, event: normalizedEvent }, { auth: 'apiKey', headers });
41
+ }
42
+ async health(dsnKey) {
43
+ const resolvedDsnKey = dsnKey ?? this.defaultDsnKey;
44
+ ensureDefined(resolvedDsnKey, 'dsnKey');
45
+ ensureNonEmpty(resolvedDsnKey, 'dsnKey');
46
+ return this.http.post('/ingest/health', { dsnKey: resolvedDsnKey }, { auth: 'apiKey' });
47
+ }
48
+ }
49
+ function normalizeEvent(event, options) {
50
+ const config = { ...DEFAULT_NORMALIZATION, ...options };
51
+ const normalizedTimestamp = normalizeTimestamp(event.timestamp);
52
+ const normalizedLevel = event.level ? event.level.toLowerCase() : undefined;
53
+ const normalizedMessage = event.message ? truncate(event.message, config.maxMessageLength) : undefined;
54
+ const normalizedException = normalizeException(event.exception, config);
55
+ if (!normalizedMessage && !normalizedException) {
56
+ throw new ValidationError('event message or exception is required');
57
+ }
58
+ const normalizedContext = normalizeContext(event.context, config);
59
+ const normalizedEvent = {
60
+ ...event,
61
+ event_id: event.event_id ?? randomUUID(),
62
+ timestamp: normalizedTimestamp,
63
+ schema_version: event.schema_version ?? config.schemaVersion,
64
+ level: normalizedLevel,
65
+ message: normalizedMessage,
66
+ exception: normalizedException,
67
+ context: normalizedContext,
68
+ };
69
+ return enforceSizeLimit(normalizedEvent, config);
70
+ }
71
+ function normalizeTimestamp(timestamp) {
72
+ if (!timestamp)
73
+ return new Date().toISOString();
74
+ const parsed = new Date(timestamp);
75
+ if (Number.isNaN(parsed.getTime())) {
76
+ throw new ValidationError('timestamp must be a valid ISO date');
77
+ }
78
+ return parsed.toISOString();
79
+ }
80
+ function normalizeException(exception, config) {
81
+ if (!exception)
82
+ return undefined;
83
+ if (!exception.type || !exception.value) {
84
+ throw new ValidationError('exception.type and exception.value are required');
85
+ }
86
+ return {
87
+ ...exception,
88
+ value: truncate(exception.value, config.maxExceptionValueLength),
89
+ stacktrace: normalizeStacktrace(exception.stacktrace, config.maxFrames),
90
+ };
91
+ }
92
+ function normalizeStacktrace(stacktrace, maxFrames) {
93
+ if (!stacktrace || !Array.isArray(stacktrace.frames))
94
+ return undefined;
95
+ const frames = stacktrace.frames;
96
+ const normalizedFrames = frames.slice(0, maxFrames).map((frame) => {
97
+ const filename = typeof frame?.filename === 'string' ? frame.filename : undefined;
98
+ const functionName = typeof frame?.function === 'string' ? frame.function : undefined;
99
+ const lineno = typeof frame?.lineno === 'number' ? frame.lineno : undefined;
100
+ const colno = typeof frame?.colno === 'number' ? frame.colno : undefined;
101
+ const inferredInApp = filename ? !filename.includes('node_modules') : false;
102
+ return {
103
+ filename,
104
+ function: functionName,
105
+ lineno,
106
+ colno,
107
+ in_app: typeof frame?.in_app === 'boolean' ? frame.in_app : inferredInApp,
108
+ };
109
+ });
110
+ return { frames: normalizedFrames };
111
+ }
112
+ function normalizeContext(context, config) {
113
+ if (!config.includeContext)
114
+ return context;
115
+ const systemContext = config.includeSystemContext ? getProcessContextDynamic() : undefined;
116
+ const runtimeContext = config.includeRuntimeContext ? getProcessContextStatic({ includeVersions: false }) : undefined;
117
+ const mergedContext = {
118
+ ...context,
119
+ system: context?.system ?? systemContext,
120
+ runtime: context?.runtime ?? runtimeContext,
121
+ };
122
+ return mergedContext;
123
+ }
124
+ function enforceSizeLimit(event, config) {
125
+ let normalized = event;
126
+ if (getSize(normalized) <= config.maxEventBytes)
127
+ return normalized;
128
+ if (normalized.extra) {
129
+ normalized = { ...normalized, extra: undefined };
130
+ }
131
+ if (getSize(normalized) <= config.maxEventBytes)
132
+ return normalized;
133
+ if (normalized.tags) {
134
+ normalized = { ...normalized, tags: undefined };
135
+ }
136
+ if (getSize(normalized) <= config.maxEventBytes)
137
+ return normalized;
138
+ if (normalized.exception?.stacktrace) {
139
+ normalized = {
140
+ ...normalized,
141
+ exception: { ...normalized.exception, stacktrace: undefined },
142
+ };
143
+ }
144
+ if (getSize(normalized) <= config.maxEventBytes)
145
+ return normalized;
146
+ if (normalized.message) {
147
+ normalized = { ...normalized, message: truncate(normalized.message, config.maxMessageLength) };
148
+ }
149
+ if (normalized.exception?.value) {
150
+ normalized = {
151
+ ...normalized,
152
+ exception: { ...normalized.exception, value: truncate(normalized.exception.value, config.maxExceptionValueLength) },
153
+ };
154
+ }
155
+ if (getSize(normalized) <= config.maxEventBytes)
156
+ return normalized;
157
+ throw new ValidationError('event payload exceeds size limit');
158
+ }
159
+ function getSize(value) {
160
+ return Buffer.byteLength(JSON.stringify(value), 'utf8');
161
+ }
162
+ function truncate(value, maxLength) {
163
+ if (value.length <= maxLength)
164
+ return value;
165
+ return value.slice(0, maxLength);
166
+ }
167
+ //# sourceMappingURL=ingestApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ing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O,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAA;AAC3D,CAAC;AAED,SAAS,QAAQ,CAAC,KAAa,EAAE,SAAiB;IAC9C,IAAI,KAAK,CAAC,MAAM,IAAI,SAAS;QAAE,OAAO,KAAK,CAAA;IAC3C,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAA;AACpC,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { UptimeEvent, UptimeHeartbeatInput, UptimeSummary } from '../domain/uptime';
2
+ import type { HttpClient } from '../http/httpClient';
3
+ export declare class UptimeApi {
4
+ private readonly http;
5
+ private readonly defaultDsnKey?;
6
+ constructor(http: HttpClient, defaultDsnKey?: string | undefined);
7
+ recordHeartbeat(input: UptimeHeartbeatInput): Promise<UptimeEvent>;
8
+ history(projectId: string, date: string): Promise<UptimeEvent[]>;
9
+ latest(projectId: string): Promise<UptimeEvent | null>;
10
+ summary(projectId: string, days?: number, delayThresholdMinutes?: number): Promise<UptimeSummary[]>;
11
+ }
@@ -0,0 +1,39 @@
1
+ import { ensureDefined, ensureNonEmpty } from '../utils/validate.js';
2
+ export class UptimeApi {
3
+ http;
4
+ defaultDsnKey;
5
+ constructor(http, defaultDsnKey) {
6
+ this.http = http;
7
+ this.defaultDsnKey = defaultDsnKey;
8
+ }
9
+ async recordHeartbeat(input) {
10
+ ensureDefined(input, 'input');
11
+ const dsnKey = input.dsnKey ?? this.defaultDsnKey;
12
+ ensureDefined(dsnKey, 'dsnKey');
13
+ ensureNonEmpty(dsnKey, 'dsnKey');
14
+ ensureNonEmpty(input.status, 'status');
15
+ return this.http.post('/uptime/heartbeats', { ...input, dsnKey }, { auth: 'apiKey' });
16
+ }
17
+ async history(projectId, date) {
18
+ ensureNonEmpty(projectId, 'projectId');
19
+ ensureNonEmpty(date, 'date');
20
+ return this.http.get(`/projects/${encodeURIComponent(projectId)}/uptime/history`, {
21
+ query: { date },
22
+ auth: 'none',
23
+ });
24
+ }
25
+ async latest(projectId) {
26
+ ensureNonEmpty(projectId, 'projectId');
27
+ return this.http.get(`/projects/${encodeURIComponent(projectId)}/uptime/latest`, {
28
+ auth: 'none',
29
+ });
30
+ }
31
+ async summary(projectId, days, delayThresholdMinutes) {
32
+ ensureNonEmpty(projectId, 'projectId');
33
+ return this.http.get(`/projects/${encodeURIComponent(projectId)}/uptime/summary`, {
34
+ query: { days, delayThresholdMinutes },
35
+ auth: 'none',
36
+ });
37
+ }
38
+ }
39
+ //# sourceMappingURL=uptimeApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uptimeApi.js","sourceRoot":"","sources":["../../../src/apis/uptimeApi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAMjE,MAAM,OAAO,SAAS;IAIW;IAAmC;IAAhE,YAA6B,IAAgB,EAAmB,aAAsB;QAAzD,SAAI,GAAJ,IAAI,CAAY;QAAmB,kBAAa,GAAb,aAAa,CAAS;IAAI,CAAC;IAK3F,KAAK,CAAC,eAAe,CAAC,KAA2B;QAC7C,aAAa,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAC7B,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,IAAI,CAAC,aAAa,CAAA;QACjD,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAC/B,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAChC,cAAc,CAAC,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAc,oBAAoB,EAAE,EAAE,GAAG,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAA;IACtG,CAAC;IAKD,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,IAAY;QACzC,cAAc,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;QACtC,cAAc,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QAC5B,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAgB,aAAa,kBAAkB,CAAC,SAAS,CAAC,iBAAiB,EAAE;YAC7F,KAAK,EAAE,EAAE,IAAI,EAAE;YACf,IAAI,EAAE,MAAM;SACf,CAAC,CAAA;IACN,CAAC;IAKD,KAAK,CAAC,MAAM,CAAC,SAAiB;QAC1B,cAAc,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAqB,aAAa,kBAAkB,CAAC,SAAS,CAAC,gBAAgB,EAAE;YACjG,IAAI,EAAE,MAAM;SACf,CAAC,CAAA;IACN,CAAC;IAKD,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,IAAa,EAAE,qBAA8B;QAC1E,cAAc,CAAC,SAAS,EAAE,WAAW,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAkB,aAAa,kBAAkB,CAAC,SAAS,CAAC,iBAAiB,EAAE;YAC/F,KAAK,EAAE,EAAE,IAAI,EAAE,qBAAqB,EAAE;YACtC,IAAI,EAAE,MAAM;SACf,CAAC,CAAA;IACN,CAAC;CACJ"}
@@ -0,0 +1,47 @@
1
+ import type { ProcessContextDynamic, ProcessContextStatic } from '../utils/processContext';
2
+ export type IngestLevel = 'debug' | 'info' | 'warn' | 'error' | 'fatal';
3
+ export type StacktraceFrame = {
4
+ filename?: string;
5
+ function?: string;
6
+ lineno?: number;
7
+ colno?: number;
8
+ in_app?: boolean;
9
+ };
10
+ export type Stacktrace = {
11
+ frames: StacktraceFrame[];
12
+ };
13
+ export type IngestException = {
14
+ type: string;
15
+ value: string;
16
+ stacktrace?: Stacktrace;
17
+ };
18
+ export type RequestContext = {
19
+ requestId?: string;
20
+ userId?: string;
21
+ [key: string]: unknown;
22
+ };
23
+ export type IngestEventContext = {
24
+ system?: ProcessContextDynamic;
25
+ runtime?: ProcessContextStatic;
26
+ request?: RequestContext;
27
+ };
28
+ export type IngestEvent = {
29
+ event_id?: string;
30
+ timestamp?: string;
31
+ schema_version?: number;
32
+ level?: IngestLevel;
33
+ message?: string;
34
+ exception?: IngestException;
35
+ context?: IngestEventContext;
36
+ tags?: Record<string, string>;
37
+ extra?: Record<string, unknown>;
38
+ };
39
+ export type IngestRequest = {
40
+ dsnKey?: string;
41
+ event: IngestEvent;
42
+ idempotencyKey?: string;
43
+ sdkVersion?: string;
44
+ };
45
+ export type IngestResponse = {
46
+ event_id: string;
47
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ingest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingest.js","sourceRoot":"","sources":["../../../src/domain/ingest.ts"],"names":[],"mappings":""}
@@ -0,0 +1,23 @@
1
+ export type UptimeStatus = 'up' | 'down' | 'degraded';
2
+ export type UptimeEvent = {
3
+ id: string;
4
+ projectId: string;
5
+ status: UptimeStatus;
6
+ message?: string;
7
+ responseTimeMs?: number;
8
+ checkedAt?: string;
9
+ createdAt?: string;
10
+ };
11
+ export type UptimeHeartbeatInput = {
12
+ dsnKey?: string;
13
+ status: UptimeStatus;
14
+ responseTimeMs?: number;
15
+ checkedAt?: string;
16
+ message?: string;
17
+ };
18
+ export type UptimeSummary = {
19
+ date: string;
20
+ missingHours?: number;
21
+ delayMinutes?: number;
22
+ delayedCount?: number;
23
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=uptime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uptime.js","sourceRoot":"","sources":["../../../src/domain/uptime.ts"],"names":[],"mappings":""}
@@ -0,0 +1,33 @@
1
+ export type ErrorDetails = {
2
+ status?: number;
3
+ code?: string;
4
+ details?: unknown;
5
+ retryAfter?: number;
6
+ };
7
+ export declare class SdkError extends Error {
8
+ readonly status?: number;
9
+ readonly code?: string;
10
+ readonly details?: unknown;
11
+ constructor(message: string, details?: ErrorDetails);
12
+ }
13
+ export declare class ValidationError extends SdkError {
14
+ }
15
+ export declare class AuthError extends SdkError {
16
+ }
17
+ export declare class ForbiddenError extends SdkError {
18
+ }
19
+ export declare class NotFoundError extends SdkError {
20
+ }
21
+ export declare class ConflictError extends SdkError {
22
+ }
23
+ export declare class RateLimitError extends SdkError {
24
+ readonly retryAfter?: number;
25
+ constructor(message: string, details?: ErrorDetails);
26
+ }
27
+ export declare class ServerError extends SdkError {
28
+ }
29
+ export declare class NetworkError extends SdkError {
30
+ }
31
+ export declare class TimeoutError extends SdkError {
32
+ }
33
+ export declare function mapHttpError(status: number, details?: unknown, retryAfter?: number): SdkError;
@@ -0,0 +1,54 @@
1
+ export class SdkError extends Error {
2
+ status;
3
+ code;
4
+ details;
5
+ constructor(message, details) {
6
+ super(message);
7
+ this.name = new.target.name;
8
+ this.status = details?.status;
9
+ this.code = details?.code;
10
+ this.details = details?.details;
11
+ }
12
+ }
13
+ export class ValidationError extends SdkError {
14
+ }
15
+ export class AuthError extends SdkError {
16
+ }
17
+ export class ForbiddenError extends SdkError {
18
+ }
19
+ export class NotFoundError extends SdkError {
20
+ }
21
+ export class ConflictError extends SdkError {
22
+ }
23
+ export class RateLimitError extends SdkError {
24
+ retryAfter;
25
+ constructor(message, details) {
26
+ super(message, details);
27
+ this.retryAfter = details?.retryAfter;
28
+ }
29
+ }
30
+ export class ServerError extends SdkError {
31
+ }
32
+ export class NetworkError extends SdkError {
33
+ }
34
+ export class TimeoutError extends SdkError {
35
+ }
36
+ export function mapHttpError(status, details, retryAfter) {
37
+ const info = { status, details, retryAfter };
38
+ if (status === 400)
39
+ return new ValidationError('Invalid request', info);
40
+ if (status === 401)
41
+ return new AuthError('Unauthorized', info);
42
+ if (status === 403)
43
+ return new ForbiddenError('Forbidden', info);
44
+ if (status === 404)
45
+ return new NotFoundError('Resource not found', info);
46
+ if (status === 409)
47
+ return new ConflictError('Conflict', info);
48
+ if (status === 429)
49
+ return new RateLimitError('Rate limit exceeded', info);
50
+ if (status >= 500)
51
+ return new ServerError('Server error', info);
52
+ return new SdkError('HTTP error', info);
53
+ }
54
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/http/errors.ts"],"names":[],"mappings":"AAaA,MAAM,OAAO,QAAS,SAAQ,KAAK;IACtB,MAAM,CAAS;IACf,IAAI,CAAS;IACb,OAAO,CAAU;IAK1B,YAAY,OAAe,EAAE,OAAsB;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAA;QAC3B,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,MAAM,CAAA;QAC7B,IAAI,CAAC,IAAI,GAAG,OAAO,EAAE,IAAI,CAAA;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,EAAE,OAAO,CAAA;IACnC,CAAC;CACJ;AAKD,MAAM,OAAO,eAAgB,SAAQ,QAAQ;CAAI;AAIjD,MAAM,OAAO,SAAU,SAAQ,QAAQ;CAAI;AAI3C,MAAM,OAAO,cAAe,SAAQ,QAAQ;CAAI;AAIhD,MAAM,OAAO,aAAc,SAAQ,QAAQ;CAAI;AAI/C,MAAM,OAAO,aAAc,SAAQ,QAAQ;CAAI;AAI/C,MAAM,OAAO,cAAe,SAAQ,QAAQ;IAC/B,UAAU,CAAS;IAE5B,YAAY,OAAe,EAAE,OAAsB;QAC/C,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACvB,IAAI,CAAC,UAAU,GAAG,OAAO,EAAE,UAAU,CAAA;IACzC,CAAC;CACJ;AAID,MAAM,OAAO,WAAY,SAAQ,QAAQ;CAAI;AAI7C,MAAM,OAAO,YAAa,SAAQ,QAAQ;CAAI;AAI9C,MAAM,OAAO,YAAa,SAAQ,QAAQ;CAAI;AAK9C,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,OAAiB,EAAE,UAAmB;IAC/E,MAAM,IAAI,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,CAAA;IAC5C,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,eAAe,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAA;IACvE,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,SAAS,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IAC9D,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,cAAc,CAAC,WAAW,EAAE,IAAI,CAAC,CAAA;IAChE,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,aAAa,CAAC,oBAAoB,EAAE,IAAI,CAAC,CAAA;IACxE,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,aAAa,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IAC9D,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,cAAc,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAA;IAC1E,IAAI,MAAM,IAAI,GAAG;QAAE,OAAO,IAAI,WAAW,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IAC/D,OAAO,IAAI,QAAQ,CAAC,YAAY,EAAE,IAAI,CAAC,CAAA;AAC3C,CAAC"}
@@ -0,0 +1,45 @@
1
+ export type AuthMode = 'apiKey' | 'none';
2
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
3
+ export type QueryParams = Record<string, string | number | boolean | undefined>;
4
+ export type RetryPolicy = {
5
+ retries: number;
6
+ retryDelayMs?: (attempt: number, response?: Response, error?: unknown) => number;
7
+ retryOn?: (response?: Response, error?: unknown) => boolean;
8
+ };
9
+ export type HttpClientOptions = {
10
+ baseUrl: string;
11
+ apiKey?: string;
12
+ timeoutMs?: number;
13
+ headers?: Record<string, string>;
14
+ retry?: RetryPolicy;
15
+ };
16
+ export type RequestOptions = {
17
+ method: HttpMethod;
18
+ path: string;
19
+ query?: QueryParams;
20
+ body?: unknown;
21
+ headers?: Record<string, string>;
22
+ auth?: AuthMode;
23
+ };
24
+ export declare class HttpClient {
25
+ private apiKey?;
26
+ private readonly baseUrl;
27
+ private readonly timeoutMs;
28
+ private readonly headers;
29
+ private readonly retry?;
30
+ private healthCheckPromise?;
31
+ constructor(options: HttpClientOptions);
32
+ setApiKey(apiKey?: string): void;
33
+ startHealthCheck(healthCheck: () => Promise<unknown>): void;
34
+ get<T>(path: string, options?: Omit<RequestOptions, 'method' | 'path'>): Promise<T>;
35
+ post<T>(path: string, body?: unknown, options?: Omit<RequestOptions, 'method' | 'path' | 'body'>): Promise<T>;
36
+ put<T>(path: string, body?: unknown, options?: Omit<RequestOptions, 'method' | 'path' | 'body'>): Promise<T>;
37
+ patch<T>(path: string, body?: unknown, options?: Omit<RequestOptions, 'method' | 'path' | 'body'>): Promise<T>;
38
+ delete<T>(path: string, options?: Omit<RequestOptions, 'method' | 'path'>): Promise<T>;
39
+ request<T>(options: RequestOptions): Promise<T>;
40
+ private execute;
41
+ private buildUrl;
42
+ private readBody;
43
+ private parseRetryAfter;
44
+ private delay;
45
+ }
@@ -0,0 +1,165 @@
1
+ import { AuthError, NetworkError, TimeoutError, mapHttpError } from './errors.js';
2
+ export class HttpClient {
3
+ apiKey;
4
+ baseUrl;
5
+ timeoutMs;
6
+ headers;
7
+ retry;
8
+ healthCheckPromise;
9
+ constructor(options) {
10
+ this.baseUrl = options.baseUrl.replace(/\/+$/, '');
11
+ this.apiKey = options.apiKey;
12
+ this.timeoutMs = options.timeoutMs ?? 5000;
13
+ this.headers = options.headers ?? {};
14
+ this.retry = options.retry;
15
+ }
16
+ setApiKey(apiKey) {
17
+ this.apiKey = apiKey;
18
+ }
19
+ startHealthCheck(healthCheck) {
20
+ if (!this.healthCheckPromise) {
21
+ const promise = healthCheck();
22
+ this.healthCheckPromise = promise;
23
+ promise.catch(() => undefined);
24
+ }
25
+ }
26
+ async get(path, options) {
27
+ return this.request({ method: 'GET', path, ...options });
28
+ }
29
+ async post(path, body, options) {
30
+ return this.request({ method: 'POST', path, body, ...options });
31
+ }
32
+ async put(path, body, options) {
33
+ return this.request({ method: 'PUT', path, body, ...options });
34
+ }
35
+ async patch(path, body, options) {
36
+ return this.request({ method: 'PATCH', path, body, ...options });
37
+ }
38
+ async delete(path, options) {
39
+ return this.request({ method: 'DELETE', path, ...options });
40
+ }
41
+ async request(options) {
42
+ if (this.healthCheckPromise) {
43
+ await this.healthCheckPromise;
44
+ }
45
+ const retry = this.retry ?? { retries: 0 };
46
+ const retryOn = retry.retryOn ?? ((response, error) => {
47
+ if (response)
48
+ return response.status === 429 || response.status >= 500;
49
+ return error instanceof NetworkError || error instanceof TimeoutError || error instanceof TypeError;
50
+ });
51
+ const retryDelayMs = retry.retryDelayMs ?? ((attempt) => Math.min(1000 * 2 ** (attempt - 1), 8000));
52
+ let lastError;
53
+ for (let attempt = 0; attempt <= retry.retries; attempt += 1) {
54
+ try {
55
+ const { response, data } = await this.execute(options);
56
+ if (!response.ok) {
57
+ const retryAfter = this.parseRetryAfter(response.headers.get('retry-after'));
58
+ const error = mapHttpError(response.status, data, retryAfter);
59
+ if (retryOn(response, error) && attempt < retry.retries) {
60
+ await this.delay(retryDelayMs(attempt + 1, response, error));
61
+ continue;
62
+ }
63
+ throw error;
64
+ }
65
+ return data;
66
+ }
67
+ catch (error) {
68
+ lastError = error;
69
+ if (retryOn(undefined, error) && attempt < retry.retries) {
70
+ await this.delay(retryDelayMs(attempt + 1, undefined, error));
71
+ continue;
72
+ }
73
+ throw error;
74
+ }
75
+ }
76
+ throw lastError;
77
+ }
78
+ async execute(options) {
79
+ const url = this.buildUrl(options.path, options.query);
80
+ const headers = { ...this.headers, ...options.headers };
81
+ if (options.body !== undefined)
82
+ headers['content-type'] = 'application/json';
83
+ const authMode = options.auth ?? 'none';
84
+ if (authMode === 'apiKey') {
85
+ if (!this.apiKey)
86
+ throw new AuthError('API key is required');
87
+ headers['x-api-key'] = this.apiKey;
88
+ }
89
+ const controller = new AbortController();
90
+ const timer = setTimeout(() => controller.abort(), this.timeoutMs);
91
+ try {
92
+ const response = await fetch(url, {
93
+ method: options.method,
94
+ headers,
95
+ body: options.body === undefined ? undefined : JSON.stringify(options.body),
96
+ signal: controller.signal,
97
+ });
98
+ const data = await this.readBody(response);
99
+ return { response, data };
100
+ }
101
+ catch (error) {
102
+ if (error?.name === 'AbortError') {
103
+ throw new TimeoutError('Request timeout');
104
+ }
105
+ if (error instanceof NetworkError || error instanceof TimeoutError)
106
+ throw error;
107
+ if (error instanceof Error) {
108
+ if (error instanceof AuthError)
109
+ throw error;
110
+ }
111
+ if (error instanceof Error && 'status' in error)
112
+ throw error;
113
+ throw new NetworkError('Network error', { details: error });
114
+ }
115
+ finally {
116
+ clearTimeout(timer);
117
+ }
118
+ }
119
+ buildUrl(path, query) {
120
+ const base = this.baseUrl;
121
+ const fullPath = path.startsWith('http') ? path : `${base}${path.startsWith('/') ? '' : '/'}${path}`;
122
+ if (!query)
123
+ return fullPath;
124
+ const params = new URLSearchParams();
125
+ for (const [key, value] of Object.entries(query)) {
126
+ if (value === undefined)
127
+ continue;
128
+ params.set(key, String(value));
129
+ }
130
+ const suffix = params.toString();
131
+ return suffix ? `${fullPath}?${suffix}` : fullPath;
132
+ }
133
+ async readBody(response) {
134
+ if (response.status === 204)
135
+ return undefined;
136
+ const text = await response.text();
137
+ if (!text)
138
+ return undefined;
139
+ try {
140
+ return JSON.parse(text);
141
+ }
142
+ catch {
143
+ return text;
144
+ }
145
+ }
146
+ parseRetryAfter(value) {
147
+ if (!value)
148
+ return undefined;
149
+ const seconds = Number(value);
150
+ if (!Number.isNaN(seconds) && Number.isFinite(seconds)) {
151
+ return Math.max(0, seconds);
152
+ }
153
+ const parsedDate = Date.parse(value);
154
+ if (Number.isNaN(parsedDate))
155
+ return undefined;
156
+ const diffMs = parsedDate - Date.now();
157
+ if (diffMs <= 0)
158
+ return 0;
159
+ return Math.ceil(diffMs / 1000);
160
+ }
161
+ async delay(ms) {
162
+ await new Promise(resolve => setTimeout(resolve, ms));
163
+ }
164
+ }
165
+ //# sourceMappingURL=httpClient.js.map