@sfutureapps/db-sdk 0.3.11 → 0.3.13

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
@@ -1,3 +1,5 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+
1
3
  type FilterOp = "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "like" | "notLike" | "between" | "notBetween" | "in" | "notIn" | "isNull" | "notNull" | "regexp" | "notRegexp" | "time" | "findInSet";
2
4
  type Filter = {
3
5
  col: string;
@@ -22,22 +24,17 @@ type DbResponse<T> = {
22
24
  type ClientOptions = {
23
25
  apiKey?: string;
24
26
  accessToken?: () => string | null;
25
- fetch?: typeof fetch;
26
27
  headers?: Record<string, string>;
27
- timeoutMs?: number;
28
28
  };
29
29
 
30
30
  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;
35
+ private ax;
37
36
  constructor(baseUrl: string, opts?: ClientOptions);
38
- post<T>(path: string, body: any, init?: (RequestInit & {
39
- timeoutMs?: number;
40
- })): Promise<DbResponse<T>>;
37
+ post<T>(path: string, body: any, config?: AxiosRequestConfig): Promise<DbResponse<T>>;
41
38
  }
42
39
 
43
40
  type OrderOpts = {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+
1
3
  type FilterOp = "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "like" | "notLike" | "between" | "notBetween" | "in" | "notIn" | "isNull" | "notNull" | "regexp" | "notRegexp" | "time" | "findInSet";
2
4
  type Filter = {
3
5
  col: string;
@@ -22,22 +24,17 @@ type DbResponse<T> = {
22
24
  type ClientOptions = {
23
25
  apiKey?: string;
24
26
  accessToken?: () => string | null;
25
- fetch?: typeof fetch;
26
27
  headers?: Record<string, string>;
27
- timeoutMs?: number;
28
28
  };
29
29
 
30
30
  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;
35
+ private ax;
37
36
  constructor(baseUrl: string, opts?: ClientOptions);
38
- post<T>(path: string, body: any, init?: (RequestInit & {
39
- timeoutMs?: number;
40
- })): Promise<DbResponse<T>>;
37
+ post<T>(path: string, body: any, config?: AxiosRequestConfig): Promise<DbResponse<T>>;
41
38
  }
42
39
 
43
40
  type OrderOpts = {
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -25,80 +35,53 @@ __export(index_exports, {
25
35
  module.exports = __toCommonJS(index_exports);
26
36
 
27
37
  // src/client.ts
38
+ var import_axios = __toESM(require("axios"));
28
39
  var HttpClient = class {
29
40
  constructor(baseUrl, opts = {}) {
30
41
  this.baseUrl = baseUrl.replace(/\/+$/, "");
31
42
  this.apiKey = opts.apiKey;
32
43
  this.accessToken = opts.accessToken;
33
- this.timeoutMs = opts.timeoutMs ?? 15e3;
34
- const g = globalThis;
35
- const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
36
- 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
- );
40
- }
41
- const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
42
- this.fetcher = fetchImpl.bind(thisArg);
43
44
  this.extraHeaders = opts.headers ?? {};
45
+ this.ax = import_axios.default.create({
46
+ baseURL: this.baseUrl,
47
+ timeout: 15e3,
48
+ headers: {
49
+ "Content-Type": "application/json",
50
+ ...this.extraHeaders
51
+ }
52
+ });
44
53
  }
45
- async post(path, body, init = {}) {
54
+ async post(path, body, config = {}) {
46
55
  const headers = {
47
- "Content-Type": "application/json",
48
- ...this.extraHeaders
56
+ ...this.extraHeaders,
57
+ ...config.headers
49
58
  };
50
59
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
51
60
  const token = this.accessToken?.();
52
61
  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
62
  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
63
+ const res = await this.ax.post(path, body, {
64
+ ...config,
65
+ headers
74
66
  });
67
+ const payload = res.data;
68
+ return {
69
+ data: payload?.data ?? null,
70
+ count: payload?.count ?? null,
71
+ meta: payload?.meta ?? null,
72
+ error: payload?.error ?? null
73
+ };
75
74
  } catch (e) {
76
- const isAbort = e?.name === "AbortError";
75
+ const status = e?.response?.status ?? 0;
76
+ const payload = e?.response?.data ?? null;
77
77
  return {
78
78
  data: null,
79
79
  error: {
80
- status: 0,
81
- message: isAbort ? "Request timeout" : e?.message || "Network error",
82
- details: e
80
+ status,
81
+ message: payload?.error?.message ?? e?.message ?? "Network error",
82
+ details: payload ?? e
83
83
  }
84
84
  };
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
85
  }
103
86
  }
104
87
  };
package/dist/index.mjs CHANGED
@@ -1,78 +1,51 @@
1
1
  // src/client.ts
2
+ import axios from "axios";
2
3
  var HttpClient = class {
3
4
  constructor(baseUrl, opts = {}) {
4
5
  this.baseUrl = baseUrl.replace(/\/+$/, "");
5
6
  this.apiKey = opts.apiKey;
6
7
  this.accessToken = opts.accessToken;
7
- this.timeoutMs = opts.timeoutMs ?? 15e3;
8
- const g = globalThis;
9
- const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
10
- 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
- );
14
- }
15
- const thisArg = g.window && g.window.fetch === fetchImpl ? g.window : g;
16
- this.fetcher = fetchImpl.bind(thisArg);
17
8
  this.extraHeaders = opts.headers ?? {};
9
+ this.ax = axios.create({
10
+ baseURL: this.baseUrl,
11
+ timeout: 15e3,
12
+ headers: {
13
+ "Content-Type": "application/json",
14
+ ...this.extraHeaders
15
+ }
16
+ });
18
17
  }
19
- async post(path, body, init = {}) {
18
+ async post(path, body, config = {}) {
20
19
  const headers = {
21
- "Content-Type": "application/json",
22
- ...this.extraHeaders
20
+ ...this.extraHeaders,
21
+ ...config.headers
23
22
  };
24
23
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
25
24
  const token = this.accessToken?.();
26
25
  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
26
  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
27
+ const res = await this.ax.post(path, body, {
28
+ ...config,
29
+ headers
48
30
  });
31
+ const payload = res.data;
32
+ return {
33
+ data: payload?.data ?? null,
34
+ count: payload?.count ?? null,
35
+ meta: payload?.meta ?? null,
36
+ error: payload?.error ?? null
37
+ };
49
38
  } catch (e) {
50
- const isAbort = e?.name === "AbortError";
39
+ const status = e?.response?.status ?? 0;
40
+ const payload = e?.response?.data ?? null;
51
41
  return {
52
42
  data: null,
53
43
  error: {
54
- status: 0,
55
- message: isAbort ? "Request timeout" : e?.message || "Network error",
56
- details: e
44
+ status,
45
+ message: payload?.error?.message ?? e?.message ?? "Network error",
46
+ details: payload ?? e
57
47
  }
58
48
  };
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
49
  }
77
50
  }
78
51
  };
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.13",
4
4
  "description": "SfutureApps JS SDK for ThinkPHP DB Gateway (MySQL)",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
@@ -35,5 +35,8 @@
35
35
  "typescript",
36
36
  "sFutureApps",
37
37
  "sfutureapps.com"
38
- ]
38
+ ],
39
+ "dependencies": {
40
+ "axios": "^1.13.2"
41
+ }
39
42
  }