@sfutureapps/db-sdk 0.3.12 → 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,9 +24,7 @@ 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 {
@@ -32,13 +32,9 @@ declare class HttpClient {
32
32
  private apiKey?;
33
33
  private accessToken?;
34
34
  private extraHeaders;
35
+ private ax;
35
36
  constructor(baseUrl: string, opts?: ClientOptions);
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
+ post<T>(path: string, body: any, config?: AxiosRequestConfig): Promise<DbResponse<T>>;
42
38
  }
43
39
 
44
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,9 +24,7 @@ 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 {
@@ -32,13 +32,9 @@ declare class HttpClient {
32
32
  private apiKey?;
33
33
  private accessToken?;
34
34
  private extraHeaders;
35
+ private ax;
35
36
  constructor(baseUrl: string, opts?: ClientOptions);
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
+ post<T>(path: string, body: any, config?: AxiosRequestConfig): Promise<DbResponse<T>>;
42
38
  }
43
39
 
44
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,72 +35,51 @@ __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
- const g = globalThis;
34
- const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
35
- if (typeof fetchImpl !== "function") {
36
- throw new Error("Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill.");
37
- }
38
44
  this.extraHeaders = opts.headers ?? {};
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;
45
+ this.ax = import_axios.default.create({
46
+ baseURL: this.baseUrl,
47
+ timeout: 15e3,
48
+ headers: {
49
+ "Content-Type": "application/json",
50
+ ...this.extraHeaders
53
51
  }
54
- return payload;
55
- } finally {
56
- clearTimeout(id);
57
- }
52
+ });
58
53
  }
59
- async post(path, body, init = {}) {
54
+ async post(path, body, config = {}) {
60
55
  const headers = {
61
- "Content-Type": "application/json",
62
- ...this.extraHeaders
56
+ ...this.extraHeaders,
57
+ ...config.headers
63
58
  };
64
59
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
65
60
  const token = this.accessToken?.();
66
61
  if (token) headers["Authorization"] = `Bearer ${token}`;
67
62
  try {
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)
63
+ const res = await this.ax.post(path, body, {
64
+ ...config,
65
+ headers
76
66
  });
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 } };
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
+ };
86
74
  } catch (e) {
87
- const isAbort = e?.name === "AbortError";
75
+ const status = e?.response?.status ?? 0;
76
+ const payload = e?.response?.data ?? null;
88
77
  return {
89
78
  data: null,
90
79
  error: {
91
- status: e?.status ?? 0,
92
- message: isAbort ? "Request timeout" : e?.message || "Network error",
93
- details: e?.body ?? e
80
+ status,
81
+ message: payload?.error?.message ?? e?.message ?? "Network error",
82
+ details: payload ?? e
94
83
  }
95
84
  };
96
85
  }
package/dist/index.mjs CHANGED
@@ -1,70 +1,49 @@
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
- const g = globalThis;
8
- const fetchImpl = opts.fetch ?? g.fetch ?? g.window?.fetch;
9
- if (typeof fetchImpl !== "function") {
10
- throw new Error("Fetch API is not available in this environment. Provide ClientOptions.fetch or a global fetch polyfill.");
11
- }
12
8
  this.extraHeaders = opts.headers ?? {};
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;
9
+ this.ax = axios.create({
10
+ baseURL: this.baseUrl,
11
+ timeout: 15e3,
12
+ headers: {
13
+ "Content-Type": "application/json",
14
+ ...this.extraHeaders
27
15
  }
28
- return payload;
29
- } finally {
30
- clearTimeout(id);
31
- }
16
+ });
32
17
  }
33
- async post(path, body, init = {}) {
18
+ async post(path, body, config = {}) {
34
19
  const headers = {
35
- "Content-Type": "application/json",
36
- ...this.extraHeaders
20
+ ...this.extraHeaders,
21
+ ...config.headers
37
22
  };
38
23
  if (this.apiKey) headers["x-api-key"] = this.apiKey;
39
24
  const token = this.accessToken?.();
40
25
  if (token) headers["Authorization"] = `Bearer ${token}`;
41
26
  try {
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)
27
+ const res = await this.ax.post(path, body, {
28
+ ...config,
29
+ headers
50
30
  });
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 } };
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
+ };
60
38
  } catch (e) {
61
- const isAbort = e?.name === "AbortError";
39
+ const status = e?.response?.status ?? 0;
40
+ const payload = e?.response?.data ?? null;
62
41
  return {
63
42
  data: null,
64
43
  error: {
65
- status: e?.status ?? 0,
66
- message: isAbort ? "Request timeout" : e?.message || "Network error",
67
- details: e?.body ?? e
44
+ status,
45
+ message: payload?.error?.message ?? e?.message ?? "Network error",
46
+ details: payload ?? e
68
47
  }
69
48
  };
70
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfutureapps/db-sdk",
3
- "version": "0.3.12",
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
  }