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