76 lines
1.7 KiB
TypeScript
76 lines
1.7 KiB
TypeScript
import { Query } from './api.ts';
|
|
import { Meta } from './meta/index.ts';
|
|
import { Record } from './record.ts';
|
|
import { Upload } from './data/upload.ts';
|
|
export type NocoApiOptions = {
|
|
table?: string;
|
|
token?: string;
|
|
baseURL?: string;
|
|
};
|
|
|
|
export class NocoApi {
|
|
query: Query;
|
|
record: Record;
|
|
meta: Meta;
|
|
upload: Upload;
|
|
|
|
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 });
|
|
this.upload = new Upload(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[];
|
|
} |