@gzhangx/googleapi 0.0.76 → 0.0.77

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 (40) hide show
  1. package/lib/google/drive.d.ts +15 -15
  2. package/lib/google/drive.js +54 -45
  3. package/lib/google/drive.js.map +1 -1
  4. package/lib/google/googleApiServiceAccount.d.ts +131 -132
  5. package/lib/google/googleApiServiceAccount.js +397 -551
  6. package/lib/google/googleApiServiceAccount.js.map +1 -1
  7. package/lib/googleApi.d.ts +58 -58
  8. package/lib/googleApi.js +325 -459
  9. package/lib/googleApi.js.map +1 -1
  10. package/lib/httpRequest.d.ts +24 -25
  11. package/lib/httpRequest.js +138 -184
  12. package/lib/httpRequest.js.map +1 -1
  13. package/lib/index.d.ts +24 -24
  14. package/lib/index.js +62 -55
  15. package/lib/index.js.map +1 -1
  16. package/lib/jwt.d.ts +11 -11
  17. package/lib/jwt.js +45 -46
  18. package/lib/jwt.js.map +1 -1
  19. package/lib/msGraph/msExcell.d.ts +47 -47
  20. package/lib/msGraph/msExcell.js +69 -157
  21. package/lib/msGraph/msExcell.js.map +1 -1
  22. package/lib/msGraph/msauth.d.ts +91 -91
  23. package/lib/msGraph/msauth.js +309 -533
  24. package/lib/msGraph/msauth.js.map +1 -1
  25. package/lib/msGraph/msdir.d.ts +124 -125
  26. package/lib/msGraph/msdir.js +171 -263
  27. package/lib/msGraph/msdir.js.map +1 -1
  28. package/lib/msGraph/types.d.ts +4 -4
  29. package/lib/msGraph/types.js +2 -2
  30. package/lib/tests/gtest.d.ts +1 -1
  31. package/lib/tests/gtest.js +170 -234
  32. package/lib/tests/gtest.js.map +1 -1
  33. package/lib/tests/mstest.d.ts +1 -1
  34. package/lib/tests/mstest.js +184 -269
  35. package/lib/tests/mstest.js.map +1 -1
  36. package/lib/tsconfig.tsbuildinfo +1 -1
  37. package/lib/util.d.ts +9 -9
  38. package/lib/util.js +95 -96
  39. package/lib/util.js.map +1 -1
  40. package/package.json +3 -3
