@gzhangx/googleapi 0.0.1 → 0.0.4

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.
@@ -1,8 +1,10 @@
1
- export interface IRefresCreds {
2
- refresh_token: string;
1
+ export interface IGClientCreds {
3
2
  client_id: string;
4
3
  client_secret: string;
5
4
  }
5
+ export interface IRefresCreds extends IGClientCreds {
6
+ refresh_token: string;
7
+ }
6
8
  export declare function getFormData(obj: {
7
9
  [id: string]: any;
8
10
  }): (string | null);
@@ -25,7 +27,16 @@ export interface IGoogleClient {
25
27
  read: (range: string) => Promise<any>;
26
28
  };
27
29
  }
30
+ export interface IGoogleToken {
31
+ access_token: string;
32
+ expires_in: number;
33
+ refresh_token: string;
34
+ scope: string;
35
+ token_type: string;
36
+ }
37
+ export declare function getTokenFromCode(creds: IGClientCreds, code: string, redirect_uri: string): Promise<IGoogleToken>;
28
38
  export declare function getClient(creds: IRefresCreds): Promise<IGoogleClient>;
39
+ export declare function getClientCredsByEnv(envName: string): IGClientCreds;
29
40
  export declare function getClientByEnv(envName: string): Promise<IGoogleClient>;
30
41
  export declare function test(d: boolean): Promise<void>;
31
42
  export {};
package/lib/googleApi.js CHANGED
@@ -43,7 +43,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
43
43
  return (mod && mod.__esModule) ? mod : { "default": mod };
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.test = exports.getClientByEnv = exports.getClient = exports.getFormData = void 0;
46
+ exports.test = exports.getClientByEnv = exports.getClientCredsByEnv = exports.getClient = exports.getTokenFromCode = exports.getFormData = void 0;
47
47
  var axios_1 = __importDefault(require("axios"));
