@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 +116 -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 -28
- 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/tests/index.test.ts
DELETED
@@ -1,334 +0,0 @@
|
|
1
|
-
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
2
|
-
import { Sheet2DB, Sheet2DBOptions, ReadOptions } from '../dist'; // Adjust the import path
|
3
|
-
|
4
|
-
const mockFetch = jest.fn(async () => {
|
5
|
-
return {
|
6
|
-
ok: true,
|
7
|
-
json: () => Promise.resolve({}),
|
8
|
-
text: () => Promise.resolve(''),
|
9
|
-
}
|
10
|
-
}) as jest.Mock<any>;
|
11
|
-
|
12
|
-
const apiKeyOptions: Sheet2DBOptions = {
|
13
|
-
mode: 'connectionId',
|
14
|
-
connectionId: 'test-spreadsheet-id',
|
15
|
-
version: 'v1',
|
16
|
-
//@ts-ignore
|
17
|
-
fetchFn: mockFetch
|
18
|
-
};
|
19
|
-
|
20
|
-
describe('Sheet2DB', () => {
|
21
|
-
let sheet2DB: Sheet2DB;
|
22
|
-
|
23
|
-
beforeEach(() => {
|
24
|
-
sheet2DB = new Sheet2DB(apiKeyOptions); // Create a new instance of Sheet2DB
|
25
|
-
jest.clearAllMocks(); // Reset mocks before each test
|
26
|
-
});
|
27
|
-
|
28
|
-
it('ReadContent', async () => {
|
29
|
-
// Arrange
|
30
|
-
const readOptions = {
|
31
|
-
limit: 10,
|
32
|
-
offset: 5,
|
33
|
-
sheet: 'Sheet1',
|
34
|
-
format: 'records' as const,
|
35
|
-
cast_numbers: ['col1'],
|
36
|
-
columns: ['col1', 'col2'],
|
37
|
-
value_render: 'FORMATTED_VALUE' as const,
|
38
|
-
}
|
39
|
-
|
40
|
-
// Act
|
41
|
-
await sheet2DB.ReadContent(readOptions);
|
42
|
-
|
43
|
-
// Assert
|
44
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
45
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
46
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
47
|
-
expect(calledUrl.pathname).toBe('/v1/test-spreadsheet-id');
|
48
|
-
expect(calledUrl.searchParams.get('limit')).toBe(readOptions.limit?.toString());
|
49
|
-
expect(calledUrl.searchParams.get('offset')).toBe(readOptions.offset?.toString());
|
50
|
-
expect(calledUrl.searchParams.get('sheet')).toBe(readOptions.sheet);
|
51
|
-
expect(calledUrl.searchParams.get('format')).toBe(readOptions.format);
|
52
|
-
expect(calledUrl.searchParams.get('columns')).toBe(readOptions.columns);
|
53
|
-
expect(calledUrl.searchParams.get('cast_numbers')).toBe(readOptions.cast_numbers);
|
54
|
-
expect(calledUrl.searchParams.get('value_render')).toBe(readOptions.value_render);
|
55
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
56
|
-
expect(options.method).toBe('GET');
|
57
|
-
});
|
58
|
-
|
59
|
-
it('Keys', async () => {
|
60
|
-
// Arrange
|
61
|
-
const keysOptions = {
|
62
|
-
sheet: 'Sheet1',
|
63
|
-
};
|
64
|
-
|
65
|
-
// Act
|
66
|
-
await sheet2DB.Keys(keysOptions);
|
67
|
-
|
68
|
-
// Assert
|
69
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
70
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
71
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
72
|
-
expect(calledUrl.pathname).toBe('/v1/test-spreadsheet-id/keys');
|
73
|
-
expect(calledUrl.searchParams.get('sheet')).toBe(keysOptions.sheet);
|
74
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
75
|
-
expect(options.method).toBe('GET');
|
76
|
-
})
|
77
|
-
|
78
|
-
it('Count', async () => {
|
79
|
-
// Arrange
|
80
|
-
const countOptions = {
|
81
|
-
sheet: 'Sheet1',
|
82
|
-
};
|
83
|
-
|
84
|
-
// Act
|
85
|
-
await sheet2DB.Count(countOptions);
|
86
|
-
|
87
|
-
// Assert
|
88
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
89
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
90
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
91
|
-
expect(calledUrl.pathname).toBe('/v1/test-spreadsheet-id/count');
|
92
|
-
expect(calledUrl.searchParams.get('sheet')).toBe(countOptions.sheet);
|
93
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
94
|
-
expect(options.method).toBe('GET');
|
95
|
-
})
|
96
|
-
|
97
|
-
it('Title', async () => {
|
98
|
-
// Act
|
99
|
-
await sheet2DB.Title();
|
100
|
-
|
101
|
-
// Assert
|
102
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
103
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
104
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
105
|
-
expect(calledUrl.pathname).toBe('/v1/test-spreadsheet-id/title');
|
106
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
107
|
-
expect(options.method).toBe('GET');
|
108
|
-
})
|
109
|
-
|
110
|
-
it('Range', async () => {
|
111
|
-
// Arrange
|
112
|
-
const rangeOptions = {
|
113
|
-
range: 'A1:B2',
|
114
|
-
}
|
115
|
-
|
116
|
-
// Act
|
117
|
-
await sheet2DB.Range(rangeOptions);
|
118
|
-
|
119
|
-
// Assert
|
120
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
121
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
122
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
123
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/range/${rangeOptions.range}`);
|
124
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
125
|
-
expect(options.method).toBe('GET');
|
126
|
-
})
|
127
|
-
|
128
|
-
it('Search', async () => {
|
129
|
-
// Arrange
|
130
|
-
const searchOptions = {
|
131
|
-
sheet: 'Sheet1',
|
132
|
-
query: 'name=John',
|
133
|
-
or: true,
|
134
|
-
};
|
135
|
-
|
136
|
-
// Act
|
137
|
-
await sheet2DB.Search(searchOptions);
|
138
|
-
|
139
|
-
// Assert
|
140
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
141
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
142
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
143
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/search_or/${searchOptions.sheet}`);
|
144
|
-
expect(decodeURIComponent(calledUrl.search)).toBe(`?${searchOptions.query}`);
|
145
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
146
|
-
expect(options.method).toBe('GET');
|
147
|
-
});
|
148
|
-
|
149
|
-
it('Insert', async () => {
|
150
|
-
// Arrange
|
151
|
-
const insertOptions = {
|
152
|
-
data: { name: 'John', age: 30 },
|
153
|
-
sheet: 'Sheet1',
|
154
|
-
};
|
155
|
-
|
156
|
-
// Act
|
157
|
-
await sheet2DB.Insert(insertOptions);
|
158
|
-
|
159
|
-
// Assert
|
160
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
161
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
162
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
163
|
-
expect(calledUrl.pathname).toBe('/v1/test-spreadsheet-id');
|
164
|
-
expect(calledUrl.searchParams.get('sheet')).toBe(insertOptions.sheet);
|
165
|
-
const otherOptions = mockFetch.mock.calls[0][1] as RequestInit;
|
166
|
-
expect(otherOptions.body).toBe(JSON.stringify(insertOptions.data))
|
167
|
-
expect(otherOptions.method).toBe('POST');
|
168
|
-
});
|
169
|
-
|
170
|
-
it('UpdateRow', async () => {
|
171
|
-
// Arrange
|
172
|
-
const updateRowOptions = {
|
173
|
-
data: { name: 'John', age: 30 },
|
174
|
-
row: 4,
|
175
|
-
sheet: 'Sheet1',
|
176
|
-
};
|
177
|
-
|
178
|
-
// Act
|
179
|
-
await sheet2DB.UpdateRow(updateRowOptions);
|
180
|
-
|
181
|
-
// Assert
|
182
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
183
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
184
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
185
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/row/${updateRowOptions.row}`);
|
186
|
-
expect(calledUrl.searchParams.get('sheet')).toBe(updateRowOptions.sheet);
|
187
|
-
const otherOptions = mockFetch.mock.calls[0][1] as RequestInit;
|
188
|
-
expect(otherOptions.body).toBe(JSON.stringify(updateRowOptions.data))
|
189
|
-
expect(otherOptions.method).toBe('PATCH');
|
190
|
-
});
|
191
|
-
|
192
|
-
it('UpdateWithQuery', async () => {
|
193
|
-
// Arrange
|
194
|
-
const updateWithQueryOptions = {
|
195
|
-
sheet: 'Sheet1',
|
196
|
-
query: 'name=John',
|
197
|
-
data: { name: 'John Doe' },
|
198
|
-
};
|
199
|
-
|
200
|
-
// Act
|
201
|
-
await sheet2DB.UpdateWithQuery(updateWithQueryOptions);
|
202
|
-
|
203
|
-
// Assert
|
204
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
205
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
206
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
207
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/${updateWithQueryOptions.sheet}`);
|
208
|
-
expect(decodeURIComponent(calledUrl.search)).toBe(`?${updateWithQueryOptions.query}`);
|
209
|
-
const otherOptions = mockFetch.mock.calls[0][1] as RequestInit;
|
210
|
-
expect(otherOptions.body).toBe(JSON.stringify(updateWithQueryOptions.data))
|
211
|
-
expect(otherOptions.method).toBe('PATCH');
|
212
|
-
});
|
213
|
-
|
214
|
-
it('BatchUpdate', async () => {
|
215
|
-
// Arrange
|
216
|
-
const batchUpdateOptions = {
|
217
|
-
sheet: 'Sheet1',
|
218
|
-
batches: [
|
219
|
-
{ query: "name=John", record: { age: 21 } },
|
220
|
-
{ query: "age>24", record: { name: "Jane Doe" } }
|
221
|
-
],
|
222
|
-
};
|
223
|
-
|
224
|
-
// Act
|
225
|
-
await sheet2DB.BatchUpdate(batchUpdateOptions);
|
226
|
-
|
227
|
-
// Assert
|
228
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
229
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
230
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
231
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/batch/${batchUpdateOptions.sheet}`);
|
232
|
-
const otherOptions = mockFetch.mock.calls[0][1] as RequestInit;
|
233
|
-
expect(otherOptions.body).toBe(JSON.stringify(batchUpdateOptions.batches))
|
234
|
-
expect(otherOptions.method).toBe('PATCH');
|
235
|
-
})
|
236
|
-
|
237
|
-
it('DeleteRow', async () => {
|
238
|
-
// Arrange
|
239
|
-
const deleteRowOptions = {
|
240
|
-
row: 4,
|
241
|
-
sheet: 'Sheet1',
|
242
|
-
};
|
243
|
-
|
244
|
-
// Act
|
245
|
-
await sheet2DB.DeleteRow(deleteRowOptions);
|
246
|
-
|
247
|
-
// Assert
|
248
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
249
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
250
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
251
|
-
expect(calledUrl.pathname).toBe('/v1/test-spreadsheet-id/row/4');
|
252
|
-
expect(calledUrl.searchParams.get('sheet')).toBe('Sheet1');
|
253
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
254
|
-
expect(options.method).toBe('DELETE');
|
255
|
-
});
|
256
|
-
|
257
|
-
it('DeleteWithQuery', async () => {
|
258
|
-
// Arrange
|
259
|
-
const deleteWithQueryOptions = {
|
260
|
-
query: "id=0",
|
261
|
-
sheet: 'Sheet1',
|
262
|
-
};
|
263
|
-
|
264
|
-
// Act
|
265
|
-
await sheet2DB.DeleteWithQuery(deleteWithQueryOptions);
|
266
|
-
|
267
|
-
// Assert
|
268
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
269
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
270
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
271
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/${deleteWithQueryOptions.sheet}`);
|
272
|
-
expect(decodeURIComponent(calledUrl.search)).toBe(`?${deleteWithQueryOptions.query}`);
|
273
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
274
|
-
expect(options.method).toBe('DELETE');
|
275
|
-
})
|
276
|
-
|
277
|
-
it('Clear', async () => {
|
278
|
-
// Arrange
|
279
|
-
const clearOptions = {
|
280
|
-
sheet: 'Sheet1',
|
281
|
-
};
|
282
|
-
|
283
|
-
// Act
|
284
|
-
await sheet2DB.Clear(clearOptions);
|
285
|
-
|
286
|
-
// Assert
|
287
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
288
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
289
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
290
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/clear/${clearOptions.sheet}`);
|
291
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
292
|
-
expect(options.method).toBe('DELETE');
|
293
|
-
})
|
294
|
-
|
295
|
-
it('Create Sheet', async () => {
|
296
|
-
// Arrange
|
297
|
-
const createSheetOptions = {
|
298
|
-
title: 'Sheet1',
|
299
|
-
};
|
300
|
-
|
301
|
-
// Act
|
302
|
-
await sheet2DB.CreateSheet(createSheetOptions);
|
303
|
-
|
304
|
-
// Assert
|
305
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
306
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
307
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
308
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/sheet`);
|
309
|
-
expect(calledUrl.searchParams.get('title')).toBe(createSheetOptions.title);
|
310
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
311
|
-
expect(options.method).toBe('POST');
|
312
|
-
})
|
313
|
-
|
314
|
-
it('Delete Sheet', async ()=>{
|
315
|
-
// Arrange
|
316
|
-
const deleteSheetOptions = {
|
317
|
-
sheet: 'Sheet1',
|
318
|
-
};
|
319
|
-
|
320
|
-
// Act
|
321
|
-
await sheet2DB.DeleteSheet(deleteSheetOptions);
|
322
|
-
|
323
|
-
// Assert
|
324
|
-
expect(mockFetch).toHaveBeenCalledTimes(1);
|
325
|
-
const calledUrl = new URL(mockFetch.mock.calls[0][0] as string);
|
326
|
-
expect(calledUrl.origin).toBe('https://api.sheet2db.com');
|
327
|
-
expect(calledUrl.pathname).toBe(`/v1/test-spreadsheet-id/sheet/${deleteSheetOptions.sheet}`);
|
328
|
-
const options = mockFetch.mock.calls[0][1] as RequestInit;
|
329
|
-
expect(options.method).toBe('DELETE');
|
330
|
-
})
|
331
|
-
|
332
|
-
|
333
|
-
});
|
334
|
-
|