@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 CHANGED
@@ -1,329 +1,179 @@
1
- # Sheet2DB Documentation
1
+ # ๐Ÿ“˜ Sheet2DBClient SDK
2
2
 
3
- ## Overview
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
- `Sheet2DB` is a TypeScript class that provides an easy interface to interact with Google Sheets via the [`sheet2db`](https://sheet2db.com) API. This class abstracts the complexity of API calls and provides a simple way to read, write, update, and delete data in Google Sheets. It supports both browser and Node.js environments.
5
+ ---
6
6
 
7
- ## Installation
7
+ ## ๐Ÿ“ฆ Installation
8
8
 
9
9
  ```bash
10
10
  npm install @sheet2db/sdk
11
11
  ```
12
12
 
13
- ## Configuration Options
13
+ ---
14
14
 
15
- ### Sheet2DBOptions
15
+ ## ๐Ÿš€ Usage
16
16
 
17
- - **mode**: `'apikey' | 'connectionId'` - Authentication mode.
18
- - **apiKey**: `string` (required for `'apikey'` mode) - API key for accessing the spreadsheet.
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
- ## Usage
20
+ const client = new Sheet2DBClient('your-api-id');
27
21
 
28
- ### Import and Initialize
22
+ // Optional authentication
23
+ client.useBearerTokenAuthentication('your-token');
29
24
 
30
- ```typescript
31
- import { Sheet2DB,Sheet2DBOptions } from 'sheet2db';
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
- ### Search
29
+ ---
133
30
 
134
- Searches for records in the spreadsheet.
31
+ ## ๐Ÿ” Authentication Methods
135
32
 
136
- #### Parameters
137
-
138
- - **sheet** (optional): `string` - The name of the sheet to search in.
139
- - **or** (optional): `boolean` - If true, uses OR logic for the search.
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
- ### UpdateRow
39
+ ---
168
40
 
169
- Updates a specific row in the spreadsheet.
41
+ ## ๐Ÿง  Type Definitions
170
42
 
171
- #### Parameters
43
+ ### `ReadOptions`
172
44
 
173
- - **row**: `number` - The row number to update.
174
- - **data**: `Record<string, any>` - The data to update.
175
- - **sheet** (optional): `string` - The name of the sheet to update data in.
176
-
177
- #### Example
178
-
179
- ```typescript
180
- sheet2db.UpdateRow({ row: 1, data: { age: 31 }, sheet: 'Sheet1' })
181
- .then(response => console.log(response))
182
- .catch(error => console.error(error));
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
- ### UpdateWithQuery
186
-
187
- Updates rows based on a query.
188
-
189
- #### Parameters
59
+ ### `InsertOptions`
190
60
 
191
- - **sheet** (optional): `string` - The name of the sheet to update data in.
192
- - **query**: `string` - The query to identify rows to update.
193
- - **data**: `Record<string, any>` - The data to update.
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
- ### BatchUpdate
204
-
205
- Updates multiple records in batches.
67
+ ### `UpdateOptions`
206
68
 
207
- #### Parameters
208
-
209
- - **sheet** (optional): `string` - The name of the sheet to update data in.
210
- - **batches**: `{ query: string, record: Record<string, any> }[]` - The batch update data.
211
-
212
- #### Example
69
+ ```ts
70
+ // Update by row
71
+ {
72
+ type: 'row';
73
+ row: number;
74
+ data: Record<string, any>;
75
+ }
213
76
 
214
- ```typescript
215
- sheet2db.BatchUpdate({ batches: [{ query: 'name=John', record: { age: 33 } }], sheet: 'Sheet1' })
216
- .then(response => console.log(response))
217
- .catch(error => console.error(error));
77
+ // Update by filter
78
+ {
79
+ type: 'filter';
80
+ filter: string;
81
+ data: Record<string, any>;
82
+ }
218
83
  ```
219
84
 
220
- ### DeleteRow
221
-
222
- Deletes a specific row in the spreadsheet.
85
+ ### `DeleteOptions`
223
86
 
224
- #### Parameters
225
-
226
- - **row**: `number` - The row number to delete.
227
- - **sheet** (optional): `string` - The name of the sheet to delete the row from.
228
-
229
- #### Example
87
+ ```ts
88
+ // Delete by row
89
+ {
90
+ type: 'row';
91
+ row: number;
92
+ }
230
93
 
231
- ```typescript
232
- sheet2db.DeleteRow({ row: 1, sheet: 'Sheet1' })
233
- .then(response => console.log(response))
234
- .catch(error => console.error(error));
94
+ // Delete by filter
95
+ {
96
+ type: 'filter';
97
+ filter: string;
98
+ }
235
99
  ```
236
100
 
237
- ### DeleteWithQuery
101
+ ### `AddSheetOptions`
238
102
 
239
- Deletes rows based on a query.
240
-
241
- #### Parameters
242
-
243
- - **query**: `string` - The query to identify rows to delete.
244
- - **sheet** (optional): `string` - The name of the sheet to delete rows from.
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
- ### Clear
111
+ ---
255
112
 
256
- Clears all data from a sheet.
113
+ ## ๐Ÿ“š Methods
257
114
 
258
- #### Parameters
115
+ ### `Read<T>(options: ReadOptions, sheet?: string): Promise<T>`
259
116
 
260
- - **sheet**: `string` - The name of the sheet to clear.
117
+ Reads data from the sheet based on options provided.
261
118
 
262
- #### Example
119
+ ---
263
120
 
264
- ```typescript
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
- ### CreateSheet
123
+ Inserts rows of data into the specified sheet.
271
124
 
272
- Creates a new sheet in the spreadsheet.
125
+ ---
273
126
 
274
- #### Parameters
127
+ ### `Update(options: UpdateOptions, sheet?: string): Promise<{ updated: number }>`
275
128
 
276
- - **title** (optional): `string` - The title of the new sheet.
129
+ Updates rows based on a filter or row number.
277
130
 
278
- #### Example
131
+ ---
279
132
 
280
- ```typescript
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
- ### DeleteSheet
135
+ Deletes rows based on a filter or row number.
287
136
 
288
- Deletes a sheet from the spreadsheet.
137
+ ---
289
138
 
290
- #### Parameters
139
+ ### `AddSheet(options: AddSheetOptions): Promise<{ ok: true }>`
291
140
 
292
- - **sheet**: `string` - The name of the sheet to delete.
141
+ Adds a new sheet with optional headers or by copying from another sheet.
293
142
 
294
- #### Example
143
+ ---
295
144
 
296
- ```typescript
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
- ## Environment Detection
151
+ ## ๐Ÿงช Example
305
152
 
306
- The library automatically detects the environment (browser or Node.js) and uses the appropriate `fetch` implementation.
153
+ ```ts
154
+ await client.Insert({
155
+ data: [
156
+ { name: 'Alice', age: 28 },
157
+ { name: 'Bob', age: 31 }
158
+ ]
159
+ }, 'People');
307
160
 
308
- ```typescript
309
- let fetchFunction: typeof fetch;
161
+ await client.Update({
162
+ type: 'row',
163
+ row: 2,
164
+ data: { age: 32 }
165
+ }, 'People');
310
166
 
311
- if (typeof window !== 'undefined' && typeof window.fetch !== 'undefined') {
312
- fetchFunction = window.fetch.bind(window);
313
- } else {
314
- fetchFunction = async (...args) => {
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
- ## Error Handling
173
+ ---
322
174
 
323
- Each method returns a promise that resolves to the API response or rejects with an error.
175
+ ## ๐Ÿงพ Notes
324
176
 
325
- ```typescript
326
- sheet2db.ReadContent()
327
- .then(data => console.log(data))
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.
@@ -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 };