@sfutureapps/db-sdk 0.3.11 → 0.3.12

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
@@ -31,13 +31,14 @@ declare class HttpClient {
31
31
  private baseUrl;
32
32
  private apiKey?;
33
33
  private accessToken?;
34
- private fetcher;
35
34
  private extraHeaders;
36
- private timeoutMs;
37
35
  constructor(baseUrl: string, opts?: ClientOptions);
38
- post<T>(path: string, body: any, init?: (RequestInit & {
36
+ httpFetch(input: RequestInfo, init?: RequestInit & {
39
37
  timeoutMs?: number;
40
- })): Promise<DbResponse<T>>;
38
+ }): Promise<any>;
39
+ post<T>(path: string, body: any, init?: RequestInit & {
40
+ timeoutMs?: number;
41
+ }): Promise<DbResponse<T>>;
41
42
  }
42
43
 
43
44
  type OrderOpts = {
package/dist/index.d.ts CHANGED
@@ -31,13 +31,14 @@ declare class HttpClient {
31
31
  private baseUrl;
32
32
  private apiKey?;
33
33
  private accessToken?;
34
- private fetcher;
35
34
  private extraHeaders;
36
- private timeoutMs;
37
35
  constructor(baseUrl: string, opts?: ClientOptions);
38
- post<T>(path: string, body: any, init?: (RequestInit & {
36
+ httpFetch(input: RequestInfo, init?: RequestInit & {
39
37
  timeoutMs?: number;
40
- })): Promise<DbResponse<T>>;
38
+ }): Promise<any>;
39
+ post<T>(path: string, body: any, init?: RequestInit & {
40
+ timeoutMs?: number;
41
+ }): Promise<DbResponse<T>>;
41
42
  }
42
43
 
43
44
  type OrderOpts = {
package/dist/index.js CHANGED
@@ -30,18 +30,32 @@ var HttpClient = class {
30
30
  this.baseUrl = baseUrl.replace(/\/+$/, "");
31
31
  this.apiKey = opts.apiKey;
32
32
  this.accessToken = opts.accessToken;
33
- this.timeoutMs = opts.timeoutMs ?? 15e3;
34
33
  const g = globalThis;
35
34
  const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
36
35
  if (typeof fetchImpl !== "function") {
37
- throw new Error(
38
- "Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill."
39
- );
36
+ throw new Error("Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill.");
40
37
  }
41
- const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
42
- this.fetcher = fetchImpl.bind(thisArg);
43
38
  this.extraHeaders = opts.headers ?? {};
44
39
  }
40
+ async httpFetch(input, init = {}) {
41
+ const { timeoutMs = 15e3, ...rest } = init;
42
+ const ac = new AbortController();
43
+ const id = setTimeout(() => ac.abort(), timeoutMs);
44
+ try {
45
+ const res = await fetch(input, { ...rest, signal: ac.signal });
46
+ const ct = res.headers.get("content-type") || "";
47
+ const payload = ct.includes("application/json") ? await res.json() : await res.text();
48
+ if (!res.ok) {
49
+ const err = new Error(`HTTP ${res.status} ${res.statusText}`);
50
+ err.status = res.status;
51
+ err.body = payload;
52
+ throw err;
53
+ }
54
+ return payload;
55
+ } finally {
56
+ clearTimeout(id);
57
+ }
58
+ }
45
59
  async post(path, body, init = {}) {
46
60
  const headers = {
47
61
  "Content-Type": "application/json",
@@ -50,55 +64,35 @@ var HttpClient = class {
50
64
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
51
65
  const token = this.accessToken?.();
52
66
  if (token) headers["Authorization"] = `Bearer ${token}`;
53
- const { timeoutMs = this.timeoutMs, ...restInit } = init;
54
- const ac = new AbortController();
55
- const timeoutId = setTimeout(() => ac.abort(), Math.max(0, timeoutMs));
56
- if (restInit.signal) {
57
- if (restInit.signal.aborted) {
58
- ac.abort();
59
- } else {
60
- restInit.signal.addEventListener("abort", () => ac.abort(), { once: true });
61
- }
62
- }
63
- let res;
64
67
  try {
65
- res = await this.fetcher(`${this.baseUrl}${path}`, {
66
- ...restInit,
68
+ const { payload } = await this.httpFetch(`${this.baseUrl}${path}`, {
69
+ ...init,
67
70
  method: "POST",
68
71
  headers: {
69
72
  ...headers,
70
- ...restInit.headers
73
+ ...init.headers
71
74
  },
72
- body: JSON.stringify(body),
73
- signal: ac.signal
75
+ body: JSON.stringify(body)
74
76
  });
77
+ if (payload && typeof payload === "object") {
78
+ return {
79
+ data: payload.data ?? null,
80
+ count: payload.count ?? null,
81
+ meta: payload.meta ?? null,
82
+ error: payload.error ?? null
83
+ };
84
+ }
85
+ return { data: null, error: { status: 200, message: "Invalid JSON response", details: payload } };
75
86
  } catch (e) {
76
87
  const isAbort = e?.name === "AbortError";
77
88
  return {
78
89
  data: null,
79
90
  error: {
80
- status: 0,
91
+ status: e?.status ?? 0,
81
92
  message: isAbort ? "Request timeout" : e?.message || "Network error",
82
- details: e
93
+ details: e?.body ?? e
83
94
  }
84
95
  };
85
- } finally {
86
- clearTimeout(timeoutId);
87
- }
88
- const text = await res.text();
89
- if (!res.ok) {
90
- return { data: null, error: { status: res.status, message: text || res.statusText } };
91
- }
92
- try {
93
- const json = JSON.parse(text || "{}");
94
- return {
95
- data: json.data ?? null,
96
- count: json.count ?? null,
97
- meta: json.meta ?? null,
98
- error: json.error ?? null
99
- };
100
- } catch {
101
- return { data: null, error: { status: res.status, message: "Invalid JSON response", details: text } };
102
96
  }
103
97
  }
104
98
  };
package/dist/index.mjs CHANGED
@@ -4,18 +4,32 @@ var HttpClient = class {
4
4
  this.baseUrl = baseUrl.replace(/\/+$/, "");
5
5
  this.apiKey = opts.apiKey;
6
6
  this.accessToken = opts.accessToken;
7
- this.timeoutMs = opts.timeoutMs ?? 15e3;
8
7
  const g = globalThis;
9
8
  const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
10
9
  if (typeof fetchImpl !== "function") {
11
- throw new Error(
12
- "Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill."
13
- );
10
+ throw new Error("Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill.");
14
11
  }
15
- const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
16
- this.fetcher = fetchImpl.bind(thisArg);
17
12
  this.extraHeaders = opts.headers ?? {};
18
13
  }
14
+ async httpFetch(input, init = {}) {
15
+ const { timeoutMs = 15e3, ...rest } = init;
16
+ const ac = new AbortController();
17
+ const id = setTimeout(() => ac.abort(), timeoutMs);
18
+ try {
19
+ const res = await fetch(input, { ...rest, signal: ac.signal });
20
+ const ct = res.headers.get("content-type") || "";
21
+ const payload = ct.includes("application/json") ? await res.json() : await res.text();
22
+ if (!res.ok) {
23
+ const err = new Error(`HTTP ${res.status} ${res.statusText}`);
24
+ err.status = res.status;
25
+ err.body = payload;
26
+ throw err;
27
+ }
28
+ return payload;
29
+ } finally {
30
+ clearTimeout(id);
31
+ }
32
+ }
19
33
  async post(path, body, init = {}) {
20
34
  const headers = {
21
35
  "Content-Type": "application/json",
@@ -24,55 +38,35 @@ var HttpClient = class {
24
38
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
25
39
  const token = this.accessToken?.();
26
40
  if (token) headers["Authorization"] = `Bearer ${token}`;
27
- const { timeoutMs = this.timeoutMs, ...restInit } = init;
28
- const ac = new AbortController();
29
- const timeoutId = setTimeout(() => ac.abort(), Math.max(0, timeoutMs));
30
- if (restInit.signal) {
31
- if (restInit.signal.aborted) {
32
- ac.abort();
33
- } else {
34
- restInit.signal.addEventListener("abort", () => ac.abort(), { once: true });
35
- }
36
- }
37
- let res;
38
41
  try {
39
- res = await this.fetcher(`${this.baseUrl}${path}`, {
40
- ...restInit,
42
+ const { payload } = await this.httpFetch(`${this.baseUrl}${path}`, {
43
+ ...init,
41
44
  method: "POST",
42
45
  headers: {
43
46
  ...headers,
44
- ...restInit.headers
47
+ ...init.headers
45
48
  },
46
- body: JSON.stringify(body),
47
- signal: ac.signal
49
+ body: JSON.stringify(body)
48
50
  });
51
+ if (payload && typeof payload === "object") {
52
+ return {
53
+ data: payload.data ?? null,
54
+ count: payload.count ?? null,
55
+ meta: payload.meta ?? null,
56
+ error: payload.error ?? null
57
+ };
58
+ }
59
+ return { data: null, error: { status: 200, message: "Invalid JSON response", details: payload } };
49
60
  } catch (e) {
50
61
  const isAbort = e?.name === "AbortError";
51
62
  return {
52
63
  data: null,
53
64
  error: {
54
- status: 0,
65
+ status: e?.status ?? 0,
55
66
  message: isAbort ? "Request timeout" : e?.message || "Network error",
56
- details: e
67
+ details: e?.body ?? e
57
68
  }
58
69
  };
59
- } finally {
60
- clearTimeout(timeoutId);
61
- }
62
- const text = await res.text();
63
- if (!res.ok) {
64
- return { data: null, error: { status: res.status, message: text || res.statusText } };
65
- }
66
- try {
67
- const json = JSON.parse(text || "{}");
68
- return {
69
- data: json.data ?? null,
70
- count: json.count ?? null,
71
- meta: json.meta ?? null,
72
- error: json.error ?? null
73
- };
74
- } catch {
75
- return { data: null, error: { status: res.status, message: "Invalid JSON response", details: text } };
76
70
  }
77
71
  }
78
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfutureapps/db-sdk",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "description": "SfutureApps JS SDK for ThinkPHP DB Gateway (MySQL)",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",