@sheet2db/sdk 1.0.8 โ†’ 2.0.0

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,181 @@
1
- # Sheet2DB Documentation
1
+ Sure! Here's a clean and well-structured **Markdown documentation** for your `Sheet2DBClient` SDK with the type definitions and method usage.
2
2
 
3
- ## Overview
3
+ # ๐Ÿ“˜ Sheet2DBClient SDK
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
+ 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.
6
6
 
7
- ## Installation
7
+ ---
8
+
9
+ ## ๐Ÿ“ฆ Installation
8
10
 
9
11
  ```bash
10
12
  npm install @sheet2db/sdk
11
13
  ```
12
14
 
13
- ## Configuration Options
14
-
15
- ### Sheet2DBOptions
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.
25
-
26
- ## Usage
27
-
28
- ### Import and Initialize
29
-
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
15
+ ---
73
16
 
74
- Gets the keys of the spreadsheet.
17
+ ## ๐Ÿš€ Usage
75
18
 
76
- #### Parameters
19
+ ```ts
20
+ import { Sheet2DBClient } from '@sheet2db/sdk';
77
21
 
78
- - **sheet** (optional): `string` - The name of the sheet to get keys from.
22
+ const client = new Sheet2DBClient('your-api-id');
79
23
 
80
- #### Example
24
+ // Optional authentication
25
+ client.useBearerTokenAuthentication('your-token');
81
26
 
82
- ```typescript
83
- sheet2db.Keys({ sheet: 'Sheet1' })
84
- .then(keys => console.log(keys))
85
- .catch(error => console.error(error));
27
+ // Example: Read rows
28
+ const data = await client.Read({ limit: 10 }, 'Sheet1');
86
29
  ```
87
30
 
88
- ### Count
31
+ ---
89
32
 
90
- Gets the count of rows in the spreadsheet.
33
+ ## ๐Ÿ” Authentication Methods
91
34
 
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));
35
+ ```ts
36
+ client.useBasicAuthentication(username: string, password: string);
37
+ client.useJWTAuthentication(token: string);
38
+ client.useBearerTokenAuthentication(token: string);
114
39
  ```
115
40
 
116
- ### Range
117
-
118
- Gets a range of values from the spreadsheet.
119
-
120
- #### Parameters
41
+ ---
121
42
 
122
- - **range**: `string` - The range to fetch, in A1 notation.
43
+ ## ๐Ÿง  Type Definitions
123
44
 
124
- #### Example
125
-
126
- ```typescript
127
- sheet2db.Range({ range: 'A1:B2' })
128
- .then(range => console.log(range))
129
- .catch(error => console.error(error));
130
- ```
131
-
132
- ### Search
133
-
134
- Searches for records in the spreadsheet.
135
-
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
- ```
45
+ ### `ReadOptions`
149
46
 
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));
165
- ```
166
-
167
- ### UpdateRow
168
-
169
- Updates a specific row in the spreadsheet.
170
-
171
- #### Parameters
172
-
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));
47
+ ```ts
48
+ {
49
+ limit?: number;
50
+ offset: number;
51
+ sortColumn?: string;
52
+ sortDirection: 'asc' | 'desc';
53
+ sortColumnFormat?: 'date';
54
+ filter?: string;
55
+ format: 'rows' | 'columns' | 'matrix';
56
+ columns?: string[];
57
+ castNumbers?: string[];
58
+ }
183
59
  ```
184
60
 
185
- ### UpdateWithQuery
186
-
187
- Updates rows based on a query.
188
-
189
- #### Parameters
61
+ ### `InsertOptions`
190
62
 
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));
63
+ ```ts
64
+ {
65
+ data: any[];
66
+ }
201
67
  ```
202
68
 
203
- ### BatchUpdate
204
-
205
- Updates multiple records in batches.
69
+ ### `UpdateOptions`
206
70
 
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
71
+ ```ts
72
+ // Update by row
73
+ {
74
+ type: 'row';
75
+ row: number;
76
+ data: Record<string, any>;
77
+ }
213
78
 
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));
79
+ // Update by filter
80
+ {
81
+ type: 'filter';
82
+ filter: string;
83
+ data: Record<string, any>;
84
+ }
218
85
  ```
219
86
 
220
- ### DeleteRow
221
-
222
- Deletes a specific row in the spreadsheet.
87
+ ### `DeleteOptions`
223
88
 
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
89
+ ```ts
90
+ // Delete by row
91
+ {
92
+ type: 'row';
93
+ row: number;
94
+ }
230
95
 