48
48
  function getFormData(obj) {
49
49
  if (!obj)
@@ -58,6 +58,31 @@ function getFormData(obj) {
58
58
  return data;
59
59
  }
60
60
  exports.getFormData = getFormData;
61
+ function getTokenFromCode(creds, code, redirect_uri) {
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var client_id, client_secret, dataStr, tokenBody;
64
+ return __generator(this, function (_a) {
65
+ switch (_a.label) {
66
+ case 0:
67
+ client_id = creds.client_id, client_secret = creds.client_secret;
68
+ dataStr = getFormData({
69
+ client_secret: client_secret,
70
+ client_id: client_id,
71
+ code: code,
72
+ redirect_uri: redirect_uri,
73
+ grant_type: 'authorization_code'
74
+ });
75
+ return [4 /*yield*/, axios_1.default.post('https://oauth2.googleapis.com/token', dataStr, { headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(function (r) {
76
+ return r.data;
77
+ })];
78
+ case 1:
79
+ tokenBody = _a.sent();
80
+ return [2 /*return*/, tokenBody];
81
+ }
82
+ });
83
+ });
84
+ }
85
+ exports.getTokenFromCode = getTokenFromCode;
61
86
  function doRefresh(creds) {
62
87
  return __awaiter(this, void 0, void 0, function () {
63
88
  var refresh_token, client_id, client_secret, dataStr, refreshBody, access_token, expires_in, token_type, doOp, doPost, doBatchUpdate, append, read;
@@ -161,6 +186,14 @@ function getClient(creds) {
161
186
  });
162
187
  }
163
188
  exports.getClient = getClient;
189
+ function getClientCredsByEnv(envName) {
190
+ var creds = {
191
+ client_id: process.env["google.".concat(envName, ".client_id")],
192
+ client_secret: process.env["google.".concat(envName, ".client_secret")],
193
+ };
194
+ return creds;
195
+ }
196
+ exports.getClientCredsByEnv = getClientCredsByEnv;
164
197
  function getClientByEnv(envName) {
165
198
  return __awaiter(this, void 0, void 0, function () {
166
199
  var creds;
@@ -314,3 +347,15 @@ exports.test = test;
314
347
  //test().catch(err => {
315
348
  // console.log(err.response.text);
316
349
  //})
350
+ /*
351
+ async function test2() {
352
+ const creds = getClientCredsByEnv('gzperm');
353
+ await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
354
+ }
355
+ console.log('invoking test2')
356
+ test2().catch(err => {
357
+ console.log('error');
358
+ //console.log(err);
359
+ console.log(err.response.text || err.response.data);
360
+ })
361
+ */
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { getFormData, getClient, getClientByEnv } from './googleApi';
2
- export { getFormData, getClient, getClientByEnv };
1
+ import { getFormData, getClient, getClientByEnv, getTokenFromCode, getClientCredsByEnv } from './googleApi';
2
+ export { getFormData, getClient, getClientByEnv, getTokenFromCode, getClientCredsByEnv };
package/lib/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getClientByEnv = exports.getClient = exports.getFormData = void 0;
3
+ exports.getClientCredsByEnv = exports.getTokenFromCode = exports.getClientByEnv = exports.getClient = exports.getFormData = void 0;
4
4
  var googleApi_1 = require("./googleApi");
5
5
  Object.defineProperty(exports, "getFormData", { enumerable: true, get: function () { return googleApi_1.getFormData; } });
6
6
  Object.defineProperty(exports, "getClient", { enumerable: true, get: function () { return googleApi_1.getClient; } });
7
7
  Object.defineProperty(exports, "getClientByEnv", { enumerable: true, get: function () { return googleApi_1.getClientByEnv; } });
8
+ Object.defineProperty(exports, "getTokenFromCode", { enumerable: true, get: function () { return googleApi_1.getTokenFromCode; } });
9
+ Object.defineProperty(exports, "getClientCredsByEnv", { enumerable: true, get: function () { return googleApi_1.getClientCredsByEnv; } });
@@ -0,0 +1 @@
1
+ {"program":{"fileNames":["../node_modules/typescript/lib/lib.d.ts","../node_modules/typescript/lib/lib.es5.d.ts","../node_modules/typescript/lib/lib.es2015.d.ts","../node_modules/typescript/lib/lib.es2016.d.ts","../node_modules/typescript/lib/lib.es2017.d.ts","../node_modules/typescript/lib/lib.es2018.d.ts","../node_modules/typescript/lib/lib.es2019.d.ts","../node_modules/typescript/lib/lib.es2020.d.ts","../node_modules/typescript/lib/lib.dom.d.ts","../node_modules/typescript/lib/lib.webworker.importscripts.d.ts","../node_modules/typescript/lib/lib.scripthost.d.ts","../node_modules/typescript/lib/lib.es2015.core.d.ts","../node_modules/typescript/lib/lib.es2015.collection.d.ts","../node_modules/typescript/lib/lib.es2015.generator.d.ts","../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../node_modules/typescript/lib/lib.es2015.promise.d.ts","../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../node_modules/typescript/lib/lib.es2017.object.d.ts","../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2017.string.d.ts","../node_modules/typescript/lib/lib.es2017.intl.d.ts","../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../node_modules/typescript/lib/lib.es2018.intl.d.ts","../node_modules/typescript/lib/lib.es2018.promise.d.ts","../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../node_modules/typescript/lib/lib.es2019.array.d.ts","../node_modules/typescript/lib/lib.es2019.object.d.ts","../node_modules/typescript/lib/lib.es2019.string.d.ts","../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../node_modules/typescript/lib/lib.es2020.promise.d.ts","../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../node_modules/typescript/lib/lib.es2020.string.d.ts","../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../node_modules/typescript/lib/lib.es2020.intl.d.ts","../node_modules/typescript/lib/lib.esnext.intl.d.ts","../node_modules/axios/index.d.ts","../src/googleapi.ts","../src/index.ts","../node_modules/@types/node/assert.d.ts","../node_modules/@types/node/assert/strict.d.ts","../node_modules/@types/node/globals.d.ts","../node_modules/@types/node/async_hooks.d.ts","../node_modules/@types/node/buffer.d.ts","../node_modules/@types/node/child_process.d.ts","../node_modules/@types/node/cluster.d.ts","../node_modules/@types/node/console.d.ts","../node_modules/@types/node/constants.d.ts","../node_modules/@types/node/crypto.d.ts","../node_modules/@types/node/dgram.d.ts","../node_modules/@types/node/diagnostics_channel.d.ts","../node_modules/@types/node/dns.d.ts","../node_modules/@types/node/dns/promises.d.ts","../node_modules/@types/node/domain.d.ts","../node_modules/@types/node/events.d.ts","../node_modules/@types/node/fs.d.ts","../node_modules/@types/node/fs/promises.d.ts","../node_modules/@types/node/http.d.ts","../node_modules/@types/node/http2.d.ts","../node_modules/@types/node/https.d.ts","../node_modules/@types/node/inspector.d.ts","../node_modules/@types/node/module.d.ts","../node_modules/@types/node/net.d.ts","../node_modules/@types/node/os.d.ts","../node_modules/@types/node/path.d.ts","../node_modules/@types/node/perf_hooks.d.ts","../node_modules/@types/node/process.d.ts","../node_modules/@types/node/punycode.d.ts","../node_modules/@types/node/querystring.d.ts","../node_modules/@types/node/readline.d.ts","../node_modules/@types/node/repl.d.ts","../node_modules/@types/node/stream.d.ts","../node_modules/@types/node/stream/promises.d.ts","../node_modules/@types/node/stream/consumers.d.ts","../node_modules/@types/node/stream/web.d.ts","../node_modules/@types/node/string_decoder.d.ts","../node_modules/@types/node/timers.d.ts","../node_modules/@types/node/timers/promises.d.ts","../node_modules/@types/node/tls.d.ts","../node_modules/@types/node/trace_events.d.ts","../node_modules/@types/node/tty.d.ts","../node_modules/@types/node/url.d.ts","../node_modules/@types/node/util.d.ts","../node_modules/@types/node/v8.d.ts","../node_modules/@types/node/vm.d.ts","../node_modules/@types/node/wasi.d.ts","../node_modules/@types/node/worker_threads.d.ts","../node_modules/@types/node/zlib.d.ts","../node_modules/@types/node/globals.global.d.ts","../node_modules/@types/node/index.d.ts"],"fileInfos":["2dc8c927c9c162a773c6bb3cdc4f3286c23f10eedc67414028f9cb5951610f60",{"version":"3ac1b83264055b28c0165688fda6dfcc39001e9e7828f649299101c23ad0a0c3","affectsGlobalScope":true},"dc47c4fa66b9b9890cf076304de2a9c5201e94b740cffdf09f87296d877d71f6","7a387c58583dfca701b6c85e0adaf43fb17d590fb16d5b2dc0a2fbd89f35c467","8a12173c586e95f4433e0c6dc446bc88346be73ffe9ca6eec7aa63c8f3dca7f9","5f4e733ced4e129482ae2186aae29fde948ab7182844c3a5a51dd346182c7b06","e6b724280c694a9f588847f754198fb96c43d805f065c3a5b28bbc9594541c84","e21c071ca3e1b4a815d5f04a7475adcaeea5d64367e840dd0154096d705c3940",{"version":"72704b10d97777e15f1a581b73f88273037ef752d2e50b72287bd0a90af64fe6","affectsGlobalScope":true},{"version":"7fac8cb5fc820bc2a59ae11ef1c5b38d3832c6d0dfaec5acdb5569137d09a481","affectsGlobalScope":true},{"version":"097a57355ded99c68e6df1b738990448e0bf170e606707df5a7c0481ff2427cd","affectsGlobalScope":true},{"version":"d8996609230d17e90484a2dd58f22668f9a05a3bfe00bfb1d6271171e54a31fb","affectsGlobalScope":true},{"version":"43fb1d932e4966a39a41b464a12a81899d9ae5f2c829063f5571b6b87e6d2f9c","affectsGlobalScope":true},{"version":"cdccba9a388c2ee3fd6ad4018c640a471a6c060e96f1232062223063b0a5ac6a","affectsGlobalScope":true},{"version":"c5c05907c02476e4bde6b7e76a79ffcd948aedd14b6a8f56e4674221b0417398","affectsGlobalScope":true},{"version":"0d5f52b3174bee6edb81260ebcd792692c32c81fd55499d69531496f3f2b25e7","affectsGlobalScope":true},{"version":"810627a82ac06fb5166da5ada4159c4ec11978dfbb0805fe804c86406dab8357","affectsGlobalScope":true},{"version":"62d80405c46c3f4c527ee657ae9d43fda65a0bf582292429aea1e69144a522a6","affectsGlobalScope":true},{"version":"3013574108c36fd3aaca79764002b3717da09725a36a6fc02eac386593110f93","affectsGlobalScope":true},{"version":"75ec0bdd727d887f1b79ed6619412ea72ba3c81d92d0787ccb64bab18d261f14","affectsGlobalScope":true},{"version":"3be5a1453daa63e031d266bf342f3943603873d890ab8b9ada95e22389389006","affectsGlobalScope":true},{"version":"17bb1fc99591b00515502d264fa55dc8370c45c5298f4a5c2083557dccba5a2a","affectsGlobalScope":true},{"version":"7ce9f0bde3307ca1f944119f6365f2d776d281a393b576a18a2f2893a2d75c98","affectsGlobalScope":true},{"version":"6a6b173e739a6a99629a8594bfb294cc7329bfb7b227f12e1f7c11bc163b8577","affectsGlobalScope":true},{"version":"12a310447c5d23c7d0d5ca2af606e3bd08afda69100166730ab92c62999ebb9d","affectsGlobalScope":true},{"version":"b0124885ef82641903d232172577f2ceb5d3e60aed4da1153bab4221e1f6dd4e","affectsGlobalScope":true},{"version":"0eb85d6c590b0d577919a79e0084fa1744c1beba6fd0d4e951432fa1ede5510a","affectsGlobalScope":true},{"version":"da233fc1c8a377ba9e0bed690a73c290d843c2c3d23a7bd7ec5cd3d7d73ba1e0","affectsGlobalScope":true},{"version":"d154ea5bb7f7f9001ed9153e876b2d5b8f5c2bb9ec02b3ae0d239ec769f1f2ae","affectsGlobalScope":true},{"version":"bb2d3fb05a1d2ffbca947cc7cbc95d23e1d053d6595391bd325deb265a18d36c","affectsGlobalScope":true},{"version":"c80df75850fea5caa2afe43b9949338ce4e2de086f91713e9af1a06f973872b8","affectsGlobalScope":true},{"version":"9d57b2b5d15838ed094aa9ff1299eecef40b190722eb619bac4616657a05f951","affectsGlobalScope":true},{"version":"6c51b5dd26a2c31dbf37f00cfc32b2aa6a92e19c995aefb5b97a3a64f1ac99de","affectsGlobalScope":true},{"version":"6e7997ef61de3132e4d4b2250e75343f487903ddf5370e7ce33cf1b9db9a63ed","affectsGlobalScope":true},{"version":"2ad234885a4240522efccd77de6c7d99eecf9b4de0914adb9a35c0c22433f993","affectsGlobalScope":true},{"version":"1b3fe904465430e030c93239a348f05e1be80640d91f2f004c3512c2c2c89f34","affectsGlobalScope":true},{"version":"3787b83e297de7c315d55d4a7c546ae28e5f6c0a361b7a1dcec1f1f50a54ef11","affectsGlobalScope":true},{"version":"e7e8e1d368290e9295ef18ca23f405cf40d5456fa9f20db6373a61ca45f75f40","affectsGlobalScope":true},{"version":"faf0221ae0465363c842ce6aa8a0cbda5d9296940a8e26c86e04cc4081eea21e","affectsGlobalScope":true},{"version":"06393d13ea207a1bfe08ec8d7be562549c5e2da8983f2ee074e00002629d1871","affectsGlobalScope":true},{"version":"5075b36ab861c8c0c45377cb8c96270d7c65f0eeaf105d53fac6850da61f1027","affectsGlobalScope":true},{"version":"10bbdc1981b8d9310ee75bfac28ee0477bb2353e8529da8cff7cb26c409cb5e8","affectsGlobalScope":true},"2808645b990069e5f8b5ff14c9f1e6077eb642583c3f7854012d60757f23c70e","7a565d5da742a6b6641b12c1b37ddb73728669ad6aab836ae8029e88cb2df6a7",{"version":"e36a9b826666321fa2b0b93e01b518b02a368d9c1d763434ef10c9af588fb062","signature":"a8f1828e5ce593bc69306af5c40073ebc31ecd70aee4281ee4a052b4f95eb3b8"},"0cba3a5d7b81356222594442753cf90dd2892e5ccfe1d262aaca6896ba6c1380","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"c2ab70bbc7a24c42a790890739dd8a0ba9d2e15038b40dff8163a97a5d148c00","affectsGlobalScope":true},"422dbb183fdced59425ca072c8bd09efaa77ce4e2ab928ec0d8a1ce062d2a45a",{"version":"712ba0d43b44d144dfd01593f61af6e2e21cfae83e834d297643e7973e55ed61","affectsGlobalScope":true},"1dab5ab6bcf11de47ab9db295df8c4f1d92ffa750e8f095e88c71ce4c3299628","f71f46ccd5a90566f0a37b25b23bc4684381ab2180bdf6733f4e6624474e1894",{"version":"54e65985a3ee3cec182e6a555e20974ea936fc8b8d1738c14e8ed8a42bd921d4","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","98a3ebfa494b46265634a73459050befba5da8fdc6ca0ef9b7269421780f4ff3","34e5de87d983bc6aefef8b17658556e3157003e8d9555d3cb098c6bef0b5fbc8","cc0b61316c4f37393f1f9595e93b673f4184e9d07f4c127165a490ec4a928668","f27371653aded82b2b160f7a7033fb4a5b1534b6f6081ef7be1468f0f15327d3","c762cd6754b13a461c54b59d0ae0ab7aeef3c292c6cf889873f786ee4d8e75c9","f4ea7d5df644785bd9fbf419930cbaec118f0d8b4160037d2339b8e23c059e79",{"version":"bfea28e6162ed21a0aeed181b623dcf250aa79abf49e24a6b7e012655af36d81","affectsGlobalScope":true},"7a5459efa09ea82088234e6533a203d528c594b01787fb90fba148885a36e8b6","ae97e20f2e10dbeec193d6a2f9cd9a367a1e293e7d6b33b68bacea166afd7792","10d4796a130577d57003a77b95d8723530bbec84718e364aa2129fa8ffba0378","ad41bb744149e92adb06eb953da195115620a3f2ad48e7d3ae04d10762dae197","bf73c576885408d4a176f44a9035d798827cc5020d58284cb18d7573430d9022","7ae078ca42a670445ae0c6a97c029cb83d143d62abd1730efb33f68f0b2c0e82",{"version":"e8b18c6385ff784228a6f369694fcf1a6b475355ba89090a88de13587a9391d5","affectsGlobalScope":true},"5d0a9ea09d990b5788f867f1c79d4878f86f7384cb7dab38eecbf22f9efd063d","12eea70b5e11e924bb0543aea5eadc16ced318aa26001b453b0d561c2fd0bd1e","08777cd9318d294646b121838574e1dd7acbb22c21a03df84e1f2c87b1ad47f2","08a90bcdc717df3d50a2ce178d966a8c353fd23e5c392fd3594a6e39d9bb6304",{"version":"4cd4cff679c9b3d9239fd7bf70293ca4594583767526916af8e5d5a47d0219c7","affectsGlobalScope":true},"2a12d2da5ac4c4979401a3f6eaafa874747a37c365e4bc18aa2b171ae134d21b","002b837927b53f3714308ecd96f72ee8a053b8aeb28213d8ec6de23ed1608b66","1dc9c847473bb47279e398b22c740c83ea37a5c88bf66629666e3cf4c5b9f99c","a9e4a5a24bf2c44de4c98274975a1a705a0abbaad04df3557c2d3cd8b1727949","00fa7ce8bc8acc560dc341bbfdf37840a8c59e6a67c9bfa3fa5f36254df35db2","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff",{"version":"806ef4cac3b3d9fa4a48d849c8e084d7c72fcd7b16d76e06049a9ed742ff79c0","affectsGlobalScope":true},"44b8b584a338b190a59f4f6929d072431950c7bd92ec2694821c11bce180c8a5","5f0ed51db151c2cdc4fa3bb0f44ce6066912ad001b607a34e65a96c52eb76248",{"version":"3345c276cab0e76dda86c0fb79104ff915a4580ba0f3e440870e183b1baec476","affectsGlobalScope":true},"664d8f2d59164f2e08c543981453893bc7e003e4dfd29651ce09db13e9457980","e383ff72aabf294913f8c346f5da1445ae6ad525836d28efd52cbadc01a361a6","f52fbf64c7e480271a9096763c4882d356b05cab05bf56a64e68a95313cd2ce2","59bdb65f28d7ce52ccfc906e9aaf422f8b8534b2d21c32a27d7819be5ad81df7",{"version":"3a2da34079a2567161c1359316a32e712404b56566c45332ac9dcee015ecce9f","affectsGlobalScope":true},"28a2e7383fd898c386ffdcacedf0ec0845e5d1a86b5a43f25b86bc315f556b79","3aff9c8c36192e46a84afe7b926136d520487155154ab9ba982a8b544ea8fc95","a880cf8d85af2e4189c709b0fea613741649c0e40fffb4360ec70762563d5de0","85bbf436a15bbeda4db888be3062d47f99c66fd05d7c50f0f6473a9151b6a070","9f9c49c95ecd25e0cb2587751925976cf64fd184714cb11e213749c80cf0f927","f0c75c08a71f9212c93a719a25fb0320d53f2e50ca89a812640e08f8ad8c408c",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"9cafe917bf667f1027b2bb62e2de454ecd2119c80873ad76fc41d941089753b8"],"options":{"declaration":true,"esModuleInterop":true,"module":1,"outDir":"./","skipLibCheck":true,"target":1},"fileIdsList":[[46,89],[49,89],[50,55,89],[51,61,62,69,78,88,89],[51,52,61,69,89],[53,89],[54,55,62,70,89],[55,78,85,89],[56,58,61,69,89],[57,89],[58,59,89],[60,61,89],[61,89],[61,62,63,78,88,89],[61,62,63,78,89],[89],[64,69,78,88,89],[61,62,64,65,69,78,85,88,89],[64,66,78,85,88,89],[46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95],[61,67,89],[68,88,89],[58,61,69,78,89],[70,89],[71,89],[49,72,89],[73,87,89,93],[74,89],[75,89],[61,76,89],[76,77,89,91],[61,78,79,80,89],[78,80,89],[78,79,89],[81,89],[82,89],[61,83,84,89],[83,84,89],[55,69,78,85,89],[86,89],[69,87,89],[50,64,75,88,89],[55,89],[78,89,90],[89,91],[89,92],[50,55,61,63,72,78,88,89,91,93],[78,89,94],[43,89],[44,89],[44]],"referencedMap":[[46,1],[47,1],[49,2],[50,3],[51,4],[52,5],[53,6],[54,7],[55,8],[56,9],[57,10],[58,11],[59,11],[60,12],[61,13],[62,14],[63,15],[48,16],[95,16],[64,17],[65,18],[66,19],[96,20],[67,21],[68,22],[69,23],[70,24],[71,25],[72,26],[73,27],[74,28],[75,29],[76,30],[77,31],[78,32],[80,33],[79,34],[81,35],[82,36],[83,37],[84,38],[85,39],[86,40],[87,41],[88,42],[89,43],[90,44],[91,45],[92,46],[93,47],[94,48],[43,16],[1,16],[9,16],[13,16],[12,16],[3,16],[14,16],[15,16],[16,16],[17,16],[18,16],[19,16],[20,16],[21,16],[4,16],[5,16],[25,16],[22,16],[23,16],[24,16],[26,16],[27,16],[28,16],[6,16],[29,16],[30,16],[31,16],[32,16],[7,16],[33,16],[34,16],[35,16],[36,16],[8,16],[41,16],[37,16],[38,16],[39,16],[40,16],[2,16],[42,16],[11,16],[10,16],[44,49],[45,50]],"exportedModulesMap":[[46,1],[47,1],[49,2],[50,3],[51,4],[52,5],[53,6],[54,7],[55,8],[56,9],[57,10],[58,11],[59,11],[60,12],[61,13],[62,14],[63,15],[48,16],[95,16],[64,17],[65,18],[66,19],[96,20],[67,21],[68,22],[69,23],[70,24],[71,25],[72,26],[73,27],[74,28],[75,29],[76,30],[77,31],[78,32],[80,33],[79,34],[81,35],[82,36],[83,37],[84,38],[85,39],[86,40],[87,41],[88,42],[89,43],[90,44],[91,45],[92,46],[93,47],[94,48],[43,16],[1,16],[9,16],[13,16],[12,16],[3,16],[14,16],[15,16],[16,16],[17,16],[18,16],[19,16],[20,16],[21,16],[4,16],[5,16],[25,16],[22,16],[23,16],[24,16],[26,16],[27,16],[28,16],[6,16],[29,16],[30,16],[31,16],[32,16],[7,16],[33,16],[34,16],[35,16],[36,16],[8,16],[41,16],[37,16],[38,16],[39,16],[40,16],[2,16],[42,16],[11,16],[10,16],[44,49],[45,51]],"semanticDiagnosticsPerFile":[46,47,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,48,95,64,65,66,96,67,68,69,70,71,72,73,74,75,76,77,78,80,79,81,82,83,84,85,86,87,88,89,90,91,92,93,94,43,1,9,13,12,3,14,15,16,17,18,19,20,21,4,5,25,22,23,24,26,27,28,6,29,30,31,32,7,33,34,35,36,8,41,37,38,39,40,2,42,11,10,44,45]},"version":"4.6.2"}
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@gzhangx/googleapi",
3
- "version": "0.0.1",
3
+ "version": "0.0.04",
4
4
  "description": "google api helper",
5
- "main": "index.js",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "files": ["lib/**/*"],
6
8
  "scripts": {
7
9
  "test": "echo \"Error: no test specified\" && exit 1"
8
10
  },
package/pub.bat DELETED
@@ -1,2 +0,0 @@
1
- call npx tsc
2
- npm publish --access public
package/src/googleApi.ts DELETED
@@ -1,249 +0,0 @@
1
-
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
-
7
- import axios, {Method} from 'axios';
8
-
9
- export interface IRefresCreds {
10
- refresh_token: string;
11
- client_id: string;
12
- client_secret: string;
13
- }
14
-
15
-
16
- export function getFormData(obj: { [id: string]: any }): (string|null) {
17
- if (!obj) return null;
18
- const keys = Object.keys(obj);
19
- const data = keys.map(key => {
20
- let v = obj[key];
21
- if (typeof v === 'number') v = '' + v;
22
- return `${key}=${encodeURIComponent(obj[key])}`;
23
- }).join('&')
24
- return data;
25
- }
26
-
27
- interface IIdRange {
28
- id: string; range: string;
29
- }
30
- type IAppendFunc = (idRng: IIdRange, data: any, opts?: any) => Promise<any>;
31
- type IReadFunc = (idRng: IIdRange) => Promise<any>;
32
- export interface IGoogleClient {
33
- access_token: string;
34
- expires_on: number;
35
- token_type: string;
36
- doBatchUpdate: (id: string, data: any) => Promise<any>;
37
- append: IAppendFunc;
38
- read: IReadFunc;
39
- getSheeOps: (id: string) => {
40
- doBatchUpdate: (data: any) => Promise<any>;
41
- append: (range:string, data: any, opts?: any) => Promise<any>;
42
- read: (range:string)=>Promise<any>;
43
- };
44
- }
45
-
46
- async function doRefresh(creds: IRefresCreds): Promise<IGoogleClient> {
47
- const { refresh_token, client_id, client_secret } = creds;
48
-
49
- const dataStr = getFormData({
50
- client_secret,
51
- client_id,
52
- refresh_token,
53
- grant_type: 'refresh_token'
54
- });
55
- const refreshBody = await axios.post('https://oauth2.googleapis.com/token', dataStr,
56
- { headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(r => {
57
- return r.data;
58
- });
59
-
60
- const {
61
- access_token, expires_in, token_type
62
- } = refreshBody;
63
- const doOp = (op: string, id: string, postFix: string, data?: any) =>
64
- axios({
65
- url: `https://sheets.googleapis.com/v4/spreadsheets/${id}${postFix}`,
66
- headers: {
67
- "Content-Type": "application/json",
68
- "Authorization": `Bearer ${access_token}`,
69
- },
70
- method:op as Method,
71
- data,
72
- }).then(r => {
73
- return (r.data)
74
- });
75
- const doPost = (id:string, postFix:string, data:any) => doOp('post', id, postFix, data);
76
- const doBatchUpdate = async (id:string, data:any) => doPost(id, ':batchUpdate', data);
77
- const append: IAppendFunc = async ({ id, range }, data, opts) => {
78
- if (!opts) {
79
- opts = {}
80
- }
81
- if (!opts.valueInputOption) opts.valueInputOption = 'USER_ENTERED';
82
- return await doPost(id, `/values/${range}:append?${getFormData(opts)}`, { values: data });
83
- };
84
- const read: IReadFunc = async ({ id, range }) => doOp('get', id, `/values/${range}`);
85
- return {
86
- access_token,
87
- expires_on: new Date().getTime() + (expires_in * 1000 - 2000),
88
- token_type,
89
- doBatchUpdate,
90
- append,
91
- read,
92
- getSheeOps: id => {
93
- return {
94
- doBatchUpdate: data => doBatchUpdate(id, data),
95
- append: (range, data, ops) => append({ id, range }, data, ops),
96
- read: range => read({ id, range }),
97
- }
98
- }
99
- }
100
- }
101
-
102
- const clients = {} as {
103
- [name: string]: IGoogleClient;
104
- };
105
- export async function getClient(creds: IRefresCreds) {
106
- const name = creds.client_id;
107
- let client = clients[name];
108
- const now = new Date().getTime();
109
- if (!client || client.expires_on <= now) {
110
- client = await doRefresh(creds);
111
- if (!client) return null;
112
- clients[name] = client;
113
- }
114
- return client;
115
- }
116
-
117
- export async function getClientByEnv(envName: string) {
118
- const creds: IRefresCreds = {
119
- client_id: process.env[`google.${envName}.client_id`] as string,
120
- client_secret: process.env[`google.${envName}.client_secret`] as string,
121
- refresh_token: process.env[`google.${envName}.refresh_token`] as string,
122
- };
123
- return getClient(creds);
124
- }
125
-
126
- export async function test(d:boolean) {
127
- const cli = await getClientByEnv('gzprem');
128
-
129
- if (!cli) return console.log('failed to get client');
130
- const id = '1MO27odjCsxk6MWL0DygubU53hrtt3OB8SEnqjpUHJ-U';
131
- if (d) return;
132
- console.log('do batch update');
133
- await cli.doBatchUpdate(id, {
134
- "requests": [
135
- {
136
- "updateDimensionProperties": {
137
- "range": {
138
- "sheetId": 0,
139
- "dimension": "COLUMNS",
140
- "startIndex": 0,
141
- "endIndex": 1
142
- },
143
- "properties": {
144
- "pixelSize": 160
145
- },
146
- "fields": "pixelSize"
147
- },
148
-
149
- }
150
- ]
151
- })
152
- console.log('do batch update 2');
153
- const upres = await cli.doBatchUpdate(id, {
154
- "requests": [
155
- {
156
-
157
- "updateCells": {
158
- "fields": "*",
159
- "range": {
160
- "sheetId": 0,
161
- "startColumnIndex": 0,
162
- "endColumnIndex": 10,
163
- "startRowIndex": 0,
164
- "endRowIndex": 10
165
- },
166
- "rows": [
167
- {
168
- "values": [
169
- {
170
- "userEnteredFormat": {
171
- "backgroundColor": {
172
- "blue": 10,
173
- "green": 10,
174
- "red": 255
175
- },
176
- "borders": {
177
- "bottom": {
178
- "style": "SOLID",
179
- "width": 8,
180
- "color": {
181
- "blue": 0,
182
- "green": 255,
183
- "red": 0
184
- }
185
- }
186
- }
187
- },
188
- "userEnteredValue": { "stringValue": "strstsdfasdf" }
189
- },
190
- {
191
- "userEnteredValue": { "stringValue": "col1" }
192
- }
193
- ]
194
- }
195
- ]
196
- }
197
- }
198
- ]
199
- });
200
- console.log(upres);
201
-
202
- console.log('append 1');
203
- const appres = await cli.append({
204
- id,
205
- range: `'Sheet1'!A1:B2`
206
- }, [
207
- ['aaa', 'bbb1']
208
- ])
209
- console.log('append res');
210
- console.log(appres);
211
-
212
- console.log('read');
213
- const rres = await cli.read({
214
- id,
215
- range: 'A1:B4'
216
- });
217
- console.log('read res');
218
- console.log(rres);
219
-
220
-
221
-
222
- const sheet = cli.getSheeOps(id);
223
- sheet.doBatchUpdate({
224
- "requests": [
225
- {
226
- "updateDimensionProperties": {
227
- "range": {
228
- "sheetId": 0,
229
- "dimension": "COLUMNS",
230
- "startIndex": 0,
231
- "endIndex": 1
232
- },
233
- "properties": {
234
- "pixelSize": 100
235
- },
236
- "fields": "pixelSize"
237
- },
238
-
239
- }
240
- ]
241
- })
242
- await sheet.append('A:B', [['c', 'D']]);
243
- console.log(await sheet.read('A1:B4'));
244
- }
245
-
246
- //test().catch(err => {
247
- // console.log(err.response.text);
248
- //})
249
-
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- import { getFormData, getClient, getClientByEnv } from './googleApi'
2
-
3
- export { getFormData, getClient, getClientByEnv }
package/test.bat DELETED
@@ -1,2 +0,0 @@
1
- call node_modules\.bin\tsc
2
- call node build\index
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "incremental": true, /* Enable incremental compilation */
4
- "target": "es5", /* Specify ECMAScript target version: */
5
- "module": "commonjs", /* 'none', 'commonjs', 'amd', 'system', etc */
6
- "declaration": true, /* Concatenate & emit output to single file.*/
7
- "outDir": "lib", /* Redirect output to the directory. */
8
- "esModuleInterop": true, /* Enables intero between CommonJS and ES */
9
- "skipLibCheck": true, /* Skip type checking of declaration files. */
10
- "forceConsistentCasingInFileNames": true /* Disallow inconsistently */
11
- },
12
- "include": [
13
- "src"
14
- ]
15
- }