teable-api-sdk 1.0.0-test.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Truraly
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6
+ associated documentation files (the "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial
12
+ portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
16
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,267 @@
1
+ # Teable API SDK
2
+
3
+ 一个用于 Teable API 的 TypeScript 客户端 SDK,提供了完整的功能来与 Teable 数据库和电子表格进行交互。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 TypeScript 支持,完整的类型定义
8
+ - ✅ 基于 Zod 的数据验证
9
+ - 🔄 Promise 支持,支持 async/await
10
+ - 🛡️ 内置错误处理和拦截器
11
+ - 📦 支持 CommonJS、ES Module 和 UMD
12
+ - 🧪 完整的测试覆盖
13
+ - 📝 详细的文档和示例
14
+
15
+ ## 安装
16
+
17
+ ```bash
18
+ npm install @teble/api-sdk
19
+ # 或者
20
+ yarn add @teble/api-sdk
21
+ # 或者
22
+ pnpm add @teble/api-sdk
23
+ ```
24
+
25
+ ## 快速开始
26
+
27
+ ```typescript
28
+ import { createTeableClient } from '@teble/api-sdk';
29
+
30
+ // 创建客户端实例
31
+ const client = createTeableClient({
32
+ apiKey: 'your-api-key',
33
+ baseUrl: 'https://your-teable-instance.com',
34
+ });
35
+
36
+ // 获取记录
37
+ const result = await client.record.get({
38
+ tableId: 'your-table-id',
39
+ take: 10
40
+ });
41
+
42
+ if (result.success) {
43
+ console.log('获取到的记录:', result.data.records);
44
+ } else {
45
+ console.error('获取失败:', result.error);
46
+ }
47
+ ```
48
+
49
+ ## API 文档
50
+
51
+ ### 创建客户端
52
+
53
+ ```typescript
54
+ import { createTeableClient } from '@teble/api-sdk';
55
+
56
+ const client = createTeableClient({
57
+ apiKey: string; // API 密钥(必需)
58
+ baseUrl: string; // Teable 服务器基础 URL(必需)
59
+ databaseId?: string; // 数据库 ID(可选)
60
+ diaryTableId?: string; // 日记表 ID(可选)
61
+ });
62
+ ```
63
+
64
+ ### 记录操作
65
+
66
+ #### 创建记录
67
+
68
+ ```typescript
69
+ const result = await client.record.create({
70
+ tableId: 'table-id',
71
+ records: [
72
+ {
73
+ fields: {
74
+ '字段名1': '值1',
75
+ '字段名2': 123,
76
+ '字段名3': ['选择值1', '选择值2']
77
+ }
78
+ }
79
+ ],
80
+ fieldKeyType?: 'name' | 'id' | 'dbFieldName', // 可选,默认 'name'
81
+ typecast?: boolean, // 可选,是否自动类型转换
82
+ order?: { // 可选,记录排序
83
+ viewId: 'view-id',
84
+ anchorId: 'anchor-record-id',
85
+ position: 'before' | 'after'
86
+ }
87
+ });
88
+ ```
89
+
90
+ #### 获取记录
91
+
92
+ ```typescript
93
+ // 获取记录列表
94
+ const result = await client.record.get({
95
+ tableId: 'table-id',
96
+ viewId?: 'view-id', // 可选
97
+ take?: 100, // 可选,最大 1000
98
+ skip?: 0, // 可选,用于分页
99
+ fieldKeyType?: 'name' | 'id' | 'dbFieldName', // 可选,默认 'name'
100
+ cellFormat?: 'json' | 'text', // 可选,默认 'json'
101
+ projection?: ['字段1', '字段2'], // 可选,指定返回字段
102
+ orderBy?: [{ // 可选,排序
103
+ field: '字段名',
104
+ order: 'asc' | 'desc'
105
+ }],
106
+ filter?: { /* 筛选条件 */ }, // 可选
107
+ search?: [{ // 可选,搜索条件
108
+ field: '字段名',
109
+ value: '搜索值'
110
+ }]
111
+ });
112
+
113
+ // 获取单个记录
114
+ const result = await client.record.getById({
115
+ tableId: 'table-id',
116
+ recordId: 'record-id',
117
+ projection?: ['字段1', '字段2'], // 可选
118
+ cellFormat?: 'json' | 'text', // 可选
119
+ fieldKeyType?: 'name' | 'id' | 'dbFieldName' // 可选
120
+ });
121
+
122
+ // 别名方法 list
123
+ const result = await client.record.list({
124
+ tableId: 'table-id',
125
+ take: 10
126
+ });
127
+ ```
128
+
129
+ #### 更新记录
130
+
131
+ ```typescript
132
+ const result = await client.record.update({
133
+ tableId: 'table-id',
134
+ recordId: 'record-id',
135
+ record: {
136
+ fields: {
137
+ '字段名1': '新值',
138
+ '字段名2': 456
139
+ }
140
+ },
141
+ fieldKeyType?: 'name' | 'id' | 'dbFieldName', // 可选,默认 'name'
142
+ typecast?: boolean, // 可选,是否自动类型转换
143
+ order?: { // 可选,记录排序
144
+ viewId: 'view-id',
145
+ anchorId: 'anchor-record-id',
146
+ position: 'before' | 'after'
147
+ }
148
+ });
149
+ ```
150
+
151
+ #### 删除记录
152
+
153
+ ```typescript
154
+ const result = await client.record.delete({
155
+ tableId: 'table-id',
156
+ recordId: 'record-id'
157
+ });
158
+ ```
159
+
160
+ #### 上传附件
161
+
162
+ ```typescript
163
+ const fileInput = document.getElementById('file-input') as HTMLInputElement;
164
+ const file = fileInput.files[0];
165
+
166
+ const result = await client.record.uploadAttachment({
167
+ tableId: 'table-id',
168
+ recordId: 'record-id',
169
+ fieldId: 'attachment-field-id',
170
+ file: file,
171
+ fileUrl?: 'optional-file-url' // 可选,作为 file 的替代
172
+ });
173
+ ```
174
+
175
+ ## 错误处理
176
+
177
+ SDK 使用统一的响应格式和错误处理:
178
+
179
+ ```typescript
180
+ interface TeableResponse<T = any> {
181
+ success: boolean; // 操作是否成功
182
+ data?: T; // 响应数据
183
+ error?: string; // 错误信息
184
+ message?: string; // 消息
185
+ }
186
+
187
+ // 使用示例
188
+ const result = await client.record.get({ tableId: 'table-id' });
189
+
190
+ if (result.success) {
191
+ // 成功处理
192
+ console.log('数据:', result.data);
193
+ } else {
194
+ // 错误处理
195
+ console.error('错误:', result.error);
196
+ }
197
+ ```
198
+
199
+ ## 配置验证
200
+
201
+ SDK 提供了配置验证功能:
202
+
203
+ ```typescript
204
+ const client = createTeableClient({
205
+ apiKey: 'your-api-key',
206
+ baseUrl: 'https://your-teable-instance.com'
207
+ });
208
+
209
+ // 验证配置
210
+ if (client.validateConfig()) {
211
+ console.log('配置有效');
212
+ } else {
213
+ console.error('配置无效');
214
+ }
215
+ ```
216
+
217
+ ## 浏览器使用
218
+
219
+ 在浏览器中使用时,可以直接引入 UMD 版本:
220
+
221
+ ```html
222
+ <script src="https://unpkg.com/@teble/api-sdk/dist/index.umd.js"></script>
223
+ <script>
224
+ const { createTeableClient } = TeableSDK;
225
+
226
+ const client = createTeableClient({
227
+ apiKey: 'your-api-key',
228
+ baseUrl: 'https://your-teable-instance.com'
229
+ });
230
+ </script>
231
+ ```
232
+
233
+ ## 开发
234
+
235
+ ```bash
236
+ # 安装依赖
237
+ npm install
238
+
239
+ # 开发模式
240
+ npm run dev
241
+
242
+ # 构建
243
+ npm run build
244
+
245
+ # 类型检查
246
+ npm run type-check
247
+
248
+ # 代码检查
249
+ npm run lint
250
+
251
+ # 修复代码
252
+ npm run lint:fix
253
+
254
+ # 测试
255
+ npm test
256
+
257
+ # 清理构建文件
258
+ npm run clean
259
+ ```
260
+
261
+ ## 许可证
262
+
263
+ MIT
264
+
265
+ ## 贡献
266
+
267
+ 欢迎提交 Issue 和 Pull Request!
@@ -0,0 +1,38 @@
1
+ import type { TeableConfig, TableRecord, TeableResponse, FieldKeyType, RecordOrder, RecordPosition, CellFormat, SortObject, SearchCondition } from "./types";
2
+ import type { RecordCreateOptions, RecordCreateResponse, RecordGetOptions, RecordGetResponse, RecordGetByIdOptions, RecordGetByIdResponse, RecordUpdateOptions, RecordUpdateResponse, RecordDeleteOptions, RecordDeleteResponse, RecordUploadAttachmentOptions, RecordUploadAttachmentResponse } from "./record/types";
3
+ /**
4
+ * 创建 Teable 客户端
5
+ * @param config - Teable 配置
6
+ */
7
+ export declare function createTeableClient(config: TeableConfig): {
8
+ record: {
9
+ create(options: RecordCreateOptions): Promise<TeableResponse<RecordCreateResponse>>;
10
+ get(options: RecordGetOptions): Promise<TeableResponse<RecordGetResponse>>;
11
+ getById(options: RecordGetByIdOptions): Promise<TeableResponse<RecordGetByIdResponse>>;
12
+ update(options: RecordUpdateOptions): Promise<TeableResponse<RecordUpdateResponse>>;
13
+ delete(options: RecordDeleteOptions): Promise<TeableResponse<RecordDeleteResponse>>;
14
+ uploadAttachment(options: RecordUploadAttachmentOptions): Promise<TeableResponse<RecordUploadAttachmentResponse>>;
15
+ };
16
+ /**
17
+ * 验证配置是否有效
18
+ */
19
+ validateConfig: () => boolean;
20
+ };
21
+ export type TeableClient = {
22
+ record: {
23
+ create: (options: RecordCreateOptions) => Promise<TeableResponse<RecordCreateResponse>>;
24
+ get: (options: RecordGetOptions) => Promise<TeableResponse<RecordGetResponse>>;
25
+ getById: (options: RecordGetByIdOptions) => Promise<TeableResponse<RecordGetByIdResponse>>;
26
+ update: (options: RecordUpdateOptions) => Promise<TeableResponse<RecordUpdateResponse>>;
27
+ delete: (options: RecordDeleteOptions) => Promise<TeableResponse<RecordDeleteResponse>>;
28
+ uploadAttachment: (options: RecordUploadAttachmentOptions) => Promise<TeableResponse<RecordUploadAttachmentResponse>>;
29
+ };
30
+ validateConfig: () => boolean;
31
+ };
32
+ export type { TeableConfig, TableRecord, RecordCreateOptions, RecordCreateResponse, RecordGetOptions, RecordGetResponse, RecordGetByIdOptions, RecordGetByIdResponse, RecordUpdateOptions, RecordUpdateResponse, RecordDeleteOptions, RecordDeleteResponse, RecordUploadAttachmentOptions, RecordUploadAttachmentResponse, TeableResponse, FieldKeyType, RecordPosition, RecordOrder, CellFormat, SortObject, SearchCondition, };
33
+ export declare const version = "1.0.0";
34
+ declare const _default: {
35
+ createTeableClient: typeof createTeableClient;
36
+ version: string;
37
+ };
38
+ export default _default;