231
- ```typescript
232
- sheet2db.DeleteRow({ row: 1, sheet: 'Sheet1' })
233
- .then(response => console.log(response))
234
- .catch(error => console.error(error));
96
+ // Delete by filter
97
+ {
98
+ type: 'filter';
99
+ filter: string;
100
+ }
235
101
  ```
236
102
 
237
- ### DeleteWithQuery
103
+ ### `AddSheetOptions`
238
104
 
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));
105
+ ```ts
106
+ {
107
+ name: string;
108
+ firstRow?: string[];
109
+ copyColumnsFrom?: string;
110
+ }
252
111
  ```
253
112
 
254
- ### Clear
113
+ ---
255
114
 
256
- Clears all data from a sheet.
115
+ ## ๐Ÿ“š Methods
257
116
 
258
- #### Parameters
117
+ ### `Read<T>(options: ReadOptions, sheet?: string): Promise<T>`
259
118
 
260
- - **sheet**: `string` - The name of the sheet to clear.
119
+ Reads data from the sheet based on options provided.
261
120
 
262
- #### Example
121
+ ---
263
122
 
264
- ```typescript
265
- sheet2db.Clear({ sheet: 'Sheet1' })
266
- .then(response => console.log(response))
267
- .catch(error => console.error(error));
268
- ```
123
+ ### `Insert(options: InsertOptions, sheet?: string): Promise<{ inserted: number }>`
269
124
 
270
- ### CreateSheet
125
+ Inserts rows of data into the specified sheet.
271
126
 
272
- Creates a new sheet in the spreadsheet.
127
+ ---
273
128
 
274
- #### Parameters
129
+ ### `Update(options: UpdateOptions, sheet?: string): Promise<{ updated: number }>`
275
130
 
276
- - **title** (optional): `string` - The title of the new sheet.
131
+ Updates rows based on a filter or row number.
277
132
 
278
- #### Example
133
+ ---
279
134
 
280
- ```typescript
281
- sheet2db.CreateSheet({ title: 'NewSheet' })
282
- .then(response => console.log(response))
283
- .catch(error => console.error(error));
284
- ```
135
+ ### `Delete(options: DeleteOptions, sheet?: string): Promise<{ deleted: number }>`
285
136
 
286
- ### DeleteSheet
137
+ Deletes rows based on a filter or row number.
287
138
 
288
- Deletes a sheet from the spreadsheet.
139
+ ---
289
140
 
290
- #### Parameters
141
+ ### `AddSheet(options: AddSheetOptions): Promise<{ ok: true }>`
291
142
 
292
- - **sheet**: `string` - The name of the sheet to delete.
143
+ Adds a new sheet with optional headers or by copying from another sheet.
293
144
 
294
- #### Example
145
+ ---
295
146
 
296
- ```typescript
297
- sheet2db.DeleteSheet({ sheet: 'Sheet1' })
298
- .then(response => console.log(response))
299
- .catch(error => console.error(error));
300
- ```
147
+ ### `DeleteSheet(sheet: string): Promise<{ ok: true }>`
301
148
 
149
+ Deletes a sheet by name.
302
150
 
151
+ ---
303
152
 
304
- ## Environment Detection
153
+ ## ๐Ÿงช Example
305
154
 
306
- The library automatically detects the environment (browser or Node.js) and uses the appropriate `fetch` implementation.
155
+ ```ts
156
+ await client.Insert({
157
+ data: [
158
+ { name: 'Alice', age: 28 },
159
+ { name: 'Bob', age: 31 }
160
+ ]
161
+ }, 'People');
307
162
 
308
- ```typescript
309
- let fetchFunction: typeof fetch;
163
+ await client.Update({
164
+ type: 'row',
165
+ row: 2,
166
+ data: { age: 32 }
167
+ }, 'People');
310
168
 
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
- }
169
+ await client.Delete({
170
+ type: 'filter',
171
+ filter: 'name:eq:Bob'
172
+ }, 'People');
319
173
  ```
320
174
 
321
- ## Error Handling
175
+ ---
322
176
 
323
- Each method returns a promise that resolves to the API response or rejects with an error.
177
+ ## ๐Ÿงพ Notes
324
178
 
325
- ```typescript
326
- sheet2db.ReadContent()
327
- .then(data => console.log(data))
328
- .catch(error => console.error(error));
329
- ```
179
+ * All options are validated using `zod` for runtime type safety.
180
+ * `sheet` argument is optional; default sheet will be used if not specified.
181
+ * 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 };