package/lib/googleApi.js CHANGED
@@ -1,460 +1,326 @@
1
- "use strict";
2
- //rootUrl = 'https://accounts.google.com/o/oauth2/v2/auth';
3
- //opts = { access_type: 'offline', scope: 'https://www.googleapis.com/auth/spreadsheets', response_type: 'code', client_id: 'client_id', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob' }
4
- //return rootUrl + '?' + querystring.stringify(opts);
5
- //'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&response_type=code&client_id=client_id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob'
6
- var __assign = (this && this.__assign) || function () {
7
- __assign = Object.assign || function(t) {
8
- for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
- t[p] = s[p];
12
- }
13
- return t;
14
- };
15
- return __assign.apply(this, arguments);
16
- };
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- Object.defineProperty(exports, "__esModule", { value: true });
54
- exports.test = exports.getClientByEnv = exports.getClientCredsByEnv = exports.getClient = exports.getTokenFromCode = void 0;
55
- //import axios, { Method } from 'axios';
56
- var httpRequest_1 = require("./httpRequest");
57
- var util_1 = require("./util");
58
- ;
59
- //old, does not need refresh token
60
- function getTokenFromCode(creds, code, redirect_uri) {
61
- return __awaiter(this, void 0, void 0, function () {
62
- var client_id, client_secret, dataStr, tokenBody;
63
- return __generator(this, function (_a) {
64
- switch (_a.label) {
65
- case 0:
66
- client_id = creds.client_id, client_secret = creds.client_secret;
67
- dataStr = (0, util_1.getFormData)({
68
- client_secret: client_secret,
69
- client_id: client_id,
70
- code: code,
71
- redirect_uri: redirect_uri,
72
- grant_type: 'authorization_code'
73
- });
74
- return [4 /*yield*/, (0, httpRequest_1.doHttpRequest)({
75
- url: 'https://oauth2.googleapis.com/token',
76
- method: 'POST',
77
- data: dataStr,
78
- headers: { "Content-Type": "application/x-www-form-urlencoded" }
79
- }).then(function (r) {
80
- return r;
81
- })];
82
- case 1:
83
- tokenBody = _a.sent();
84
- return [2 /*return*/, tokenBody];
85
- }
86
- });
87
- });
88
- }
89
- exports.getTokenFromCode = getTokenFromCode;
90
- function betterErr(desc) {
91
- return function (err) {
92
- var webRsp = err.response;
93
- if (webRsp) {
94
- throw __assign({ desc: desc }, (0, util_1.pick)(webRsp, ['data', 'status', 'statusText', 'headers', 'config.url']));
95
- }
96
- throw err;
97
- };
98
- }
99
- function doRefresh(creds) {
100
- return __awaiter(this, void 0, void 0, function () {
101
- var refresh_token, client_id, client_secret, dataStr, refreshBody, access_token, expires_in, token_type, doOp, doPost, doBatchUpdate, append, read;
102
- var _this = this;
103
- return __generator(this, function (_a) {
104
- switch (_a.label) {
105
- case 0:
106
- refresh_token = creds.refresh_token, client_id = creds.client_id, client_secret = creds.client_secret;
107
- if (!client_id || !client_secret)
108
- throw "doRefresh needs client_id and client_secret in creds";
109
- if (!refresh_token)
110
- throw "missing refresh_token from creds";
111
- dataStr = (0, util_1.getFormData)({
112
- client_secret: client_secret,
113
- client_id: client_id,
114
- refresh_token: refresh_token,
115
- grant_type: 'refresh_token'
116
- });
117
- return [4 /*yield*/, (0, httpRequest_1.doHttpRequest)({ url: 'https://oauth2.googleapis.com/token', data: dataStr,
118
- method: 'POST',
119
- headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(function (r) {
120
- return r.data;
121
- }).catch(betterErr("refreshToken https://oauth2.googleapis.com/token"))];
122
- case 1:
123
- refreshBody = _a.sent();
124
- access_token = refreshBody.access_token, expires_in = refreshBody.expires_in, token_type = refreshBody.token_type;
125
- doOp = function (op, id, postFix, data) {
126
- var url = "https://sheets.googleapis.com/v4/spreadsheets/".concat(id).concat(postFix);
127
- return (0, httpRequest_1.doHttpRequest)({
128
- url: url,
129
- headers: {
130
- "Content-Type": "application/json",
131
- "Authorization": "Bearer ".concat(access_token),
132
- },
133
- method: op,
134
- data: data,
135
- }).then(function (r) {
136
- return (r.data);
137
- }).catch(betterErr("doOps error ".concat(url)));
138
- };
139
- doPost = function (id, postFix, data) { return doOp('POST', id, postFix, data); };
140
- doBatchUpdate = function (id, data) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
141
- return [2 /*return*/, doPost(id, ':batchUpdate', data)];
142
- }); }); };
143
- append = function (_a, data, opts) {
144
- var id = _a.id, range = _a.range;
145
- return __awaiter(_this, void 0, void 0, function () {
146
- return __generator(this, function (_b) {
147
- switch (_b.label) {
148
- case 0:
149
- if (!opts) {
150
- opts = {};
151
- }
152
- if (!opts.valueInputOption)
153
- opts.valueInputOption = 'USER_ENTERED';
154
- return [4 /*yield*/, doPost(id, "/values/".concat(range, ":append?").concat((0, util_1.getFormData)(opts)), { values: data })];
155
- case 1: return [2 /*return*/, _b.sent()];
156
- }
157
- });
158
- });
159
- };
160
- read = function (_a) {
161
- var id = _a.id, range = _a.range;
162
- return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
163
- return [2 /*return*/, doOp('GET', id, "/values/".concat(range))];
164
- }); });
165
- };
166
- return [2 /*return*/, {
167
- access_token: access_token,
168
- expires_on: new Date().getTime() + (expires_in * 1000 - 2000),
169
- token_type: token_type,
170
- doBatchUpdate: doBatchUpdate,
171
- append: append,
172
- read: read,
173
- getSheetOps: function (id) {
174
- var getInfo = function () { return doOp('GET', id, ''); };
175
- var createSheet = function (sheetId, title) { return __awaiter(_this, void 0, void 0, function () {
176
- return __generator(this, function (_a) {
177
- return [2 /*return*/, doBatchUpdate(id, {
178
- requests: [
179
- {
180
- addSheet: {
181
- properties: {
182
- sheetId: sheetId,
183
- title: title,
184
- }
185
- }
186
- }
187
- ]
188
- })];
189
- });
190
- }); };
191
- var sheetInfo = function () { return __awaiter(_this, void 0, void 0, function () {
192
- var sheetInfos;
193
- return __generator(this, function (_a) {
194
- switch (_a.label) {
195
- case 0: return [4 /*yield*/, getInfo()];
196
- case 1:
197
- sheetInfos = _a.sent();
198
- return [2 /*return*/, sheetInfos.sheets.map(function (s) {
199
- var props = s.properties;
200
- return __assign({ sheetId: props.sheetId, title: props.title, index: props.index }, props.gridProperties);
201
- })];
202
- }
203
- });
204
- }); };
205
- return {
206
- doBatchUpdate: function (data) { return doBatchUpdate(id, data); },
207
- append: function (range, data, ops) { return append({ id: id, range: range }, data, ops); },
208
- read: function (range) { return read({ id: id, range: range }); },
209
- sheetInfo: sheetInfo,
210
- createSheet: createSheet,
211
- updateValues: function (range, values, opts) {
212
- if (!opts) {
213
- opts = {
214
- valueInputOption: 'USER_ENTERED'
215
- };
216
- }
217
- if (!opts.valueInputOption)
218
- opts.valueInputOption = 'USER_ENTERED';
219
- return doOp('PUT', id, "/values/".concat(encodeURIComponent(range), "?").concat((0, util_1.getFormData)(opts)), {
220
- values: values,
221
- });
222
- },
223
- addSheet: function (title) { return __awaiter(_this, void 0, void 0, function () {
224
- var sheetsInfo, found, newId, existing;
225
- return __generator(this, function (_a) {
226
- switch (_a.label) {
227
- case 0: return [4 /*yield*/, sheetInfo()];
228
- case 1:
229
- sheetsInfo = _a.sent();
230
- found = sheetsInfo.find(function (s) { return s.title === title; });
231
- if (found) {
232
- return [2 /*return*/, {
233
- found: found,
234
- }];
235
- }
236
- newId = 0;
237
- for (;; newId++) {
238
- existing = sheetsInfo.find(function (s) { return s.sheetId === newId; });
239
- if (existing)
240
- continue;
241
- break;
242
- }
243
- return [2 /*return*/, createSheet(newId.toString(), title)];
244
- }
245
- });
246
- }); },
247
- };
248
- }
249
- }];
250
- }
251
- });
252
- });
253
- }
254
- var clients = {};
255
- function getClient(creds) {
256
- return __awaiter(this, void 0, void 0, function () {
257
- var name, client, now;
258
- return __generator(this, function (_a) {
259
- switch (_a.label) {
260
- case 0:
261
- name = creds.client_id;
262
- client = clients[name];
263
- now = new Date().getTime();
264
- if (!(!client || client.expires_on <= now)) return [3 /*break*/, 2];
265
- return [4 /*yield*/, doRefresh(creds)];
266
- case 1:
267
- client = _a.sent();
268
- if (!client)
269
- return [2 /*return*/, null];
270
- clients[name] = client;
271
- _a.label = 2;
272
- case 2: return [2 /*return*/, client];
273
- }
274
- });
275
- });
276
- }
277
- exports.getClient = getClient;
278
- function getClientCredsByEnv(envName) {
279
- var creds = {
280
- client_id: process.env["google_".concat(envName, "_client_id")],
281
- client_secret: process.env["google_".concat(envName, "_client_secret")],
282
- refresh_token: process.env["google_".concat(envName, "_refresh_token")],
283
- };
284
- return creds;
285
- }
286
- exports.getClientCredsByEnv = getClientCredsByEnv;
287
- function getClientByEnv(envName) {
288
- return __awaiter(this, void 0, void 0, function () {
289
- var creds;
290
- return __generator(this, function (_a) {
291
- creds = getClientCredsByEnv(envName);
292
- return [2 /*return*/, getClient(creds)];
293
- });
294
- });
295
- }
296
- exports.getClientByEnv = getClientByEnv;
297
- function test(d) {
298
- return __awaiter(this, void 0, void 0, function () {
299
- var cli, ops, rrr, id, upres, appres, rres, sheet, _a, _b;
300
- return __generator(this, function (_c) {
301
- switch (_c.label) {
302
- case 0: return [4 /*yield*/, getClientByEnv('gzperm')];
303
- case 1:
304
- cli = _c.sent();
305
- return [4 /*yield*/, cli.getSheetOps('1u_AR8y7iCRPGyDhdOb1cHhjL-vclCIxuLkMhIxd08mU')];
306
- case 2:
307
- ops = _c.sent();
308
- console.log('update val');
309
- return [4 /*yield*/, ops.updateValues('Sheet1!G18:G18', [['1']])];
310
- case 3:
311
- rrr = _c.sent();
312
- console.log('update done');
313
- console.log(rrr);
314
- if (d)
315
- return [2 /*return*/];
316
- if (!cli)
317
- return [2 /*return*/, console.log('failed to get client')];
318
- id = '1MO27odjCsxk6MWL0DygubU53hrtt3OB8SEnqjpUHJ-U';
319
- if (d)
320
- return [2 /*return*/];
321
- console.log('do batch update');
322
- return [4 /*yield*/, cli.doBatchUpdate(id, {
323
- "requests": [
324
- {
325
- "updateDimensionProperties": {
326
- "range": {
327
- "sheetId": 0,
328
- "dimension": "COLUMNS",
329
- "startIndex": 0,
330
- "endIndex": 1
331
- },
332
- "properties": {
333
- "pixelSize": 160
334
- },
335
- "fields": "pixelSize"
336
- },
337
- }
338
- ]
339
- })];
340
- case 4:
341
- _c.sent();
342
- console.log('do batch update 2');
343
- return [4 /*yield*/, cli.doBatchUpdate(id, {
344
- "requests": [
345
- {
346
- "updateCells": {
347
- "fields": "*",
348
- "range": {
349
- "sheetId": 0,
350
- "startColumnIndex": 0,
351
- "endColumnIndex": 10,
352
- "startRowIndex": 0,
353
- "endRowIndex": 10
354
- },
355
- "rows": [
356
- {
357
- "values": [
358
- {
359
- "userEnteredFormat": {
360
- "backgroundColor": {
361
- "blue": 10,
362
- "green": 10,
363
- "red": 255
364
- },
365
- "borders": {
366
- "bottom": {
367
- "style": "SOLID",
368
- "width": 8,
369
- "color": {
370
- "blue": 0,
371
- "green": 255,
372
- "red": 0
373
- }
374
- }
375
- }
376
- },
377
- "userEnteredValue": { "stringValue": "strstsdfasdf" }
378
- },
379
- {
380
- "userEnteredValue": { "stringValue": "col1" }
381
- }
382
- ]
383
- }
384
- ]
385
- }
386
- }
387
- ]
388
- })];
389
- case 5:
390
- upres = _c.sent();
391
- console.log(upres);
392
- console.log('append 1');
393
- return [4 /*yield*/, cli.append({
394
- id: id,
395
- range: "'Sheet1'!A1:B2"
396
- }, [
397
- ['aaa', 'bbb1']
398
- ])];
399
- case 6:
400
- appres = _c.sent();
401
- console.log('append res');
402
- console.log(appres);
403
- console.log('read');
404
- return [4 /*yield*/, cli.read({
405
- id: id,
406
- range: 'A1:B4'
407
- })];
408
- case 7:
409
- rres = _c.sent();
410
- console.log('read res');
411
- console.log(rres);
412
- sheet = cli.getSheetOps(id);
413
- sheet.doBatchUpdate({
414
- "requests": [
415
- {
416
- "updateDimensionProperties": {
417
- "range": {
418
- "sheetId": 0,
419
- "dimension": "COLUMNS",
420
- "startIndex": 0,
421
- "endIndex": 1
422
- },
423
- "properties": {
424
- "pixelSize": 100
425
- },
426
- "fields": "pixelSize"
427
- },
428
- }
429
- ]
430
- });
431
- return [4 /*yield*/, sheet.append('A:B', [['c', 'D']])];
432
- case 8:
433
- _c.sent();
434
- _b = (_a = console).log;
435
- return [4 /*yield*/, sheet.read('A1:B4')];
436
- case 9:
437
- _b.apply(_a, [_c.sent()]);
438
- return [2 /*return*/];
439
- }
440
- });
441
- });
442
- }
443
- exports.test = test;
444
- //test(true).catch(err => {
445
- ///console.log(err)
446
- //console.log(err.response.data);
447
- //})
448
- /*
449
- async function test2() {
450
- const creds = getClientCredsByEnv('gzperm');
451
- await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
452
- }
453
- console.log('invoking test2')
454
- test2().catch(err => {
455
- console.log('error');
456
- //console.log(err);
457
- console.log(err.response.text || err.response.data);
458
- })
459
- */
1
+ "use strict";
2
+ //rootUrl = 'https://accounts.google.com/o/oauth2/v2/auth';
3
+ //opts = { access_type: 'offline', scope: 'https://www.googleapis.com/auth/spreadsheets', response_type: 'code', client_id: 'client_id', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob' }
4
+ //return rootUrl + '?' + querystring.stringify(opts);
5
+ //'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&response_type=code&client_id=client_id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob'
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.getTokenFromCode = getTokenFromCode;
8
+ exports.getClient = getClient;
9
+ exports.getClientCredsByEnv = getClientCredsByEnv;
10
+ exports.getClientByEnv = getClientByEnv;
11
+ exports.test = test;
12
+ //import axios, { Method } from 'axios';
13
+ const httpRequest_1 = require("./httpRequest");
14
+ const util_1 = require("./util");
15
+ ;
16
+ //old, does not need refresh token
17
+ async function getTokenFromCode(creds, code, redirect_uri) {
18
+ const { client_id, client_secret } = creds;
19
+ const dataStr = (0, util_1.getFormData)({
20
+ client_secret,
21
+ client_id,
22
+ code,
23
+ redirect_uri,
24
+ grant_type: 'authorization_code'
25
+ });
26
+ const tokenBody = await (0, httpRequest_1.doHttpRequest)({
27
+ url: 'https://oauth2.googleapis.com/token',
28
+ method: 'POST',
29
+ data: dataStr,
30
+ headers: { "Content-Type": "application/x-www-form-urlencoded" }
31
+ }).then(r => {
32
+ return r;
33
+ });
34
+ return tokenBody;
35
+ }
36
+ function betterErr(desc) {
37
+ return err => {
38
+ const webRsp = err.response;
39
+ if (webRsp) {
40
+ throw {
41
+ desc,
42
+ ...(0, util_1.pick)(webRsp, ['data', 'status', 'statusText', 'headers', 'config.url'])
43
+ };
44
+ }
45
+ throw err;
46
+ };
47
+ }
48
+ async function doRefresh(creds) {
49
+ const { refresh_token, client_id, client_secret } = creds;
50
+ if (!client_id || !client_secret)
51
+ throw `doRefresh needs client_id and client_secret in creds`;
52
+ if (!refresh_token)
53
+ throw `missing refresh_token from creds`;
54
+ const dataStr = (0, util_1.getFormData)({
55
+ client_secret,
56
+ client_id,
57
+ refresh_token,
58
+ grant_type: 'refresh_token'
59
+ });
60
+ const refreshBody = await (0, httpRequest_1.doHttpRequest)({ url: 'https://oauth2.googleapis.com/token', data: dataStr,
61
+ method: 'POST',
62
+ headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(r => {
63
+ return r.data;
64
+ }).catch(betterErr(`refreshToken https://oauth2.googleapis.com/token`));
65
+ const { access_token, expires_in, token_type } = refreshBody;
66
+ const doOp = (op, id, postFix, data) => {
67
+ const url = `https://sheets.googleapis.com/v4/spreadsheets/${id}${postFix}`;
68
+ return (0, httpRequest_1.doHttpRequest)({
69
+ url,
70
+ headers: {
71
+ "Content-Type": "application/json",
72
+ "Authorization": `Bearer ${access_token}`,
73
+ },
74
+ method: op,
75
+ data,
76
+ }).then(r => {
77
+ return (r.data);
78
+ }).catch(betterErr(`doOps error ${url}`));
79
+ };
80
+ const doPost = (id, postFix, data) => doOp('POST', id, postFix, data);
81
+ const doBatchUpdate = async (id, data) => doPost(id, ':batchUpdate', data);
82
+ const append = async ({ id, range }, data, opts) => {
83
+ if (!opts) {
84
+ opts = {};
85
+ }
86
+ if (!opts.valueInputOption)
87
+ opts.valueInputOption = 'USER_ENTERED';
88
+ return await doPost(id, `/values/${range}:append?${(0, util_1.getFormData)(opts)}`, { values: data });
89
+ };
90
+ const read = async ({ id, range }) => doOp('GET', id, `/values/${range}`);
91
+ return {
92
+ access_token,
93
+ expires_on: new Date().getTime() + (expires_in * 1000 - 2000),
94
+ token_type,
95
+ doBatchUpdate,
96
+ append,
97
+ read,
98
+ getSheetOps: id => {
99
+ const getInfo = () => doOp('GET', id, '');
100
+ const createSheet = async (sheetId, title) => {
101
+ return doBatchUpdate(id, {
102
+ requests: [
103
+ {
104
+ addSheet: {
105
+ properties: {
106
+ sheetId,
107
+ title,
108
+ }
109
+ }
110
+ }
111
+ ]
112
+ });
113
+ };
114
+ const sheetInfo = async () => {
115
+ const sheetInfos = await getInfo();
116
+ return sheetInfos.sheets.map(s => {
117
+ const props = s.properties;
118
+ return {
119
+ sheetId: props.sheetId,
120
+ title: props.title,
121
+ index: props.index, //not important,
122
+ ...props.gridProperties, //rowCount, columnCount
123
+ };
124
+ });
125
+ };
126
+ return {
127
+ doBatchUpdate: data => doBatchUpdate(id, data),
128
+ append: (range, data, ops) => append({ id, range }, data, ops),
129
+ read: range => read({ id, range }),
130
+ sheetInfo,
131
+ createSheet,
132
+ updateValues: (range, values, opts) => {
133
+ if (!opts) {
134
+ opts = {
135
+ valueInputOption: 'USER_ENTERED'
136
+ };
137
+ }
138
+ if (!opts.valueInputOption)
139
+ opts.valueInputOption = 'USER_ENTERED';
140
+ return doOp('PUT', id, `/values/${encodeURIComponent(range)}?${(0, util_1.getFormData)(opts)}`, {
141
+ values,
142
+ });
143
+ },
144
+ addSheet: async (title) => {
145
+ const sheetsInfo = await sheetInfo();
146
+ //input YYYY, sheetId,
147
+ const found = sheetsInfo.find(s => s.title === title);
148
+ if (found) {
149
+ return {
150
+ found,
151
+ };
152
+ }
153
+ let newId = 0;
154
+ for (;; newId++) {
155
+ const existing = sheetsInfo.find(s => s.sheetId === newId);
156
+ if (existing)
157
+ continue;
158
+ break;
159
+ }
160
+ return createSheet(newId.toString(), title);
161
+ },
162
+ };
163
+ }
164
+ };
165
+ }
166
+ const clients = {};
167
+ async function getClient(creds) {
168
+ const name = creds.client_id;
169
+ let client = clients[name];
170
+ const now = new Date().getTime();
171
+ if (!client || client.expires_on <= now) {
172
+ client = await doRefresh(creds);
173
+ if (!client)
174
+ return null;
175
+ clients[name] = client;
176
+ }
177
+ return client;
178
+ }
179
+ function getClientCredsByEnv(envName) {
180
+ const creds = {
181
+ client_id: process.env[`google_${envName}_client_id`],
182
+ client_secret: process.env[`google_${envName}_client_secret`],
183
+ refresh_token: process.env[`google_${envName}_refresh_token`],
184
+ };
185
+ return creds;
186
+ }
187
+ async function getClientByEnv(envName) {
188
+ const creds = getClientCredsByEnv(envName);
189
+ return getClient(creds);
190
+ }
191
+ async function test(d) {
192
+ const cli = await getClientByEnv('gzperm');
193
+ const ops = await cli.getSheetOps('1u_AR8y7iCRPGyDhdOb1cHhjL-vclCIxuLkMhIxd08mU');
194
+ console.log('update val');
195
+ const rrr = await ops.updateValues('Sheet1!G18:G18', [['1']]);
196
+ console.log('update done');
197
+ console.log(rrr);
198
+ if (d)
199
+ return;
200
+ if (!cli)
201
+ return console.log('failed to get client');
202
+ const id = '1MO27odjCsxk6MWL0DygubU53hrtt3OB8SEnqjpUHJ-U';
203
+ if (d)
204
+ return;
205
+ console.log('do batch update');
206
+ await cli.doBatchUpdate(id, {
207
+ "requests": [
208
+ {
209
+ "updateDimensionProperties": {
210
+ "range": {
211
+ "sheetId": 0,
212
+ "dimension": "COLUMNS",
213
+ "startIndex": 0,
214
+ "endIndex": 1
215
+ },
216
+ "properties": {
217
+ "pixelSize": 160
218
+ },
219
+ "fields": "pixelSize"
220
+ },
221
+ }
222
+ ]
223
+ });
224
+ console.log('do batch update 2');
225
+ const upres = await cli.doBatchUpdate(id, {
226
+ "requests": [
227
+ {
228
+ "updateCells": {
229
+ "fields": "*",
230
+ "range": {
231
+ "sheetId": 0,
232
+ "startColumnIndex": 0,
233
+ "endColumnIndex": 10,
234
+ "startRowIndex": 0,
235
+ "endRowIndex": 10
236
+ },
237
+ "rows": [
238
+ {
239
+ "values": [
240
+ {
241
+ "userEnteredFormat": {
242
+ "backgroundColor": {
243
+ "blue": 10,
244
+ "green": 10,
245
+ "red": 255
246
+ },
247
+ "borders": {
248
+ "bottom": {
249
+ "style": "SOLID",
250
+ "width": 8,
251
+ "color": {
252
+ "blue": 0,
253
+ "green": 255,
254
+ "red": 0
255
+ }
256
+ }
257
+ }
258
+ },
259
+ "userEnteredValue": { "stringValue": "strstsdfasdf" }
260
+ },
261
+ {
262
+ "userEnteredValue": { "stringValue": "col1" }
263
+ }
264
+ ]
265
+ }
266
+ ]
267
+ }
268
+ }
269
+ ]
270
+ });
271
+ console.log(upres);
272
+ console.log('append 1');
273
+ const appres = await cli.append({
274
+ id,
275
+ range: `'Sheet1'!A1:B2`
276
+ }, [
277
+ ['aaa', 'bbb1']
278
+ ]);
279
+ console.log('append res');
280
+ console.log(appres);
281
+ console.log('read');
282
+ const rres = await cli.read({
283
+ id,
284
+ range: 'A1:B4'
285
+ });
286
+ console.log('read res');
287
+ console.log(rres);
288
+ const sheet = cli.getSheetOps(id);
289
+ sheet.doBatchUpdate({
290
+ "requests": [
291
+ {
292
+ "updateDimensionProperties": {
293
+ "range": {
294
+ "sheetId": 0,
295
+ "dimension": "COLUMNS",
296
+ "startIndex": 0,
297
+ "endIndex": 1
298
+ },
299
+ "properties": {
300
+ "pixelSize": 100
301
+ },
302
+ "fields": "pixelSize"
303
+ },
304
+ }
305
+ ]
306
+ });
307
+ await sheet.append('A:B', [['c', 'D']]);
308
+ console.log(await sheet.read('A1:B4'));
309
+ }
310
+ //test(true).catch(err => {
311
+ ///console.log(err)
312
+ //console.log(err.response.data);
313
+ //})
314
+ /*
315
+ async function test2() {
316
+ const creds = getClientCredsByEnv('gzperm');
317
+ await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
318
+ }
319
+ console.log('invoking test2')
320
+ test2().catch(err => {
321
+ console.log('error');
322
+ //console.log(err);
323
+ console.log(err.response.text || err.response.data);
324
+ })
325
+ */
460
326
  //# sourceMappingURL=googleApi.js.map