@kopai/sdk 0.0.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.
- package/LICENSE +190 -0
- package/dist/index.cjs +336 -0
- package/dist/index.d.cts +137 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +137 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +305 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 Kopai Authors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
let _kopai_core = require("@kopai/core");
|
|
29
|
+
let zod = require("zod");
|
|
30
|
+
zod = __toESM(zod);
|
|
31
|
+
|
|
32
|
+
//#region src/errors.ts
|
|
33
|
+
/**
|
|
34
|
+
* Base error for Kopai API errors (non-2xx responses).
|
|
35
|
+
* Follows RFC 7807 Problem Details format.
|
|
36
|
+
*/
|
|
37
|
+
var KopaiError = class extends Error {
|
|
38
|
+
/** Error code from problem type */
|
|
39
|
+
code;
|
|
40
|
+
/** HTTP status code */
|
|
41
|
+
status;
|
|
42
|
+
/** URI identifying the problem type */
|
|
43
|
+
type;
|
|
44
|
+
/** Human-readable explanation */
|
|
45
|
+
detail;
|
|
46
|
+
constructor(options) {
|
|
47
|
+
super(options.message);
|
|
48
|
+
this.name = "KopaiError";
|
|
49
|
+
this.code = options.code;
|
|
50
|
+
this.status = options.status;
|
|
51
|
+
this.type = options.type;
|
|
52
|
+
this.detail = options.detail;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Network-level errors (connection failed, DNS resolution, etc.)
|
|
57
|
+
*/
|
|
58
|
+
var KopaiNetworkError = class extends Error {
|
|
59
|
+
/** Original error that caused this */
|
|
60
|
+
cause;
|
|
61
|
+
constructor(message, cause) {
|
|
62
|
+
super(message);
|
|
63
|
+
this.name = "KopaiNetworkError";
|
|
64
|
+
this.cause = cause;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Request timeout errors
|
|
69
|
+
*/
|
|
70
|
+
var KopaiTimeoutError = class extends Error {
|
|
71
|
+
/** Timeout duration in milliseconds */
|
|
72
|
+
timeout;
|
|
73
|
+
constructor(timeout) {
|
|
74
|
+
super(`Request timed out after ${timeout}ms`);
|
|
75
|
+
this.name = "KopaiTimeoutError";
|
|
76
|
+
this.timeout = timeout;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Response validation errors (Zod schema validation failed)
|
|
81
|
+
*/
|
|
82
|
+
var KopaiValidationError = class extends Error {
|
|
83
|
+
/** Original Zod error */
|
|
84
|
+
zodError;
|
|
85
|
+
constructor(zodError) {
|
|
86
|
+
super(`Response validation failed: ${zodError.message}`);
|
|
87
|
+
this.name = "KopaiValidationError";
|
|
88
|
+
this.zodError = zodError;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/request.ts
|
|
94
|
+
const DEFAULT_TIMEOUT$1 = 3e4;
|
|
95
|
+
/**
|
|
96
|
+
* Internal fetch wrapper with timeout, signal, and Zod validation.
|
|
97
|
+
*/
|
|
98
|
+
async function request(url, schema, options) {
|
|
99
|
+
const { method = "GET", body, headers = {}, baseHeaders = {}, signal, timeout, fetchFn, defaultTimeout } = options;
|
|
100
|
+
const effectiveTimeout = timeout ?? defaultTimeout ?? DEFAULT_TIMEOUT$1;
|
|
101
|
+
const timeoutController = new AbortController();
|
|
102
|
+
const timeoutId = setTimeout(() => timeoutController.abort(), effectiveTimeout);
|
|
103
|
+
const combinedSignal = signal ? AbortSignal.any([signal, timeoutController.signal]) : timeoutController.signal;
|
|
104
|
+
try {
|
|
105
|
+
const response = await fetchFn(url, {
|
|
106
|
+
method,
|
|
107
|
+
headers: {
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
...baseHeaders,
|
|
110
|
+
...headers
|
|
111
|
+
},
|
|
112
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
113
|
+
signal: combinedSignal
|
|
114
|
+
});
|
|
115
|
+
clearTimeout(timeoutId);
|
|
116
|
+
if (!response.ok) await handleErrorResponse(response);
|
|
117
|
+
const data = await response.json();
|
|
118
|
+
const result = schema.safeParse(data);
|
|
119
|
+
if (!result.success) throw new KopaiValidationError(result.error);
|
|
120
|
+
return result.data;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
clearTimeout(timeoutId);
|
|
123
|
+
if (error instanceof KopaiError || error instanceof KopaiValidationError || error instanceof KopaiTimeoutError || error instanceof KopaiNetworkError) throw error;
|
|
124
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
125
|
+
if (timeoutController.signal.aborted) throw new KopaiTimeoutError(effectiveTimeout);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
if (error instanceof TypeError) throw new KopaiNetworkError(error.message || "Network request failed", error);
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async function handleErrorResponse(response) {
|
|
133
|
+
let body;
|
|
134
|
+
try {
|
|
135
|
+
body = await response.json();
|
|
136
|
+
} catch {
|
|
137
|
+
body = null;
|
|
138
|
+
}
|
|
139
|
+
if (body && typeof body === "object") {
|
|
140
|
+
const problem = body;
|
|
141
|
+
throw new KopaiError({
|
|
142
|
+
message: problem.title || problem.message || `HTTP ${response.status}`,
|
|
143
|
+
code: problem.code || `HTTP_${response.status}`,
|
|
144
|
+
status: response.status,
|
|
145
|
+
type: problem.type || `about:blank`,
|
|
146
|
+
detail: problem.detail
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
throw new KopaiError({
|
|
150
|
+
message: `HTTP ${response.status}`,
|
|
151
|
+
code: `HTTP_${response.status}`,
|
|
152
|
+
status: response.status,
|
|
153
|
+
type: "about:blank"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/pagination.ts
|
|
159
|
+
/**
|
|
160
|
+
* Creates an async iterator that auto-paginates through results.
|
|
161
|
+
* Yields individual items from each page.
|
|
162
|
+
*/
|
|
163
|
+
async function* paginate(fetcher, signal) {
|
|
164
|
+
let cursor;
|
|
165
|
+
do {
|
|
166
|
+
signal?.throwIfAborted();
|
|
167
|
+
const result = await fetcher(cursor, signal);
|
|
168
|
+
for (const item of result.data) yield item;
|
|
169
|
+
cursor = result.nextCursor ?? void 0;
|
|
170
|
+
} while (cursor);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/client.ts
|
|
175
|
+
const DEFAULT_TIMEOUT = 3e4;
|
|
176
|
+
const tracesResponseSchema = zod.default.object({
|
|
177
|
+
data: zod.default.array(_kopai_core.denormalizedSignals.otelTracesSchema),
|
|
178
|
+
nextCursor: zod.default.string().nullable()
|
|
179
|
+
});
|
|
180
|
+
const logsResponseSchema = zod.default.object({
|
|
181
|
+
data: zod.default.array(_kopai_core.denormalizedSignals.otelLogsSchema),
|
|
182
|
+
nextCursor: zod.default.string().nullable()
|
|
183
|
+
});
|
|
184
|
+
const metricsResponseSchema = zod.default.object({
|
|
185
|
+
data: zod.default.array(_kopai_core.denormalizedSignals.otelMetricsSchema),
|
|
186
|
+
nextCursor: zod.default.string().nullable()
|
|
187
|
+
});
|
|
188
|
+
const metricsDiscoverySchema = zod.default.object({ metrics: zod.default.array(zod.default.object({
|
|
189
|
+
name: zod.default.string(),
|
|
190
|
+
type: zod.default.enum([
|
|
191
|
+
"Gauge",
|
|
192
|
+
"Sum",
|
|
193
|
+
"Histogram",
|
|
194
|
+
"ExponentialHistogram",
|
|
195
|
+
"Summary"
|
|
196
|
+
]),
|
|
197
|
+
unit: zod.default.string().optional(),
|
|
198
|
+
description: zod.default.string().optional(),
|
|
199
|
+
attributes: zod.default.object({
|
|
200
|
+
values: zod.default.record(zod.default.string(), zod.default.array(zod.default.string())),
|
|
201
|
+
_truncated: zod.default.boolean().optional()
|
|
202
|
+
}),
|
|
203
|
+
resourceAttributes: zod.default.object({
|
|
204
|
+
values: zod.default.record(zod.default.string(), zod.default.array(zod.default.string())),
|
|
205
|
+
_truncated: zod.default.boolean().optional()
|
|
206
|
+
})
|
|
207
|
+
})) });
|
|
208
|
+
var KopaiClient = class {
|
|
209
|
+
baseUrl;
|
|
210
|
+
fetchFn;
|
|
211
|
+
defaultTimeout;
|
|
212
|
+
baseHeaders;
|
|
213
|
+
constructor(options) {
|
|
214
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
215
|
+
this.fetchFn = options.fetch ?? fetch;
|
|
216
|
+
this.defaultTimeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
217
|
+
this.baseHeaders = { ...options.headers };
|
|
218
|
+
if (options.token) this.baseHeaders["Authorization"] = `Bearer ${options.token}`;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get all spans for a specific trace by ID.
|
|
222
|
+
*/
|
|
223
|
+
async getTrace(traceId, opts) {
|
|
224
|
+
const schema = zod.default.array(_kopai_core.denormalizedSignals.otelTracesSchema);
|
|
225
|
+
return request(`${this.baseUrl}/traces/${traceId}`, schema, {
|
|
226
|
+
method: "GET",
|
|
227
|
+
...opts,
|
|
228
|
+
baseHeaders: this.baseHeaders,
|
|
229
|
+
fetchFn: this.fetchFn,
|
|
230
|
+
defaultTimeout: this.defaultTimeout
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Search traces with auto-pagination.
|
|
235
|
+
* Yields individual trace rows.
|
|
236
|
+
*/
|
|
237
|
+
searchTraces(filter, opts) {
|
|
238
|
+
return paginate((cursor, signal) => this.searchTracesPage({
|
|
239
|
+
...filter,
|
|
240
|
+
cursor
|
|
241
|
+
}, {
|
|
242
|
+
...opts,
|
|
243
|
+
signal
|
|
244
|
+
}), opts?.signal);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Search traces for a single page.
|
|
248
|
+
* Use this for manual pagination.
|
|
249
|
+
*/
|
|
250
|
+
async searchTracesPage(filter, opts) {
|
|
251
|
+
const validatedFilter = _kopai_core.dataFilterSchemas.tracesDataFilterSchema.parse(filter);
|
|
252
|
+
return request(`${this.baseUrl}/traces/search`, tracesResponseSchema, {
|
|
253
|
+
method: "POST",
|
|
254
|
+
body: validatedFilter,
|
|
255
|
+
...opts,
|
|
256
|
+
baseHeaders: this.baseHeaders,
|
|
257
|
+
fetchFn: this.fetchFn,
|
|
258
|
+
defaultTimeout: this.defaultTimeout
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Search logs with auto-pagination.
|
|
263
|
+
* Yields individual log rows.
|
|
264
|
+
*/
|
|
265
|
+
searchLogs(filter, opts) {
|
|
266
|
+
return paginate((cursor, signal) => this.searchLogsPage({
|
|
267
|
+
...filter,
|
|
268
|
+
cursor
|
|
269
|
+
}, {
|
|
270
|
+
...opts,
|
|
271
|
+
signal
|
|
272
|
+
}), opts?.signal);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Search logs for a single page.
|
|
276
|
+
* Use this for manual pagination.
|
|
277
|
+
*/
|
|
278
|
+
async searchLogsPage(filter, opts) {
|
|
279
|
+
const validatedFilter = _kopai_core.dataFilterSchemas.logsDataFilterSchema.parse(filter);
|
|
280
|
+
return request(`${this.baseUrl}/logs/search`, logsResponseSchema, {
|
|
281
|
+
method: "POST",
|
|
282
|
+
body: validatedFilter,
|
|
283
|
+
...opts,
|
|
284
|
+
baseHeaders: this.baseHeaders,
|
|
285
|
+
fetchFn: this.fetchFn,
|
|
286
|
+
defaultTimeout: this.defaultTimeout
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Search metrics with auto-pagination.
|
|
291
|
+
* Yields individual metric rows.
|
|
292
|
+
*/
|
|
293
|
+
searchMetrics(filter, opts) {
|
|
294
|
+
return paginate((cursor, signal) => this.searchMetricsPage({
|
|
295
|
+
...filter,
|
|
296
|
+
cursor
|
|
297
|
+
}, {
|
|
298
|
+
...opts,
|
|
299
|
+
signal
|
|
300
|
+
}), opts?.signal);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Search metrics for a single page.
|
|
304
|
+
* Use this for manual pagination.
|
|
305
|
+
*/
|
|
306
|
+
async searchMetricsPage(filter, opts) {
|
|
307
|
+
const validatedFilter = _kopai_core.dataFilterSchemas.metricsDataFilterSchema.parse(filter);
|
|
308
|
+
return request(`${this.baseUrl}/metrics/search`, metricsResponseSchema, {
|
|
309
|
+
method: "POST",
|
|
310
|
+
body: validatedFilter,
|
|
311
|
+
...opts,
|
|
312
|
+
baseHeaders: this.baseHeaders,
|
|
313
|
+
fetchFn: this.fetchFn,
|
|
314
|
+
defaultTimeout: this.defaultTimeout
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Discover available metrics and their attributes.
|
|
319
|
+
*/
|
|
320
|
+
async discoverMetrics(opts) {
|
|
321
|
+
return request(`${this.baseUrl}/metrics/discover`, metricsDiscoverySchema, {
|
|
322
|
+
method: "GET",
|
|
323
|
+
...opts,
|
|
324
|
+
baseHeaders: this.baseHeaders,
|
|
325
|
+
fetchFn: this.fetchFn,
|
|
326
|
+
defaultTimeout: this.defaultTimeout
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
//#endregion
|
|
332
|
+
exports.KopaiClient = KopaiClient;
|
|
333
|
+
exports.KopaiError = KopaiError;
|
|
334
|
+
exports.KopaiNetworkError = KopaiNetworkError;
|
|
335
|
+
exports.KopaiTimeoutError = KopaiTimeoutError;
|
|
336
|
+
exports.KopaiValidationError = KopaiValidationError;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { dataFilterSchemas, datasource, denormalizedSignals } from "@kopai/core";
|
|
2
|
+
import { ZodError } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/types.d.ts
|
|
5
|
+
type TracesDataFilter = dataFilterSchemas.TracesDataFilter;
|
|
6
|
+
type LogsDataFilter = dataFilterSchemas.LogsDataFilter;
|
|
7
|
+
type MetricsDataFilter = dataFilterSchemas.MetricsDataFilter;
|
|
8
|
+
type OtelTracesRow = denormalizedSignals.OtelTracesRow;
|
|
9
|
+
type OtelLogsRow = denormalizedSignals.OtelLogsRow;
|
|
10
|
+
type OtelMetricsRow = denormalizedSignals.OtelMetricsRow;
|
|
11
|
+
type MetricsDiscoveryResult = datasource.MetricsDiscoveryResult;
|
|
12
|
+
type DiscoveredMetric = datasource.DiscoveredMetric;
|
|
13
|
+
type DiscoveredMetricAttributes = datasource.DiscoveredMetricAttributes;
|
|
14
|
+
/** Options for KopaiClient constructor */
|
|
15
|
+
interface KopaiClientOptions {
|
|
16
|
+
/** Base URL for the Kopai API */
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
/** Bearer token for authentication */
|
|
19
|
+
token?: string;
|
|
20
|
+
/** Additional headers to include in requests */
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
/** Custom fetch implementation (defaults to global fetch) */
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
/** Default timeout in milliseconds (default: 30000) */
|
|
25
|
+
timeout?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Options for individual requests */
|
|
28
|
+
interface RequestOptions {
|
|
29
|
+
/** AbortSignal for cancellation */
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
/** Request-specific timeout in milliseconds */
|
|
32
|
+
timeout?: number;
|
|
33
|
+
}
|
|
34
|
+
/** Paginated search result */
|
|
35
|
+
interface SearchResult<T> {
|
|
36
|
+
/** Array of items */
|
|
37
|
+
data: T[];
|
|
38
|
+
/** Cursor for next page, null if no more pages */
|
|
39
|
+
nextCursor: string | null;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/client.d.ts
|
|
43
|
+
declare class KopaiClient {
|
|
44
|
+
private readonly baseUrl;
|
|
45
|
+
private readonly fetchFn;
|
|
46
|
+
private readonly defaultTimeout;
|
|
47
|
+
private readonly baseHeaders;
|
|
48
|
+
constructor(options: KopaiClientOptions);
|
|
49
|
+
/**
|
|
50
|
+
* Get all spans for a specific trace by ID.
|
|
51
|
+
*/
|
|
52
|
+
getTrace(traceId: string, opts?: RequestOptions): Promise<OtelTracesRow[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Search traces with auto-pagination.
|
|
55
|
+
* Yields individual trace rows.
|
|
56
|
+
*/
|
|
57
|
+
searchTraces(filter: Omit<TracesDataFilter, "cursor">, opts?: RequestOptions): AsyncIterable<OtelTracesRow>;
|
|
58
|
+
/**
|
|
59
|
+
* Search traces for a single page.
|
|
60
|
+
* Use this for manual pagination.
|
|
61
|
+
*/
|
|
62
|
+
searchTracesPage(filter: TracesDataFilter, opts?: RequestOptions): Promise<SearchResult<OtelTracesRow>>;
|
|
63
|
+
/**
|
|
64
|
+
* Search logs with auto-pagination.
|
|
65
|
+
* Yields individual log rows.
|
|
66
|
+
*/
|
|
67
|
+
searchLogs(filter: Omit<LogsDataFilter, "cursor">, opts?: RequestOptions): AsyncIterable<OtelLogsRow>;
|
|
68
|
+
/**
|
|
69
|
+
* Search logs for a single page.
|
|
70
|
+
* Use this for manual pagination.
|
|
71
|
+
*/
|
|
72
|
+
searchLogsPage(filter: LogsDataFilter, opts?: RequestOptions): Promise<SearchResult<OtelLogsRow>>;
|
|
73
|
+
/**
|
|
74
|
+
* Search metrics with auto-pagination.
|
|
75
|
+
* Yields individual metric rows.
|
|
76
|
+
*/
|
|
77
|
+
searchMetrics(filter: Omit<MetricsDataFilter, "cursor">, opts?: RequestOptions): AsyncIterable<OtelMetricsRow>;
|
|
78
|
+
/**
|
|
79
|
+
* Search metrics for a single page.
|
|
80
|
+
* Use this for manual pagination.
|
|
81
|
+
*/
|
|
82
|
+
searchMetricsPage(filter: MetricsDataFilter, opts?: RequestOptions): Promise<SearchResult<OtelMetricsRow>>;
|
|
83
|
+
/**
|
|
84
|
+
* Discover available metrics and their attributes.
|
|
85
|
+
*/
|
|
86
|
+
discoverMetrics(opts?: RequestOptions): Promise<MetricsDiscoveryResult>;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/errors.d.ts
|
|
90
|
+
/**
|
|
91
|
+
* Base error for Kopai API errors (non-2xx responses).
|
|
92
|
+
* Follows RFC 7807 Problem Details format.
|
|
93
|
+
*/
|
|
94
|
+
declare class KopaiError extends Error {
|
|
95
|
+
/** Error code from problem type */
|
|
96
|
+
code: string;
|
|
97
|
+
/** HTTP status code */
|
|
98
|
+
status: number;
|
|
99
|
+
/** URI identifying the problem type */
|
|
100
|
+
type: string;
|
|
101
|
+
/** Human-readable explanation */
|
|
102
|
+
detail?: string;
|
|
103
|
+
constructor(options: {
|
|
104
|
+
message: string;
|
|
105
|
+
code: string;
|
|
106
|
+
status: number;
|
|
107
|
+
type: string;
|
|
108
|
+
detail?: string;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Network-level errors (connection failed, DNS resolution, etc.)
|
|
113
|
+
*/
|
|
114
|
+
declare class KopaiNetworkError extends Error {
|
|
115
|
+
/** Original error that caused this */
|
|
116
|
+
cause?: Error;
|
|
117
|
+
constructor(message: string, cause?: Error);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Request timeout errors
|
|
121
|
+
*/
|
|
122
|
+
declare class KopaiTimeoutError extends Error {
|
|
123
|
+
/** Timeout duration in milliseconds */
|
|
124
|
+
timeout: number;
|
|
125
|
+
constructor(timeout: number);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Response validation errors (Zod schema validation failed)
|
|
129
|
+
*/
|
|
130
|
+
declare class KopaiValidationError extends Error {
|
|
131
|
+
/** Original Zod error */
|
|
132
|
+
zodError: ZodError;
|
|
133
|
+
constructor(zodError: ZodError);
|
|
134
|
+
}
|
|
135
|
+
//#endregion
|
|
136
|
+
export { type DiscoveredMetric, type DiscoveredMetricAttributes, KopaiClient, type KopaiClientOptions, KopaiError, KopaiNetworkError, KopaiTimeoutError, KopaiValidationError, type LogsDataFilter, type MetricsDataFilter, type MetricsDiscoveryResult, type OtelLogsRow, type OtelMetricsRow, type OtelTracesRow, type RequestOptions, type SearchResult, type TracesDataFilter };
|
|
137
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/client.ts","../src/errors.ts"],"mappings":";;;;KAUY,gBAAA,GAAmB,iBAAA,CAAkB,gBAA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,EAAQ,cAAA,EACR,IAAA,GAAO,cAAA,GACN,OAAA,CAAQ,YAAA,CAAa,WAAA;;;;AD1I1B;EC6JE,aAAA,CACE,MAAA,EAAQ,IAAA,CAAK,iBAAA,aACb,IAAA,GAAO,cAAA,GACN,aAAA,CAAc,cAAA;;;;AD/JnB;EC2KQ,iBAAA,CACJ,MAAA,EAAQ,iBAAA,EACR,IAAA,GAAO,cAAA,GACN,OAAA,CAAQ,YAAA,CAAa,cAAA;;;;EAkBlB,eAAA,CACJ,IAAA,GAAO,cAAA,GACN,OAAA,CAAQ,sBAAA;AAAA;;;;;;AD3Mb;cEJa,UAAA,SAAmB,KAAA;;EAE9B,IAAA;EFE+D;EEA/D,MAAA;EFCwB;EECxB,IAAA;EFD2B;EEG3B,MAAA;cAEY,OAAA;IACV,OAAA;IACA,IAAA;IACA,MAAA;IACA,IAAA;IACA,MAAA;EAAA;AAAA;;;AFNJ;cEoBa,iBAAA,SAA0B,KAAA;;EAErC,KAAA,GAAQ,KAAA;cAEI,OAAA,UAAiB,KAAA,GAAQ,KAAA;AAAA;;;;cAU1B,iBAAA,SAA0B,KAAA;EF/B3B;EEiCV,OAAA;cAEY,OAAA;AAAA;;AFlCd;;cE4Ca,oBAAA,SAA6B,KAAA;EF5CX;EE8C7B,QAAA,EAAU,QAAA;cAEE,QAAA,EAAU,QAAA;AAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { dataFilterSchemas, datasource, denormalizedSignals } from "@kopai/core";
|
|
2
|
+
import { ZodError } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/types.d.ts
|
|
5
|
+
type TracesDataFilter = dataFilterSchemas.TracesDataFilter;
|
|
6
|
+
type LogsDataFilter = dataFilterSchemas.LogsDataFilter;
|
|
7
|
+
type MetricsDataFilter = dataFilterSchemas.MetricsDataFilter;
|
|
8
|
+
type OtelTracesRow = denormalizedSignals.OtelTracesRow;
|
|
9
|
+
type OtelLogsRow = denormalizedSignals.OtelLogsRow;
|
|
10
|
+
type OtelMetricsRow = denormalizedSignals.OtelMetricsRow;
|
|
11
|
+
type MetricsDiscoveryResult = datasource.MetricsDiscoveryResult;
|
|
12
|
+
type DiscoveredMetric = datasource.DiscoveredMetric;
|
|
13
|
+
type DiscoveredMetricAttributes = datasource.DiscoveredMetricAttributes;
|
|
14
|
+
/** Options for KopaiClient constructor */
|
|
15
|
+
interface KopaiClientOptions {
|
|
16
|
+
/** Base URL for the Kopai API */
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
/** Bearer token for authentication */
|
|
19
|
+
token?: string;
|
|
20
|
+
/** Additional headers to include in requests */
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
/** Custom fetch implementation (defaults to global fetch) */
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
/** Default timeout in milliseconds (default: 30000) */
|
|
25
|
+
timeout?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Options for individual requests */
|
|
28
|
+
interface RequestOptions {
|
|
29
|
+
/** AbortSignal for cancellation */
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
/** Request-specific timeout in milliseconds */
|
|
32
|
+
timeout?: number;
|
|
33
|
+
}
|
|
34
|
+
/** Paginated search result */
|
|
35
|
+
interface SearchResult<T> {
|
|
36
|
+
/** Array of items */
|
|
37
|
+
data: T[];
|
|
38
|
+
/** Cursor for next page, null if no more pages */
|
|
39
|
+
nextCursor: string | null;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/client.d.ts
|
|
43
|
+
declare class KopaiClient {
|
|
44
|
+
private readonly baseUrl;
|
|
45
|
+
private readonly fetchFn;
|
|
46
|
+
private readonly defaultTimeout;
|
|
47
|
+
private readonly baseHeaders;
|
|
48
|
+
constructor(options: KopaiClientOptions);
|
|
49
|
+
/**
|
|
50
|
+
* Get all spans for a specific trace by ID.
|
|
51
|
+
*/
|
|
52
|
+
getTrace(traceId: string, opts?: RequestOptions): Promise<OtelTracesRow[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Search traces with auto-pagination.
|
|
55
|
+
* Yields individual trace rows.
|
|
56
|
+
*/
|
|
57
|
+
searchTraces(filter: Omit<TracesDataFilter, "cursor">, opts?: RequestOptions): AsyncIterable<OtelTracesRow>;
|
|
58
|
+
/**
|
|
59
|
+
* Search traces for a single page.
|
|
60
|
+
* Use this for manual pagination.
|
|
61
|
+
*/
|
|
62
|
+
searchTracesPage(filter: TracesDataFilter, opts?: RequestOptions): Promise<SearchResult<OtelTracesRow>>;
|
|
63
|
+
/**
|
|
64
|
+
* Search logs with auto-pagination.
|
|
65
|
+
* Yields individual log rows.
|
|
66
|
+
*/
|
|
67
|
+
searchLogs(filter: Omit<LogsDataFilter, "cursor">, opts?: RequestOptions): AsyncIterable<OtelLogsRow>;
|
|
68
|
+
/**
|
|
69
|
+
* Search logs for a single page.
|
|
70
|
+
* Use this for manual pagination.
|
|
71
|
+
*/
|
|
72
|
+
searchLogsPage(filter: LogsDataFilter, opts?: RequestOptions): Promise<SearchResult<OtelLogsRow>>;
|
|
73
|
+
/**
|
|
74
|
+
* Search metrics with auto-pagination.
|
|
75
|
+
* Yields individual metric rows.
|
|
76
|
+
*/
|
|
77
|
+
searchMetrics(filter: Omit<MetricsDataFilter, "cursor">, opts?: RequestOptions): AsyncIterable<OtelMetricsRow>;
|
|
78
|
+
/**
|
|
79
|
+
* Search metrics for a single page.
|
|
80
|
+
* Use this for manual pagination.
|
|
81
|
+
*/
|
|
82
|
+
searchMetricsPage(filter: MetricsDataFilter, opts?: RequestOptions): Promise<SearchResult<OtelMetricsRow>>;
|
|
83
|
+
/**
|
|
84
|
+
* Discover available metrics and their attributes.
|
|
85
|
+
*/
|
|
86
|
+
discoverMetrics(opts?: RequestOptions): Promise<MetricsDiscoveryResult>;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/errors.d.ts
|
|
90
|
+
/**
|
|
91
|
+
* Base error for Kopai API errors (non-2xx responses).
|
|
92
|
+
* Follows RFC 7807 Problem Details format.
|
|
93
|
+
*/
|
|
94
|
+
declare class KopaiError extends Error {
|
|
95
|
+
/** Error code from problem type */
|
|
96
|
+
code: string;
|
|
97
|
+
/** HTTP status code */
|
|
98
|
+
status: number;
|
|
99
|
+
/** URI identifying the problem type */
|
|
100
|
+
type: string;
|
|
101
|
+
/** Human-readable explanation */
|
|
102
|
+
detail?: string;
|
|
103
|
+
constructor(options: {
|
|
104
|
+
message: string;
|
|
105
|
+
code: string;
|
|
106
|
+
status: number;
|
|
107
|
+
type: string;
|
|
108
|
+
detail?: string;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Network-level errors (connection failed, DNS resolution, etc.)
|
|
113
|
+
*/
|
|
114
|
+
declare class KopaiNetworkError extends Error {
|
|
115
|
+
/** Original error that caused this */
|
|
116
|
+
cause?: Error;
|
|
117
|
+
constructor(message: string, cause?: Error);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Request timeout errors
|
|
121
|
+
*/
|
|
122
|
+
declare class KopaiTimeoutError extends Error {
|
|
123
|
+
/** Timeout duration in milliseconds */
|
|
124
|
+
timeout: number;
|
|
125
|
+
constructor(timeout: number);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Response validation errors (Zod schema validation failed)
|
|
129
|
+
*/
|
|
130
|
+
declare class KopaiValidationError extends Error {
|
|
131
|
+
/** Original Zod error */
|
|
132
|
+
zodError: ZodError;
|
|
133
|
+
constructor(zodError: ZodError);
|
|
134
|
+
}
|
|
135
|
+
//#endregion
|
|
136
|
+
export { type DiscoveredMetric, type DiscoveredMetricAttributes, KopaiClient, type KopaiClientOptions, KopaiError, KopaiNetworkError, KopaiTimeoutError, KopaiValidationError, type LogsDataFilter, type MetricsDataFilter, type MetricsDiscoveryResult, type OtelLogsRow, type OtelMetricsRow, type OtelTracesRow, type RequestOptions, type SearchResult, type TracesDataFilter };
|
|
137
|
+
//#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,CAAQ,YAAA,CAAa,aAAA;;;;ADxG1B;EC2HE,UAAA,CACE,MAAA,EAAQ,IAAA,CAAK,cAAA,aACb,IAAA,GAAO,cAAA,GACN,aAAA,CAAc,WAAA;;;;AD7HnB;ECyIQ,cAAA,CACJ,MAAA,EAAQ,cAAA,EACR,IAAA,GAAO,cAAA,GACN,OAAA,CAAQ,YAAA,CAAa,WAAA;;;;AD1I1B;EC6JE,aAAA,CACE,MAAA,EAAQ,IAAA,CAAK,iBAAA,aACb,IAAA,GAAO,cAAA,GACN,aAAA,CAAc,cAAA;;;;AD/JnB;EC2KQ,iBAAA,CACJ,MAAA,EAAQ,iBAAA,EACR,IAAA,GAAO,cAAA,GACN,OAAA,CAAQ,YAAA,CAAa,cAAA;;;;EAkBlB,eAAA,CACJ,IAAA,GAAO,cAAA,GACN,OAAA,CAAQ,sBAAA;AAAA;;;;;;AD3Mb;cEJa,UAAA,SAAmB,KAAA;;EAE9B,IAAA;EFE+D;EEA/D,MAAA;EFCwB;EECxB,IAAA;EFD2B;EEG3B,MAAA;cAEY,OAAA;IACV,OAAA;IACA,IAAA;IACA,MAAA;IACA,IAAA;IACA,MAAA;EAAA;AAAA;;;AFNJ;cEoBa,iBAAA,SAA0B,KAAA;;EAErC,KAAA,GAAQ,KAAA;cAEI,OAAA,UAAiB,KAAA,GAAQ,KAAA;AAAA;;;;cAU1B,iBAAA,SAA0B,KAAA;EF/B3B;EEiCV,OAAA;cAEY,OAAA;AAAA;;AFlCd;;cE4Ca,oBAAA,SAA6B,KAAA;EF5CX;EE8C7B,QAAA,EAAU,QAAA;cAEE,QAAA,EAAU,QAAA;AAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { dataFilterSchemas, denormalizedSignals } from "@kopai/core";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/errors.ts
|
|
5
|
+
/**
|
|
6
|
+
* Base error for Kopai API errors (non-2xx responses).
|
|
7
|
+
* Follows RFC 7807 Problem Details format.
|
|
8
|
+
*/
|
|
9
|
+
var KopaiError = class extends Error {
|
|
10
|
+
/** Error code from problem type */
|
|
11
|
+
code;
|
|
12
|
+
/** HTTP status code */
|
|
13
|
+
status;
|
|
14
|
+
/** URI identifying the problem type */
|
|
15
|
+
type;
|
|
16
|
+
/** Human-readable explanation */
|
|
17
|
+
detail;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
super(options.message);
|
|
20
|
+
this.name = "KopaiError";
|
|
21
|
+
this.code = options.code;
|
|
22
|
+
this.status = options.status;
|
|
23
|
+
this.type = options.type;
|
|
24
|
+
this.detail = options.detail;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Network-level errors (connection failed, DNS resolution, etc.)
|
|
29
|
+
*/
|
|
30
|
+
var KopaiNetworkError = class extends Error {
|
|
31
|
+
/** Original error that caused this */
|
|
32
|
+
cause;
|
|
33
|
+
constructor(message, cause) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "KopaiNetworkError";
|
|
36
|
+
this.cause = cause;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Request timeout errors
|
|
41
|
+
*/
|
|
42
|
+
var KopaiTimeoutError = class extends Error {
|
|
43
|
+
/** Timeout duration in milliseconds */
|
|
44
|
+
timeout;
|
|
45
|
+
constructor(timeout) {
|
|
46
|
+
super(`Request timed out after ${timeout}ms`);
|
|
47
|
+
this.name = "KopaiTimeoutError";
|
|
48
|
+
this.timeout = timeout;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Response validation errors (Zod schema validation failed)
|
|
53
|
+
*/
|
|
54
|
+
var KopaiValidationError = class extends Error {
|
|
55
|
+
/** Original Zod error */
|
|
56
|
+
zodError;
|
|
57
|
+
constructor(zodError) {
|
|
58
|
+
super(`Response validation failed: ${zodError.message}`);
|
|
59
|
+
this.name = "KopaiValidationError";
|
|
60
|
+
this.zodError = zodError;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/request.ts
|
|
66
|
+
const DEFAULT_TIMEOUT$1 = 3e4;
|
|
67
|
+
/**
|
|
68
|
+
* Internal fetch wrapper with timeout, signal, and Zod validation.
|
|
69
|
+
*/
|
|
70
|
+
async function request(url, schema, options) {
|
|
71
|
+
const { method = "GET", body, headers = {}, baseHeaders = {}, signal, timeout, fetchFn, defaultTimeout } = options;
|
|
72
|
+
const effectiveTimeout = timeout ?? defaultTimeout ?? DEFAULT_TIMEOUT$1;
|
|
73
|
+
const timeoutController = new AbortController();
|
|
74
|
+
const timeoutId = setTimeout(() => timeoutController.abort(), effectiveTimeout);
|
|
75
|
+
const combinedSignal = signal ? AbortSignal.any([signal, timeoutController.signal]) : timeoutController.signal;
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetchFn(url, {
|
|
78
|
+
method,
|
|
79
|
+
headers: {
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
...baseHeaders,
|
|
82
|
+
...headers
|
|
83
|
+
},
|
|
84
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
85
|
+
signal: combinedSignal
|
|
86
|
+
});
|
|
87
|
+
clearTimeout(timeoutId);
|
|
88
|
+
if (!response.ok) await handleErrorResponse(response);
|
|
89
|
+
const data = await response.json();
|
|
90
|
+
const result = schema.safeParse(data);
|
|
91
|
+
if (!result.success) throw new KopaiValidationError(result.error);
|
|
92
|
+
return result.data;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
clearTimeout(timeoutId);
|
|
95
|
+
if (error instanceof KopaiError || error instanceof KopaiValidationError || error instanceof KopaiTimeoutError || error instanceof KopaiNetworkError) throw error;
|
|
96
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
97
|
+
if (timeoutController.signal.aborted) throw new KopaiTimeoutError(effectiveTimeout);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
if (error instanceof TypeError) throw new KopaiNetworkError(error.message || "Network request failed", error);
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function handleErrorResponse(response) {
|
|
105
|
+
let body;
|
|
106
|
+
try {
|
|
107
|
+
body = await response.json();
|
|
108
|
+
} catch {
|
|
109
|
+
body = null;
|
|
110
|
+
}
|
|
111
|
+
if (body && typeof body === "object") {
|
|
112
|
+
const problem = body;
|
|
113
|
+
throw new KopaiError({
|
|
114
|
+
message: problem.title || problem.message || `HTTP ${response.status}`,
|
|
115
|
+
code: problem.code || `HTTP_${response.status}`,
|
|
116
|
+
status: response.status,
|
|
117
|
+
type: problem.type || `about:blank`,
|
|
118
|
+
detail: problem.detail
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
throw new KopaiError({
|
|
122
|
+
message: `HTTP ${response.status}`,
|
|
123
|
+
code: `HTTP_${response.status}`,
|
|
124
|
+
status: response.status,
|
|
125
|
+
type: "about:blank"
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/pagination.ts
|
|
131
|
+
/**
|
|
132
|
+
* Creates an async iterator that auto-paginates through results.
|
|
133
|
+
* Yields individual items from each page.
|
|
134
|
+
*/
|
|
135
|
+
async function* paginate(fetcher, signal) {
|
|
136
|
+
let cursor;
|
|
137
|
+
do {
|
|
138
|
+
signal?.throwIfAborted();
|
|
139
|
+
const result = await fetcher(cursor, signal);
|
|
140
|
+
for (const item of result.data) yield item;
|
|
141
|
+
cursor = result.nextCursor ?? void 0;
|
|
142
|
+
} while (cursor);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/client.ts
|
|
147
|
+
const DEFAULT_TIMEOUT = 3e4;
|
|
148
|
+
const tracesResponseSchema = z.object({
|
|
149
|
+
data: z.array(denormalizedSignals.otelTracesSchema),
|
|
150
|
+
nextCursor: z.string().nullable()
|
|
151
|
+
});
|
|
152
|
+
const logsResponseSchema = z.object({
|
|
153
|
+
data: z.array(denormalizedSignals.otelLogsSchema),
|
|
154
|
+
nextCursor: z.string().nullable()
|
|
155
|
+
});
|
|
156
|
+
const metricsResponseSchema = z.object({
|
|
157
|
+
data: z.array(denormalizedSignals.otelMetricsSchema),
|
|
158
|
+
nextCursor: z.string().nullable()
|
|
159
|
+
});
|
|
160
|
+
const metricsDiscoverySchema = z.object({ metrics: z.array(z.object({
|
|
161
|
+
name: z.string(),
|
|
162
|
+
type: z.enum([
|
|
163
|
+
"Gauge",
|
|
164
|
+
"Sum",
|
|
165
|
+
"Histogram",
|
|
166
|
+
"ExponentialHistogram",
|
|
167
|
+
"Summary"
|
|
168
|
+
]),
|
|
169
|
+
unit: z.string().optional(),
|
|
170
|
+
description: z.string().optional(),
|
|
171
|
+
attributes: z.object({
|
|
172
|
+
values: z.record(z.string(), z.array(z.string())),
|
|
173
|
+
_truncated: z.boolean().optional()
|
|
174
|
+
}),
|
|
175
|
+
resourceAttributes: z.object({
|
|
176
|
+
values: z.record(z.string(), z.array(z.string())),
|
|
177
|
+
_truncated: z.boolean().optional()
|
|
178
|
+
})
|
|
179
|
+
})) });
|
|
180
|
+
var KopaiClient = class {
|
|
181
|
+
baseUrl;
|
|
182
|
+
fetchFn;
|
|
183
|
+
defaultTimeout;
|
|
184
|
+
baseHeaders;
|
|
185
|
+
constructor(options) {
|
|
186
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
187
|
+
this.fetchFn = options.fetch ?? fetch;
|
|
188
|
+
this.defaultTimeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
189
|
+
this.baseHeaders = { ...options.headers };
|
|
190
|
+
if (options.token) this.baseHeaders["Authorization"] = `Bearer ${options.token}`;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get all spans for a specific trace by ID.
|
|
194
|
+
*/
|
|
195
|
+
async getTrace(traceId, opts) {
|
|
196
|
+
const schema = z.array(denormalizedSignals.otelTracesSchema);
|
|
197
|
+
return request(`${this.baseUrl}/traces/${traceId}`, schema, {
|
|
198
|
+
method: "GET",
|
|
199
|
+
...opts,
|
|
200
|
+
baseHeaders: this.baseHeaders,
|
|
201
|
+
fetchFn: this.fetchFn,
|
|
202
|
+
defaultTimeout: this.defaultTimeout
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Search traces with auto-pagination.
|
|
207
|
+
* Yields individual trace rows.
|
|
208
|
+
*/
|
|
209
|
+
searchTraces(filter, opts) {
|
|
210
|
+
return paginate((cursor, signal) => this.searchTracesPage({
|
|
211
|
+
...filter,
|
|
212
|
+
cursor
|
|
213
|
+
}, {
|
|
214
|
+
...opts,
|
|
215
|
+
signal
|
|
216
|
+
}), opts?.signal);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Search traces for a single page.
|
|
220
|
+
* Use this for manual pagination.
|
|
221
|
+
*/
|
|
222
|
+
async searchTracesPage(filter, opts) {
|
|
223
|
+
const validatedFilter = dataFilterSchemas.tracesDataFilterSchema.parse(filter);
|
|
224
|
+
return request(`${this.baseUrl}/traces/search`, tracesResponseSchema, {
|
|
225
|
+
method: "POST",
|
|
226
|
+
body: validatedFilter,
|
|
227
|
+
...opts,
|
|
228
|
+
baseHeaders: this.baseHeaders,
|
|
229
|
+
fetchFn: this.fetchFn,
|
|
230
|
+
defaultTimeout: this.defaultTimeout
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Search logs with auto-pagination.
|
|
235
|
+
* Yields individual log rows.
|
|
236
|
+
*/
|
|
237
|
+
searchLogs(filter, opts) {
|
|
238
|
+
return paginate((cursor, signal) => this.searchLogsPage({
|
|
239
|
+
...filter,
|
|
240
|
+
cursor
|
|
241
|
+
}, {
|
|
242
|
+
...opts,
|
|
243
|
+
signal
|
|
244
|
+
}), opts?.signal);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Search logs for a single page.
|
|
248
|
+
* Use this for manual pagination.
|
|
249
|
+
*/
|
|
250
|
+
async searchLogsPage(filter, opts) {
|
|
251
|
+
const validatedFilter = dataFilterSchemas.logsDataFilterSchema.parse(filter);
|
|
252
|
+
return request(`${this.baseUrl}/logs/search`, logsResponseSchema, {
|
|
253
|
+
method: "POST",
|
|
254
|
+
body: validatedFilter,
|
|
255
|
+
...opts,
|
|
256
|
+
baseHeaders: this.baseHeaders,
|
|
257
|
+
fetchFn: this.fetchFn,
|
|
258
|
+
defaultTimeout: this.defaultTimeout
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Search metrics with auto-pagination.
|
|
263
|
+
* Yields individual metric rows.
|
|
264
|
+
*/
|
|
265
|
+
searchMetrics(filter, opts) {
|
|
266
|
+
return paginate((cursor, signal) => this.searchMetricsPage({
|
|
267
|
+
...filter,
|
|
268
|
+
cursor
|
|
269
|
+
}, {
|
|
270
|
+
...opts,
|
|
271
|
+
signal
|
|
272
|
+
}), opts?.signal);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Search metrics for a single page.
|
|
276
|
+
* Use this for manual pagination.
|
|
277
|
+
*/
|
|
278
|
+
async searchMetricsPage(filter, opts) {
|
|
279
|
+
const validatedFilter = dataFilterSchemas.metricsDataFilterSchema.parse(filter);
|
|
280
|
+
return request(`${this.baseUrl}/metrics/search`, metricsResponseSchema, {
|
|
281
|
+
method: "POST",
|
|
282
|
+
body: validatedFilter,
|
|
283
|
+
...opts,
|
|
284
|
+
baseHeaders: this.baseHeaders,
|
|
285
|
+
fetchFn: this.fetchFn,
|
|
286
|
+
defaultTimeout: this.defaultTimeout
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Discover available metrics and their attributes.
|
|
291
|
+
*/
|
|
292
|
+
async discoverMetrics(opts) {
|
|
293
|
+
return request(`${this.baseUrl}/metrics/discover`, metricsDiscoverySchema, {
|
|
294
|
+
method: "GET",
|
|
295
|
+
...opts,
|
|
296
|
+
baseHeaders: this.baseHeaders,
|
|
297
|
+
fetchFn: this.fetchFn,
|
|
298
|
+
defaultTimeout: this.defaultTimeout
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
//#endregion
|
|
304
|
+
export { KopaiClient, KopaiError, KopaiNetworkError, KopaiTimeoutError, KopaiValidationError };
|
|
305
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["DEFAULT_TIMEOUT"],"sources":["../src/errors.ts","../src/request.ts","../src/pagination.ts","../src/client.ts"],"sourcesContent":["import type { ZodError } from \"zod\";\n\n/**\n * Base error for Kopai API errors (non-2xx responses).\n * Follows RFC 7807 Problem Details format.\n */\nexport class KopaiError extends Error {\n /** Error code from problem type */\n code: string;\n /** HTTP status code */\n status: number;\n /** URI identifying the problem type */\n type: string;\n /** Human-readable explanation */\n detail?: string;\n\n constructor(options: {\n message: string;\n code: string;\n status: number;\n type: string;\n detail?: string;\n }) {\n super(options.message);\n this.name = \"KopaiError\";\n this.code = options.code;\n this.status = options.status;\n this.type = options.type;\n this.detail = options.detail;\n }\n}\n\n/**\n * Network-level errors (connection failed, DNS resolution, etc.)\n */\nexport class KopaiNetworkError extends Error {\n /** Original error that caused this */\n cause?: Error;\n\n constructor(message: string, cause?: Error) {\n super(message);\n this.name = \"KopaiNetworkError\";\n this.cause = cause;\n }\n}\n\n/**\n * Request timeout errors\n */\nexport class KopaiTimeoutError extends Error {\n /** Timeout duration in milliseconds */\n timeout: number;\n\n constructor(timeout: number) {\n super(`Request timed out after ${timeout}ms`);\n this.name = \"KopaiTimeoutError\";\n this.timeout = timeout;\n }\n}\n\n/**\n * Response validation errors (Zod schema validation failed)\n */\nexport class KopaiValidationError extends Error {\n /** Original Zod error */\n zodError: ZodError;\n\n constructor(zodError: ZodError) {\n super(`Response validation failed: ${zodError.message}`);\n this.name = \"KopaiValidationError\";\n this.zodError = zodError;\n }\n}\n","import type { ZodType } from \"zod\";\nimport {\n KopaiError,\n KopaiNetworkError,\n KopaiTimeoutError,\n KopaiValidationError,\n} from \"./errors.js\";\nimport type { RequestOptions } from \"./types.js\";\n\nconst DEFAULT_TIMEOUT = 30_000;\n\ninterface FetchOptions extends RequestOptions {\n method?: \"GET\" | \"POST\";\n body?: unknown;\n headers?: Record<string, string>;\n baseHeaders?: Record<string, string>;\n fetchFn: typeof fetch;\n defaultTimeout: number;\n}\n\n/**\n * Internal fetch wrapper with timeout, signal, and Zod validation.\n */\nexport async function request<T>(\n url: string,\n schema: ZodType<T>,\n options: FetchOptions\n): Promise<T> {\n const {\n method = \"GET\",\n body,\n headers = {},\n baseHeaders = {},\n signal,\n timeout,\n fetchFn,\n defaultTimeout,\n } = options;\n\n const effectiveTimeout = timeout ?? defaultTimeout ?? DEFAULT_TIMEOUT;\n\n // Create timeout controller\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(\n () => timeoutController.abort(),\n effectiveTimeout\n );\n\n // Combine signals if user provided one\n const combinedSignal = signal\n ? AbortSignal.any([signal, timeoutController.signal])\n : timeoutController.signal;\n\n try {\n const response = await fetchFn(url, {\n method,\n headers: {\n \"Content-Type\": \"application/json\",\n ...baseHeaders,\n ...headers,\n },\n body: body ? JSON.stringify(body) : undefined,\n signal: combinedSignal,\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n await handleErrorResponse(response);\n }\n\n const data = await response.json();\n\n // Validate response with Zod schema\n const result = schema.safeParse(data);\n if (!result.success) {\n throw new KopaiValidationError(result.error);\n }\n\n return result.data;\n } catch (error) {\n clearTimeout(timeoutId);\n\n // Re-throw our errors\n if (\n error instanceof KopaiError ||\n error instanceof KopaiValidationError ||\n error instanceof KopaiTimeoutError ||\n error instanceof KopaiNetworkError\n ) {\n throw error;\n }\n\n // Handle abort\n if (error instanceof DOMException && error.name === \"AbortError\") {\n // Check if it was our timeout or user cancellation\n if (timeoutController.signal.aborted) {\n throw new KopaiTimeoutError(effectiveTimeout);\n }\n // User cancelled\n throw error;\n }\n\n // Network errors\n if (error instanceof TypeError) {\n throw new KopaiNetworkError(\n error.message || \"Network request failed\",\n error\n );\n }\n\n throw error;\n }\n}\n\nasync function handleErrorResponse(response: Response): Promise<never> {\n let body: unknown;\n try {\n body = await response.json();\n } catch {\n body = null;\n }\n\n // Try to parse as RFC 7807 Problem Details\n if (body && typeof body === \"object\") {\n const problem = body as Record<string, unknown>;\n throw new KopaiError({\n message:\n (problem.title as string) ||\n (problem.message as string) ||\n `HTTP ${response.status}`,\n code: (problem.code as string) || `HTTP_${response.status}`,\n status: response.status,\n type: (problem.type as string) || `about:blank`,\n detail: problem.detail as string | undefined,\n });\n }\n\n throw new KopaiError({\n message: `HTTP ${response.status}`,\n code: `HTTP_${response.status}`,\n status: response.status,\n type: \"about:blank\",\n });\n}\n","import type { SearchResult } from \"./types.js\";\n\ntype PageFetcher<T> = (\n cursor: string | undefined,\n signal?: AbortSignal\n) => Promise<SearchResult<T>>;\n\n/**\n * Creates an async iterator that auto-paginates through results.\n * Yields individual items from each page.\n */\nexport async function* paginate<T>(\n fetcher: PageFetcher<T>,\n signal?: AbortSignal\n): AsyncGenerator<T, void, undefined> {\n let cursor: string | undefined;\n\n do {\n signal?.throwIfAborted();\n\n const result = await fetcher(cursor, signal);\n\n for (const item of result.data) {\n yield item;\n }\n\n cursor = result.nextCursor ?? undefined;\n } while (cursor);\n}\n","import { dataFilterSchemas, denormalizedSignals } from \"@kopai/core\";\nimport z from \"zod\";\nimport { request } from \"./request.js\";\nimport { paginate } from \"./pagination.js\";\nimport type {\n KopaiClientOptions,\n RequestOptions,\n SearchResult,\n TracesDataFilter,\n LogsDataFilter,\n MetricsDataFilter,\n OtelTracesRow,\n OtelLogsRow,\n OtelMetricsRow,\n MetricsDiscoveryResult,\n} from \"./types.js\";\n\nconst DEFAULT_TIMEOUT = 30_000;\n\n// Response schemas\nconst tracesResponseSchema = z.object({\n data: z.array(denormalizedSignals.otelTracesSchema),\n nextCursor: z.string().nullable(),\n});\n\nconst logsResponseSchema = z.object({\n data: z.array(denormalizedSignals.otelLogsSchema),\n nextCursor: z.string().nullable(),\n});\n\nconst metricsResponseSchema = z.object({\n data: z.array(denormalizedSignals.otelMetricsSchema),\n nextCursor: z.string().nullable(),\n});\n\nconst metricsDiscoverySchema = z.object({\n metrics: z.array(\n z.object({\n name: z.string(),\n type: z.enum([\n \"Gauge\",\n \"Sum\",\n \"Histogram\",\n \"ExponentialHistogram\",\n \"Summary\",\n ]),\n unit: z.string().optional(),\n description: z.string().optional(),\n attributes: z.object({\n values: z.record(z.string(), z.array(z.string())),\n _truncated: z.boolean().optional(),\n }),\n resourceAttributes: z.object({\n values: z.record(z.string(), z.array(z.string())),\n _truncated: z.boolean().optional(),\n }),\n })\n ),\n});\n\nexport class KopaiClient {\n private readonly baseUrl: string;\n private readonly fetchFn: typeof fetch;\n private readonly defaultTimeout: number;\n private readonly baseHeaders: Record<string, string>;\n\n constructor(options: KopaiClientOptions) {\n this.baseUrl = options.baseUrl.replace(/\\/$/, \"\"); // Remove trailing slash\n this.fetchFn = options.fetch ?? fetch;\n this.defaultTimeout = options.timeout ?? DEFAULT_TIMEOUT;\n\n this.baseHeaders = {\n ...options.headers,\n };\n\n if (options.token) {\n this.baseHeaders[\"Authorization\"] = `Bearer ${options.token}`;\n }\n }\n\n /**\n * Get all spans for a specific trace by ID.\n */\n async getTrace(\n traceId: string,\n opts?: RequestOptions\n ): Promise<OtelTracesRow[]> {\n const schema = z.array(denormalizedSignals.otelTracesSchema);\n return request(`${this.baseUrl}/traces/${traceId}`, schema, {\n method: \"GET\",\n ...opts,\n baseHeaders: this.baseHeaders,\n fetchFn: this.fetchFn,\n defaultTimeout: this.defaultTimeout,\n });\n }\n\n /**\n * Search traces with auto-pagination.\n * Yields individual trace rows.\n */\n searchTraces(\n filter: Omit<TracesDataFilter, \"cursor\">,\n opts?: RequestOptions\n ): AsyncIterable<OtelTracesRow> {\n return paginate(\n (cursor, signal) =>\n this.searchTracesPage({ ...filter, cursor }, { ...opts, signal }),\n opts?.signal\n );\n }\n\n /**\n * Search traces for a single page.\n * Use this for manual pagination.\n */\n async searchTracesPage(\n filter: TracesDataFilter,\n opts?: RequestOptions\n ): Promise<SearchResult<OtelTracesRow>> {\n // Validate filter\n const validatedFilter =\n dataFilterSchemas.tracesDataFilterSchema.parse(filter);\n\n return request(`${this.baseUrl}/traces/search`, tracesResponseSchema, {\n method: \"POST\",\n body: validatedFilter,\n ...opts,\n baseHeaders: this.baseHeaders,\n fetchFn: this.fetchFn,\n defaultTimeout: this.defaultTimeout,\n });\n }\n\n /**\n * Search logs with auto-pagination.\n * Yields individual log rows.\n */\n searchLogs(\n filter: Omit<LogsDataFilter, \"cursor\">,\n opts?: RequestOptions\n ): AsyncIterable<OtelLogsRow> {\n return paginate(\n (cursor, signal) =>\n this.searchLogsPage({ ...filter, cursor }, { ...opts, signal }),\n opts?.signal\n );\n }\n\n /**\n * Search logs for a single page.\n * Use this for manual pagination.\n */\n async searchLogsPage(\n filter: LogsDataFilter,\n opts?: RequestOptions\n ): Promise<SearchResult<OtelLogsRow>> {\n // Validate filter\n const validatedFilter =\n dataFilterSchemas.logsDataFilterSchema.parse(filter);\n\n return request(`${this.baseUrl}/logs/search`, logsResponseSchema, {\n method: \"POST\",\n body: validatedFilter,\n ...opts,\n baseHeaders: this.baseHeaders,\n fetchFn: this.fetchFn,\n defaultTimeout: this.defaultTimeout,\n });\n }\n\n /**\n * Search metrics with auto-pagination.\n * Yields individual metric rows.\n */\n searchMetrics(\n filter: Omit<MetricsDataFilter, \"cursor\">,\n opts?: RequestOptions\n ): AsyncIterable<OtelMetricsRow> {\n return paginate(\n (cursor, signal) =>\n this.searchMetricsPage({ ...filter, cursor }, { ...opts, signal }),\n opts?.signal\n );\n }\n\n /**\n * Search metrics for a single page.\n * Use this for manual pagination.\n */\n async searchMetricsPage(\n filter: MetricsDataFilter,\n opts?: RequestOptions\n ): Promise<SearchResult<OtelMetricsRow>> {\n // Validate filter\n const validatedFilter =\n dataFilterSchemas.metricsDataFilterSchema.parse(filter);\n\n return request(`${this.baseUrl}/metrics/search`, metricsResponseSchema, {\n method: \"POST\",\n body: validatedFilter,\n ...opts,\n baseHeaders: this.baseHeaders,\n fetchFn: this.fetchFn,\n defaultTimeout: this.defaultTimeout,\n });\n }\n\n /**\n * Discover available metrics and their attributes.\n */\n async discoverMetrics(\n opts?: RequestOptions\n ): Promise<MetricsDiscoveryResult> {\n return request(`${this.baseUrl}/metrics/discover`, metricsDiscoverySchema, {\n method: \"GET\",\n ...opts,\n baseHeaders: this.baseHeaders,\n fetchFn: this.fetchFn,\n defaultTimeout: this.defaultTimeout,\n });\n }\n}\n"],"mappings":";;;;;;;;AAMA,IAAa,aAAb,cAAgC,MAAM;;CAEpC;;CAEA;;CAEA;;CAEA;CAEA,YAAY,SAMT;AACD,QAAM,QAAQ,QAAQ;AACtB,OAAK,OAAO;AACZ,OAAK,OAAO,QAAQ;AACpB,OAAK,SAAS,QAAQ;AACtB,OAAK,OAAO,QAAQ;AACpB,OAAK,SAAS,QAAQ;;;;;;AAO1B,IAAa,oBAAb,cAAuC,MAAM;;CAE3C;CAEA,YAAY,SAAiB,OAAe;AAC1C,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,QAAQ;;;;;;AAOjB,IAAa,oBAAb,cAAuC,MAAM;;CAE3C;CAEA,YAAY,SAAiB;AAC3B,QAAM,2BAA2B,QAAQ,IAAI;AAC7C,OAAK,OAAO;AACZ,OAAK,UAAU;;;;;;AAOnB,IAAa,uBAAb,cAA0C,MAAM;;CAE9C;CAEA,YAAY,UAAoB;AAC9B,QAAM,+BAA+B,SAAS,UAAU;AACxD,OAAK,OAAO;AACZ,OAAK,WAAW;;;;;;AC7DpB,MAAMA,oBAAkB;;;;AAcxB,eAAsB,QACpB,KACA,QACA,SACY;CACZ,MAAM,EACJ,SAAS,OACT,MACA,UAAU,EAAE,EACZ,cA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;;;CAOH,MAAM,eACJ,QACA,MACoC;EAEpC,MAAM,kBACJ,kBAAkB,qBAAqB,MAAM,OAAO;AAEtD,SAAO,QAAQ,GAAG,KAAK,QAAQ,eAAe,oBAAoB;GAChE,QAAQ;GACR,MAAM;GACN,GAAG;GACH,aAAa,KAAK;GAClB,SAAS,KAAK;GACd,gBAAgB,KAAK;GACtB,CAAC;;;;;;CAOJ,cACE,QACA,MAC+B;AAC/B,SAAO,UACJ,QAAQ,WACP,KAAK,kBAAkB;GAAE,GAAG;GAAQ;GAAQ,EAAE;GAAE,GAAG;GAAM;GAAQ,CAAC,EACpE,MAAM,OACP;;;;;;CAOH,MAAM,kBACJ,QACA,MACuC;EAEvC,MAAM,kBACJ,kBAAkB,wBAAwB,MAAM,OAAO;AAEzD,SAAO,QAAQ,GAAG,KAAK,QAAQ,kBAAkB,uBAAuB;GACtE,QAAQ;GACR,MAAM;GACN,GAAG;GACH,aAAa,KAAK;GAClB,SAAS,KAAK;GACd,gBAAgB,KAAK;GACtB,CAAC;;;;;CAMJ,MAAM,gBACJ,MACiC;AACjC,SAAO,QAAQ,GAAG,KAAK,QAAQ,oBAAoB,wBAAwB;GACzE,QAAQ;GACR,GAAG;GACH,aAAa,KAAK;GAClB,SAAS,KAAK;GACd,gBAAgB,KAAK;GACtB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kopai/sdk",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"author": "Vladimir Adamic",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kopai-app/kopai-mono.git",
|
|
9
|
+
"directory": "packages/sdk"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/kopai-app/kopai-mono/tree/main/packages/sdk#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kopai-app/kopai-mono/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"zod": "^4.3.6",
|
|
31
|
+
"@kopai/core": "0.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"msw": "^2.8.4",
|
|
35
|
+
"tsdown": "^0.20.1",
|
|
36
|
+
"@kopai/tsconfig": "0.0.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsdown",
|
|
40
|
+
"dev": "tsdown --watch",
|
|
41
|
+
"lint": "eslint src",
|
|
42
|
+
"type-check": "tsc --noEmit",
|
|
43
|
+
"test": "vitest run"
|
|
44
|
+
}
|
|
45
|
+
}
|