@kite-01/sdk 0.0.0-stage → 0.1.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.
package/dist/index.cjs ADDED
@@ -0,0 +1,1988 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/gen/core/bodySerializer.gen.ts
3
+ const jsonBodySerializer = { bodySerializer: (body) => JSON.stringify(body, (_key, value) => typeof value === "bigint" ? value.toString() : value) };
4
+ //#endregion
5
+ //#region src/gen/core/serverSentEvents.gen.ts
6
+ function createSseClient({ onRequest, onSseError, onSseEvent, responseTransformer, responseValidator, sseDefaultRetryDelay, sseMaxRetryAttempts, sseMaxRetryDelay, sseSleepFn, url, ...options }) {
7
+ let lastEventId;
8
+ const sleep = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
9
+ const createStream = async function* () {
10
+ let retryDelay = sseDefaultRetryDelay ?? 3e3;
11
+ let attempt = 0;
12
+ const signal = options.signal ?? new AbortController().signal;
13
+ while (true) {
14
+ if (signal.aborted) break;
15
+ attempt++;
16
+ const headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers);
17
+ if (lastEventId !== void 0) headers.set("Last-Event-ID", lastEventId);
18
+ try {
19
+ const requestInit = {
20
+ redirect: "follow",
21
+ ...options,
22
+ body: options.serializedBody,
23
+ headers,
24
+ signal
25
+ };
26
+ let request = new Request(url, requestInit);
27
+ if (onRequest) request = await onRequest(url, requestInit);
28
+ const response = await (options.fetch ?? globalThis.fetch)(request);
29
+ if (!response.ok) throw new Error(`SSE failed: ${response.status} ${response.statusText}`);
30
+ if (!response.body) throw new Error("No body in SSE response");
31
+ const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
32
+ let buffer = "";
33
+ const abortHandler = () => {
34
+ try {
35
+ reader.cancel();
36
+ } catch {}
37
+ };
38
+ signal.addEventListener("abort", abortHandler);
39
+ try {
40
+ while (true) {
41
+ const { done, value } = await reader.read();
42
+ if (done) break;
43
+ buffer += value;
44
+ buffer = buffer.replace(/\r\n?/g, "\n");
45
+ const chunks = buffer.split("\n\n");
46
+ buffer = chunks.pop() ?? "";
47
+ for (const chunk of chunks) {
48
+ const lines = chunk.split("\n");
49
+ const dataLines = [];
50
+ let eventName;
51
+ for (const line of lines) if (line.startsWith("data:")) dataLines.push(line.replace(/^data:\s*/, ""));
52
+ else if (line.startsWith("event:")) eventName = line.replace(/^event:\s*/, "");
53
+ else if (line.startsWith("id:")) lastEventId = line.replace(/^id:\s*/, "");
54
+ else if (line.startsWith("retry:")) {
55
+ const parsed = Number.parseInt(line.replace(/^retry:\s*/, ""), 10);
56
+ if (!Number.isNaN(parsed)) retryDelay = parsed;
57
+ }
58
+ let data;
59
+ let parsedJson = false;
60
+ if (dataLines.length) {
61
+ const rawData = dataLines.join("\n");
62
+ try {
63
+ data = JSON.parse(rawData);
64
+ parsedJson = true;
65
+ } catch {
66
+ data = rawData;
67
+ }
68
+ }
69
+ if (parsedJson) {
70
+ if (responseValidator) await responseValidator(data);
71
+ if (responseTransformer) data = await responseTransformer(data);
72
+ }
73
+ onSseEvent?.({
74
+ data,
75
+ event: eventName,
76
+ id: lastEventId,
77
+ retry: retryDelay
78
+ });
79
+ if (dataLines.length) yield data;
80
+ }
81
+ }
82
+ } finally {
83
+ signal.removeEventListener("abort", abortHandler);
84
+ reader.releaseLock();
85
+ }
86
+ break;
87
+ } catch (error) {
88
+ onSseError?.(error);
89
+ if (sseMaxRetryAttempts !== void 0 && attempt >= sseMaxRetryAttempts) break;
90
+ const backoff = Math.min(retryDelay * 2 ** (attempt - 1), sseMaxRetryDelay ?? 3e4);
91
+ await sleep(backoff);
92
+ }
93
+ }
94
+ };
95
+ return { stream: createStream() };
96
+ }
97
+ //#endregion
98
+ //#region src/gen/core/pathSerializer.gen.ts
99
+ const separatorArrayExplode = (style) => {
100
+ switch (style) {
101
+ case "label": return ".";
102
+ case "matrix": return ";";
103
+ case "simple": return ",";
104
+ default: return "&";
105
+ }
106
+ };
107
+ const separatorArrayNoExplode = (style) => {
108
+ switch (style) {
109
+ case "form": return ",";
110
+ case "pipeDelimited": return "|";
111
+ case "spaceDelimited": return "%20";
112
+ default: return ",";
113
+ }
114
+ };
115
+ const separatorObjectExplode = (style) => {
116
+ switch (style) {
117
+ case "label": return ".";
118
+ case "matrix": return ";";
119
+ case "simple": return ",";
120
+ default: return "&";
121
+ }
122
+ };
123
+ const serializeArrayParam = ({ allowReserved, explode, name, style, value }) => {
124
+ if (!explode) {
125
+ const joinedValues = (allowReserved ? value : value.map((v) => encodeURIComponent(v))).join(separatorArrayNoExplode(style));
126
+ switch (style) {
127
+ case "label": return `.${joinedValues}`;
128
+ case "matrix": return `;${name}=${joinedValues}`;
129
+ case "simple": return joinedValues;
130
+ default: return `${name}=${joinedValues}`;
131
+ }
132
+ }
133
+ const separator = separatorArrayExplode(style);
134
+ const joinedValues = value.map((v) => {
135
+ if (style === "label" || style === "simple") return allowReserved ? v : encodeURIComponent(v);
136
+ return serializePrimitiveParam({
137
+ allowReserved,
138
+ name,
139
+ value: v
140
+ });
141
+ }).join(separator);
142
+ return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
143
+ };
144
+ const serializePrimitiveParam = ({ allowReserved, name, value }) => {
145
+ if (value === void 0 || value === null) return "";
146
+ if (typeof value === "object") throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");
147
+ return `${name}=${allowReserved ? value : encodeURIComponent(value)}`;
148
+ };
149
+ const serializeObjectParam = ({ allowReserved, explode, name, style, value, valueOnly }) => {
150
+ if (value instanceof Date) return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`;
151
+ if (style !== "deepObject" && !explode) {
152
+ let values = [];
153
+ Object.entries(value).forEach(([key, v]) => {
154
+ values = [
155
+ ...values,
156
+ key,
157
+ allowReserved ? v : encodeURIComponent(v)
158
+ ];
159
+ });
160
+ const joinedValues = values.join(",");
161
+ switch (style) {
162
+ case "form": return `${name}=${joinedValues}`;
163
+ case "label": return `.${joinedValues}`;
164
+ case "matrix": return `;${name}=${joinedValues}`;
165
+ default: return joinedValues;
166
+ }
167
+ }
168
+ const separator = separatorObjectExplode(style);
169
+ const joinedValues = Object.entries(value).map(([key, v]) => serializePrimitiveParam({
170
+ allowReserved,
171
+ name: style === "deepObject" ? `${name}[${key}]` : key,
172
+ value: v
173
+ })).join(separator);
174
+ return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
175
+ };
176
+ //#endregion
177
+ //#region src/gen/core/utils.gen.ts
178
+ const PATH_PARAM_RE = /\{[^{}]+\}/g;
179
+ const defaultPathSerializer = ({ path, url: _url }) => {
180
+ let url = _url;
181
+ const matches = _url.match(PATH_PARAM_RE);
182
+ if (matches) for (const match of matches) {
183
+ let explode = false;
184
+ let name = match.substring(1, match.length - 1);
185
+ let style = "simple";
186
+ if (name.endsWith("*")) {
187
+ explode = true;
188
+ name = name.substring(0, name.length - 1);
189
+ }
190
+ if (name.startsWith(".")) {
191
+ name = name.substring(1);
192
+ style = "label";
193
+ } else if (name.startsWith(";")) {
194
+ name = name.substring(1);
195
+ style = "matrix";
196
+ }
197
+ const value = path[name];
198
+ if (value === void 0 || value === null) continue;
199
+ if (Array.isArray(value)) {
200
+ url = url.replace(match, serializeArrayParam({
201
+ explode,
202
+ name,
203
+ style,
204
+ value
205
+ }));
206
+ continue;
207
+ }
208
+ if (typeof value === "object") {
209
+ url = url.replace(match, serializeObjectParam({
210
+ explode,
211
+ name,
212
+ style,
213
+ value,
214
+ valueOnly: true
215
+ }));
216
+ continue;
217
+ }
218
+ if (style === "matrix") {
219
+ url = url.replace(match, `;${serializePrimitiveParam({
220
+ name,
221
+ value
222
+ })}`);
223
+ continue;
224
+ }
225
+ const replaceValue = encodeURIComponent(style === "label" ? `.${value}` : value);
226
+ url = url.replace(match, replaceValue);
227
+ }
228
+ return url;
229
+ };
230
+ const getUrl = ({ baseUrl, path, query, querySerializer, url: _url }) => {
231
+ const pathUrl = _url.startsWith("/") ? _url : `/${_url}`;
232
+ let url = (baseUrl ?? "") + pathUrl;
233
+ if (path) url = defaultPathSerializer({
234
+ path,
235
+ url
236
+ });
237
+ let search = query ? querySerializer(query) : "";
238
+ if (search.startsWith("?")) search = search.substring(1);
239
+ if (search) url += `?${search}`;
240
+ return url;
241
+ };
242
+ function getValidRequestBody(options) {
243
+ const hasBody = options.body !== void 0;
244
+ if (hasBody && options.bodySerializer) {
245
+ if ("serializedBody" in options) return options.serializedBody !== void 0 && options.serializedBody !== "" ? options.serializedBody : null;
246
+ return options.body !== "" ? options.body : null;
247
+ }
248
+ if (hasBody) return options.body;
249
+ }
250
+ //#endregion
251
+ //#region src/gen/core/auth.gen.ts
252
+ const getAuthToken = async (auth, callback) => {
253
+ const token = typeof callback === "function" ? await callback(auth) : callback;
254
+ if (!token) return;
255
+ if (auth.scheme === "bearer") return `Bearer ${token}`;
256
+ if (auth.scheme === "basic") return `Basic ${btoa(token)}`;
257
+ return token;
258
+ };
259
+ //#endregion
260
+ //#region src/gen/client/utils.gen.ts
261
+ const createQuerySerializer = ({ parameters = {}, ...args } = {}) => {
262
+ const querySerializer = (queryParams) => {
263
+ const search = [];
264
+ if (queryParams && typeof queryParams === "object") for (const name in queryParams) {
265
+ const value = queryParams[name];
266
+ if (value === void 0 || value === null) continue;
267
+ const options = parameters[name] || args;
268
+ if (Array.isArray(value)) {
269
+ const serializedArray = serializeArrayParam({
270
+ allowReserved: options.allowReserved,
271
+ explode: true,
272
+ name,
273
+ style: "form",
274
+ value,
275
+ ...options.array
276
+ });
277
+ if (serializedArray) search.push(serializedArray);
278
+ } else if (typeof value === "object") {
279
+ const serializedObject = serializeObjectParam({
280
+ allowReserved: options.allowReserved,
281
+ explode: true,
282
+ name,
283
+ style: "deepObject",
284
+ value,
285
+ ...options.object
286
+ });
287
+ if (serializedObject) search.push(serializedObject);
288
+ } else {
289
+ const serializedPrimitive = serializePrimitiveParam({
290
+ allowReserved: options.allowReserved,
291
+ name,
292
+ value
293
+ });
294
+ if (serializedPrimitive) search.push(serializedPrimitive);
295
+ }
296
+ }
297
+ return search.join("&");
298
+ };
299
+ return querySerializer;
300
+ };
301
+ /**
302
+ * Infers parseAs value from provided Content-Type header.
303
+ */
304
+ const getParseAs = (contentType) => {
305
+ if (!contentType) return "stream";
306
+ const cleanContent = contentType.split(";")[0]?.trim();
307
+ if (!cleanContent) return;
308
+ if (cleanContent.startsWith("application/json") || cleanContent.endsWith("+json")) return "json";
309
+ if (cleanContent === "multipart/form-data") return "formData";
310
+ if ([
311
+ "application/",
312
+ "audio/",
313
+ "image/",
314
+ "video/"
315
+ ].some((type) => cleanContent.startsWith(type))) return "blob";
316
+ if (cleanContent.startsWith("text/")) return "text";
317
+ };
318
+ const checkForExistence = (options, name) => {
319
+ if (!name) return false;
320
+ if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) return true;
321
+ return false;
322
+ };
323
+ async function setAuthParams(options) {
324
+ for (const auth of options.security ?? []) {
325
+ if (checkForExistence(options, auth.name)) continue;
326
+ const token = await getAuthToken(auth, options.auth);
327
+ if (!token) continue;
328
+ const name = auth.name ?? "Authorization";
329
+ switch (auth.in) {
330
+ case "query":
331
+ if (!options.query) options.query = {};
332
+ options.query[name] = token;
333
+ break;
334
+ case "cookie":
335
+ options.headers.append("Cookie", `${name}=${token}`);
336
+ break;
337
+ default: options.headers.set(name, token);
338
+ }
339
+ }
340
+ }
341
+ const buildUrl = (options) => getUrl({
342
+ baseUrl: options.baseUrl,
343
+ path: options.path,
344
+ query: options.query,
345
+ querySerializer: typeof options.querySerializer === "function" ? options.querySerializer : createQuerySerializer(options.querySerializer),
346
+ url: options.url
347
+ });
348
+ const mergeConfigs = (a, b) => {
349
+ const config = {
350
+ ...a,
351
+ ...b
352
+ };
353
+ if (config.baseUrl?.endsWith("/")) config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
354
+ config.headers = mergeHeaders(a.headers, b.headers);
355
+ return config;
356
+ };
357
+ const headersEntries = (headers) => {
358
+ const entries = [];
359
+ headers.forEach((value, key) => {
360
+ entries.push([key, value]);
361
+ });
362
+ return entries;
363
+ };
364
+ const mergeHeaders = (...headers) => {
365
+ const mergedHeaders = new Headers();
366
+ for (const header of headers) {
367
+ if (!header) continue;
368
+ const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
369
+ for (const [key, value] of iterator) if (value === null) mergedHeaders.delete(key);
370
+ else if (Array.isArray(value)) for (const v of value) mergedHeaders.append(key, v);
371
+ else if (value !== void 0) mergedHeaders.set(key, typeof value === "object" ? JSON.stringify(value) : value);
372
+ }
373
+ return mergedHeaders;
374
+ };
375
+ var Interceptors = class {
376
+ fns = [];
377
+ clear() {
378
+ this.fns = [];
379
+ }
380
+ eject(id) {
381
+ const index = this.getInterceptorIndex(id);
382
+ if (this.fns[index]) this.fns[index] = null;
383
+ }
384
+ exists(id) {
385
+ const index = this.getInterceptorIndex(id);
386
+ return Boolean(this.fns[index]);
387
+ }
388
+ getInterceptorIndex(id) {
389
+ if (typeof id === "number") return this.fns[id] ? id : -1;
390
+ return this.fns.indexOf(id);
391
+ }
392
+ update(id, fn) {
393
+ const index = this.getInterceptorIndex(id);
394
+ if (this.fns[index]) {
395
+ this.fns[index] = fn;
396
+ return id;
397
+ }
398
+ return false;
399
+ }
400
+ use(fn) {
401
+ this.fns.push(fn);
402
+ return this.fns.length - 1;
403
+ }
404
+ };
405
+ const createInterceptors = () => ({
406
+ error: new Interceptors(),
407
+ request: new Interceptors(),
408
+ response: new Interceptors()
409
+ });
410
+ const defaultQuerySerializer = createQuerySerializer({
411
+ allowReserved: false,
412
+ array: {
413
+ explode: true,
414
+ style: "form"
415
+ },
416
+ object: {
417
+ explode: true,
418
+ style: "deepObject"
419
+ }
420
+ });
421
+ const defaultHeaders = { "Content-Type": "application/json" };
422
+ const createConfig = (override = {}) => ({
423
+ ...jsonBodySerializer,
424
+ headers: defaultHeaders,
425
+ parseAs: "auto",
426
+ querySerializer: defaultQuerySerializer,
427
+ ...override
428
+ });
429
+ //#endregion
430
+ //#region src/gen/client/client.gen.ts
431
+ const createClient = (config = {}) => {
432
+ let _config = mergeConfigs(createConfig(), config);
433
+ const getConfig = () => ({ ..._config });
434
+ const setConfig = (config) => {
435
+ _config = mergeConfigs(_config, config);
436
+ return getConfig();
437
+ };
438
+ const interceptors = createInterceptors();
439
+ const beforeRequest = async (options) => {
440
+ const opts = {
441
+ ..._config,
442
+ ...options,
443
+ fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
444
+ headers: mergeHeaders(_config.headers, options.headers),
445
+ serializedBody: void 0
446
+ };
447
+ if (opts.security) await setAuthParams(opts);
448
+ if (opts.requestValidator) await opts.requestValidator(opts);
449
+ if (opts.body !== void 0 && opts.bodySerializer) opts.serializedBody = opts.bodySerializer(opts.body);
450
+ if (opts.body === void 0 || opts.serializedBody === "") opts.headers.delete("Content-Type");
451
+ const resolvedOpts = opts;
452
+ return {
453
+ opts: resolvedOpts,
454
+ url: buildUrl(resolvedOpts)
455
+ };
456
+ };
457
+ const request = async (options) => {
458
+ const throwOnError = options.throwOnError ?? _config.throwOnError;
459
+ const responseStyle = options.responseStyle ?? _config.responseStyle;
460
+ let request;
461
+ let response;
462
+ try {
463
+ const { opts, url } = await beforeRequest(options);
464
+ const requestInit = {
465
+ redirect: "follow",
466
+ ...opts,
467
+ body: getValidRequestBody(opts)
468
+ };
469
+ request = new Request(url, requestInit);
470
+ for (const fn of interceptors.request.fns) if (fn) request = await fn(request, opts);
471
+ const _fetch = opts.fetch;
472
+ response = await _fetch(request);
473
+ for (const fn of interceptors.response.fns) if (fn) response = await fn(response, request, opts);
474
+ const result = {
475
+ request,
476
+ response
477
+ };
478
+ if (response.ok) {
479
+ const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json";
480
+ if (response.status === 204 || response.headers.get("Content-Length") === "0") {
481
+ let emptyData;
482
+ switch (parseAs) {
483
+ case "arrayBuffer":
484
+ case "blob":
485
+ case "text":
486
+ emptyData = await response[parseAs]();
487
+ break;
488
+ case "formData":
489
+ emptyData = new FormData();
490
+ break;
491
+ case "stream":
492
+ emptyData = response.body;
493
+ break;
494
+ default: emptyData = {};
495
+ }
496
+ return opts.responseStyle === "data" ? emptyData : {
497
+ data: emptyData,
498
+ ...result
499
+ };
500
+ }
501
+ let data;
502
+ switch (parseAs) {
503
+ case "arrayBuffer":
504
+ case "blob":
505
+ case "formData":
506
+ case "text":
507
+ data = await response[parseAs]();
508
+ break;
509
+ case "json": {
510
+ const text = await response.text();
511
+ data = text ? JSON.parse(text) : {};
512
+ break;
513
+ }
514
+ case "stream": return opts.responseStyle === "data" ? response.body : {
515
+ data: response.body,
516
+ ...result
517
+ };
518
+ }
519
+ if (parseAs === "json") {
520
+ if (opts.responseValidator) await opts.responseValidator(data);
521
+ if (opts.responseTransformer) data = await opts.responseTransformer(data);
522
+ }
523
+ return opts.responseStyle === "data" ? data : {
524
+ data,
525
+ ...result
526
+ };
527
+ }
528
+ const textError = await response.text();
529
+ let jsonError;
530
+ try {
531
+ jsonError = JSON.parse(textError);
532
+ } catch {}
533
+ throw jsonError ?? textError;
534
+ } catch (error) {
535
+ let finalError = error;
536
+ for (const fn of interceptors.error.fns) if (fn) finalError = await fn(finalError, response, request, options);
537
+ finalError = finalError || {};
538
+ if (throwOnError) throw finalError;
539
+ return responseStyle === "data" ? void 0 : {
540
+ error: finalError,
541
+ request,
542
+ response
543
+ };
544
+ }
545
+ };
546
+ const makeMethodFn = (method) => (options) => request({
547
+ ...options,
548
+ method
549
+ });
550
+ const makeSseFn = (method) => async (options) => {
551
+ const { opts, url } = await beforeRequest(options);
552
+ return createSseClient({
553
+ ...opts,
554
+ body: opts.body,
555
+ method,
556
+ onRequest: async (url, init) => {
557
+ let request = new Request(url, init);
558
+ for (const fn of interceptors.request.fns) if (fn) request = await fn(request, opts);
559
+ return request;
560
+ },
561
+ serializedBody: getValidRequestBody(opts),
562
+ url
563
+ });
564
+ };
565
+ const _buildUrl = (options) => buildUrl({
566
+ ..._config,
567
+ ...options
568
+ });
569
+ return {
570
+ buildUrl: _buildUrl,
571
+ connect: makeMethodFn("CONNECT"),
572
+ delete: makeMethodFn("DELETE"),
573
+ get: makeMethodFn("GET"),
574
+ getConfig,
575
+ head: makeMethodFn("HEAD"),
576
+ interceptors,
577
+ options: makeMethodFn("OPTIONS"),
578
+ patch: makeMethodFn("PATCH"),
579
+ post: makeMethodFn("POST"),
580
+ put: makeMethodFn("PUT"),
581
+ request,
582
+ setConfig,
583
+ sse: {
584
+ connect: makeSseFn("CONNECT"),
585
+ delete: makeSseFn("DELETE"),
586
+ get: makeSseFn("GET"),
587
+ head: makeSseFn("HEAD"),
588
+ options: makeSseFn("OPTIONS"),
589
+ patch: makeSseFn("PATCH"),
590
+ post: makeSseFn("POST"),
591
+ put: makeSseFn("PUT"),
592
+ trace: makeSseFn("TRACE")
593
+ },
594
+ trace: makeMethodFn("TRACE")
595
+ };
596
+ };
597
+ //#endregion
598
+ //#region src/encoding.ts
599
+ const encoder = new TextEncoder();
600
+ const toHex = (bytes) => Array.from(new Uint8Array(bytes), (byte) => byte.toString(16).padStart(2, "0")).join("");
601
+ const fromHex = (value) => {
602
+ if (!/^(?:[0-9a-f]{2})*$/i.test(value)) return null;
603
+ return Uint8Array.from(value.match(/../g) ?? [], (pair) => Number.parseInt(pair, 16));
604
+ };
605
+ const toBase64Url = (bytes) => btoa(String.fromCharCode(...new Uint8Array(bytes))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
606
+ const fromBase64Url = (value) => Uint8Array.from(atob(value.replace(/-/g, "+").replace(/_/g, "/")), (char) => char.charCodeAt(0));
607
+ //#endregion
608
+ //#region src/signing.ts
609
+ const signingString = async (request) => {
610
+ const bodyHash = await crypto.subtle.digest("SHA-256", encoder.encode(request.body));
611
+ return [
612
+ request.timestamp,
613
+ request.nonce,
614
+ request.method.toUpperCase(),
615
+ request.target,
616
+ toHex(bodyHash)
617
+ ].join("\n");
618
+ };
619
+ const importPrivateKey = (privateKey) => crypto.subtle.importKey("pkcs8", fromBase64Url(privateKey), { name: "Ed25519" }, false, ["sign"]);
620
+ const sign = async (key, request) => {
621
+ const signature = await crypto.subtle.sign("Ed25519", key, encoder.encode(await signingString(request)));
622
+ return toBase64Url(signature);
623
+ };
624
+ const createNonce = () => toHex(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)));
625
+ //#endregion
626
+ //#region src/errors.ts
627
+ var KiteError = class extends Error {
628
+ status;
629
+ code;
630
+ response;
631
+ name = "KiteError";
632
+ constructor(message, status, code, response, options) {
633
+ super(message, options);
634
+ this.status = status;
635
+ this.code = code;
636
+ this.response = response;
637
+ }
638
+ };
639
+ const toKiteError = (error, response) => {
640
+ if (error instanceof KiteError) return error;
641
+ if (!response) return new KiteError(error instanceof Error ? error.message : "The request failed", void 0, "NetworkError", void 0, { cause: error });
642
+ const body = error ?? {};
643
+ const detail = typeof body.error === "object" ? body.error : void 0;
644
+ return new KiteError(typeof detail?.message === "string" ? detail.message : typeof body.error === "string" ? body.error : `Kite answered ${response.status}`, response.status, typeof detail?.code === "string" ? detail.code : "HttpError", response, { cause: error });
645
+ };
646
+ //#endregion
647
+ //#region src/gen/client.gen.ts
648
+ const client = createClient(createConfig({ throwOnError: true }));
649
+ //#endregion
650
+ //#region src/gen/sdk.gen.ts
651
+ var HeyApiClient = class {
652
+ client;
653
+ constructor(args) {
654
+ this.client = args?.client ?? client;
655
+ }
656
+ };
657
+ var HeyApiRegistry = class {
658
+ defaultKey = "default";
659
+ instances = /* @__PURE__ */ new Map();
660
+ get(key) {
661
+ const instance = this.instances.get(key ?? this.defaultKey);
662
+ if (!instance) throw new Error(`No SDK client found. Create one with "new Api()" to fix this error.`);
663
+ return instance;
664
+ }
665
+ set(value, key) {
666
+ this.instances.set(key ?? this.defaultKey, value);
667
+ }
668
+ };
669
+ var Apps = class extends HeyApiClient {
670
+ /**
671
+ * List apps
672
+ */
673
+ list(options) {
674
+ return (options?.client ?? this.client).get({
675
+ security: [{
676
+ name: "x-kite-root-key",
677
+ type: "apiKey"
678
+ }],
679
+ url: "/api/apps/all",
680
+ ...options
681
+ });
682
+ }
683
+ /**
684
+ * Delete app
685
+ */
686
+ delete(options) {
687
+ return (options?.client ?? this.client).delete({
688
+ security: [{
689
+ name: "x-kite-signature",
690
+ type: "apiKey"
691
+ }, {
692
+ name: "x-kite-root-key",
693
+ type: "apiKey"
694
+ }],
695
+ url: "/api/apps",
696
+ ...options
697
+ });
698
+ }
699
+ /**
700
+ * Get app
701
+ */
702
+ get(options) {
703
+ return (options?.client ?? this.client).get({
704
+ security: [{
705
+ name: "x-kite-signature",
706
+ type: "apiKey"
707
+ }, {
708
+ name: "x-kite-root-key",
709
+ type: "apiKey"
710
+ }],
711
+ url: "/api/apps",
712
+ ...options
713
+ });
714
+ }
715
+ /**
716
+ * Update app
717
+ */
718
+ update(options) {
719
+ return (options.client ?? this.client).patch({
720
+ security: [{
721
+ name: "x-kite-signature",
722
+ type: "apiKey"
723
+ }, {
724
+ name: "x-kite-root-key",
725
+ type: "apiKey"
726
+ }],
727
+ url: "/api/apps",
728
+ ...options,
729
+ headers: {
730
+ "Content-Type": "application/json",
731
+ ...options.headers
732
+ }
733
+ });
734
+ }
735
+ /**
736
+ * Create an app
737
+ */
738
+ create(options) {
739
+ return (options.client ?? this.client).post({
740
+ security: [{
741
+ name: "x-kite-root-key",
742
+ type: "apiKey"
743
+ }],
744
+ url: "/api/apps",
745
+ ...options,
746
+ headers: {
747
+ "Content-Type": "application/json",
748
+ ...options.headers
749
+ }
750
+ });
751
+ }
752
+ };
753
+ var ApiKeys = class extends HeyApiClient {
754
+ /**
755
+ * List API keys
756
+ */
757
+ list(options) {
758
+ return (options?.client ?? this.client).get({
759
+ security: [{
760
+ name: "x-kite-signature",
761
+ type: "apiKey"
762
+ }, {
763
+ name: "x-kite-root-key",
764
+ type: "apiKey"
765
+ }],
766
+ url: "/api/api-key",
767
+ ...options
768
+ });
769
+ }
770
+ /**
771
+ * Register an API key's public key
772
+ */
773
+ create(options) {
774
+ return (options.client ?? this.client).post({
775
+ security: [{
776
+ name: "x-kite-signature",
777
+ type: "apiKey"
778
+ }, {
779
+ name: "x-kite-root-key",
780
+ type: "apiKey"
781
+ }],
782
+ url: "/api/api-key",
783
+ ...options,
784
+ headers: {
785
+ "Content-Type": "application/json",
786
+ ...options.headers
787
+ }
788
+ });
789
+ }
790
+ /**
791
+ * Delete an API key
792
+ */
793
+ delete(options) {
794
+ return (options.client ?? this.client).delete({
795
+ security: [{
796
+ name: "x-kite-signature",
797
+ type: "apiKey"
798
+ }, {
799
+ name: "x-kite-root-key",
800
+ type: "apiKey"
801
+ }],
802
+ url: "/api/api-key/{id}",
803
+ ...options
804
+ });
805
+ }
806
+ /**
807
+ * Get an API key
808
+ */
809
+ get(options) {
810
+ return (options.client ?? this.client).get({
811
+ security: [{
812
+ name: "x-kite-signature",
813
+ type: "apiKey"
814
+ }, {
815
+ name: "x-kite-root-key",
816
+ type: "apiKey"
817
+ }],
818
+ url: "/api/api-key/{id}",
819
+ ...options
820
+ });
821
+ }
822
+ /**
823
+ * Update an API key's label
824
+ */
825
+ update(options) {
826
+ return (options.client ?? this.client).patch({
827
+ security: [{
828
+ name: "x-kite-signature",
829
+ type: "apiKey"
830
+ }, {
831
+ name: "x-kite-root-key",
832
+ type: "apiKey"
833
+ }],
834
+ url: "/api/api-key/{id}",
835
+ ...options,
836
+ headers: {
837
+ "Content-Type": "application/json",
838
+ ...options.headers
839
+ }
840
+ });
841
+ }
842
+ /**
843
+ * Revoke an API key
844
+ */
845
+ revoke(options) {
846
+ return (options.client ?? this.client).post({
847
+ security: [{
848
+ name: "x-kite-signature",
849
+ type: "apiKey"
850
+ }, {
851
+ name: "x-kite-root-key",
852
+ type: "apiKey"
853
+ }],
854
+ url: "/api/api-key/{id}/revoke",
855
+ ...options
856
+ });
857
+ }
858
+ };
859
+ var Chains = class extends HeyApiClient {
860
+ /**
861
+ * List configured chains
862
+ */
863
+ list(options) {
864
+ return (options?.client ?? this.client).get({
865
+ security: [{
866
+ name: "x-kite-signature",
867
+ type: "apiKey"
868
+ }, {
869
+ name: "x-kite-root-key",
870
+ type: "apiKey"
871
+ }],
872
+ url: "/api/chains",
873
+ ...options
874
+ });
875
+ }
876
+ /**
877
+ * Add a chain
878
+ */
879
+ create(options) {
880
+ return (options.client ?? this.client).post({
881
+ security: [{
882
+ name: "x-kite-root-key",
883
+ type: "apiKey"
884
+ }],
885
+ url: "/api/chains",
886
+ ...options,
887
+ headers: {
888
+ "Content-Type": "application/json",
889
+ ...options.headers
890
+ }
891
+ });
892
+ }
893
+ /**
894
+ * List chains (with full metadata)
895
+ */
896
+ listFull(options) {
897
+ return (options?.client ?? this.client).get({
898
+ security: [{
899
+ name: "x-kite-root-key",
900
+ type: "apiKey"
901
+ }],
902
+ url: "/api/chains/full",
903
+ ...options
904
+ });
905
+ }
906
+ /**
907
+ * Chains this server knows how to run on
908
+ */
909
+ listSupported(options) {
910
+ return (options?.client ?? this.client).get({
911
+ security: [{
912
+ name: "x-kite-root-key",
913
+ type: "apiKey"
914
+ }],
915
+ url: "/api/chains/supported",
916
+ ...options
917
+ });
918
+ }
919
+ /**
920
+ * Delete a chain
921
+ */
922
+ delete(options) {
923
+ return (options.client ?? this.client).delete({
924
+ security: [{
925
+ name: "x-kite-root-key",
926
+ type: "apiKey"
927
+ }],
928
+ url: "/api/chains/{id}",
929
+ ...options
930
+ });
931
+ }
932
+ /**
933
+ * Update chain active status
934
+ */
935
+ update(options) {
936
+ return (options.client ?? this.client).patch({
937
+ security: [{
938
+ name: "x-kite-root-key",
939
+ type: "apiKey"
940
+ }],
941
+ url: "/api/chains/{id}",
942
+ ...options,
943
+ headers: {
944
+ "Content-Type": "application/json",
945
+ ...options.headers
946
+ }
947
+ });
948
+ }
949
+ /**
950
+ * Configure a chain
951
+ */
952
+ configure(options) {
953
+ return (options.client ?? this.client).put({
954
+ security: [{
955
+ name: "x-kite-root-key",
956
+ type: "apiKey"
957
+ }],
958
+ url: "/api/chains/{id}/config",
959
+ ...options,
960
+ headers: {
961
+ "Content-Type": "application/json",
962
+ ...options.headers
963
+ }
964
+ });
965
+ }
966
+ };
967
+ var Tokens = class extends HeyApiClient {
968
+ /**
969
+ * Get a token by its contract address
970
+ */
971
+ getByAddress(options) {
972
+ return (options.client ?? this.client).get({
973
+ security: [{
974
+ name: "x-kite-signature",
975
+ type: "apiKey"
976
+ }, {
977
+ name: "x-kite-root-key",
978
+ type: "apiKey"
979
+ }],
980
+ url: "/api/tokens/{chainId}/{address}",
981
+ ...options
982
+ });
983
+ }
984
+ /**
985
+ * List tokens
986
+ */
987
+ list(options) {
988
+ return (options?.client ?? this.client).get({
989
+ security: [{
990
+ name: "x-kite-signature",
991
+ type: "apiKey"
992
+ }, {
993
+ name: "x-kite-root-key",
994
+ type: "apiKey"
995
+ }],
996
+ url: "/api/tokens",
997
+ ...options
998
+ });
999
+ }
1000
+ /**
1001
+ * Delete a token
1002
+ */
1003
+ delete(options) {
1004
+ return (options.client ?? this.client).delete({
1005
+ security: [{
1006
+ name: "x-kite-root-key",
1007
+ type: "apiKey"
1008
+ }],
1009
+ url: "/api/tokens/{id}",
1010
+ ...options
1011
+ });
1012
+ }
1013
+ };
1014
+ var Checkout = class extends HeyApiClient {
1015
+ /**
1016
+ * What a payment link charges and how it can be paid
1017
+ */
1018
+ get(options) {
1019
+ return (options.client ?? this.client).get({
1020
+ url: "/api/checkout/{id}",
1021
+ ...options
1022
+ });
1023
+ }
1024
+ /**
1025
+ * Quote a payment link in a token
1026
+ */
1027
+ quote(options) {
1028
+ return (options.client ?? this.client).post({
1029
+ url: "/api/checkout/{id}/quote",
1030
+ ...options,
1031
+ headers: {
1032
+ "Content-Type": "application/json",
1033
+ ...options.headers
1034
+ }
1035
+ });
1036
+ }
1037
+ /**
1038
+ * Pay a link with x402 or receive payment instructions
1039
+ */
1040
+ pay(options) {
1041
+ return (options.client ?? this.client).post({
1042
+ url: "/api/checkout/{id}/pay",
1043
+ ...options,
1044
+ headers: {
1045
+ "Content-Type": "application/json",
1046
+ ...options.headers
1047
+ }
1048
+ });
1049
+ }
1050
+ /**
1051
+ * Open a transfer to be paid by sending tokens
1052
+ */
1053
+ openTransfer(options) {
1054
+ return (options.client ?? this.client).post({
1055
+ url: "/api/checkout/{id}/transfer",
1056
+ ...options,
1057
+ headers: {
1058
+ "Content-Type": "application/json",
1059
+ ...options.headers
1060
+ }
1061
+ });
1062
+ }
1063
+ /**
1064
+ * Track a transfer opened from a payment link
1065
+ */
1066
+ getTransfer(options) {
1067
+ return (options.client ?? this.client).get({
1068
+ url: "/api/checkout/{id}/transfer/{transferId}",
1069
+ ...options
1070
+ });
1071
+ }
1072
+ };
1073
+ var Customers = class extends HeyApiClient {
1074
+ /**
1075
+ * List customers
1076
+ */
1077
+ list(options) {
1078
+ return (options?.client ?? this.client).get({
1079
+ security: [{
1080
+ name: "x-kite-signature",
1081
+ type: "apiKey"
1082
+ }, {
1083
+ name: "x-kite-root-key",
1084
+ type: "apiKey"
1085
+ }],
1086
+ url: "/api/customers",
1087
+ ...options
1088
+ });
1089
+ }
1090
+ /**
1091
+ * Create a customer, or update the one with this externalId
1092
+ */
1093
+ upsert(options) {
1094
+ return (options.client ?? this.client).post({
1095
+ security: [{
1096
+ name: "x-kite-signature",
1097
+ type: "apiKey"
1098
+ }, {
1099
+ name: "x-kite-root-key",
1100
+ type: "apiKey"
1101
+ }],
1102
+ url: "/api/customers",
1103
+ ...options,
1104
+ headers: {
1105
+ "Content-Type": "application/json",
1106
+ ...options.headers
1107
+ }
1108
+ });
1109
+ }
1110
+ /**
1111
+ * Delete a customer
1112
+ */
1113
+ delete(options) {
1114
+ return (options.client ?? this.client).delete({
1115
+ security: [{
1116
+ name: "x-kite-signature",
1117
+ type: "apiKey"
1118
+ }, {
1119
+ name: "x-kite-root-key",
1120
+ type: "apiKey"
1121
+ }],
1122
+ url: "/api/customers/{id}",
1123
+ ...options
1124
+ });
1125
+ }
1126
+ /**
1127
+ * Get a customer
1128
+ */
1129
+ get(options) {
1130
+ return (options.client ?? this.client).get({
1131
+ security: [{
1132
+ name: "x-kite-signature",
1133
+ type: "apiKey"
1134
+ }, {
1135
+ name: "x-kite-root-key",
1136
+ type: "apiKey"
1137
+ }],
1138
+ url: "/api/customers/{id}",
1139
+ ...options
1140
+ });
1141
+ }
1142
+ };
1143
+ var Fees = class extends HeyApiClient {
1144
+ /**
1145
+ * List fees
1146
+ */
1147
+ list(options) {
1148
+ return (options?.client ?? this.client).get({
1149
+ security: [{
1150
+ name: "x-kite-signature",
1151
+ type: "apiKey"
1152
+ }, {
1153
+ name: "x-kite-root-key",
1154
+ type: "apiKey"
1155
+ }],
1156
+ url: "/api/fees",
1157
+ ...options
1158
+ });
1159
+ }
1160
+ /**
1161
+ * Create a fee
1162
+ */
1163
+ create(options) {
1164
+ return (options.client ?? this.client).post({
1165
+ security: [{
1166
+ name: "x-kite-signature",
1167
+ type: "apiKey"
1168
+ }, {
1169
+ name: "x-kite-root-key",
1170
+ type: "apiKey"
1171
+ }],
1172
+ url: "/api/fees",
1173
+ ...options,
1174
+ headers: {
1175
+ "Content-Type": "application/json",
1176
+ ...options.headers
1177
+ }
1178
+ });
1179
+ }
1180
+ /**
1181
+ * Delete a fee
1182
+ */
1183
+ delete(options) {
1184
+ return (options.client ?? this.client).delete({
1185
+ security: [{
1186
+ name: "x-kite-signature",
1187
+ type: "apiKey"
1188
+ }, {
1189
+ name: "x-kite-root-key",
1190
+ type: "apiKey"
1191
+ }],
1192
+ url: "/api/fees/{id}",
1193
+ ...options
1194
+ });
1195
+ }
1196
+ /**
1197
+ * Get a fee
1198
+ */
1199
+ get(options) {
1200
+ return (options.client ?? this.client).get({
1201
+ security: [{
1202
+ name: "x-kite-signature",
1203
+ type: "apiKey"
1204
+ }, {
1205
+ name: "x-kite-root-key",
1206
+ type: "apiKey"
1207
+ }],
1208
+ url: "/api/fees/{id}",
1209
+ ...options
1210
+ });
1211
+ }
1212
+ /**
1213
+ * Update a fee
1214
+ */
1215
+ update(options) {
1216
+ return (options.client ?? this.client).patch({
1217
+ security: [{
1218
+ name: "x-kite-signature",
1219
+ type: "apiKey"
1220
+ }, {
1221
+ name: "x-kite-root-key",
1222
+ type: "apiKey"
1223
+ }],
1224
+ url: "/api/fees/{id}",
1225
+ ...options,
1226
+ headers: {
1227
+ "Content-Type": "application/json",
1228
+ ...options.headers
1229
+ }
1230
+ });
1231
+ }
1232
+ };
1233
+ var Kv = class extends HeyApiClient {
1234
+ /**
1235
+ * List configuration values
1236
+ */
1237
+ list(options) {
1238
+ return (options?.client ?? this.client).get({
1239
+ security: [{
1240
+ name: "x-kite-root-key",
1241
+ type: "apiKey"
1242
+ }],
1243
+ url: "/api/kv",
1244
+ ...options
1245
+ });
1246
+ }
1247
+ /**
1248
+ * List providers secrets configuration status
1249
+ */
1250
+ listProviders(options) {
1251
+ return (options?.client ?? this.client).get({
1252
+ security: [{
1253
+ name: "x-kite-root-key",
1254
+ type: "apiKey"
1255
+ }],
1256
+ url: "/api/kv/providers",
1257
+ ...options
1258
+ });
1259
+ }
1260
+ /**
1261
+ * Set a provider key
1262
+ */
1263
+ set(options) {
1264
+ return (options.client ?? this.client).put({
1265
+ security: [{
1266
+ name: "x-kite-root-key",
1267
+ type: "apiKey"
1268
+ }],
1269
+ url: "/api/kv/{key}",
1270
+ ...options,
1271
+ headers: {
1272
+ "Content-Type": "application/json",
1273
+ ...options.headers
1274
+ }
1275
+ });
1276
+ }
1277
+ };
1278
+ var Overview = class extends HeyApiClient {
1279
+ /**
1280
+ * Service version & operator address
1281
+ */
1282
+ info(options) {
1283
+ return (options?.client ?? this.client).get({
1284
+ security: [{
1285
+ name: "x-kite-root-key",
1286
+ type: "apiKey"
1287
+ }],
1288
+ url: "/api/overview/info",
1289
+ ...options
1290
+ });
1291
+ }
1292
+ /**
1293
+ * Basic dashboard statistics
1294
+ */
1295
+ stats(options) {
1296
+ return (options.client ?? this.client).get({
1297
+ security: [{
1298
+ name: "x-kite-root-key",
1299
+ type: "apiKey"
1300
+ }],
1301
+ url: "/api/overview/stats",
1302
+ ...options
1303
+ });
1304
+ }
1305
+ /**
1306
+ * Recent events
1307
+ */
1308
+ events(options) {
1309
+ return (options?.client ?? this.client).get({
1310
+ security: [{
1311
+ name: "x-kite-root-key",
1312
+ type: "apiKey"
1313
+ }],
1314
+ url: "/api/overview/events",
1315
+ ...options
1316
+ });
1317
+ }
1318
+ };
1319
+ var PaymentLinks = class extends HeyApiClient {
1320
+ /**
1321
+ * Currencies a payment link can be priced in
1322
+ */
1323
+ listCurrencies(options) {
1324
+ return (options?.client ?? this.client).get({
1325
+ security: [{
1326
+ name: "x-kite-signature",
1327
+ type: "apiKey"
1328
+ }, {
1329
+ name: "x-kite-root-key",
1330
+ type: "apiKey"
1331
+ }],
1332
+ url: "/api/payment-links/currencies",
1333
+ ...options
1334
+ });
1335
+ }
1336
+ /**
1337
+ * List payment links
1338
+ */
1339
+ list(options) {
1340
+ return (options?.client ?? this.client).get({
1341
+ security: [{
1342
+ name: "x-kite-signature",
1343
+ type: "apiKey"
1344
+ }, {
1345
+ name: "x-kite-root-key",
1346
+ type: "apiKey"
1347
+ }],
1348
+ url: "/api/payment-links",
1349
+ ...options
1350
+ });
1351
+ }
1352
+ /**
1353
+ * Create a payment link
1354
+ */
1355
+ create(options) {
1356
+ return (options.client ?? this.client).post({
1357
+ security: [{
1358
+ name: "x-kite-signature",
1359
+ type: "apiKey"
1360
+ }, {
1361
+ name: "x-kite-root-key",
1362
+ type: "apiKey"
1363
+ }],
1364
+ url: "/api/payment-links",
1365
+ ...options,
1366
+ headers: {
1367
+ "Content-Type": "application/json",
1368
+ ...options.headers
1369
+ }
1370
+ });
1371
+ }
1372
+ /**
1373
+ * Delete a payment link
1374
+ */
1375
+ delete(options) {
1376
+ return (options.client ?? this.client).delete({
1377
+ security: [{
1378
+ name: "x-kite-signature",
1379
+ type: "apiKey"
1380
+ }, {
1381
+ name: "x-kite-root-key",
1382
+ type: "apiKey"
1383
+ }],
1384
+ url: "/api/payment-links/{id}",
1385
+ ...options
1386
+ });
1387
+ }
1388
+ /**
1389
+ * Get a payment link
1390
+ */
1391
+ get(options) {
1392
+ return (options.client ?? this.client).get({
1393
+ security: [{
1394
+ name: "x-kite-signature",
1395
+ type: "apiKey"
1396
+ }, {
1397
+ name: "x-kite-root-key",
1398
+ type: "apiKey"
1399
+ }],
1400
+ url: "/api/payment-links/{id}",
1401
+ ...options
1402
+ });
1403
+ }
1404
+ /**
1405
+ * Update a payment link
1406
+ */
1407
+ update(options) {
1408
+ return (options.client ?? this.client).patch({
1409
+ security: [{
1410
+ name: "x-kite-signature",
1411
+ type: "apiKey"
1412
+ }, {
1413
+ name: "x-kite-root-key",
1414
+ type: "apiKey"
1415
+ }],
1416
+ url: "/api/payment-links/{id}",
1417
+ ...options,
1418
+ headers: {
1419
+ "Content-Type": "application/json",
1420
+ ...options.headers
1421
+ }
1422
+ });
1423
+ }
1424
+ /**
1425
+ * Create a quote for a payment link
1426
+ */
1427
+ quote(options) {
1428
+ return (options.client ?? this.client).post({
1429
+ security: [{
1430
+ name: "x-kite-signature",
1431
+ type: "apiKey"
1432
+ }, {
1433
+ name: "x-kite-root-key",
1434
+ type: "apiKey"
1435
+ }],
1436
+ url: "/api/payment-links/{id}/pay",
1437
+ ...options,
1438
+ headers: {
1439
+ "Content-Type": "application/json",
1440
+ ...options.headers
1441
+ }
1442
+ });
1443
+ }
1444
+ };
1445
+ var Rates = class extends HeyApiClient {
1446
+ /**
1447
+ * Get live currency rate
1448
+ */
1449
+ get(options) {
1450
+ return (options.client ?? this.client).get({
1451
+ security: [{
1452
+ name: "x-kite-signature",
1453
+ type: "apiKey"
1454
+ }, {
1455
+ name: "x-kite-root-key",
1456
+ type: "apiKey"
1457
+ }],
1458
+ url: "/api/rate",
1459
+ ...options
1460
+ });
1461
+ }
1462
+ };
1463
+ var PriceFeeds = class extends HeyApiClient {
1464
+ /**
1465
+ * List price feeds
1466
+ */
1467
+ list(options) {
1468
+ return (options?.client ?? this.client).get({
1469
+ security: [{
1470
+ name: "x-kite-root-key",
1471
+ type: "apiKey"
1472
+ }],
1473
+ url: "/api/price-feeds",
1474
+ ...options
1475
+ });
1476
+ }
1477
+ /**
1478
+ * Add a Chainlink price feed
1479
+ */
1480
+ create(options) {
1481
+ return (options.client ?? this.client).post({
1482
+ security: [{
1483
+ name: "x-kite-root-key",
1484
+ type: "apiKey"
1485
+ }],
1486
+ url: "/api/price-feeds",
1487
+ ...options,
1488
+ headers: {
1489
+ "Content-Type": "application/json",
1490
+ ...options.headers
1491
+ }
1492
+ });
1493
+ }
1494
+ /**
1495
+ * Delete a price feed
1496
+ */
1497
+ delete(options) {
1498
+ return (options.client ?? this.client).delete({
1499
+ security: [{
1500
+ name: "x-kite-root-key",
1501
+ type: "apiKey"
1502
+ }],
1503
+ url: "/api/price-feeds/{id}",
1504
+ ...options
1505
+ });
1506
+ }
1507
+ };
1508
+ var Quotes = class extends HeyApiClient {
1509
+ /**
1510
+ * List quotes
1511
+ */
1512
+ list(options) {
1513
+ return (options?.client ?? this.client).get({
1514
+ security: [{
1515
+ name: "x-kite-signature",
1516
+ type: "apiKey"
1517
+ }, {
1518
+ name: "x-kite-root-key",
1519
+ type: "apiKey"
1520
+ }],
1521
+ url: "/api/quotes",
1522
+ ...options
1523
+ });
1524
+ }
1525
+ /**
1526
+ * Quote a payment of an exact amount in a token
1527
+ */
1528
+ create(options) {
1529
+ return (options.client ?? this.client).post({
1530
+ security: [{
1531
+ name: "x-kite-signature",
1532
+ type: "apiKey"
1533
+ }, {
1534
+ name: "x-kite-root-key",
1535
+ type: "apiKey"
1536
+ }],
1537
+ url: "/api/quotes",
1538
+ ...options,
1539
+ headers: {
1540
+ "Content-Type": "application/json",
1541
+ ...options.headers
1542
+ }
1543
+ });
1544
+ }
1545
+ /**
1546
+ * Get a quote
1547
+ */
1548
+ get(options) {
1549
+ return (options.client ?? this.client).get({
1550
+ security: [{
1551
+ name: "x-kite-signature",
1552
+ type: "apiKey"
1553
+ }, {
1554
+ name: "x-kite-root-key",
1555
+ type: "apiKey"
1556
+ }],
1557
+ url: "/api/quotes/{id}",
1558
+ ...options
1559
+ });
1560
+ }
1561
+ };
1562
+ var Transfers = class extends HeyApiClient {
1563
+ /**
1564
+ * List transfers
1565
+ */
1566
+ list(options) {
1567
+ return (options?.client ?? this.client).get({
1568
+ security: [{
1569
+ name: "x-kite-signature",
1570
+ type: "apiKey"
1571
+ }, {
1572
+ name: "x-kite-root-key",
1573
+ type: "apiKey"
1574
+ }],
1575
+ url: "/api/transfers",
1576
+ ...options
1577
+ });
1578
+ }
1579
+ /**
1580
+ * Create transfer for a quote
1581
+ */
1582
+ create(options) {
1583
+ return (options.client ?? this.client).post({
1584
+ security: [{
1585
+ name: "x-kite-signature",
1586
+ type: "apiKey"
1587
+ }, {
1588
+ name: "x-kite-root-key",
1589
+ type: "apiKey"
1590
+ }],
1591
+ url: "/api/transfers",
1592
+ ...options,
1593
+ headers: {
1594
+ "Content-Type": "application/json",
1595
+ ...options.headers
1596
+ }
1597
+ });
1598
+ }
1599
+ /**
1600
+ * Get a transfer
1601
+ */
1602
+ get(options) {
1603
+ return (options.client ?? this.client).get({
1604
+ security: [{
1605
+ name: "x-kite-signature",
1606
+ type: "apiKey"
1607
+ }, {
1608
+ name: "x-kite-root-key",
1609
+ type: "apiKey"
1610
+ }],
1611
+ url: "/api/transfers/{id}",
1612
+ ...options
1613
+ });
1614
+ }
1615
+ /**
1616
+ * Refund a transfer
1617
+ */
1618
+ refund(options) {
1619
+ return (options.client ?? this.client).post({
1620
+ security: [{
1621
+ name: "x-kite-signature",
1622
+ type: "apiKey"
1623
+ }, {
1624
+ name: "x-kite-root-key",
1625
+ type: "apiKey"
1626
+ }],
1627
+ url: "/api/transfers/{id}/refund",
1628
+ ...options,
1629
+ headers: {
1630
+ "Content-Type": "application/json",
1631
+ ...options.headers
1632
+ }
1633
+ });
1634
+ }
1635
+ /**
1636
+ * Check a transfer pay-in token balance
1637
+ */
1638
+ recoverBalance(options) {
1639
+ return (options.client ?? this.client).post({
1640
+ security: [{
1641
+ name: "x-kite-signature",
1642
+ type: "apiKey"
1643
+ }, {
1644
+ name: "x-kite-root-key",
1645
+ type: "apiKey"
1646
+ }],
1647
+ url: "/api/transfers/{id}/recover/balance",
1648
+ ...options,
1649
+ headers: {
1650
+ "Content-Type": "application/json",
1651
+ ...options.headers
1652
+ }
1653
+ });
1654
+ }
1655
+ /**
1656
+ * Recover transfer tokens
1657
+ */
1658
+ recover(options) {
1659
+ return (options.client ?? this.client).post({
1660
+ security: [{
1661
+ name: "x-kite-signature",
1662
+ type: "apiKey"
1663
+ }, {
1664
+ name: "x-kite-root-key",
1665
+ type: "apiKey"
1666
+ }],
1667
+ url: "/api/transfers/{id}/recover",
1668
+ ...options,
1669
+ headers: {
1670
+ "Content-Type": "application/json",
1671
+ ...options.headers
1672
+ }
1673
+ });
1674
+ }
1675
+ };
1676
+ var Webhooks = class extends HeyApiClient {
1677
+ /**
1678
+ * List webhooks
1679
+ */
1680
+ list(options) {
1681
+ return (options?.client ?? this.client).get({
1682
+ security: [{
1683
+ name: "x-kite-signature",
1684
+ type: "apiKey"
1685
+ }, {
1686
+ name: "x-kite-root-key",
1687
+ type: "apiKey"
1688
+ }],
1689
+ url: "/api/webhooks",
1690
+ ...options
1691
+ });
1692
+ }
1693
+ /**
1694
+ * Create a webhook
1695
+ */
1696
+ create(options) {
1697
+ return (options.client ?? this.client).post({
1698
+ security: [{
1699
+ name: "x-kite-signature",
1700
+ type: "apiKey"
1701
+ }, {
1702
+ name: "x-kite-root-key",
1703
+ type: "apiKey"
1704
+ }],
1705
+ url: "/api/webhooks",
1706
+ ...options,
1707
+ headers: {
1708
+ "Content-Type": "application/json",
1709
+ ...options.headers
1710
+ }
1711
+ });
1712
+ }
1713
+ /**
1714
+ * List a webhook's delivery attempts
1715
+ */
1716
+ listDeliveries(options) {
1717
+ return (options.client ?? this.client).get({
1718
+ security: [{
1719
+ name: "x-kite-signature",
1720
+ type: "apiKey"
1721
+ }, {
1722
+ name: "x-kite-root-key",
1723
+ type: "apiKey"
1724
+ }],
1725
+ url: "/api/webhooks/{id}/deliveries",
1726
+ ...options
1727
+ });
1728
+ }
1729
+ /**
1730
+ * Delete a webhook
1731
+ */
1732
+ delete(options) {
1733
+ return (options.client ?? this.client).delete({
1734
+ security: [{
1735
+ name: "x-kite-signature",
1736
+ type: "apiKey"
1737
+ }, {
1738
+ name: "x-kite-root-key",
1739
+ type: "apiKey"
1740
+ }],
1741
+ url: "/api/webhooks/{id}",
1742
+ ...options
1743
+ });
1744
+ }
1745
+ };
1746
+ var X402 = class extends HeyApiClient {
1747
+ /**
1748
+ * Payment kinds this facilitator supports
1749
+ */
1750
+ supported(options) {
1751
+ return (options?.client ?? this.client).get({
1752
+ security: [{
1753
+ name: "x-kite-signature",
1754
+ type: "apiKey"
1755
+ }, {
1756
+ name: "x-kite-root-key",
1757
+ type: "apiKey"
1758
+ }],
1759
+ url: "/api/x402/supported",
1760
+ ...options
1761
+ });
1762
+ }
1763
+ /**
1764
+ * Open a transfer to be paid with x402
1765
+ */
1766
+ pay(options) {
1767
+ return (options.client ?? this.client).post({
1768
+ security: [{
1769
+ name: "x-kite-signature",
1770
+ type: "apiKey"
1771
+ }, {
1772
+ name: "x-kite-root-key",
1773
+ type: "apiKey"
1774
+ }],
1775
+ url: "/api/x402/pay",
1776
+ ...options,
1777
+ headers: {
1778
+ "Content-Type": "application/json",
1779
+ ...options.headers
1780
+ }
1781
+ });
1782
+ }
1783
+ /**
1784
+ * Payment requirements for a transfer
1785
+ */
1786
+ requirements(options) {
1787
+ return (options.client ?? this.client).get({
1788
+ security: [{
1789
+ name: "x-kite-signature",
1790
+ type: "apiKey"
1791
+ }, {
1792
+ name: "x-kite-root-key",
1793
+ type: "apiKey"
1794
+ }],
1795
+ url: "/api/x402/transfer/{id}/requirements",
1796
+ ...options
1797
+ });
1798
+ }
1799
+ /**
1800
+ * Verify an x402 payment
1801
+ */
1802
+ verify(options) {
1803
+ return (options.client ?? this.client).post({
1804
+ security: [{
1805
+ name: "x-kite-signature",
1806
+ type: "apiKey"
1807
+ }, {
1808
+ name: "x-kite-root-key",
1809
+ type: "apiKey"
1810
+ }],
1811
+ url: "/api/x402/verify",
1812
+ ...options,
1813
+ headers: {
1814
+ "Content-Type": "application/json",
1815
+ ...options.headers
1816
+ }
1817
+ });
1818
+ }
1819
+ /**
1820
+ * Settle an x402 payment
1821
+ */
1822
+ settle(options) {
1823
+ return (options.client ?? this.client).post({
1824
+ security: [{
1825
+ name: "x-kite-signature",
1826
+ type: "apiKey"
1827
+ }, {
1828
+ name: "x-kite-root-key",
1829
+ type: "apiKey"
1830
+ }],
1831
+ url: "/api/x402/settle",
1832
+ ...options,
1833
+ headers: {
1834
+ "Content-Type": "application/json",
1835
+ ...options.headers
1836
+ }
1837
+ });
1838
+ }
1839
+ };
1840
+ var Api = class Api extends HeyApiClient {
1841
+ static __registry = new HeyApiRegistry();
1842
+ constructor(args) {
1843
+ super(args);
1844
+ Api.__registry.set(this, args?.key);
1845
+ }
1846
+ _apps;
1847
+ get apps() {
1848
+ return this._apps ??= new Apps({ client: this.client });
1849
+ }
1850
+ _apiKeys;
1851
+ get apiKeys() {
1852
+ return this._apiKeys ??= new ApiKeys({ client: this.client });
1853
+ }
1854
+ _chains;
1855
+ get chains() {
1856
+ return this._chains ??= new Chains({ client: this.client });
1857
+ }
1858
+ _tokens;
1859
+ get tokens() {
1860
+ return this._tokens ??= new Tokens({ client: this.client });
1861
+ }
1862
+ _checkout;
1863
+ get checkout() {
1864
+ return this._checkout ??= new Checkout({ client: this.client });
1865
+ }
1866
+ _customers;
1867
+ get customers() {
1868
+ return this._customers ??= new Customers({ client: this.client });
1869
+ }
1870
+ _fees;
1871
+ get fees() {
1872
+ return this._fees ??= new Fees({ client: this.client });
1873
+ }
1874
+ _kv;
1875
+ get kv() {
1876
+ return this._kv ??= new Kv({ client: this.client });
1877
+ }
1878
+ _overview;
1879
+ get overview() {
1880
+ return this._overview ??= new Overview({ client: this.client });
1881
+ }
1882
+ _paymentLinks;
1883
+ get paymentLinks() {
1884
+ return this._paymentLinks ??= new PaymentLinks({ client: this.client });
1885
+ }
1886
+ _rates;
1887
+ get rates() {
1888
+ return this._rates ??= new Rates({ client: this.client });
1889
+ }
1890
+ _priceFeeds;
1891
+ get priceFeeds() {
1892
+ return this._priceFeeds ??= new PriceFeeds({ client: this.client });
1893
+ }
1894
+ _quotes;
1895
+ get quotes() {
1896
+ return this._quotes ??= new Quotes({ client: this.client });
1897
+ }
1898
+ _transfers;
1899
+ get transfers() {
1900
+ return this._transfers ??= new Transfers({ client: this.client });
1901
+ }
1902
+ _webhooks;
1903
+ get webhooks() {
1904
+ return this._webhooks ??= new Webhooks({ client: this.client });
1905
+ }
1906
+ _x402;
1907
+ get x402() {
1908
+ return this._x402 ??= new X402({ client: this.client });
1909
+ }
1910
+ };
1911
+ //#endregion
1912
+ //#region src/kite.ts
1913
+ const inBrowser = () => typeof window !== "undefined" && typeof window.document !== "undefined";
1914
+ const authenticate = (client, auth) => {
1915
+ if ("rootKey" in auth) {
1916
+ client.interceptors.request.use((request) => {
1917
+ request.headers.set("x-kite-root-key", auth.rootKey);
1918
+ if (auth.appId) request.headers.set("x-kite-app-id", auth.appId);
1919
+ return request;
1920
+ });
1921
+ return;
1922
+ }
1923
+ const key = importPrivateKey(auth.privateKey);
1924
+ client.interceptors.request.use(async (request, options) => {
1925
+ const url = new URL(request.url);
1926
+ const timestamp = String(Math.floor(Date.now() / 1e3));
1927
+ const nonce = createNonce();
1928
+ const signature = await sign(await key, {
1929
+ timestamp,
1930
+ nonce,
1931
+ method: request.method,
1932
+ target: `${url.pathname}${url.search}`,
1933
+ body: options.serializedBody ?? ""
1934
+ });
1935
+ request.headers.set("x-kite-api-key-id", auth.apiKeyId);
1936
+ request.headers.set("x-kite-timestamp", timestamp);
1937
+ request.headers.set("x-kite-nonce", nonce);
1938
+ request.headers.set("x-kite-signature", signature);
1939
+ return request;
1940
+ });
1941
+ };
1942
+ var Kite = class extends Api {
1943
+ constructor(options) {
1944
+ if (inBrowser() && !options.dangerouslyAllowBrowser) throw new Error("@kite-01/sdk holds secret keys and must not run in a browser. Create payment links on your server and open them with @kite-01/js.");
1945
+ const client = createClient(createConfig({
1946
+ baseUrl: options.baseUrl,
1947
+ fetch: options.fetch,
1948
+ throwOnError: true
1949
+ }));
1950
+ authenticate(client, options.auth);
1951
+ client.interceptors.error.use((error, response) => toKiteError(error, response));
1952
+ super({ client });
1953
+ }
1954
+ };
1955
+ //#endregion
1956
+ //#region src/webhooks.ts
1957
+ var WebhookVerificationError = class extends Error {
1958
+ name = "WebhookVerificationError";
1959
+ };
1960
+ const header = (headers, name) => {
1961
+ if (headers instanceof Headers) return headers.get(name) ?? void 0;
1962
+ const match = Object.entries(headers).find(([key]) => key.toLowerCase() === name)?.[1];
1963
+ return Array.isArray(match) ? match[0] : match ?? void 0;
1964
+ };
1965
+ /**
1966
+ * Checks a webhook delivery came from your Kite server and returns its event.
1967
+ * Pass the body exactly as it arrived, before any JSON parsing.
1968
+ */
1969
+ const verifyWebhook = async (input) => {
1970
+ const signature = header(input.headers, "x-kite-signature");
1971
+ const timestamp = header(input.headers, "x-kite-timestamp");
1972
+ if (!signature || !timestamp) throw new WebhookVerificationError("x-kite-signature and x-kite-timestamp are required");
1973
+ const age = Math.abs(Date.now() / 1e3 - Number(timestamp));
1974
+ if (!/^\d+$/.test(timestamp) || !(age <= (input.tolerance ?? 300))) throw new WebhookVerificationError("The delivery is outside the allowed time window");
1975
+ const body = typeof input.body === "string" ? input.body : new TextDecoder().decode(input.body);
1976
+ const key = await crypto.subtle.importKey("raw", encoder.encode(input.secret), {
1977
+ name: "HMAC",
1978
+ hash: "SHA-256"
1979
+ }, false, ["verify"]);
1980
+ const expected = fromHex(signature);
1981
+ if (!(!!expected && await crypto.subtle.verify("HMAC", key, expected, encoder.encode(`t=${timestamp}&${body}`)))) throw new WebhookVerificationError("Invalid webhook signature");
1982
+ return JSON.parse(body);
1983
+ };
1984
+ //#endregion
1985
+ exports.Kite = Kite;
1986
+ exports.KiteError = KiteError;
1987
+ exports.WebhookVerificationError = WebhookVerificationError;
1988
+ exports.verifyWebhook = verifyWebhook;