update
This commit is contained in:
		@@ -4,6 +4,7 @@
 | 
				
			|||||||
  "description": "",
 | 
					  "description": "",
 | 
				
			||||||
  "main": "index.js",
 | 
					  "main": "index.js",
 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
 | 
					    "watch": "bun bun.config.mjs watch",
 | 
				
			||||||
    "build": " bun bun.config.mjs"
 | 
					    "build": " bun bun.config.mjs"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "publishConfig": {
 | 
					  "publishConfig": {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										11
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								readme.md
									
									
									
									
									
								
							@@ -1 +1,12 @@
 | 
				
			|||||||
# nocodb api sdk
 | 
					# nocodb api sdk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```ts
 | 
				
			||||||
 | 
					const nocoAPi = new NocoApi({
 | 
				
			||||||
 | 
					  baseURL: config.NOCODB_URL,
 | 
				
			||||||
 | 
					  token: config.NOCODB_API_KEY,
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const listResult = await nocoAPi.record.list();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					await nocoAPi.record.update({ id: 1, Title: '123' });
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										31
									
								
								src/api.ts
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/api.ts
									
									
									
									
									
								
							@@ -11,17 +11,19 @@ type FetchOptions = {
 | 
				
			|||||||
type MakeRequestOptions = {
 | 
					type MakeRequestOptions = {
 | 
				
			||||||
  params?: Record<string, any>;
 | 
					  params?: Record<string, any>;
 | 
				
			||||||
  data?: Record<string, any>;
 | 
					  data?: Record<string, any>;
 | 
				
			||||||
  method?: 'GET' | 'POST';
 | 
					  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
 | 
				
			||||||
 | 
					  json?: boolean;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
export class Query {
 | 
					export class Query {
 | 
				
			||||||
  baseUrl: string;
 | 
					  baseURL: string;
 | 
				
			||||||
  token: string;
 | 
					  token: string;
 | 
				
			||||||
  constructor({ baseUrl, token }: { baseUrl: string; token: string }) {
 | 
					  constructor({ baseURL, token }: { baseURL: string; token: string }) {
 | 
				
			||||||
    this.baseUrl = baseUrl;
 | 
					    this.baseURL = baseURL;
 | 
				
			||||||
    this.token = token;
 | 
					    this.token = token;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  makeRequest(endpoint: string, options: MakeRequestOptions) {
 | 
					  makeRequest(endpoint: string, options: MakeRequestOptions) {
 | 
				
			||||||
    const url = new URL(endpoint, this.baseUrl);
 | 
					    const url = new URL(endpoint, this.baseURL);
 | 
				
			||||||
 | 
					    const isJson = options.json ?? true;
 | 
				
			||||||
    if (options.params) {
 | 
					    if (options.params) {
 | 
				
			||||||
      Object.entries(options.params).forEach(([key, value]) => {
 | 
					      Object.entries(options.params).forEach(([key, value]) => {
 | 
				
			||||||
        url.searchParams.append(key, String(value));
 | 
					        url.searchParams.append(key, String(value));
 | 
				
			||||||
@@ -31,16 +33,25 @@ export class Query {
 | 
				
			|||||||
    const headers: HeadersInit = {
 | 
					    const headers: HeadersInit = {
 | 
				
			||||||
      'xc-token': `${this.token}`,
 | 
					      'xc-token': `${this.token}`,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    if (method === 'POST') {
 | 
					    headers['Content-Type'] = 'application/json';
 | 
				
			||||||
      headers['Content-Type'] = 'application/json';
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    const fetchOptions: FetchOptions = {
 | 
					    const fetchOptions: FetchOptions = {
 | 
				
			||||||
      method,
 | 
					      method,
 | 
				
			||||||
      headers,
 | 
					      headers,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    if (method === 'POST' && options.data) {
 | 
					    if (options.data) {
 | 
				
			||||||
      fetchOptions.body = JSON.stringify(options.data);
 | 
					      fetchOptions.body = JSON.stringify(options.data);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return fetch(url, fetchOptions);
 | 
					    console.log(url.href, fetchOptions);
 | 
				
			||||||
 | 
					    return fetch(url.href, fetchOptions).then(async (response) => {
 | 
				
			||||||
 | 
					      if (!response.ok) {
 | 
				
			||||||
 | 
					        return { code: response.status, message: response.statusText };
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (isJson) {
 | 
				
			||||||
 | 
					        const result = await response.json();
 | 
				
			||||||
 | 
					        result.code = 200;
 | 
				
			||||||
 | 
					        return result;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return response;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										114
									
								
								src/base.ts
									
									
									
									
									
								
							
							
						
						
									
										114
									
								
								src/base.ts
									
									
									
									
									
								
							@@ -3,15 +3,117 @@ import { Query } from './api.ts';
 | 
				
			|||||||
type NocoApiOptions = {
 | 
					type NocoApiOptions = {
 | 
				
			||||||
  table?: string;
 | 
					  table?: string;
 | 
				
			||||||
  token?: string;
 | 
					  token?: string;
 | 
				
			||||||
  baseUrl?: string;
 | 
					  baseURL?: string;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
export class NocoApi {
 | 
					export class NocoApi {
 | 
				
			||||||
  table?: string;
 | 
					  query: Query;
 | 
				
			||||||
  query?: Query;
 | 
					  record: Record;
 | 
				
			||||||
  constructor(options?: NocoApiOptions) {
 | 
					  constructor(options?: NocoApiOptions) {
 | 
				
			||||||
    this.table = options.table;
 | 
					    const table = options.table;
 | 
				
			||||||
    const token = options.token;
 | 
					    const token = options.token;
 | 
				
			||||||
    const baseUrl = options.baseUrl;
 | 
					    const baseURL = options.baseURL;
 | 
				
			||||||
    this.query = new Query({ baseUrl, token });
 | 
					    this.query = new Query({ baseURL, token });
 | 
				
			||||||
 | 
					    this.record = new Record(this.query, table);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					type QueryParams = {
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * fields=field1,field2
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  fields?: string | string[];
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * sort=field1,-field2
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  sort?: string | string[];
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * where=(field1,eq,value1)~and(field2,eq,value2)
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  where?: string;
 | 
				
			||||||
 | 
					  offset?: number;
 | 
				
			||||||
 | 
					  limit?: number;
 | 
				
			||||||
 | 
					  viewId?: string;
 | 
				
			||||||
 | 
					  [key: string]: any;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					type ResultList<T = any> = {
 | 
				
			||||||
 | 
					  code: number;
 | 
				
			||||||
 | 
					  list: T[];
 | 
				
			||||||
 | 
					  pageInfo?: {
 | 
				
			||||||
 | 
					    totalRows?: number;
 | 
				
			||||||
 | 
					    page?: number;
 | 
				
			||||||
 | 
					    pageSize?: number;
 | 
				
			||||||
 | 
					    isFirstPage?: boolean;
 | 
				
			||||||
 | 
					    isLastPage?: boolean;
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					type Id = string | number;
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @url https://nocodb.com/apis/v2/data#tag/Table-Records
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class Record {
 | 
				
			||||||
 | 
					  query: Query;
 | 
				
			||||||
 | 
					  table: string;
 | 
				
			||||||
 | 
					  constructor(query: Query, table: string) {
 | 
				
			||||||
 | 
					    this.query = query;
 | 
				
			||||||
 | 
					    this.table = table;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  list<T = any>(params: QueryParams = {}): Promise<ResultList<T>> {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, {
 | 
				
			||||||
 | 
					      method: 'GET',
 | 
				
			||||||
 | 
					      params,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  create(data: any) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, {
 | 
				
			||||||
 | 
					      method: 'POST',
 | 
				
			||||||
 | 
					      data,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  read(id: Id) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/records/${id}`, {
 | 
				
			||||||
 | 
					      method: 'GET',
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  update(data: { id: Id; [key: string]: any }) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, {
 | 
				
			||||||
 | 
					      method: 'PATCH',
 | 
				
			||||||
 | 
					      data,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  delete(data: { id: Id }) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, {
 | 
				
			||||||
 | 
					      method: 'DELETE',
 | 
				
			||||||
 | 
					      data,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  count() {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/records/count`, {
 | 
				
			||||||
 | 
					      method: 'GET',
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  listLinks(linkFieldId: Id, id: Id) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/links/${linkFieldId}/records/${id}`, {
 | 
				
			||||||
 | 
					      method: 'GET',
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  updateLinks(linkFieldId: Id, id: Id, data: any) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/links/${linkFieldId}/records/${id}`, {
 | 
				
			||||||
 | 
					      method: 'POST',
 | 
				
			||||||
 | 
					      data,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  deleteLinks(linkFieldId: Id, id: Id) {
 | 
				
			||||||
 | 
					    return this.query.makeRequest(`/api/v2/tables/${this.table}/links/${linkFieldId}/records/${id}`, {
 | 
				
			||||||
 | 
					      method: 'DELETE',
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @url https://nocodb.com/apis/v2/meta
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class Meta {
 | 
				
			||||||
 | 
					  bases: string;
 | 
				
			||||||
 | 
					  constructor() {
 | 
				
			||||||
 | 
					    this.bases = '';
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user