@kevisual/api 0.0.46 → 0.0.48

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.
@@ -0,0 +1,45 @@
1
+ import { Query } from '@kevisual/query';
2
+
3
+ /**
4
+ * 配置查询
5
+ * @updatedAt 2025-12-03 10:33:00
6
+ */
7
+
8
+ type QueryConfigOpts = {
9
+ query?: Query;
10
+ };
11
+ type Config<T = any> = {
12
+ id?: string;
13
+ title?: string;
14
+ key?: string;
15
+ description?: string;
16
+ data?: T;
17
+ createdAt?: string;
18
+ updatedAt?: string;
19
+ };
20
+ type UploadConfig = {
21
+ key?: string;
22
+ version?: string;
23
+ };
24
+ type PostOpts = {
25
+ token?: string;
26
+ payload?: Record<string, any>;
27
+ };
28
+ declare class QueryConfig {
29
+ query: Query;
30
+ constructor(opts?: QueryConfigOpts);
31
+ post<T = Config>(data: any): Promise<any>;
32
+ getItem({ id, key }: {
33
+ id?: string;
34
+ key?: string;
35
+ }, opts?: PostOpts): Promise<any>;
36
+ updateItem(data: Config, opts?: PostOpts): Promise<any>;
37
+ deleteItem(data: {
38
+ id?: string;
39
+ key?: string;
40
+ }, opts?: PostOpts): Promise<any>;
41
+ listItems(opts?: PostOpts): Promise<any>;
42
+ }
43
+
44
+ export { QueryConfig };
45
+ export type { Config, UploadConfig };
@@ -0,0 +1,301 @@
1
+ // node_modules/.pnpm/@kevisual+query@0.0.40/node_modules/@kevisual/query/dist/query-browser.js
2
+ var isTextForContentType = (contentType) => {
3
+ if (!contentType)
4
+ return false;
5
+ const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
6
+ return textTypes.some((type) => contentType.includes(type));
7
+ };
8
+ var adapter = async (opts = {}, overloadOpts) => {
9
+ const controller = new AbortController;
10
+ const signal = controller.signal;
11
+ const isPostFile = opts.isPostFile || false;
12
+ let responseType = opts.responseType || "json";
13
+ if (opts.isBlob) {
14
+ responseType = "blob";
15
+ } else if (opts.isText) {
16
+ responseType = "text";
17
+ }
18
+ const timeout = opts.timeout || 60000 * 3;
19
+ const timer = setTimeout(() => {
20
+ controller.abort();
21
+ }, timeout);
22
+ let method = overloadOpts?.method || opts?.method || "POST";
23
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
24
+ let origin = "";
25
+ let url;
26
+ if (opts?.url?.startsWith("http")) {
27
+ url = new URL(opts.url);
28
+ } else {
29
+ origin = window?.location?.origin || "http://localhost:51515";
30
+ url = new URL(opts?.url || "", origin);
31
+ }
32
+ const isGet = method === "GET";
33
+ const oldSearchParams = url.searchParams;
34
+ if (isGet) {
35
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts?.params, ...opts?.body });
36
+ url.search = searchParams.toString();
37
+ } else {
38
+ const params = {
39
+ ...Object.fromEntries(oldSearchParams),
40
+ ...opts.params
41
+ };
42
+ const searchParams = new URLSearchParams(params);
43
+ if (typeof opts.body === "object" && opts.body !== null) {
44
+ let body2 = opts.body || {};
45
+ if (!params.path && body2?.path) {
46
+ searchParams.set("path", body2.path);
47
+ if (body2?.key) {
48
+ searchParams.set("key", body2.key);
49
+ }
50
+ }
51
+ }
52
+ url.search = searchParams.toString();
53
+ }
54
+ let body = undefined;
55
+ if (isGet) {
56
+ body = undefined;
57
+ } else if (isPostFile) {
58
+ body = opts.body;
59
+ } else {
60
+ if (opts.body && typeof opts.body === "object" && !(opts.body instanceof FormData)) {
61
+ headers = {
62
+ "Content-Type": "application/json",
63
+ ...headers
64
+ };
65
+ body = JSON.stringify(opts.body);
66
+ }
67
+ }
68
+ return fetch(url, {
69
+ method: method.toUpperCase(),
70
+ signal,
71
+ body,
72
+ ...overloadOpts,
73
+ headers
74
+ }).then(async (response) => {
75
+ const contentType = response.headers.get("Content-Type");
76
+ if (responseType === "blob") {
77
+ return await response.blob();
78
+ }
79
+ const isText = responseType === "text";
80
+ const isJson = contentType && contentType.includes("application/json");
81
+ if (isJson && !isText) {
82
+ return await response.json();
83
+ } else if (isTextForContentType(contentType)) {
84
+ return {
85
+ code: response.status,
86
+ status: response.status,
87
+ data: await response.text()
88
+ };
89
+ } else {
90
+ return response;
91
+ }
92
+ }).catch((err) => {
93
+ if (err.name === "AbortError") {
94
+ return {
95
+ code: 408,
96
+ message: "请求超时"
97
+ };
98
+ }
99
+ return {
100
+ code: 500,
101
+ message: err.message || "网络错误"
102
+ };
103
+ }).finally(() => {
104
+ clearTimeout(timer);
105
+ });
106
+ };
107
+ var wrapperError = ({ code, message }) => {
108
+ const result = {
109
+ code: code || 500,
110
+ success: false,
111
+ message: message || "api request error",
112
+ showError: (fn) => {},
113
+ noMsg: true
114
+ };
115
+ return result;
116
+ };
117
+
118
+ class Query {
119
+ adapter;
120
+ url;
121
+ beforeRequest;
122
+ afterResponse;
123
+ headers;
124
+ timeout;
125
+ stop;
126
+ qws;
127
+ isClient = false;
128
+ constructor(opts) {
129
+ this.adapter = opts?.adapter || adapter;
130
+ const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
131
+ this.url = opts?.url || defaultURL;
132
+ this.headers = opts?.headers || {
133
+ "Content-Type": "application/json"
134
+ };
135
+ this.timeout = opts?.timeout || 60000 * 3;
136
+ if (opts?.beforeRequest) {
137
+ this.beforeRequest = opts.beforeRequest;
138
+ } else {
139
+ this.beforeRequest = async (opts2) => {
140
+ const token = globalThis?.localStorage?.getItem("token");
141
+ if (token) {
142
+ opts2.headers = {
143
+ ...opts2.headers,
144
+ Authorization: `Bearer ${token}`
145
+ };
146
+ }
147
+ return opts2;
148
+ };
149
+ }
150
+ }
151
+ setQueryWs(qws) {
152
+ this.qws = qws;
153
+ }
154
+ setStop(stop) {
155
+ this.stop = stop;
156
+ }
157
+ async get(params, options) {
158
+ return this.post(params, options);
159
+ }
160
+ async post(body, options) {
161
+ const url = options?.url || this.url;
162
+ const { headers, adapter: adapter2, beforeRequest, afterResponse, timeout, ...rest } = options || {};
163
+ const _headers = { ...this.headers, ...headers };
164
+ const _adapter = adapter2 || this.adapter;
165
+ const _beforeRequest = beforeRequest || this.beforeRequest;
166
+ const _afterResponse = afterResponse || this.afterResponse;
167
+ const _timeout = timeout || this.timeout;
168
+ const req = {
169
+ url,
170
+ headers: _headers,
171
+ body,
172
+ timeout: _timeout,
173
+ ...rest
174
+ };
175
+ try {
176
+ if (_beforeRequest) {
177
+ const res = await _beforeRequest(req);
178
+ if (res === false) {
179
+ return wrapperError({
180
+ code: 500,
181
+ message: "request is cancel",
182
+ req
183
+ });
184
+ }
185
+ }
186
+ } catch (e) {
187
+ console.error("request beforeFn error", e, req);
188
+ return wrapperError({
189
+ code: 500,
190
+ message: "api request beforeFn error"
191
+ });
192
+ }
193
+ if (this.stop && !options?.noStop) {
194
+ const that = this;
195
+ await new Promise((resolve) => {
196
+ let timer = 0;
197
+ const detect = setInterval(() => {
198
+ if (!that.stop) {
199
+ clearInterval(detect);
200
+ resolve(true);
201
+ }
202
+ timer++;
203
+ if (timer > 30) {
204
+ console.error("request stop: timeout", req.url, timer);
205
+ }
206
+ }, 1000);
207
+ });
208
+ }
209
+ return _adapter(req).then(async (res) => {
210
+ try {
211
+ if (_afterResponse) {
212
+ return await _afterResponse(res, {
213
+ req,
214
+ res,
215
+ fetch: adapter2
216
+ });
217
+ }
218
+ return res;
219
+ } catch (e) {
220
+ console.error("request afterFn error", e, req);
221
+ return wrapperError({
222
+ code: 500,
223
+ message: "api request afterFn error"
224
+ });
225
+ }
226
+ });
227
+ }
228
+ before(fn) {
229
+ this.beforeRequest = fn;
230
+ }
231
+ after(fn) {
232
+ this.afterResponse = fn;
233
+ }
234
+ async fetchText(urlOrOptions, options) {
235
+ let _options = { ...options };
236
+ if (typeof urlOrOptions === "string" && !_options.url) {
237
+ _options.url = urlOrOptions;
238
+ }
239
+ if (typeof urlOrOptions === "object") {
240
+ _options = { ...urlOrOptions, ..._options };
241
+ }
242
+ const res = await adapter({
243
+ method: "GET",
244
+ ..._options,
245
+ headers: {
246
+ ...this.headers,
247
+ ..._options?.headers || {}
248
+ }
249
+ });
250
+ if (res && !res.code) {
251
+ return {
252
+ code: 200,
253
+ data: res
254
+ };
255
+ }
256
+ return res;
257
+ }
258
+ }
259
+
260
+ // query/query-secret/query-secret.ts
261
+ class QueryConfig {
262
+ query;
263
+ constructor(opts) {
264
+ this.query = opts?.query || new Query;
265
+ }
266
+ async post(data) {
267
+ return this.query.post({ path: "secret", ...data });
268
+ }
269
+ async getItem({ id, key }, opts) {
270
+ return this.post({
271
+ key: "get",
272
+ data: {
273
+ id,
274
+ key
275
+ },
276
+ ...opts
277
+ });
278
+ }
279
+ async updateItem(data, opts) {
280
+ return this.post({
281
+ key: "update",
282
+ data,
283
+ ...opts
284
+ });
285
+ }
286
+ async deleteItem(data, opts) {
287
+ return this.post({
288
+ key: "delete",
289
+ data
290
+ });
291
+ }
292
+ async listItems(opts) {
293
+ return this.post({
294
+ key: "list",
295
+ ...opts
296
+ });
297
+ }
298
+ }
299
+ export {
300
+ QueryConfig
301
+ };
@@ -0,0 +1,12 @@
1
+ import { Query, BaseQuery, DataOpts } from '@kevisual/query/query';
2
+
3
+ declare const shopDefine: any;
4
+
5
+ declare class QueryShop<T extends Query = Query> extends BaseQuery<T> {
6
+ constructor(opts?: {
7
+ query: T;
8
+ });
9
+ getInstall(data: any, opts?: DataOpts): any;
10
+ }
11
+
12
+ export { QueryShop, shopDefine };