@ariadng/sheets 0.1.1 → 0.2.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.
Files changed (72) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +427 -300
  3. package/bin/sheets.js +3 -0
  4. package/dist/api/index.d.ts +31 -0
  5. package/dist/api/index.d.ts.map +1 -0
  6. package/dist/api/index.js +87 -0
  7. package/dist/api/index.js.map +1 -0
  8. package/dist/auth/constants.d.ts +13 -0
  9. package/dist/auth/constants.d.ts.map +1 -0
  10. package/dist/auth/constants.js +21 -0
  11. package/dist/auth/constants.js.map +1 -0
  12. package/dist/auth/index.d.ts +13 -0
  13. package/dist/auth/index.d.ts.map +1 -0
  14. package/dist/auth/index.js +22 -0
  15. package/dist/auth/index.js.map +1 -0
  16. package/dist/auth/oauth.d.ts +11 -0
  17. package/dist/auth/oauth.d.ts.map +1 -0
  18. package/dist/auth/oauth.js +14 -0
  19. package/dist/auth/oauth.js.map +1 -0
  20. package/dist/auth/service-account.d.ts +18 -0
  21. package/dist/auth/service-account.d.ts.map +1 -0
  22. package/dist/auth/service-account.js +92 -0
  23. package/dist/auth/service-account.js.map +1 -0
  24. package/dist/auth/user-auth.d.ts +24 -0
  25. package/dist/auth/user-auth.d.ts.map +1 -0
  26. package/dist/auth/user-auth.js +230 -0
  27. package/dist/auth/user-auth.js.map +1 -0
  28. package/dist/cli.d.ts +7 -0
  29. package/dist/cli.d.ts.map +1 -0
  30. package/dist/cli.js +318 -0
  31. package/dist/cli.js.map +1 -0
  32. package/dist/http/index.d.ts +19 -0
  33. package/dist/http/index.d.ts.map +1 -0
  34. package/dist/http/index.js +68 -0
  35. package/dist/http/index.js.map +1 -0
  36. package/dist/index.d.ts +11 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +12 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/types/index.d.ts +133 -0
  41. package/dist/types/index.d.ts.map +1 -0
  42. package/dist/types/index.js +16 -0
  43. package/dist/types/index.js.map +1 -0
  44. package/package.json +58 -79
  45. package/dist/advanced/index.d.ts +0 -5
  46. package/dist/advanced/index.d.ts.map +0 -1
  47. package/dist/advanced/index.js +0 -1063
  48. package/dist/advanced/index.mjs +0 -1005
  49. package/dist/advanced/metrics.d.ts +0 -50
  50. package/dist/advanced/metrics.d.ts.map +0 -1
  51. package/dist/advanced/rate-limit.d.ts +0 -27
  52. package/dist/advanced/rate-limit.d.ts.map +0 -1
  53. package/dist/core/auth.d.ts +0 -33
  54. package/dist/core/auth.d.ts.map +0 -1
  55. package/dist/core/client.d.ts +0 -35
  56. package/dist/core/client.d.ts.map +0 -1
  57. package/dist/core/errors.d.ts +0 -11
  58. package/dist/core/errors.d.ts.map +0 -1
  59. package/dist/core/index.d.ts +0 -6
  60. package/dist/core/index.d.ts.map +0 -1
  61. package/dist/core/index.js +0 -315
  62. package/dist/core/index.mjs +0 -271
  63. package/dist/plus/batch.d.ts +0 -25
  64. package/dist/plus/batch.d.ts.map +0 -1
  65. package/dist/plus/cache.d.ts +0 -19
  66. package/dist/plus/cache.d.ts.map +0 -1
  67. package/dist/plus/index.d.ts +0 -7
  68. package/dist/plus/index.d.ts.map +0 -1
  69. package/dist/plus/index.js +0 -742
  70. package/dist/plus/index.mjs +0 -691
  71. package/dist/plus/types.d.ts +0 -39
  72. package/dist/plus/types.d.ts.map +0 -1
