update
This commit is contained in:
		@@ -4,6 +4,7 @@
 | 
			
		||||
  "description": "",
 | 
			
		||||
  "main": "index.js",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "watch": "bun bun.config.mjs watch",
 | 
			
		||||
    "build": " bun bun.config.mjs"
 | 
			
		||||
  },
 | 
			
		||||
  "publishConfig": {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								readme.md
									
									
									
									
									
								
							@@ -1 +1,12 @@
 | 
			
		||||
# 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' });
 | 
			
		||||
```
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										29
									
								
								src/api.ts
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								src/api.ts
									
									
									
									
									
								
							@@ -11,17 +11,19 @@ type FetchOptions = {
 | 
			
		||||
type MakeRequestOptions = {
 | 
			
		||||
  params?: Record<string, any>;
 | 
			
		||||
  data?: Record<string, any>;
 | 
			
		||||
  method?: 'GET' | 'POST';
 | 
			
		||||
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
 | 
			
		||||
  json?: boolean;
 | 
			
		||||
};
 | 
			
		||||
export class Query {
 | 
			
		||||
  baseUrl: string;
 | 
			
		||||
  baseURL: string;
 | 
			
		||||
  token: string;
 | 
			
		||||
  constructor({ baseUrl, token }: { baseUrl: string; token: string }) {
 | 
			
		||||
    this.baseUrl = baseUrl;
 | 
			
		||||
  constructor({ baseURL, token }: { baseURL: string; token: string }) {
 | 
			
		||||
    this.baseURL = baseURL;
 | 
			
		||||
    this.token = token;
 | 
			
		||||
  }
 | 
			
		||||
  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) {
 | 
			
		||||
      Object.entries(options.params).forEach(([key, value]) => {
 | 
			
		||||
        url.searchParams.append(key, String(value));
 | 
			
		||||
@@ -31,16 +33,25 @@ export class Query {
 | 
			
		||||
    const headers: HeadersInit = {
 | 
			
		||||
      'xc-token': `${this.token}`,
 | 
			
		||||
    };
 | 
			
		||||
    if (method === 'POST') {
 | 
			
		||||
    headers['Content-Type'] = 'application/json';
 | 
			
		||||
    }
 | 
			
		||||
    const fetchOptions: FetchOptions = {
 | 
			
		||||
      method,
 | 
			
		||||
      headers,
 | 
			
		||||
    };
 | 
			
		||||
    if (method === 'POST' && options.data) {
 | 
			
		||||
    if (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 = {
 | 
			
		||||
  table?: string;
 | 
			
		||||
  token?: string;
 | 
			
		||||
  baseUrl?: string;
 | 
			
		||||
  baseURL?: string;
 | 
			
		||||
};
 | 
			
		||||
export class NocoApi {
 | 
			
		||||
  table?: string;
 | 
			
		||||
  query?: Query;
 | 
			
		||||
  query: Query;
 | 
			
		||||
  record: Record;
 | 
			
		||||
  constructor(options?: NocoApiOptions) {
 | 
			
		||||
    this.table = options.table;
 | 
			
		||||
    const table = options.table;
 | 
			
		||||
    const token = options.token;
 | 
			
		||||
    const baseUrl = options.baseUrl;
 | 
			
		||||
    this.query = new Query({ baseUrl, token });
 | 
			
		||||
    const baseURL = options.baseURL;
 | 
			
		||||
    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