@sfutureapps/db-sdk 0.3.10 → 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
@@ -24,16 +24,21 @@ type ClientOptions = {
24
24
  accessToken?: () => string | null;
25
25
  fetch?: typeof fetch;
26
26
  headers?: Record<string, string>;
27
+ timeoutMs?: number;
27
28
  };
28
29
 
29
30
  declare class HttpClient {
30
31
  private baseUrl;
31
32
  private apiKey?;
32
33
  private accessToken?;
33
- private fetcher;
34
34
  private extraHeaders;
35
35
  constructor(baseUrl: string, opts?: ClientOptions);
36
- post<T>(path: string, body: any): Promise<DbResponse<T>>;
36
+ httpFetch(input: RequestInfo, init?: RequestInit & {
37
+ timeoutMs?: number;
38
+ }): Promise<any>;
39
+ post<T>(path: string, body: any, init?: RequestInit & {
40
+ timeoutMs?: number;
41
+ }): Promise<DbResponse<T>>;
37
42
  }
38
43
 
39
44
  type OrderOpts = {
package/dist/index.d.ts CHANGED
@@ -24,16 +24,21 @@ type ClientOptions = {
24
24
  accessToken?: () => string | null;
25
25
  fetch?: typeof fetch;
26
26
  headers?: Record<string, string>;
27
+ timeoutMs?: number;
27
28
  };
28
29
 
29
30
  declare class HttpClient {
30
31
  private baseUrl;
31
32
  private apiKey?;
32
33
  private accessToken?;
33
- private fetcher;
34
34
  private extraHeaders;
35
35
  constructor(baseUrl: string, opts?: ClientOptions);
36
- post<T>(path: string, body: any): Promise<DbResponse<T>>;
36
+ httpFetch(input: RequestInfo, init?: RequestInit & {
37
+ timeoutMs?: number;
38
+ }): Promise<any>;
39
+ post<T>(path: string, body: any, init?: RequestInit & {
40
+ timeoutMs?: number;
41
+ }): Promise<DbResponse<T>>;
37
42
  }
38
43
 
39
44
  type OrderOpts = {
package/dist/index.js CHANGED
@@ -33,15 +33,30 @@ var HttpClient = class {
33
33
  const g = globalThis;
34
34
  const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
35
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
- );
36
+ throw new Error("Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill.");
39
37
  }
40
- const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
41
- this.fetcher = fetchImpl.bind(thisArg);
42
38
  this.extraHeaders = opts.headers ?? {};
43
39
  }
44
- async post(path, body) {
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
+ }
59
+ async post(path, body, init = {}) {
45
60
  const headers = {
46
61
  "Content-Type": "application/json",
47
62
  ...this.extraHeaders
@@ -49,25 +64,35 @@ var HttpClient = class {
49
64
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
50
65
  const token = this.accessToken?.();
51
66
  if (token) headers["Authorization"] = `Bearer ${token}`;
52
- const res = await this.fetcher(`${this.baseUrl}${path}`, {
53
- method: "POST",
54
- headers,
55
- body: JSON.stringify(body)
56
- });
57
- const text = await res.text();
58
- if (!res.ok) {
59
- return { data: null, error: { status: res.status, message: text || res.statusText } };
60
- }
61
67
  try {
62
- const json = JSON.parse(text || "{}");
68
+ const { payload } = await this.httpFetch(`${this.baseUrl}${path}`, {
69
+ ...init,
70
+ method: "POST",
71
+ headers: {
72
+ ...headers,
73
+ ...init.headers
74
+ },
75
+ body: JSON.stringify(body)
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 } };
86
+ } catch (e) {
87
+ const isAbort = e?.name === "AbortError";
63
88
  return {
64
- data: json.data ?? null,
65
- count: json.count ?? null,
66
- meta: json.meta ?? null,
67
- error: json.error ?? null
89
+ data: null,
90
+ error: {
91
+ status: e?.status ?? 0,
92
+ message: isAbort ? "Request timeout" : e?.message || "Network error",
93
+ details: e?.body ?? e
94
+ }
68
95
  };
69
- } catch {
70
- return { data: null, error: { status: res.status, message: "Invalid JSON response", details: text } };
71
96
  }
72
97
  }
73
98
  };
package/dist/index.mjs CHANGED
@@ -7,15 +7,30 @@ var HttpClient = class {
7
7
  const g = globalThis;
8
8
  const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
9
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
- );
10
+ throw new Error("Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill.");
13
11
  }
14
- const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
15
- this.fetcher = fetchImpl.bind(thisArg);
16
12
  this.extraHeaders = opts.headers ?? {};
17
13
  }
18
- async post(path, body) {
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
+ }
33
+ async post(path, body, init = {}) {
19
34
  const headers = {
20
35
  "Content-Type": "application/json",
21
36
  ...this.extraHeaders
@@ -23,25 +38,35 @@ var HttpClient = class {
23
38
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
24
39
  const token = this.accessToken?.();
25
40
  if (token) headers["Authorization"] = `Bearer ${token}`;
26
- const res = await this.fetcher(`${this.baseUrl}${path}`, {
27
- method: "POST",
28
- headers,
29
- body: JSON.stringify(body)
30
- });
31
- const text = await res.text();
32
- if (!res.ok) {
33
- return { data: null, error: { status: res.status, message: text || res.statusText } };
34
- }
35
41
  try {
36
- const json = JSON.parse(text || "{}");
42
+ const { payload } = await this.httpFetch(`${this.baseUrl}${path}`, {
43
+ ...init,
44
+ method: "POST",
45
+ headers: {
46
+ ...headers,
47
+ ...init.headers
48
+ },
49
+ body: JSON.stringify(body)
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 } };
60
+ } catch (e) {
61
+ const isAbort = e?.name === "AbortError";
37
62
  return {
38
- data: json.data ?? null,
39
- count: json.count ?? null,
40
- meta: json.meta ?? null,
41
- error: json.error ?? null
63
+ data: null,
64
+ error: {
65
+ status: e?.status ?? 0,
66
+ message: isAbort ? "Request timeout" : e?.message || "Network error",
67
+ details: e?.body ?? e
68
+ }
42
69
  };
43
- } catch {
44
- return { data: null, error: { status: res.status, message: "Invalid JSON response", details: text } };
45
70
  }
46
71
  }
47
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfutureapps/db-sdk",
3
- "version": "0.3.10",
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",