155 lines
3.8 KiB
TypeScript
155 lines
3.8 KiB
TypeScript
import { Query } from '@kevisual/query';
|
||
import type { Result, DataOpts } from '@kevisual/query/query';
|
||
|
||
export type SimpleObject = Record<string, any>;
|
||
export const markType = ['simple', 'md', 'mdx', 'wallnote', 'excalidraw', 'chat'] as const;
|
||
export type MarkType = (typeof markType)[number];
|
||
export type MarkData = {
|
||
nodes?: any[];
|
||
edges?: any[];
|
||
elements?: any[];
|
||
permission?: any;
|
||
|
||
[key: string]: any;
|
||
};
|
||
export type Mark = {
|
||
id: string;
|
||
title: string;
|
||
description: string;
|
||
markType: MarkType;
|
||
link: string;
|
||
data?: MarkData;
|
||
uid: string;
|
||
puid: string;
|
||
summary: string;
|
||
thumbnail?: string;
|
||
tags: string[];
|
||
createdAt: string;
|
||
updatedAt: string;
|
||
version: number;
|
||
};
|
||
export type ShowMarkPick = Pick<Mark, 'id' | 'title' | 'description' | 'summary' | 'link' | 'tags' | 'thumbnail' | 'updatedAt'>;
|
||
|
||
export type SearchOpts = {
|
||
page?: number;
|
||
pageSize?: number;
|
||
search?: string;
|
||
sort?: string; // DESC, ASC
|
||
markType?: MarkType; // 类型
|
||
[key: string]: any;
|
||
};
|
||
|
||
export type QueryMarkOpts<T extends SimpleObject = SimpleObject> = {
|
||
query?: Query;
|
||
isBrowser?: boolean;
|
||
onLoad?: () => void;
|
||
} & T;
|
||
|
||
export type ResultMarkList = {
|
||
list: Mark[];
|
||
pagination: {
|
||
pageSize: number;
|
||
current: number;
|
||
total: number;
|
||
};
|
||
};
|
||
export type QueryMarkData = {
|
||
id?: string;
|
||
title?: string;
|
||
description?: string;
|
||
[key: string]: any;
|
||
};
|
||
export type QueryMarkResult = {
|
||
accessToken: string;
|
||
refreshToken: string;
|
||
};
|
||
|
||
export class QueryMarkBase<T extends SimpleObject = SimpleObject> {
|
||
query: Query;
|
||
isBrowser: boolean;
|
||
load?: boolean;
|
||
storage?: Storage;
|
||
onLoad?: () => void;
|
||
|
||
constructor(opts?: QueryMarkOpts<T>) {
|
||
this.query = opts?.query || new Query();
|
||
this.isBrowser = opts?.isBrowser ?? true;
|
||
this.init();
|
||
this.onLoad = opts?.onLoad;
|
||
}
|
||
setQuery(query: Query) {
|
||
this.query = query;
|
||
}
|
||
private async init() {
|
||
this.load = true;
|
||
this.onLoad?.();
|
||
}
|
||
|
||
async post<T = Result<any>>(data: any, opts?: DataOpts): Promise<T> {
|
||
try {
|
||
return this.query.post({ path: 'mark', ...data }, opts) as Promise<T>;
|
||
} catch (error) {
|
||
console.log('error', error);
|
||
return {
|
||
code: 400,
|
||
} as any;
|
||
}
|
||
}
|
||
|
||
async getMarkList(search: SearchOpts, opts?: DataOpts) {
|
||
return this.post<Result<ResultMarkList>>({ key: 'list', ...search }, opts);
|
||
}
|
||
|
||
async getMark(id: string, opts?: DataOpts) {
|
||
return this.post<Result<Mark>>({ key: 'get', id }, opts);
|
||
}
|
||
async getVersion(id: string, opts?: DataOpts) {
|
||
return this.post<Result<{ version: number; id: string }>>({ key: 'getVersion', id }, opts);
|
||
}
|
||
/**
|
||
* 检查版本
|
||
* 当需要更新时,返回true
|
||
* @param id
|
||
* @param version
|
||
* @param opts
|
||
* @returns
|
||
*/
|
||
async checkVersion(id: string, version?: number, opts?: DataOpts) {
|
||
if (!version) {
|
||
return true;
|
||
}
|
||
const res = await this.getVersion(id, opts);
|
||
if (res.code === 200) {
|
||
if (res.data!.version > version) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
async updateMark(data: any, opts?: DataOpts) {
|
||
return this.post<Result<Mark>>({ key: 'update', data }, opts);
|
||
}
|
||
|
||
async deleteMark(id: string, opts?: DataOpts) {
|
||
return this.post<Result<Mark>>({ key: 'delete', id }, opts);
|
||
}
|
||
}
|
||
export class QueryMark extends QueryMarkBase<SimpleObject> {
|
||
markType: string;
|
||
constructor(opts?: QueryMarkOpts & { markType?: MarkType }) {
|
||
super(opts);
|
||
this.markType = opts?.markType || 'simple';
|
||
}
|
||
async getMarkList(search?: SearchOpts, opts?: DataOpts) {
|
||
return this.post<Result<ResultMarkList>>({ key: 'list', ...search, markType: this.markType }, opts);
|
||
}
|
||
async updateMark(data: any, opts?: DataOpts) {
|
||
if (!data.id) {
|
||
data.markType = this.markType || 'simple';
|
||
}
|
||
return super.updateMark(data, opts);
|
||
}
|
||
}
|