@sfutureapps/db-sdk 0.3.9 → 0.3.11

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,6 +24,7 @@ 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 {
@@ -32,8 +33,11 @@ declare class HttpClient {
32
33
  private accessToken?;
33
34
  private fetcher;
34
35
  private extraHeaders;
36
+ private timeoutMs;
35
37
  constructor(baseUrl: string, opts?: ClientOptions);
36
- post<T>(path: string, body: any): Promise<DbResponse<T>>;
38
+ post<T>(path: string, body: any, init?: (RequestInit & {
39
+ timeoutMs?: number;
40
+ })): Promise<DbResponse<T>>;
37
41
  }
38
42
 
39
43
  type OrderOpts = {
package/dist/index.d.ts CHANGED
@@ -24,6 +24,7 @@ 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 {
@@ -32,8 +33,11 @@ declare class HttpClient {
32
33
  private accessToken?;
33
34
  private fetcher;
34
35
  private extraHeaders;
36
+ private timeoutMs;
35
37
  constructor(baseUrl: string, opts?: ClientOptions);
36
- post<T>(path: string, body: any): Promise<DbResponse<T>>;
38
+ post<T>(path: string, body: any, init?: (RequestInit & {
39
+ timeoutMs?: number;
40
+ })): Promise<DbResponse<T>>;
37
41
  }
38
42
 
39
43
  type OrderOpts = {
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ 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;
33
34
  const g = globalThis;
34
35
  const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
35
36
  if (typeof fetchImpl !== "function") {
@@ -41,7 +42,7 @@ var HttpClient = class {
41
42
  this.fetcher = fetchImpl.bind(thisArg);
42
43
  this.extraHeaders = opts.headers ?? {};
43
44
  }
44
- async post(path, body) {
45
+ async post(path, body, init = {}) {
45
46
  const headers = {
46
47
  "Content-Type": "application/json",
47
48
  ...this.extraHeaders
@@ -49,11 +50,41 @@ var HttpClient = class {
49
50
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
50
51
  const token = this.accessToken?.();
51
52
  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
- });
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
+ try {
65
+ res = await this.fetcher(`${this.baseUrl}${path}`, {
66
+ ...restInit,
67
+ method: "POST",
68
+ headers: {
69
+ ...headers,
70
+ ...restInit.headers
71
+ },
72
+ body: JSON.stringify(body),
73
+ signal: ac.signal
74
+ });
75
+ } catch (e) {
76
+ const isAbort = e?.name === "AbortError";
77
+ return {
78
+ data: null,
79
+ error: {
80
+ status: 0,
81
+ message: isAbort ? "Request timeout" : e?.message || "Network error",
82
+ details: e
83
+ }
84
+ };
85
+ } finally {
86
+ clearTimeout(timeoutId);
87
+ }
57
88
  const text = await res.text();
58
89
  if (!res.ok) {
59
90
  return { data: null, error: { status: res.status, message: text || res.statusText } };
package/dist/index.mjs CHANGED
@@ -4,6 +4,7 @@ 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;
7
8
  const g = globalThis;
8
9
  const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
9
10
  if (typeof fetchImpl !== "function") {
@@ -15,7 +16,7 @@ var HttpClient = class {
15
16
  this.fetcher = fetchImpl.bind(thisArg);
16
17
  this.extraHeaders = opts.headers ?? {};
17
18
  }
18
- async post(path, body) {
19
+ async post(path, body, init = {}) {
19
20
  const headers = {
20
21
  "Content-Type": "application/json",
21
22
  ...this.extraHeaders
@@ -23,11 +24,41 @@ var HttpClient = class {
23
24
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
24
25
  const token = this.accessToken?.();
25
26
  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
- });
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
+ try {
39
+ res = await this.fetcher(`${this.baseUrl}${path}`, {
40
+ ...restInit,
41
+ method: "POST",
42
+ headers: {
43
+ ...headers,
44
+ ...restInit.headers
45
+ },
46
+ body: JSON.stringify(body),
47
+ signal: ac.signal
48
+ });
49
+ } catch (e) {
50
+ const isAbort = e?.name === "AbortError";
51
+ return {
52
+ data: null,
53
+ error: {
54
+ status: 0,
55
+ message: isAbort ? "Request timeout" : e?.message || "Network error",
56
+ details: e
57
+ }
58
+ };
59
+ } finally {
60
+ clearTimeout(timeoutId);
61
+ }
31
62
  const text = await res.text();
32
63
  if (!res.ok) {
33
64
  return { data: null, error: { status: res.status, message: text || res.statusText } };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfutureapps/db-sdk",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "description": "SfutureApps JS SDK for ThinkPHP DB Gateway (MySQL)",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",