@xsai/shared 0.0.23 → 0.0.25

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,3 +1,8 @@
1
+ declare class XSAIError extends Error {
2
+ response?: Response;
3
+ constructor(message: string, response?: Response);
4
+ }
5
+
1
6
  interface CommonRequestOptions {
2
7
  abortSignal?: AbortSignal;
3
8
  apiKey?: string;
@@ -23,4 +28,8 @@ declare const requestHeaders: (headers?: CommonRequestOptions["headers"], apiKey
23
28
 
24
29
  declare const requestURL: (path: string, baseURL: string | URL) => URL;
25
30
 
26
- export { type CommonRequestOptions, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, strCamelToSnake };
31
+ declare const responseCatch: (res: Response) => Promise<Response>;
32
+
33
+ declare const responseJSON: <T>(res: Response) => Promise<T>;
34
+
35
+ export { type CommonRequestOptions, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake };
package/dist/index.js CHANGED
@@ -1,3 +1,12 @@
1
+ class XSAIError extends Error {
2
+ response;
3
+ constructor(message, response) {
4
+ super(message);
5
+ this.name = "XSAIError";
6
+ this.response = response;
7
+ }
8
+ }
9
+
1
10
  const strCamelToSnake = (str) => str.replace(/[A-Z]/g, (s) => `_${s.toLowerCase()}`);
2
11
  const objCamelToSnake = (obj) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [strCamelToSnake(k), v]));
3
12
 
@@ -22,4 +31,23 @@ const requestURL = (path, baseURL) => {
22
31
  return new URL(path, base.endsWith("/") ? base : `${base}/`);
23
32
  };
24
33
 
25
- export { clean, objCamelToSnake, requestBody, requestHeaders, requestURL, strCamelToSnake };
34
+ const responseCatch = async (res) => {
35
+ if (!res.ok) {
36
+ const error = new XSAIError(`Remote sent ${res.status} response`, res);
37
+ error.cause = new Error(await res.text());
38
+ throw error;
39
+ }
40
+ if (!res.body) {
41
+ throw new XSAIError("Response body is empty from remote server", res);
42
+ }
43
+ if (!(res.body instanceof ReadableStream)) {
44
+ const error = new XSAIError(`Expected Response body to be a ReadableStream, but got ${String(res.body)}`, res);
45
+ error.cause = new Error(`Content-Type is ${res.headers.get("Content-Type")}`);
46
+ throw error;
47
+ }
48
+ return res;
49
+ };
50
+
51
+ const responseJSON = async (res) => responseCatch(res).then((res2) => res2.json());
52
+
53
+ export { XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsai/shared",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",