@sfutureapps/db-sdk 0.3.7 → 0.3.9

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.mts CHANGED
@@ -30,6 +30,7 @@ declare class HttpClient {
30
30
  private baseUrl;
31
31
  private apiKey?;
32
32
  private accessToken?;
33
+ private fetcher;
33
34
  private extraHeaders;
34
35
  constructor(baseUrl: string, opts?: ClientOptions);
35
36
  post<T>(path: string, body: any): Promise<DbResponse<T>>;
package/dist/index.d.ts CHANGED
@@ -30,6 +30,7 @@ declare class HttpClient {
30
30
  private baseUrl;
31
31
  private apiKey?;
32
32
  private accessToken?;
33
+ private fetcher;
33
34
  private extraHeaders;
34
35
  constructor(baseUrl: string, opts?: ClientOptions);
35
36
  post<T>(path: string, body: any): Promise<DbResponse<T>>;
package/dist/index.js CHANGED
@@ -30,6 +30,15 @@ var HttpClient = class {
30
30
  this.baseUrl = baseUrl.replace(/\/+$/, "");
31
31
  this.apiKey = opts.apiKey;
32
32
  this.accessToken = opts.accessToken;
33
+ const g = globalThis;
34
+ const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
35
+ if (typeof fetchImpl !== "function") {
36
+ throw new Error(
37
+ "Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill."
38
+ );
39
+ }
40
+ const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
41
+ this.fetcher = fetchImpl.bind(thisArg);
33
42
  this.extraHeaders = opts.headers ?? {};
34
43
  }
35
44
  async post(path, body) {
@@ -40,18 +49,14 @@ var HttpClient = class {
40
49
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
41
50
  const token = this.accessToken?.();
42
51
  if (token) headers["Authorization"] = `Bearer ${token}`;
43
- const url = `${this.baseUrl}${path}`;
44
- const res = await window.fetch(url, {
52
+ const res = await this.fetcher(`${this.baseUrl}${path}`, {
45
53
  method: "POST",
46
54
  headers,
47
55
  body: JSON.stringify(body)
48
56
  });
49
57
  const text = await res.text();
50
58
  if (!res.ok) {
51
- return {
52
- data: null,
53
- error: { status: res.status, message: text || res.statusText }
54
- };
59
+ return { data: null, error: { status: res.status, message: text || res.statusText } };
55
60
  }
56
61
  try {
57
62
  const json = JSON.parse(text || "{}");
@@ -62,10 +67,7 @@ var HttpClient = class {
62
67
  error: json.error ?? null
63
68
  };
64
69
  } catch {
65
- return {
66
- data: null,
67
- error: { message: "Invalid JSON response", details: text }
68
- };
70
+ return { data: null, error: { status: res.status, message: "Invalid JSON response", details: text } };
69
71
  }
70
72
  }
71
73
  };
package/dist/index.mjs CHANGED
@@ -4,6 +4,15 @@ var HttpClient = class {
4
4
  this.baseUrl = baseUrl.replace(/\/+$/, "");
5
5
  this.apiKey = opts.apiKey;
6
6
  this.accessToken = opts.accessToken;
7
+ const g = globalThis;
8
+ const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
9
+ if (typeof fetchImpl !== "function") {
10
+ throw new Error(
11
+ "Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill."
12
+ );
13
+ }
14
+ const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
15
+ this.fetcher = fetchImpl.bind(thisArg);
7
16
  this.extraHeaders = opts.headers ?? {};
8
17
  }
9
18
  async post(path, body) {
@@ -14,18 +23,14 @@ var HttpClient = class {
14
23
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
15
24
  const token = this.accessToken?.();
16
25
  if (token) headers["Authorization"] = `Bearer ${token}`;
17
- const url = `${this.baseUrl}${path}`;
18
- const res = await window.fetch(url, {
26
+ const res = await this.fetcher(`${this.baseUrl}${path}`, {
19
27
  method: "POST",
20
28
  headers,
21
29
  body: JSON.stringify(body)
22
30
  });
23
31
  const text = await res.text();
24
32
  if (!res.ok) {
25
- return {
26
- data: null,
27
- error: { status: res.status, message: text || res.statusText }
28
- };
33
+ return { data: null, error: { status: res.status, message: text || res.statusText } };
29
34
  }
30
35
  try {
31
36
  const json = JSON.parse(text || "{}");
@@ -36,10 +41,7 @@ var HttpClient = class {
36
41
  error: json.error ?? null
37
42
  };
38
43
  } catch {
39
- return {
40
- data: null,
41
- error: { message: "Invalid JSON response", details: text }
42
- };
44
+ return { data: null, error: { status: res.status, message: "Invalid JSON response", details: text } };
43
45
  }
44
46
  }
45
47
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfutureapps/db-sdk",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "description": "SfutureApps JS SDK for ThinkPHP DB Gateway (MySQL)",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",