diff --git a/package.json b/package.json index de68236..df9217b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/noco", - "version": "0.0.7", + "version": "0.0.8", "description": "", "main": "index.js", "scripts": { diff --git a/src/api.ts b/src/api.ts index 8970fd1..97abfe5 100644 --- a/src/api.ts +++ b/src/api.ts @@ -53,7 +53,6 @@ export class Query { fetchOptions.body = JSON.stringify(options.data); } return fetch(url.href, fetchOptions).then(async (response) => { - console.log('Response Status:', response.status, response); if (!response.ok) { return { code: response.status, message: response.statusText }; } @@ -63,8 +62,8 @@ export class Query { if (isArray) { return { code: 200, - list: result, - } as ResponseList; + data: { list: result }, + }; } else if (result && typeof result === 'object' && !('code' in result)) { return { code: 200, @@ -81,12 +80,14 @@ export class Query { export type ResponseList = { code: number; - list: T[]; - pageInfo?: { - totalRows?: number; - page?: number; - pageSize?: number; - isFirstPage?: boolean; - isLastPage?: boolean; - }; + data: { + list: T[]; + pageInfo?: { + totalRows?: number; + page?: number; + pageSize?: number; + isFirstPage?: boolean; + isLastPage?: boolean; + }; + } }; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 1201fc9..435d5d2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,9 +19,9 @@ export class NocoApi { 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); + this.record = new Record({ query: this.query, table, meta: this.meta }); } /** * diff --git a/src/record.ts b/src/record.ts index 0c5a4e4..105165f 100644 --- a/src/record.ts +++ b/src/record.ts @@ -1,5 +1,6 @@ import { Query } from './api.ts'; +import { Meta } from './meta/index.ts'; type QueryParams = { /** * fields=field1,field2 @@ -20,14 +21,16 @@ type QueryParams = { }; type ResultList = { code: number; - list: T[]; - pageInfo?: { - totalRows?: number; - page?: number; - pageSize?: number; - isFirstPage?: boolean; - isLastPage?: boolean; - }; + data: { + list: T[]; + pageInfo?: { + totalRows?: number; + page?: number; + pageSize?: number; + isFirstPage?: boolean; + isLastPage?: boolean; + }; + } }; type Id = string | number; /** @@ -36,9 +39,11 @@ type Id = string | number; export class Record { query: Query; table: string; - constructor(query: Query, table: string) { + meta: Meta + constructor({ query, table, meta }: { query: Query, table: string, meta?: Meta }) { this.query = query; this.table = table; + this.meta = meta; } list(params: QueryParams = {}): Promise> { return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, { @@ -57,7 +62,7 @@ export class Record { method: 'GET', }); } - update(data: { Id?: Id; [key: string]: any }) { + update(data: { Id?: Id;[key: string]: any }) { return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, { method: 'PATCH', data, @@ -90,6 +95,10 @@ export class Record { method: 'DELETE', }); } + + getTableMeta() { + return this.meta.tables.getTableMeta(this.table); + } } diff --git a/test/common.ts b/test/common.ts index c3ac5db..115451f 100644 --- a/test/common.ts +++ b/test/common.ts @@ -1,12 +1,13 @@ import { NocoApi } from './../src/main.ts'; import { useConfig } from '@kevisual/use-config' -import { CreateColumnData } from '../src/meta/index.ts'; +import { CreateBaseData } from '../src/meta/index.ts'; export const config = useConfig() import util from 'node:util'; +import { writeFile, writeFileSync } from 'node:fs'; // # 签到表 // const table = 'mcby44q8zrayvn9' // 本地 -const table = 'mecdgojq151iwk9' +const table = 'mecdgojq151iwk9' // gitstarred_repos export const nocoApi = new NocoApi({ baseURL: config.NOCODB_URL || 'http://localhost:8080', token: config.NOCODB_API_KEY || '', @@ -14,5 +15,11 @@ export const nocoApi = new NocoApi({ }); -// const list = await nocoApi.record.list() -// console.log(util.inspect(list, { depth: null, colors: true })) +// const res = await nocoApi.record.list() +// console.log(util.inspect(res.data, { depth: null, colors: true })) + +const res =await nocoApi.record.getTableMeta() +// console.log(util.inspect(res, { depth: null, colors: true })) +const keys = Object.keys(res.data || {}) +console.log('表字段列表:', keys); +writeFileSync('table-metadata.json', JSON.stringify(res, null, 2)); \ No newline at end of file diff --git a/test/list-table.ts b/test/list-table.ts new file mode 100644 index 0000000..3bba2bd --- /dev/null +++ b/test/list-table.ts @@ -0,0 +1,10 @@ + +import { nocoApi } from './common.ts'; +import util from 'node:util'; +import { writeFileSync } from 'node:fs'; + +const res =await nocoApi.record.getTableMeta() +// console.log(util.inspect(res, { depth: null, colors: true })) +const keys = Object.keys(res.data || {}) +console.log('表字段列表:', keys); +writeFileSync('table-metadata.json', JSON.stringify(res, null, 2)); \ No newline at end of file