@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,100 @@
1
+ import { Query } from '@kevisual/query';
2
+ import { Result, DataOpts } from '@kevisual/query/query';
3
+
4
+ type SimpleObject = Record<string, any>;
5
+ declare const markType: readonly ["simple", "md", "mdx", "wallnote", "excalidraw", "chat"];
6
+ type MarkType = (typeof markType)[number];
7
+ type MarkData = {
8
+ nodes?: any[];
9
+ edges?: any[];
10
+ elements?: any[];
11
+ permission?: any;
12
+ [key: string]: any;
13
+ };
14
+ type Mark = {
15
+ id: string;
16
+ title: string;
17
+ description: string;
18
+ markType: MarkType;
19
+ link: string;
20
+ data?: MarkData;
21
+ uid: string;
22
+ puid: string;
23
+ summary: string;
24
+ thumbnail?: string;
25
+ tags: string[];
26
+ createdAt: string;
27
+ updatedAt: string;
28
+ version: number;
29
+ };
30
+ type ShowMarkPick = Pick<Mark, 'id' | 'title' | 'description' | 'summary' | 'link' | 'tags' | 'thumbnail' | 'updatedAt'>;
31
+ type SearchOpts = {
32
+ page?: number;
33
+ pageSize?: number;
34
+ search?: string;
35
+ sort?: string;
36
+ markType?: MarkType;
37
+ [key: string]: any;
38
+ };
39
+ type QueryMarkOpts<T extends SimpleObject = SimpleObject> = {
40
+ query?: Query;
41
+ isBrowser?: boolean;
42
+ onLoad?: () => void;
43
+ } & T;
44
+ type ResultMarkList = {
45
+ list: Mark[];
46
+ pagination: {
47
+ pageSize: number;
48
+ current: number;
49
+ total: number;
50
+ };
51
+ };
52
+ type QueryMarkData = {
53
+ id?: string;
54
+ title?: string;
55
+ description?: string;
56
+ [key: string]: any;
57
+ };
58
+ type QueryMarkResult = {
59
+ accessToken: string;
60
+ refreshToken: string;
61
+ };
62
+ declare class QueryMarkBase<T extends SimpleObject = SimpleObject> {
63
+ query: Query;
64
+ isBrowser: boolean;
65
+ load?: boolean;
66
+ storage?: Storage;
67
+ onLoad?: () => void;
68
+ constructor(opts?: QueryMarkOpts<T>);
69
+ setQuery(query: Query): void;
70
+ private init;
71
+ post<T = Result<any>>(data: any, opts?: DataOpts): Promise<T>;
72
+ getMarkList(search: SearchOpts, opts?: DataOpts): Promise<Result<ResultMarkList>>;
73
+ getMark(id: string, opts?: DataOpts): Promise<Result<Mark>>;
74
+ getVersion(id: string, opts?: DataOpts): Promise<Result<{
75
+ version: number;
76
+ id: string;
77
+ }>>;
78
+ /**
79
+ * 检查版本
80
+ * 当需要更新时,返回true
81
+ * @param id
82
+ * @param version
83
+ * @param opts
84
+ * @returns
85
+ */
86
+ checkVersion(id: string, version?: number, opts?: DataOpts): Promise<boolean>;
87
+ updateMark(data: any, opts?: DataOpts): Promise<Result<Mark>>;
88
+ deleteMark(id: string, opts?: DataOpts): Promise<Result<Mark>>;
89
+ }
90
+ declare class QueryMark extends QueryMarkBase<SimpleObject> {
91
+ markType: string;
92
+ constructor(opts?: QueryMarkOpts & {
93
+ markType?: MarkType;
94
+ });
95
+ getMarkList(search?: SearchOpts, opts?: DataOpts): Promise<Result<ResultMarkList>>;
96
+ updateMark(data: any, opts?: DataOpts): Promise<Result<Mark>>;
97
+ }
98
+
99
+ export { QueryMark, QueryMarkBase, markType };
100
+ export type { Mark, MarkData, MarkType, QueryMarkData, QueryMarkOpts, QueryMarkResult, ResultMarkList, SearchOpts, ShowMarkPick, SimpleObject };
@@ -0,0 +1,342 @@
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-mark/index.ts
261
+ var markType = ["simple", "md", "mdx", "wallnote", "excalidraw", "chat"];
262
+
263
+ class QueryMarkBase {
264
+ query;
265
+ isBrowser;
266
+ load;
267
+ storage;
268
+ onLoad;
269
+ constructor(opts) {
270
+ this.query = opts?.query || new Query;
271
+ this.isBrowser = opts?.isBrowser ?? true;
272
+ this.init();
273
+ this.onLoad = opts?.onLoad;
274
+ }
275
+ setQuery(query) {
276
+ this.query = query;
277
+ }
278
+ async init() {
279
+ this.load = true;
280
+ this.onLoad?.();
281
+ }
282
+ async post(data, opts) {
283
+ try {
284
+ return this.query.post({ path: "mark", ...data }, opts);
285
+ } catch (error) {
286
+ console.log("error", error);
287
+ return {
288
+ code: 400
289
+ };
290
+ }
291
+ }
292
+ async getMarkList(search, opts) {
293
+ return this.post({ key: "list", ...search }, opts);
294
+ }
295
+ async getMark(id, opts) {
296
+ return this.post({ key: "get", id }, opts);
297
+ }
298
+ async getVersion(id, opts) {
299
+ return this.post({ key: "getVersion", id }, opts);
300
+ }
301
+ async checkVersion(id, version, opts) {
302
+ if (!version) {
303
+ return true;
304
+ }
305
+ const res = await this.getVersion(id, opts);
306
+ if (res.code === 200) {
307
+ if (res.data.version > version) {
308
+ return true;
309
+ }
310
+ return false;
311
+ }
312
+ return true;
313
+ }
314
+ async updateMark(data, opts) {
315
+ return this.post({ key: "update", data }, opts);
316
+ }
317
+ async deleteMark(id, opts) {
318
+ return this.post({ key: "delete", id }, opts);
319
+ }
320
+ }
321
+
322
+ class QueryMark extends QueryMarkBase {
323
+ markType;
324
+ constructor(opts) {
325
+ super(opts);
326
+ this.markType = opts?.markType || "simple";
327
+ }
328
+ async getMarkList(search, opts) {
329
+ return this.post({ key: "list", ...search, markType: this.markType }, opts);
330
+ }
331
+ async updateMark(data, opts) {
332
+ if (!data.id) {
333
+ data.markType = this.markType || "simple";
334
+ }
335
+ return super.updateMark(data, opts);
336
+ }
337
+ }
338
+ export {
339
+ markType,
340
+ QueryMarkBase,
341
+ QueryMark
342
+ };
@@ -0,0 +1,215 @@
1
+ import { QueryClient, Result } from '@kevisual/query';
2
+ import { QueryRouterServer, Route } from '@kevisual/router/browser';
3
+ import { EventEmitter } from 'eventemitter3';
4
+ import { QueryRouterServer as QueryRouterServer$1, App } from '@kevisual/router';
5
+
6
+ declare const RouteTypeList: readonly ["api", "context", "worker", "page"];
7
+ type RouterViewItemInfo = RouterViewApi | RouterViewContext | RouterViewWorker | RouteViewPage;
8
+ type RouterViewItem<T = {}> = RouterViewItemInfo & T;
9
+ type RouteViewBase = {
10
+ /**
11
+ * _id 用于纯本地存储标识
12
+ */
13
+ _id?: string;
14
+ id?: string;
15
+ title?: string;
16
+ description?: string;
17
+ enabled?: boolean;
18
+ /**
19
+ * 响应数据
20
+ */
21
+ response?: any;
22
+ /**
23
+ * 默认动作配置
24
+ */
25
+ action?: {
26
+ path?: string;
27
+ key?: string;
28
+ id?: string;
29
+ payload?: any;
30
+ [key: string]: any;
31
+ };
32
+ };
33
+ type RouterViewApi = {
34
+ type: 'api';
35
+ api: {
36
+ url: string;
37
+ query?: QueryClient;
38
+ };
39
+ } & RouteViewBase;
40
+ type RouterViewContext = {
41
+ type: 'context';
42
+ context: {
43
+ key: string;
44
+ router?: QueryRouterServer;
45
+ };
46
+ } & RouteViewBase;
47
+ type RouterViewWorker = {
48
+ type: 'worker';
49
+ worker: {
50
+ type: 'Worker' | 'SharedWorker' | 'serviceWorker';
51
+ url: string;
52
+ worker?: Worker | SharedWorker | ServiceWorker;
53
+ /**
54
+ * worker选项
55
+ * default: { type: 'module' }
56
+ */
57
+ workerOptions?: {
58
+ type: 'module' | 'classic';
59
+ };
60
+ };
61
+ } & RouteViewBase;
62
+ /**
63
+ * 去掉不需要保存都服务器的数据
64
+ * @param item
65
+ * @returns
66
+ */
67
+ declare const pickRouterViewData: (item: RouterViewItem) => {
68
+ type: "api";
69
+ api: {
70
+ url: string;
71
+ query?: QueryClient;
72
+ };
73
+ id?: string;
74
+ title?: string;
75
+ description?: string;
76
+ enabled?: boolean;
77
+ } | {
78
+ type: "context";
79
+ context: {
80
+ key: string;
81
+ router?: QueryRouterServer;
82
+ };
83
+ id?: string;
84
+ title?: string;
85
+ description?: string;
86
+ enabled?: boolean;
87
+ } | {
88
+ type: "worker";
89
+ worker: {
90
+ type: "Worker" | "SharedWorker" | "serviceWorker";
91
+ url: string;
92
+ worker?: Worker | SharedWorker | ServiceWorker;
93
+ /**
94
+ * worker选项
95
+ * default: { type: 'module' }
96
+ */
97
+ workerOptions?: {
98
+ type: "module" | "classic";
99
+ };
100
+ };
101
+ id?: string;
102
+ title?: string;
103
+ description?: string;
104
+ enabled?: boolean;
105
+ } | {
106
+ type: "page";
107
+ page: {
108
+ url: string;
109
+ };
110
+ id?: string;
111
+ title?: string;
112
+ description?: string;
113
+ enabled?: boolean;
114
+ };
115
+ /**
116
+ * 注入 js 的url地址,使用importScripts加载
117
+ */
118
+ type RouteViewPage = {
119
+ type: 'page';
120
+ page: {
121
+ url: string;
122
+ };
123
+ } & RouteViewBase;
124
+ type RouterViewQuery = {
125
+ id: string;
126
+ query: string;
127
+ title: string;
128
+ };
129
+ type RouterViewData = {
130
+ data: {
131
+ items: RouterViewItem[];
132
+ };
133
+ views: RouterViewQuery[];
134
+ viewId?: string;
135
+ [key: string]: any;
136
+ };
137
+ declare class QueryProxy {
138
+ router: QueryRouterServer;
139
+ token?: string;
140
+ routerViewItems: RouterViewItem[];
141
+ views: RouterViewQuery[];
142
+ emitter: EventEmitter;
143
+ constructor(opts?: {
144
+ router?: QueryRouterServer;
145
+ token?: string;
146
+ routerViewData?: RouterViewData;
147
+ });
148
+ getDefulatToken(): string;
149
+ initRouterViewQuery(): void;
150
+ initRouterView(item: RouterViewItem): RouterViewItem<{}>;
151
+ /**
152
+ * 初始化路由
153
+ * main
154
+ * @returns
155
+ */
156
+ init(): Promise<void>;
157
+ /**
158
+ * 监听初始化完成
159
+ * @returns
160
+ */
161
+ listenInitComplete(): Promise<boolean>;
162
+ initApi(item?: RouterViewApi): Promise<void>;
163
+ initContext(item?: RouterViewContext): Promise<void>;
164
+ generateId(): string;
165
+ callWorker(msg: any, viewItem: RouterViewWorker['worker']): Promise<Result>;
166
+ initWorker(item?: RouterViewWorker, initRoutes?: boolean): Promise<void>;
167
+ initPage(item?: RouteViewPage): Promise<void>;
168
+ getQueryByViewId(viewId: string): string | undefined;
169
+ /**
170
+ * 列出路由
171
+ * @param filter
172
+ * @param query WHERE metadata.tags CONTAINS 'premium'
173
+ * @returns
174
+ */
175
+ listRoutes(filterFn?: (item: Route) => boolean, opts?: {
176
+ viewId?: string;
177
+ query?: string;
178
+ }): Promise<any>;
179
+ getViewQuery(viewId: string): Promise<string>;
180
+ /**
181
+ * 运行路由
182
+ * @param msg
183
+ * @returns
184
+ */
185
+ run(msg: {
186
+ id?: string;
187
+ path?: string;
188
+ key?: string;
189
+ }): Promise<any>;
190
+ runByRouteView(routeView: RouterViewItem): Promise<RouterViewItem<{}>>;
191
+ }
192
+ type RouterItem = {
193
+ id?: string;
194
+ path?: string;
195
+ key?: string;
196
+ description?: string;
197
+ middleware?: string[];
198
+ metadata?: Record<string, any>;
199
+ };
200
+
201
+ declare const initApi: (opts: {
202
+ item?: RouterViewApi;
203
+ router: QueryRouterServer$1 | App;
204
+ token?: string;
205
+ /**
206
+ * WHERE path = 'auth' OR path = 'router' OR path = 'call'
207
+ */
208
+ exclude?: string;
209
+ }) => Promise<{
210
+ code: any;
211
+ message: string;
212
+ }>;
213
+
214
+ export { QueryProxy, RouteTypeList, initApi, pickRouterViewData };
215
+ export type { RouteViewPage, RouterItem, RouterViewApi, RouterViewContext, RouterViewData, RouterViewItem, RouterViewItemInfo, RouterViewQuery, RouterViewWorker };