@sheet2db/sdk 1.0.9 โ 2.0.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/README.md +114 -264
- package/dist/index.d.mts +141 -0
- package/dist/index.d.ts +129 -116
- package/dist/index.js +125 -145
- package/dist/index.mjs +93 -0
- package/jest.config.js +2 -3
- package/package.json +21 -29
- package/src/index.test.ts +116 -0
- package/src/index.ts +45 -234
- package/tsconfig.json +103 -7
- package/tests/index.test.ts +0 -334
package/README.md
CHANGED
@@ -1,329 +1,179 @@
|
|
1
|
-
#
|
1
|
+
# ๐ Sheet2DBClient SDK
|
2
2
|
|
3
|
-
|
3
|
+
The `Sheet2DBClient` SDK allows developers to interact with their Google Sheets via the Sheet2DB platform using a clean and typed interface. It provides full CRUD support along with helper utilities for managing sheets.
|
4
4
|
|
5
|
-
|
5
|
+
---
|
6
6
|
|
7
|
-
## Installation
|
7
|
+
## ๐ฆ Installation
|
8
8
|
|
9
9
|
```bash
|
10
10
|
npm install @sheet2db/sdk
|
11
11
|
```
|
12
12
|
|
13
|
-
|
13
|
+
---
|
14
14
|
|
15
|
-
|
15
|
+
## ๐ Usage
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
- **spreadsheetId**: `string` (required for `'apikey'` mode) - The ID of the Google spreadsheet.
|
20
|
-
- **version**: `"v1"` - API version, always "v1".
|
21
|
-
- **connectionId**: `string` (required for `'connectionId'` mode) - Connection ID for accessing the spreadsheet.
|
22
|
-
- **basicAuth**: `{ username: string; password: string; }` (optional for `'connectionId'` mode) - Basic authentication credentials.
|
23
|
-
- **jwtAuth**: `{ bearerToken: string; }` (optional for `'connectionId'` mode) - JWT authentication token.
|
24
|
-
- **fetchFn**: `typeof fetch` (optional) - Custom fetch function.
|
17
|
+
```ts
|
18
|
+
import { Sheet2DBClient } from '@sheet2db/sdk';
|
25
19
|
|
26
|
-
|
20
|
+
const client = new Sheet2DBClient('your-api-id');
|
27
21
|
|
28
|
-
|
22
|
+
// Optional authentication
|
23
|
+
client.useBearerTokenAuthentication('your-token');
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
const options: Sheet2DBOptions = {
|
34
|
-
mode: 'apikey',
|
35
|
-
apiKey: 'your-api-key',
|
36
|
-
spreadsheetId: 'your-spreadsheet-id',
|
37
|
-
version: 'v1'
|
38
|
-
};
|
39
|
-
|
40
|
-
const options: Sheet2DBOptions = {
|
41
|
-
mode: 'connectionId',
|
42
|
-
connectionId: 'your-connection-id',
|
43
|
-
version: 'v1'
|
44
|
-
};
|
45
|
-
|
46
|
-
const sheet2db = new Sheet2DB(options);
|
47
|
-
```
|
48
|
-
|
49
|
-
## Methods
|
50
|
-
|
51
|
-
### ReadContent
|
52
|
-
|
53
|
-
Fetches content from the spreadsheet.
|
54
|
-
|
55
|
-
#### Parameters
|
56
|
-
|
57
|
-
- **limit** (optional): `number` - Maximum number of records to return.
|
58
|
-
- **offset** (optional): `number` - Number of records to skip before starting to return records.
|
59
|
-
- **sheet** (optional): `string` - The name of the sheet to read from.
|
60
|
-
- **format** (optional): `'records' | 'dict' | 'series' | 'split' | 'index' | 'raw'` - The format of the returned data.
|
61
|
-
- **cast_numbers** (optional): `string` - Cast numbers to specified type.
|
62
|
-
- **value_render** (optional): `'FORMATTED_VALUE' | 'UNFORMATTED_VALUE' | 'FORMULA'` - The value render option.
|
63
|
-
|
64
|
-
#### Example
|
65
|
-
|
66
|
-
```typescript
|
67
|
-
sheet2db.ReadContent({ sheet: 'Sheet1', limit: 10 })
|
68
|
-
.then(data => console.log(data))
|
69
|
-
.catch(error => console.error(error));
|
70
|
-
```
|
71
|
-
|
72
|
-
### Keys
|
73
|
-
|
74
|
-
Gets the keys of the spreadsheet.
|
75
|
-
|
76
|
-
#### Parameters
|
77
|
-
|
78
|
-
- **sheet** (optional): `string` - The name of the sheet to get keys from.
|
79
|
-
|
80
|
-
#### Example
|
81
|
-
|
82
|
-
```typescript
|
83
|
-
sheet2db.Keys({ sheet: 'Sheet1' })
|
84
|
-
.then(keys => console.log(keys))
|
85
|
-
.catch(error => console.error(error));
|
86
|
-
```
|
87
|
-
|
88
|
-
### Count
|
89
|
-
|
90
|
-
Gets the count of rows in the spreadsheet.
|
91
|
-
|
92
|
-
#### Parameters
|
93
|
-
|
94
|
-
- **sheet** (optional): `string` - The name of the sheet to count rows in.
|
95
|
-
|
96
|
-
#### Example
|
97
|
-
|
98
|
-
```typescript
|
99
|
-
sheet2db.Count({ sheet: 'Sheet1' })
|
100
|
-
.then(count => console.log(count))
|
101
|
-
.catch(error => console.error(error));
|
102
|
-
```
|
103
|
-
|
104
|
-
### Title
|
105
|
-
|
106
|
-
Gets the title of the spreadsheet.
|
107
|
-
|
108
|
-
#### Example
|
109
|
-
|
110
|
-
```typescript
|
111
|
-
sheet2db.Title()
|
112
|
-
.then(title => console.log(title))
|
113
|
-
.catch(error => console.error(error));
|
114
|
-
```
|
115
|
-
|
116
|
-
### Range
|
117
|
-
|
118
|
-
Gets a range of values from the spreadsheet.
|
119
|
-
|
120
|
-
#### Parameters
|
121
|
-
|
122
|
-
- **range**: `string` - The range to fetch, in A1 notation.
|
123
|
-
|
124
|
-
#### Example
|
125
|
-
|
126
|
-
```typescript
|
127
|
-
sheet2db.Range({ range: 'A1:B2' })
|
128
|
-
.then(range => console.log(range))
|
129
|
-
.catch(error => console.error(error));
|
25
|
+
// Example: Read rows
|
26
|
+
const data = await client.Read({ limit: 10 }, 'Sheet1');
|
130
27
|
```
|
131
28
|
|
132
|
-
|
29
|
+
---
|
133
30
|
|
134
|
-
|
31
|
+
## ๐ Authentication Methods
|
135
32
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
- **query**: `string` - The search query.
|
141
|
-
|
142
|
-
#### Example
|
143
|
-
|
144
|
-
```typescript
|
145
|
-
sheet2db.Search({ query: 'name=John', sheet: 'Sheet1' })
|
146
|
-
.then(records => console.log(records))
|
147
|
-
.catch(error => console.error(error));
|
148
|
-
```
|
149
|
-
|
150
|
-
### Insert
|
151
|
-
|
152
|
-
Inserts data into the spreadsheet.
|
153
|
-
|
154
|
-
#### Parameters
|
155
|
-
|
156
|
-
- **data**: `Record<string, any> | Record<string, any>[]` - The data to insert.
|
157
|
-
- **sheet** (optional): `string` - The name of the sheet to insert data into.
|
158
|
-
|
159
|
-
#### Example
|
160
|
-
|
161
|
-
```typescript
|
162
|
-
sheet2db.Insert({ data: { name: 'John', age: 30 }, sheet: 'Sheet1' })
|
163
|
-
.then(response => console.log(response))
|
164
|
-
.catch(error => console.error(error));
|
33
|
+
```ts
|
34
|
+
client.useBasicAuthentication(username: string, password: string);
|
35
|
+
client.useJWTAuthentication(token: string);
|
36
|
+
client.useBearerTokenAuthentication(token: string);
|
165
37
|
```
|
166
38
|
|
167
|
-
|
39
|
+
---
|
168
40
|
|
169
|
-
|
41
|
+
## ๐ง Type Definitions
|
170
42
|
|
171
|
-
|
43
|
+
### `ReadOptions`
|
172
44
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
45
|
+
```ts
|
46
|
+
{
|
47
|
+
limit?: number;
|
48
|
+
offset: number;
|
49
|
+
sortColumn?: string;
|
50
|
+
sortDirection: 'asc' | 'desc';
|
51
|
+
sortColumnFormat?: 'date';
|
52
|
+
filter?: string;
|
53
|
+
format: 'rows' | 'columns' | 'matrix';
|
54
|
+
columns?: string[];
|
55
|
+
castNumbers?: string[];
|
56
|
+
}
|
183
57
|
```
|
184
58
|
|
185
|
-
###
|
186
|
-
|
187
|
-
Updates rows based on a query.
|
188
|
-
|
189
|
-
#### Parameters
|
59
|
+
### `InsertOptions`
|
190
60
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
#### Example
|
196
|
-
|
197
|
-
```typescript
|
198
|
-
sheet2db.UpdateWithQuery({ query: 'name=John', data: { age: 32 }, sheet: 'Sheet1' })
|
199
|
-
.then(response => console.log(response))
|
200
|
-
.catch(error => console.error(error));
|
61
|
+
```ts
|
62
|
+
{
|
63
|
+
data: any[];
|
64
|
+
}
|
201
65
|
```
|
202
66
|
|
203
|
-
###
|
204
|
-
|
205
|
-
Updates multiple records in batches.
|
67
|
+
### `UpdateOptions`
|
206
68
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
69
|
+
```ts
|
70
|
+
// Update by row
|
71
|
+
{
|
72
|
+
type: 'row';
|
73
|
+
row: number;
|
74
|
+
data: Record<string, any>;
|
75
|
+
}
|
213
76
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
77
|
+
// Update by filter
|
78
|
+
{
|
79
|
+
type: 'filter';
|
80
|
+
filter: string;
|
81
|
+
data: Record<string, any>;
|
82
|
+
}
|
218
83
|
```
|
219
84
|
|
220
|
-
###
|
221
|
-
|
222
|
-
Deletes a specific row in the spreadsheet.
|
85
|
+
### `DeleteOptions`
|
223
86
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
87
|
+
```ts
|
88
|
+
// Delete by row
|
89
|
+
{
|
90
|
+
type: 'row';
|
91
|
+
row: number;
|
92
|
+
}
|
230
93
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
94
|
+
// Delete by filter
|
95
|
+
{
|
96
|
+
type: 'filter';
|
97
|
+
filter: string;
|
98
|
+
}
|
235
99
|
```
|
236
100
|
|
237
|
-
###
|
101
|
+
### `AddSheetOptions`
|
238
102
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
#### Example
|
247
|
-
|
248
|
-
```typescript
|
249
|
-
sheet2db.DeleteWithQuery({ query: 'name=John', sheet: 'Sheet1' })
|
250
|
-
.then(response => console.log(response))
|
251
|
-
.catch(error => console.error(error));
|
103
|
+
```ts
|
104
|
+
{
|
105
|
+
name: string;
|
106
|
+
firstRow?: string[];
|
107
|
+
copyColumnsFrom?: string;
|
108
|
+
}
|
252
109
|
```
|
253
110
|
|
254
|
-
|
111
|
+
---
|
255
112
|
|
256
|
-
|
113
|
+
## ๐ Methods
|
257
114
|
|
258
|
-
|
115
|
+
### `Read<T>(options: ReadOptions, sheet?: string): Promise<T>`
|
259
116
|
|
260
|
-
|
117
|
+
Reads data from the sheet based on options provided.
|
261
118
|
|
262
|
-
|
119
|
+
---
|
263
120
|
|
264
|
-
|
265
|
-
sheet2db.Clear({ sheet: 'Sheet1' })
|
266
|
-
.then(response => console.log(response))
|
267
|
-
.catch(error => console.error(error));
|
268
|
-
```
|
121
|
+
### `Insert(options: InsertOptions, sheet?: string): Promise<{ inserted: number }>`
|
269
122
|
|
270
|
-
|
123
|
+
Inserts rows of data into the specified sheet.
|
271
124
|
|
272
|
-
|
125
|
+
---
|
273
126
|
|
274
|
-
|
127
|
+
### `Update(options: UpdateOptions, sheet?: string): Promise<{ updated: number }>`
|
275
128
|
|
276
|
-
|
129
|
+
Updates rows based on a filter or row number.
|
277
130
|
|
278
|
-
|
131
|
+
---
|
279
132
|
|
280
|
-
|
281
|
-
sheet2db.CreateSheet({ title: 'NewSheet' })
|
282
|
-
.then(response => console.log(response))
|
283
|
-
.catch(error => console.error(error));
|
284
|
-
```
|
133
|
+
### `Delete(options: DeleteOptions, sheet?: string): Promise<{ deleted: number }>`
|
285
134
|
|
286
|
-
|
135
|
+
Deletes rows based on a filter or row number.
|
287
136
|
|
288
|
-
|
137
|
+
---
|
289
138
|
|
290
|
-
|
139
|
+
### `AddSheet(options: AddSheetOptions): Promise<{ ok: true }>`
|
291
140
|
|
292
|
-
|
141
|
+
Adds a new sheet with optional headers or by copying from another sheet.
|
293
142
|
|
294
|
-
|
143
|
+
---
|
295
144
|
|
296
|
-
|
297
|
-
sheet2db.DeleteSheet({ sheet: 'Sheet1' })
|
298
|
-
.then(response => console.log(response))
|
299
|
-
.catch(error => console.error(error));
|
300
|
-
```
|
145
|
+
### `DeleteSheet(sheet: string): Promise<{ ok: true }>`
|
301
146
|
|
147
|
+
Deletes a sheet by name.
|
302
148
|
|
149
|
+
---
|
303
150
|
|
304
|
-
##
|
151
|
+
## ๐งช Example
|
305
152
|
|
306
|
-
|
153
|
+
```ts
|
154
|
+
await client.Insert({
|
155
|
+
data: [
|
156
|
+
{ name: 'Alice', age: 28 },
|
157
|
+
{ name: 'Bob', age: 31 }
|
158
|
+
]
|
159
|
+
}, 'People');
|
307
160
|
|
308
|
-
|
309
|
-
|
161
|
+
await client.Update({
|
162
|
+
type: 'row',
|
163
|
+
row: 2,
|
164
|
+
data: { age: 32 }
|
165
|
+
}, 'People');
|
310
166
|
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
const fetch = global.fetch;
|
316
|
-
return fetch(...args);
|
317
|
-
};
|
318
|
-
}
|
167
|
+
await client.Delete({
|
168
|
+
type: 'filter',
|
169
|
+
filter: 'name:eq:Bob'
|
170
|
+
}, 'People');
|
319
171
|
```
|
320
172
|
|
321
|
-
|
173
|
+
---
|
322
174
|
|
323
|
-
|
175
|
+
## ๐งพ Notes
|
324
176
|
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
.catch(error => console.error(error));
|
329
|
-
```
|
177
|
+
* All options are validated using `zod` for runtime type safety.
|
178
|
+
* `sheet` argument is optional; default sheet will be used if not specified.
|
179
|
+
* Supports pagination, sorting, and filtering.
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
2
|
+
import { z } from 'zod';
|
3
|
+
|
4
|
+
declare const DataAPIReadOptionsDTO: z.ZodObject<{
|
5
|
+
limit: z.ZodEffects<z.ZodOptional<z.ZodString>, number | undefined, string | undefined>;
|
6
|
+
offset: z.ZodEffects<z.ZodDefault<z.ZodOptional<z.ZodString>>, number, string | undefined>;
|
7
|
+
sortColumn: z.ZodOptional<z.ZodString>;
|
8
|
+
sortDirection: z.ZodDefault<z.ZodOptional<z.ZodEnum<["asc", "desc"]>>>;
|
9
|
+
sortColumnFormat: z.ZodOptional<z.ZodLiteral<"date">>;
|
10
|
+
filter: z.ZodOptional<z.ZodString>;
|
11
|
+
format: z.ZodDefault<z.ZodOptional<z.ZodEnum<["rows", "columns", "matrix"]>>>;
|
12
|
+
columns: z.ZodEffects<z.ZodOptional<z.ZodString>, string[] | undefined, string | undefined>;
|
13
|
+
castNumbers: z.ZodEffects<z.ZodOptional<z.ZodString>, string[] | undefined, string | undefined>;
|
14
|
+
}, "strip", z.ZodTypeAny, {
|
15
|
+
offset: number;
|
16
|
+
sortDirection: "asc" | "desc";
|
17
|
+
format: "rows" | "columns" | "matrix";
|
18
|
+
limit?: number | undefined;
|
19
|
+
filter?: string | undefined;
|
20
|
+
sortColumn?: string | undefined;
|
21
|
+
sortColumnFormat?: "date" | undefined;
|
22
|
+
columns?: string[] | undefined;
|
23
|
+
castNumbers?: string[] | undefined;
|
24
|
+
}, {
|
25
|
+
limit?: string | undefined;
|
26
|
+
filter?: string | undefined;
|
27
|
+
offset?: string | undefined;
|
28
|
+
sortColumn?: string | undefined;
|
29
|
+
sortDirection?: "asc" | "desc" | undefined;
|
30
|
+
sortColumnFormat?: "date" | undefined;
|
31
|
+
columns?: string | undefined;
|
32
|
+
format?: "rows" | "columns" | "matrix" | undefined;
|
33
|
+
castNumbers?: string | undefined;
|
34
|
+
}>;
|
35
|
+
declare const DataAPIInsertOptionsDTO: z.ZodObject<{
|
36
|
+
data: z.ZodArray<z.ZodAny, "many">;
|
37
|
+
}, "strip", z.ZodTypeAny, {
|
38
|
+
data: any[];
|
39
|
+
}, {
|
40
|
+
data: any[];
|
41
|
+
}>;
|
42
|
+
declare const DataAPIUpdateOptionsDTO: z.ZodUnion<[z.ZodObject<{
|
43
|
+
data: z.ZodRecord<z.ZodString, z.ZodAny>;
|
44
|
+
row: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber]>, number, string | number>;
|
45
|
+
type: z.ZodDefault<z.ZodLiteral<"row">>;
|
46
|
+
}, "strip", z.ZodTypeAny, {
|
47
|
+
data: Record<string, any>;
|
48
|
+
type: "row";
|
49
|
+
row: number;
|
50
|
+
}, {
|
51
|
+
data: Record<string, any>;
|
52
|
+
row: string | number;
|
53
|
+
type?: "row" | undefined;
|
54
|
+
}>, z.ZodObject<{
|
55
|
+
data: z.ZodRecord<z.ZodString, z.ZodAny>;
|
56
|
+
filter: z.ZodString;
|
57
|
+
type: z.ZodDefault<z.ZodLiteral<"filter">>;
|
58
|
+
}, "strip", z.ZodTypeAny, {
|
59
|
+
data: Record<string, any>;
|
60
|
+
filter: string;
|
61
|
+
type: "filter";
|
62
|
+
}, {
|
63
|
+
data: Record<string, any>;
|
64
|
+
filter: string;
|
65
|
+
type?: "filter" | undefined;
|
66
|
+
}>]>;
|
67
|
+
declare const DataAPIDeleteOptionsDTO: z.ZodEffects<z.ZodUnion<[z.ZodEffects<z.ZodObject<{
|
68
|
+
row: z.ZodEffects<z.ZodUnion<[z.ZodString, z.ZodNumber]>, number, string | number>;
|
69
|
+
}, "strip", z.ZodTypeAny, {
|
70
|
+
row: number;
|
71
|
+
}, {
|
72
|
+
row: string | number;
|
73
|
+
}>, {
|
74
|
+
row: number;
|
75
|
+
type: "row";
|
76
|
+
}, {
|
77
|
+
row: string | number;
|
78
|
+
}>, z.ZodObject<{
|
79
|
+
filter: z.ZodString;
|
80
|
+
}, "strip", z.ZodTypeAny, {
|
81
|
+
filter: string;
|
82
|
+
}, {
|
83
|
+
filter: string;
|
84
|
+
}>]>, {
|
85
|
+
row: number;
|
86
|
+
type: "row";
|
87
|
+
} | {
|
88
|
+
filter: string;
|
89
|
+
type: "filter";
|
90
|
+
}, {
|
91
|
+
row: string | number;
|
92
|
+
} | {
|
93
|
+
filter: string;
|
94
|
+
}>;
|
95
|
+
declare const DataAPIAddSheetOptionsDTO: z.ZodObject<{
|
96
|
+
name: z.ZodString;
|
97
|
+
firstRow: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
98
|
+
copyColumnsFrom: z.ZodOptional<z.ZodString>;
|
99
|
+
}, "strip", z.ZodTypeAny, {
|
100
|
+
name: string;
|
101
|
+
firstRow?: string[] | undefined;
|
102
|
+
copyColumnsFrom?: string | undefined;
|
103
|
+
}, {
|
104
|
+
name: string;
|
105
|
+
firstRow?: string[] | undefined;
|
106
|
+
copyColumnsFrom?: string | undefined;
|
107
|
+
}>;
|
108
|
+
|
109
|
+
type ReadOptions = z.infer<typeof DataAPIReadOptionsDTO>;
|
110
|
+
type InsertOptions = z.infer<typeof DataAPIInsertOptionsDTO>;
|
111
|
+
type UpdateOptions = z.infer<typeof DataAPIUpdateOptionsDTO>;
|
112
|
+
type DeleteOptions = z.infer<typeof DataAPIDeleteOptionsDTO>;
|
113
|
+
type AddSheetOptions = z.infer<typeof DataAPIAddSheetOptionsDTO>;
|
114
|
+
|
115
|
+
declare class Sheet2DBClient {
|
116
|
+
authentication: string | null;
|
117
|
+
axios: AxiosInstance;
|
118
|
+
constructor(apiId: string, version?: "v1");
|
119
|
+
private endpoint;
|
120
|
+
useBasicAuthentication(username: string, password: string): void;
|
121
|
+
useJWTAuthentication(token: string): void;
|
122
|
+
useBearerTokenAuthentication(token: string): void;
|
123
|
+
Read<T = any>(options: ReadOptions, sheet?: string): Promise<T>;
|
124
|
+
Update(options: UpdateOptions, sheet?: string): Promise<{
|
125
|
+
updated: number;
|
126
|
+
}>;
|
127
|
+
Insert(options: InsertOptions, sheet?: string): Promise<{
|
128
|
+
inserted: number;
|
129
|
+
}>;
|
130
|
+
Delete(options: DeleteOptions, sheet?: string): Promise<{
|
131
|
+
deleted: number;
|
132
|
+
}>;
|
133
|
+
AddSheet(options: AddSheetOptions): Promise<{
|
134
|
+
ok: true;
|
135
|
+
}>;
|
136
|
+
DeleteSheet(sheet: string): Promise<{
|
137
|
+
ok: true;
|
138
|
+
}>;
|
139
|
+
}
|
140
|
+
|
141
|
+
export { Sheet2DBClient };
|