@xsai/shared 0.3.5 → 0.4.0-beta.10

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.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  declare class XSAIError extends Error {
2
2
  response?: Response;
3
- constructor(message: string, response?: Response);
3
+ constructor(message: string, response?: Response, cause?: unknown);
4
4
  }
5
5
 
6
6
  type Fetch = (input: URL, init: RequestInit) => Promise<Response>;
@@ -17,6 +17,10 @@ interface CommonRequestOptions {
17
17
  model: string;
18
18
  }
19
19
 
20
+ type WithUnknown<T> = T & {
21
+ [key: string]: unknown;
22
+ };
23
+
20
24
  declare const strCamelToSnake: (str: string) => string;
21
25
  declare const objCamelToSnake: (obj: Record<string, unknown>) => {
22
26
  [k: string]: unknown;
@@ -40,6 +44,16 @@ declare const objCamelToSnake: (obj: Record<string, unknown>) => {
40
44
  */
41
45
  declare const clean: <T extends Record<string, undefined | unknown>>(obj: T) => Record<keyof T, Exclude<T[keyof T], unknown>>;
42
46
 
47
+ declare class DelayedPromise<T> {
48
+ get promise(): Promise<T>;
49
+ private _promise;
50
+ private _reject;
51
+ private _resolve;
52
+ private status;
53
+ reject(error: unknown): void;
54
+ resolve(value: T): void;
55
+ }
56
+
43
57
  declare const requestBody: (body: Record<string, unknown>) => string;
44
58
 
45
59
  declare const requestHeaders: (headers?: CommonRequestOptions["headers"], apiKey?: CommonRequestOptions["apiKey"]) => Record<"Authorization", never>;
@@ -53,4 +67,5 @@ declare const responseJSON: <T>(res: Response) => Promise<T>;
53
67
  type TrampolineFn<T> = (() => Promise<TrampolineFn<T>> | TrampolineFn<T>) | Promise<T> | T;
54
68
  declare const trampoline: <T>(fn: () => Promise<TrampolineFn<T>> | TrampolineFn<T>) => Promise<T>;
55
69
 
56
- export { type CommonRequestOptions, type Fetch, type TrampolineFn, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
70
+ export { DelayedPromise, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
71
+ export type { CommonRequestOptions, Fetch, TrampolineFn, WithUnknown };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  class XSAIError extends Error {
2
2
  response;
3
- constructor(message, response) {
4
- super(message);
3
+ constructor(message, response, cause) {
4
+ super(message, { cause });
5
5
  this.name = "XSAIError";
6
6
  this.response = response;
7
7
  }
@@ -14,6 +14,39 @@ const clean = (obj) => Object.fromEntries(
14
14
  Object.entries(obj).filter(([, v]) => v !== void 0)
15
15
  );
16
16
 
17
+ class DelayedPromise {
18
+ get promise() {
19
+ if (this._promise == null) {
20
+ this._promise = new Promise((resolve, reject) => {
21
+ if (this.status.type === "resolved") {
22
+ resolve(this.status.value);
23
+ } else if (this.status.type === "rejected") {
24
+ reject(this.status.error);
25
+ }
26
+ this._resolve = resolve;
27
+ this._reject = reject;
28
+ });
29
+ }
30
+ return this._promise;
31
+ }
32
+ _promise;
33
+ _reject;
34
+ _resolve;
35
+ status = { type: "pending" };
36
+ reject(error) {
37
+ this.status = { error, type: "rejected" };
38
+ if (this._promise) {
39
+ this._reject?.(error);
40
+ }
41
+ }
42
+ resolve(value) {
43
+ this.status = { type: "resolved", value };
44
+ if (this._promise) {
45
+ this._resolve?.(value);
46
+ }
47
+ }
48
+ }
49
+
17
50
  const requestBody = (body) => JSON.stringify(objCamelToSnake(clean({
18
51
  ...body,
19
52
  abortSignal: void 0,
@@ -43,14 +76,14 @@ const responseCatch = async (res) => {
43
76
  return res;
44
77
  };
45
78
 
46
- const responseJSON = async (res) => responseCatch(res).then(async (res2) => {
47
- const text = await res2.text();
79
+ const responseJSON = async (res) => {
80
+ const text = await res.text();
48
81
  try {
49
82
  return JSON.parse(text);
50
- } catch {
51
- throw new Error(`Failed to parse response, response body: ${text}`);
83
+ } catch (cause) {
84
+ throw new XSAIError(`Failed to parse response, response body: ${text}`, res, cause);
52
85
  }
53
- });
86
+ };
54
87
 
55
88
  const trampoline = async (fn) => {
56
89
  let result = await fn();
@@ -59,4 +92,4 @@ const trampoline = async (fn) => {
59
92
  return result;
60
93
  };
61
94
 
62
- export { XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
95
+ export { DelayedPromise, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/shared",
3
3
  "type": "module",
4
- "version": "0.3.5",
4
+ "version": "0.4.0-beta.10",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",