@gzhangx/googleapi 0.0.1 → 0.0.2
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/lib/googleApi.d.ts +13 -2
- package/lib/googleApi.js +46 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/package.json +1 -1
- package/src/googleApi.ts +49 -2
- package/src/index.ts +2 -2
- package/test.bat +1 -1
package/lib/googleApi.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export interface
|
|
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 } from './googleApi';
|
|
2
|
+
export { getFormData, getClient, getClientByEnv, getTokenFromCode };
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getClientByEnv = exports.getClient = exports.getFormData = void 0;
|
|
3
|
+
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; } });
|
package/package.json
CHANGED
package/src/googleApi.ts
CHANGED
|
@@ -6,12 +6,15 @@
|
|
|
6
6
|
|
|
7
7
|
import axios, {Method} from 'axios';
|
|
8
8
|
|
|
9
|
-
export interface
|
|
10
|
-
refresh_token: string;
|
|
9
|
+
export interface IGClientCreds {
|
|
11
10
|
client_id: string;
|
|
12
11
|
client_secret: string;
|
|
13
12
|
}
|
|
14
13
|
|
|
14
|
+
export interface IRefresCreds extends IGClientCreds {
|
|
15
|
+
refresh_token: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
15
18
|
|
|
16
19
|
export function getFormData(obj: { [id: string]: any }): (string|null) {
|
|
17
20
|
if (!obj) return null;
|
|
@@ -43,6 +46,30 @@ export interface IGoogleClient {
|
|
|
43
46
|
};
|
|
44
47
|
}
|
|
45
48
|
|
|
49
|
+
export interface IGoogleToken {
|
|
50
|
+
access_token: string;
|
|
51
|
+
expires_in: number;
|
|
52
|
+
refresh_token: string;
|
|
53
|
+
scope: string;
|
|
54
|
+
token_type: string; //'Bearer'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function getTokenFromCode(creds: IGClientCreds, code:string, redirect_uri:string) : Promise<IGoogleToken> {
|
|
58
|
+
const { client_id, client_secret } = creds;
|
|
59
|
+
const dataStr = getFormData({
|
|
60
|
+
client_secret,
|
|
61
|
+
client_id,
|
|
62
|
+
code,
|
|
63
|
+
redirect_uri,
|
|
64
|
+
grant_type: 'authorization_code'
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const tokenBody = await axios.post('https://oauth2.googleapis.com/token', dataStr,
|
|
68
|
+
{ headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(r => {
|
|
69
|
+
return r.data;
|
|
70
|
+
});
|
|
71
|
+
return tokenBody;
|
|
72
|
+
}
|
|
46
73
|
async function doRefresh(creds: IRefresCreds): Promise<IGoogleClient> {
|
|
47
74
|
const { refresh_token, client_id, client_secret } = creds;
|
|
48
75
|
|
|
@@ -114,6 +141,14 @@ export async function getClient(creds: IRefresCreds) {
|
|
|
114
141
|
return client;
|
|
115
142
|
}
|
|
116
143
|
|
|
144
|
+
export function getClientCredsByEnv(envName: string) {
|
|
145
|
+
const creds: IGClientCreds = {
|
|
146
|
+
client_id: process.env[`google.${envName}.client_id`] as string,
|
|
147
|
+
client_secret: process.env[`google.${envName}.client_secret`] as string,
|
|
148
|
+
};
|
|
149
|
+
return creds;
|
|
150
|
+
}
|
|
151
|
+
|
|
117
152
|
export async function getClientByEnv(envName: string) {
|
|
118
153
|
const creds: IRefresCreds = {
|
|
119
154
|
client_id: process.env[`google.${envName}.client_id`] as string,
|
|
@@ -247,3 +282,15 @@ export async function test(d:boolean) {
|
|
|
247
282
|
// console.log(err.response.text);
|
|
248
283
|
//})
|
|
249
284
|
|
|
285
|
+
/*
|
|
286
|
+
async function test2() {
|
|
287
|
+
const creds = getClientCredsByEnv('gzperm');
|
|
288
|
+
await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
|
|
289
|
+
}
|
|
290
|
+
console.log('invoking test2')
|
|
291
|
+
test2().catch(err => {
|
|
292
|
+
console.log('error');
|
|
293
|
+
//console.log(err);
|
|
294
|
+
console.log(err.response.text || err.response.data);
|
|
295
|
+
})
|
|
296
|
+
*/
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { getFormData, getClient, getClientByEnv } from './googleApi'
|
|
1
|
+
import { getFormData, getClient, getClientByEnv, getTokenFromCode } from './googleApi'
|
|
2
2
|
|
|
3
|
-
export { getFormData, getClient, getClientByEnv }
|
|
3
|
+
export { getFormData, getClient, getClientByEnv,getTokenFromCode }
|
package/test.bat
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
call node_modules\.bin\tsc
|
|
2
|
-
call node
|
|
2
|
+
call node lib\index
|