封装创建 table 的模块
This commit is contained in:
73
src/main.ts
Normal file
73
src/main.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { Query } from './api.ts';
|
||||
import { Meta } from './meta/index.ts';
|
||||
import { Record } from './record.ts';
|
||||
export type NocoApiOptions = {
|
||||
table?: string;
|
||||
token?: string;
|
||||
baseURL?: string;
|
||||
};
|
||||
|
||||
export class NocoApi {
|
||||
query: Query;
|
||||
record: Record;
|
||||
meta: Meta;
|
||||
|
||||
constructor(options?: NocoApiOptions) {
|
||||
const table = options?.table;
|
||||
const token = options?.token;
|
||||
const baseURL = options?.baseURL;
|
||||
this.query = new Query({ baseURL, token });
|
||||
this.record = new Record(this.query, table);
|
||||
this.meta = new Meta({ query: this.query });
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param baseId
|
||||
* @param data
|
||||
*/
|
||||
async createExampleTable(baseId: string, data?: ExampleTableData) {
|
||||
const defaultColumns = [{
|
||||
title: 'Title',
|
||||
uidt: 'SingleLineText',
|
||||
description: '标题列',
|
||||
},
|
||||
{
|
||||
title: 'Summary',
|
||||
uidt: 'LongText',
|
||||
description: '摘要列',
|
||||
},
|
||||
{
|
||||
title: 'Tags',
|
||||
uidt: 'MultiSelect',
|
||||
description: '标签列',
|
||||
},
|
||||
{
|
||||
title: 'Description',
|
||||
uidt: 'LongText',
|
||||
description: '描述列',
|
||||
},
|
||||
{
|
||||
title: 'Link',
|
||||
uidt: 'URL',
|
||||
description: '链接列',
|
||||
}]
|
||||
const columns = data?.columns || [];
|
||||
for (const col of defaultColumns) {
|
||||
if (!columns.find(c => c.title === col.title)) {
|
||||
columns.push(col);
|
||||
}
|
||||
}
|
||||
const res = await this.meta.tables.createTable(baseId, {
|
||||
title: data?.title || '基本表',
|
||||
description: data?.description || '',
|
||||
columns: columns
|
||||
})
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export type ExampleTableData = {
|
||||
title?: string;
|
||||
description?: string;
|
||||
columns?: any[];
|
||||
}
|
||||
Reference in New Issue
Block a user