@@ -1,271 +0,0 @@
1
- // src/core/client.ts
2
- import { google } from "googleapis";
3
-
4
- // src/core/errors.ts
5
- var GoogleSheetsError = class extends Error {
6
- constructor(originalError) {
7
- const message = originalError.response?.data?.error?.message || originalError.message || "Unknown error";
8
- super(message);
9
- this.name = "GoogleSheetsError";
10
- this.code = originalError.response?.status || originalError.code;
11
- this.originalError = originalError;
12
- const retryableCodes = [429, 500, 502, 503, 504, "ECONNRESET", "ETIMEDOUT", "ENOTFOUND"];
13
- this.isRetryable = retryableCodes.includes(this.code);
14
- if (originalError.stack) {
15
- this.stack = originalError.stack;
16
- }
17
- }
18
- /**
19
- * Check if error is a rate limit error
20
- */
21
- isRateLimitError() {
22
- return this.code === 429;
23
- }
24
- /**
25
- * Check if error is a permission error
26
- */
27
- isPermissionError() {
28
- return this.code === 403;
29
- }
30
- /**
31
- * Check if error is a not found error
32
- */
33
- isNotFoundError() {
34
- return this.code === 404;
35
- }
36
- /**
37
- * Get a user-friendly error message
38
- */
39
- getUserMessage() {
40
- if (this.isPermissionError()) {
41
- return "Permission denied. Please ensure the spreadsheet is shared with the service account or you have proper OAuth permissions.";
42
- }
43
- if (this.isRateLimitError()) {
44
- return "Rate limit exceeded. Please wait before making more requests.";
45
- }
46
- if (this.isNotFoundError()) {
47
- return "Spreadsheet or range not found. Please check the ID and range are correct.";
48
- }
49
- return this.message;
50
- }
51
- };
52
-
53
- // src/core/client.ts
54
- var GoogleSheetsCore = class {
55
- constructor(config) {
56
- this.sheets = google.sheets({
57
- version: "v4",
58
- auth: config.auth
59
- });
60
- this.retryConfig = {
61
- maxAttempts: config.retryConfig?.maxAttempts ?? 3,
62
- maxDelay: config.retryConfig?.maxDelay ?? 1e4,
63
- initialDelay: config.retryConfig?.initialDelay ?? 1e3
64
- };
65
- }
66
- /**
67
- * Read values from a spreadsheet
68
- * @param spreadsheetId The spreadsheet ID
69
- * @param range A1 notation range (e.g., 'Sheet1!A1:B10')
70
- * @returns 2D array of values
71
- */
72
- async read(spreadsheetId, range) {
73
- return this.withRetry(async () => {
74
- const response = await this.sheets.spreadsheets.values.get({
75
- spreadsheetId,
76
- range
77
- });
78
- return response.data.values || [];
79
- });
80
- }
81
- /**
82
- * Write values to a spreadsheet
83
- * @param spreadsheetId The spreadsheet ID
84
- * @param range A1 notation range
85
- * @param values 2D array of values to write
86
- */
87
- async write(spreadsheetId, range, values) {
88
- return this.withRetry(async () => {
89
- const response = await this.sheets.spreadsheets.values.update({
90
- spreadsheetId,
91
- range,
92
- valueInputOption: "USER_ENTERED",
93
- requestBody: { values }
94
- });
95
- return response.data;
96
- });
97
- }
98
- /**
99
- * Append values to a spreadsheet
100
- */
101
- async append(spreadsheetId, range, values) {
102
- return this.withRetry(async () => {
103
- const response = await this.sheets.spreadsheets.values.append({
104
- spreadsheetId,
105
- range,
106
- valueInputOption: "USER_ENTERED",
107
- insertDataOption: "INSERT_ROWS",
108
- requestBody: { values }
109
- });
110
- return response.data;
111
- });
112
- }
113
- /**
114
- * Clear values in a range
115
- */
116
- async clear(spreadsheetId, range) {
117
- return this.withRetry(async () => {
118
- const response = await this.sheets.spreadsheets.values.clear({
119
- spreadsheetId,
120
- range
121
- });
122
- return response.data;
123
- });
124
- }
125
- /**
126
- * Batch read multiple ranges
127
- */
128
- async batchRead(spreadsheetId, ranges) {
129
- return this.withRetry(async () => {
130
- const response = await this.sheets.spreadsheets.values.batchGet({
131
- spreadsheetId,
132
- ranges
133
- });
134
- return response.data.valueRanges || [];
135
- });
136
- }
137
- /**
138
- * Batch update multiple ranges
139
- */
140
- async batchWrite(spreadsheetId, data) {
141
- return this.withRetry(async () => {
142
- const response = await this.sheets.spreadsheets.values.batchUpdate({
143
- spreadsheetId,
144
- requestBody: {
145
- data: data.map((item) => ({
146
- range: item.range,
147
- values: item.values
148
- })),
149
- valueInputOption: "USER_ENTERED"
150
- }
151
- });
152
- return response.data;
153
- });
154
- }
155
- /**
156
- * Batch clear multiple ranges
157
- */
158
- async batchClear(spreadsheetId, ranges) {
159
- return this.withRetry(async () => {
160
- const response = await this.sheets.spreadsheets.values.batchClear({
161
- spreadsheetId,
162
- requestBody: { ranges }
163
- });
164
- return response.data;
165
- });
166
- }
167
- /**
168
- * Get spreadsheet metadata
169
- */
170
- async getSpreadsheet(spreadsheetId) {
171
- return this.withRetry(async () => {
172
- const response = await this.sheets.spreadsheets.get({
173
- spreadsheetId
174
- });
175
- return response.data;
176
- });
177
- }
178
- /**
179
- * Get the underlying Sheets API instance for advanced usage
180
- */
181
- getApi() {
182
- return this.sheets;
183
- }
184
- /**
185
- * Simple exponential backoff retry logic
186
- */
187
- async withRetry(fn) {
188
- let lastError;
189
- for (let attempt = 0; attempt < this.retryConfig.maxAttempts; attempt++) {
190
- try {
191
- return await fn();
192
- } catch (error) {
193
- lastError = error;
194
- if (!this.isRetryable(error) || attempt === this.retryConfig.maxAttempts - 1) {
195
- throw new GoogleSheetsError(error);
196
- }
197
- const baseDelay = Math.min(
198
- this.retryConfig.initialDelay * Math.pow(2, attempt),
199
- this.retryConfig.maxDelay
200
- );
201
- const jitter = Math.random() * 1e3;
202
- const delay = baseDelay + jitter;
203
- await new Promise((resolve) => setTimeout(resolve, delay));
204
- }
205
- }
206
- throw new GoogleSheetsError(lastError);
207
- }
208
- isRetryable(error) {
209
- const retryableCodes = [429, 500, 502, 503, 504];
210
- const retryableErrors = ["ECONNRESET", "ETIMEDOUT", "ENOTFOUND"];
211
- return retryableCodes.includes(error.code) || retryableCodes.includes(error.response?.status) || retryableErrors.includes(error.code);
212
- }
213
- };
214
-
215
- // src/core/auth.ts
216
- import { GoogleAuth, OAuth2Client, JWT } from "google-auth-library";
217
- import * as fs from "fs/promises";
218
- async function createServiceAccountAuth(keyFile) {
219
- const key = typeof keyFile === "string" ? JSON.parse(await fs.readFile(keyFile, "utf8")) : keyFile;
220
- const jwt = new JWT({
221
- email: key.client_email,
222
- key: key.private_key,
223
- scopes: ["https://www.googleapis.com/auth/spreadsheets"]
224
- });
225
- return jwt;
226
- }
227
- async function createOAuth2Client(credentials, tokenPath) {
228
- const client = new OAuth2Client(
229
- credentials.client_id,
230
- credentials.client_secret,
231
- credentials.redirect_uris[0]
232
- );
233
- if (tokenPath) {
234
- try {
235
- const token = JSON.parse(await fs.readFile(tokenPath, "utf8"));
236
- client.setCredentials(token);
237
- } catch {
238
- }
239
- }
240
- return client;
241
- }
242
- function generateAuthUrl(client, scopes = ["https://www.googleapis.com/auth/spreadsheets"]) {
243
- return client.generateAuthUrl({
244
- access_type: "offline",
245
- scope: scopes
246
- });
247
- }
248
- async function getTokenFromCode(client, code) {
249
- const { tokens } = await client.getToken(code);
250
- client.setCredentials(tokens);
251
- return tokens;
252
- }
253
- async function saveToken(tokens, path) {
254
- await fs.writeFile(path, JSON.stringify(tokens, null, 2));
255
- }
256
- function createAuth(auth) {
257
- if (auth instanceof GoogleAuth || auth instanceof OAuth2Client || auth instanceof JWT) {
258
- return auth;
259
- }
260
- return createServiceAccountAuth(auth);
261
- }
262
- export {
263
- GoogleSheetsCore,
264
- GoogleSheetsError,
265
- createAuth,
266
- createOAuth2Client,
267
- createServiceAccountAuth,
268
- generateAuthUrl,
269
- getTokenFromCode,
270
- saveToken
271
- };
@@ -1,25 +0,0 @@
1
- import { GoogleSheetsCore } from '../core';
2
- import { sheets_v4 } from 'googleapis';
3
- export interface BatchWriteOperation {
4
- range: string;
5
- values: any[][];
6
- }
7
- export declare class BatchOperations {
8
- private client;
9
- private readonly MAX_BATCH_SIZE;
10
- constructor(client: GoogleSheetsCore);
11
- batchWrite(spreadsheetId: string, operations: BatchWriteOperation[]): Promise<sheets_v4.Schema$BatchUpdateValuesResponse[]>;
12
- batchClear(spreadsheetId: string, ranges: string[]): Promise<sheets_v4.Schema$BatchClearValuesResponse[]>;
13
- batchRead(spreadsheetId: string, ranges: string[]): Promise<sheets_v4.Schema$ValueRange[]>;
14
- executeBatch(spreadsheetId: string, operations: {
15
- writes?: BatchWriteOperation[];
16
- clears?: string[];
17
- reads?: string[];
18
- }): Promise<{
19
- writeResults?: sheets_v4.Schema$BatchUpdateValuesResponse[];
20
- clearResults?: sheets_v4.Schema$BatchClearValuesResponse[];
21
- readResults?: sheets_v4.Schema$ValueRange[];
22
- }>;
23
- private chunk;
24
- }
25
- //# sourceMappingURL=batch.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"batch.d.ts","sourceRoot":"","sources":["../../src/plus/batch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,WAAW,mBAAmB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC;CAChB;AAED,qBAAa,eAAe;IAIf,OAAO,CAAC,MAAM;IAF1B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAO;gBAElB,MAAM,EAAE,gBAAgB;IAMtC,UAAU,CACf,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE,mBAAmB,EAAE,GAC/B,OAAO,CAAC,SAAS,CAAC,gCAAgC,EAAE,CAAC;IAelD,UAAU,CACf,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,SAAS,CAAC,+BAA+B,EAAE,CAAC;IAejD,SAAS,CACd,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,SAAS,CAAC,iBAAiB,EAAE,CAAC;IAenC,YAAY,CACjB,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE;QACX,MAAM,CAAC,EAAE,mBAAmB,EAAE,CAAC;QAC/B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;QAClB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;KACjB,GACC,OAAO,CAAC;QACV,YAAY,CAAC,EAAE,SAAS,CAAC,gCAAgC,EAAE,CAAC;QAC5D,YAAY,CAAC,EAAE,SAAS,CAAC,+BAA+B,EAAE,CAAC;QAC3D,WAAW,CAAC,EAAE,SAAS,CAAC,iBAAiB,EAAE,CAAC;KAC5C,CAAC;IAyCF,OAAO,CAAC,KAAK;CAOb"}
@@ -1,19 +0,0 @@
1
- import { GoogleSheetsCore } from '../core';
2
- export interface CacheConfig {
3
- ttlSeconds?: number;
4
- maxEntries?: number;
5
- }
6
- export declare class SimpleCache {
7
- private cache;
8
- private config;
9
- constructor(config?: CacheConfig);
10
- get(key: string): any | null;
11
- set(key: string, value: any, ttlOverride?: number): void;
12
- invalidate(pattern?: string): void;
13
- size(): number;
14
- clear(): void;
15
- }
16
- export declare function withCache(client: GoogleSheetsCore, config?: CacheConfig): GoogleSheetsCore & {
17
- cache: SimpleCache;
18
- };
19
- //# sourceMappingURL=cache.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/plus/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAG3C,MAAM,WAAW,WAAW;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAOD,qBAAa,WAAW;IACvB,OAAO,CAAC,KAAK,CAAiC;IAC9C,OAAO,CAAC,MAAM,CAAwB;gBAE1B,MAAM,CAAC,EAAE,WAAW;IAOhC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,IAAI;IAa5B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI;IAgBxD,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAelC,IAAI,IAAI,MAAM;IAId,KAAK,IAAI,IAAI;CAGb;AAKD,wBAAgB,SAAS,CACxB,MAAM,EAAE,gBAAgB,EACxB,MAAM,CAAC,EAAE,WAAW,GAClB,gBAAgB,GAAG;IAAE,KAAK,EAAE,WAAW,CAAA;CAAE,CAmH3C"}
@@ -1,7 +0,0 @@
1
- export { BatchOperations } from './batch';
2
- export type { BatchWriteOperation } from './batch';
3
- export { SimpleCache, withCache } from './cache';
4
- export type { CacheConfig } from './cache';
5
- export { A1, TypedSheets, Parsers, Serializers } from './types';
6
- export * from '../core';
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plus/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACjD,YAAY,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,EAAE,EAAE,WAAW,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAGhE,cAAc,SAAS,CAAC"}