@flashbacktech/flashbackclient 0.2.55 → 0.2.56
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/api/client.js +32 -5
- package/package.json +1 -1
package/dist/api/client.js
CHANGED
|
@@ -79,18 +79,39 @@ class ApiClient {
|
|
|
79
79
|
};
|
|
80
80
|
this.makeRequest = async (path, method, data) => {
|
|
81
81
|
const isFormData = data instanceof FormData;
|
|
82
|
+
const isGetOrHead = method === 'GET' || method === 'HEAD';
|
|
83
|
+
// GET/HEAD cannot have a body: serialize plain objects as query string
|
|
84
|
+
let requestPath = path.startsWith('/') ? path.substring(1) : path;
|
|
85
|
+
let body = null;
|
|
86
|
+
if (data != null) {
|
|
87
|
+
if (isGetOrHead && !isFormData && typeof data === 'object' && data.constructor === Object) {
|
|
88
|
+
const params = new URLSearchParams();
|
|
89
|
+
for (const [k, v] of Object.entries(data)) {
|
|
90
|
+
if (v !== undefined && v !== null) {
|
|
91
|
+
params.set(k, String(v));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const qs = params.toString();
|
|
95
|
+
if (qs) {
|
|
96
|
+
requestPath += (requestPath.includes('?') ? '&' : '?') + qs;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else if (!isGetOrHead) {
|
|
100
|
+
body = isFormData ? data : JSON.stringify(data);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
82
103
|
const options = {
|
|
83
104
|
method,
|
|
84
105
|
headers: this.headers,
|
|
85
|
-
body
|
|
106
|
+
body,
|
|
86
107
|
};
|
|
87
|
-
if (
|
|
108
|
+
if (body && !isFormData) {
|
|
88
109
|
options.headers = {
|
|
89
110
|
...options.headers,
|
|
90
111
|
'Content-Type': 'application/json',
|
|
91
112
|
};
|
|
92
113
|
}
|
|
93
|
-
const cleanPath =
|
|
114
|
+
const cleanPath = requestPath;
|
|
94
115
|
if (this.debug) {
|
|
95
116
|
console.log(`DEBUG: ${method} ${cleanPath} ${JSON.stringify(data)}`);
|
|
96
117
|
console.log(`DEBUG: ${JSON.stringify(this.headers)}`);
|
|
@@ -831,7 +852,10 @@ class ApiClient {
|
|
|
831
852
|
return this.makeRequest('credits/balance', 'GET', null);
|
|
832
853
|
};
|
|
833
854
|
this.getCreditsTransactions = async (query) => {
|
|
834
|
-
|
|
855
|
+
console.log('[ApiClient] getCreditsTransactions called', query);
|
|
856
|
+
const result = await this.makeRequest('credits/transactions', 'GET', query);
|
|
857
|
+
console.log('[ApiClient] getCreditsTransactions result', result);
|
|
858
|
+
return result;
|
|
835
859
|
};
|
|
836
860
|
/** Consumption = transactions with direction 'out' (backend uses same endpoint) */
|
|
837
861
|
this.getCreditsConsumption = async (query) => {
|
|
@@ -848,7 +872,10 @@ class ApiClient {
|
|
|
848
872
|
};
|
|
849
873
|
/** Get aggregated monthly credit stats for histogram (consumption, purchases, grants, balance) */
|
|
850
874
|
this.getCreditsMonthlyStats = async (query) => {
|
|
851
|
-
|
|
875
|
+
console.log('[ApiClient] getCreditsMonthlyStats called', query);
|
|
876
|
+
const result = await this.makeRequest('credits/stats/monthly', 'GET', query ?? null);
|
|
877
|
+
console.log('[ApiClient] getCreditsMonthlyStats result', result);
|
|
878
|
+
return result;
|
|
852
879
|
};
|
|
853
880
|
this.baseURL = baseURL;
|
|
854
881
|
this.headers = {};
|