@anmiles/google-api-wrapper 5.0.0 → 6.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/CHANGELOG.md +6 -1
- package/README.md +6 -6
- package/dist/lib/api/shared.d.ts +7 -5
- package/dist/lib/api/shared.js +4 -4
- package/dist/lib/api/shared.js.map +1 -1
- package/dist/lib/auth.d.ts +2 -2
- package/dist/lib/auth.js +9 -4
- package/dist/lib/auth.js.map +1 -1
- package/dist/lib/secrets.js +3 -3
- package/dist/lib/secrets.js.map +1 -1
- package/dist/types/common.d.ts +3 -0
- package/dist/types/{api.js → common.js} +1 -1
- package/dist/types/common.js.map +1 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/secrets.d.ts +1 -1
- package/package.json +1 -1
- package/src/lib/__tests__/auth.test.ts +44 -12
- package/src/lib/__tests__/secrets.test.ts +24 -20
- package/src/lib/api/__tests__/calendar.test.ts +3 -3
- package/src/lib/api/__tests__/shared.test.ts +4 -4
- package/src/lib/api/__tests__/youtube.test.ts +3 -3
- package/src/lib/api/shared.ts +13 -16
- package/src/lib/auth.ts +12 -5
- package/src/lib/secrets.ts +3 -3
- package/src/types/common.ts +3 -0
- package/src/types/index.ts +1 -1
- package/src/types/secrets.ts +1 -1
- package/dist/types/api.d.ts +0 -3
- package/dist/types/api.js.map +0 -1
- package/src/types/api.ts +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,9 +5,14 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [
|
|
8
|
+
## [6.0.0](../../tags/v6.0.0) - 2023-03-20
|
|
9
9
|
### Added
|
|
10
10
|
- Non-persistence mode for getAuth: ability to not save sensitive credentials into the file
|
|
11
|
+
- `hideProgress` option for `login` that is false by default
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
- `showProgress` changed to `hideProgress` and it's false by default
|
|
15
|
+
- `persist` changed to `temporary` and it's false by default
|
|
11
16
|
|
|
12
17
|
## Removed
|
|
13
18
|
- Removed getter methods from api helpers to be able to re-use auth between api usages. Now better call `getAPI` and then run needed native methods on their own.
|
package/README.md
CHANGED
|
@@ -32,22 +32,22 @@ import { getProfiles, getCalendarAPI } from '@anmiles/google-api-wrapper';
|
|
|
32
32
|
require('./auth');
|
|
33
33
|
|
|
34
34
|
getProfiles().map(async (profile) => {
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
35
|
+
const calendarAPI = getCalendarAPI(profile); // auth is persisted by login() function from `auth.js`
|
|
36
|
+
const events = await getItems<GoogleApis.calendar_v3.Schema$Event, GoogleApis.calendar_v3.Params$Resource$Events$List>(calendarAPI.events, { timeMax: new Date().toISOString() });
|
|
37
|
+
events.forEach((event) => console.log(`Event: ${event.summary}`));
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
### Example with
|
|
42
|
+
### Example with temporary auth
|
|
43
43
|
``` js
|
|
44
44
|
/* videos.js */
|
|
45
45
|
|
|
46
46
|
import { getProfiles, getYoutubeAPI } from '@anmiles/google-api-wrapper';
|
|
47
47
|
|
|
48
48
|
getProfiles().map(async (profile) => {
|
|
49
|
-
const
|
|
50
|
-
const videos = await
|
|
49
|
+
const youtubeAPI = getYoutubeAPI(profile, { temporary: true });
|
|
50
|
+
const videos = await getItems<GoogleApis.youtube_v3.Schema$PlaylistItem, GoogleApis.youtube_v3.Params$Resource$Playlistitems$List>(youtubeAPI.playlistItems, { playlistId : 'LL', part : [ 'snippet' ], maxResults : 50 });
|
|
51
51
|
videos.forEach((video) => console.log(`Downloaded: ${video.snippet?.title}`));
|
|
52
52
|
});
|
|
53
53
|
|
package/dist/lib/api/shared.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
-
import type {
|
|
2
|
+
import type { CommonOptions } from '../../types';
|
|
3
3
|
export { getItems };
|
|
4
4
|
declare const _default: {
|
|
5
5
|
getItems: typeof getItems;
|
|
6
6
|
};
|
|
7
7
|
export default _default;
|
|
8
|
-
type CommonApi<
|
|
9
|
-
list: (params
|
|
8
|
+
type CommonApi<TItem> = {
|
|
9
|
+
list: (params?: {
|
|
10
10
|
pageToken: string | undefined;
|
|
11
|
-
}, options?: GoogleApis.Common.MethodOptions
|
|
11
|
+
}, options?: GoogleApis.Common.MethodOptions) => Promise<GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>>>;
|
|
12
|
+
} & {
|
|
13
|
+
list: (callback: (err: Error | null, res?: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>> | null) => void) => void;
|
|
12
14
|
};
|
|
13
15
|
type CommonResponse<TItem> = {
|
|
14
16
|
items?: TItem[];
|
|
@@ -17,4 +19,4 @@ type CommonResponse<TItem> = {
|
|
|
17
19
|
};
|
|
18
20
|
nextPageToken?: string | null | undefined;
|
|
19
21
|
};
|
|
20
|
-
declare function getItems<
|
|
22
|
+
declare function getItems<TItem>(api: CommonApi<TItem>, params: any, options?: CommonOptions): Promise<TItem[]>;
|
package/dist/lib/api/shared.js
CHANGED
|
@@ -5,18 +5,18 @@ const logger_1 = require("../logger");
|
|
|
5
5
|
const sleep_1 = require("../sleep");
|
|
6
6
|
exports.default = { getItems };
|
|
7
7
|
const requestInterval = 300;
|
|
8
|
-
async function getItems(api,
|
|
8
|
+
async function getItems(api, params, options) {
|
|
9
9
|
var _a, _b;
|
|
10
10
|
const items = [];
|
|
11
11
|
let pageToken = undefined;
|
|
12
12
|
do {
|
|
13
|
-
const response = await api.list({ ...
|
|
13
|
+
const response = await api.list({ ...params, pageToken });
|
|
14
14
|
(_a = response.data.items) === null || _a === void 0 ? void 0 : _a.forEach((item) => items.push(item));
|
|
15
|
-
if (options === null || options === void 0 ? void 0 : options.
|
|
15
|
+
if (!(options === null || options === void 0 ? void 0 : options.hideProgress)) {
|
|
16
16
|
(0, logger_1.log)(`Getting items (${items.length} of ${((_b = response.data.pageInfo) === null || _b === void 0 ? void 0 : _b.totalResults) || 'many'})...`);
|
|
17
17
|
}
|
|
18
|
-
pageToken = response.data.nextPageToken;
|
|
19
18
|
await (0, sleep_1.sleep)(requestInterval);
|
|
19
|
+
pageToken = response.data.nextPageToken;
|
|
20
20
|
} while (pageToken);
|
|
21
21
|
return items;
|
|
22
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/lib/api/shared.ts"],"names":[],"mappings":";;;AAEA,sCAAgC;AAChC,oCAAiC;AAGjC,kBAAe,EAAE,QAAQ,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/lib/api/shared.ts"],"names":[],"mappings":";;;AAEA,sCAAgC;AAChC,oCAAiC;AAGjC,kBAAe,EAAE,QAAQ,EAAE,CAAC;AAE5B,MAAM,eAAe,GAAG,GAAG,CAAC;AAmB5B,KAAK,UAAU,QAAQ,CAAQ,GAAqB,EAAE,MAAW,EAAE,OAAuB;;IACzF,MAAM,KAAK,GAAY,EAAE,CAAC;IAE1B,IAAI,SAAS,GAA8B,SAAS,CAAC;IAErD,GAAG;QACF,MAAM,QAAQ,GAA4D,MAAM,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QACnH,MAAA,QAAQ,CAAC,IAAI,CAAC,KAAK,0CAAE,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAEzD,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,CAAA,EAAE;YAC3B,IAAA,YAAG,EAAC,kBAAkB,KAAK,CAAC,MAAM,OAAO,CAAA,MAAA,QAAQ,CAAC,IAAI,CAAC,QAAQ,0CAAE,YAAY,KAAI,MAAM,MAAM,CAAC,CAAC;SAC/F;QAED,MAAM,IAAA,aAAK,EAAC,eAAe,CAAC,CAAC;QAC7B,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC;KACxC,QAAQ,SAAS,EAAE;IAEpB,OAAO,KAAK,CAAC;AACd,CAAC;AAxCQ,4BAAQ"}
|
package/dist/lib/auth.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
-
import type { AuthOptions } from '../types';
|
|
2
|
+
import type { CommonOptions, AuthOptions } from '../types';
|
|
3
3
|
export { login, getAuth };
|
|
4
4
|
declare const _default: {
|
|
5
5
|
login: typeof login;
|
|
6
6
|
getAuth: typeof getAuth;
|
|
7
7
|
};
|
|
8
8
|
export default _default;
|
|
9
|
-
declare function login(profile?: string): Promise<void>;
|
|
9
|
+
declare function login(profile?: string, options?: CommonOptions & AuthOptions): Promise<void>;
|
|
10
10
|
declare function getAuth(profile: string, options?: AuthOptions): Promise<GoogleApis.Common.OAuth2Client>;
|
package/dist/lib/auth.js
CHANGED
|
@@ -10,12 +10,16 @@ const profiles_1 = require("./profiles");
|
|
|
10
10
|
const secrets_1 = require("./secrets");
|
|
11
11
|
const auth_1 = __importDefault(require("./auth"));
|
|
12
12
|
exports.default = { login, getAuth };
|
|
13
|
-
async function login(profile) {
|
|
13
|
+
async function login(profile, options) {
|
|
14
14
|
const profiles = (0, profiles_1.getProfiles)().filter((p) => !profile || p === profile);
|
|
15
15
|
for (const profile of profiles) {
|
|
16
|
-
(0
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (!(options === null || options === void 0 ? void 0 : options.hideProgress)) {
|
|
17
|
+
(0, logger_1.warn)(`${profile} - logging in...`);
|
|
18
|
+
}
|
|
19
|
+
await auth_1.default.getAuth(profile, options);
|
|
20
|
+
if (!(options === null || options === void 0 ? void 0 : options.hideProgress)) {
|
|
21
|
+
(0, logger_1.info)(`${profile} - logged in successfully`);
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
}
|
|
21
25
|
exports.login = login;
|
|
@@ -24,6 +28,7 @@ async function getAuth(profile, options) {
|
|
|
24
28
|
const googleAuth = new googleapis_1.google.auth.OAuth2(secrets.web.client_id, secrets.web.client_secret, secrets.web.redirect_uris[0]);
|
|
25
29
|
const tokens = await (0, secrets_1.getCredentials)(profile, googleAuth, options);
|
|
26
30
|
googleAuth.setCredentials(tokens);
|
|
31
|
+
googleapis_1.google.options({ auth: googleAuth });
|
|
27
32
|
return googleAuth;
|
|
28
33
|
}
|
|
29
34
|
exports.getAuth = getAuth;
|
package/dist/lib/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/lib/auth.ts"],"names":[],"mappings":";;;;;;AAAA,2CAAoC;AAGpC,qCAAsC;AACtC,yCAAyC;AACzC,uCAAuD;AAEvD,kDAA0B;AAG1B,kBAAe,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAElC,KAAK,UAAU,KAAK,CAAC,OAAgB;
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/lib/auth.ts"],"names":[],"mappings":";;;;;;AAAA,2CAAoC;AAGpC,qCAAsC;AACtC,yCAAyC;AACzC,uCAAuD;AAEvD,kDAA0B;AAG1B,kBAAe,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAElC,KAAK,UAAU,KAAK,CAAC,OAAgB,EAAE,OAAqC;IAC3E,MAAM,QAAQ,GAAG,IAAA,sBAAW,GAAE,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,IAAI,CAAC,KAAK,OAAO,CAAC,CAAC;IAExE,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC/B,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,CAAA,EAAE;YAC3B,IAAA,aAAI,EAAC,GAAG,OAAO,kBAAkB,CAAC,CAAC;SACnC;QAED,MAAM,cAAI,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAErC,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,CAAA,EAAE;YAC3B,IAAA,aAAI,EAAC,GAAG,OAAO,2BAA2B,CAAC,CAAC;SAC5C;KACD;AACF,CAAC;AAjBQ,sBAAK;AAmBd,KAAK,UAAU,OAAO,CAAC,OAAe,EAAE,OAAqB;IAC5D,MAAM,OAAO,GAAG,IAAA,oBAAU,EAAC,OAAO,CAAC,CAAC;IAEpC,MAAM,UAAU,GAAG,IAAI,mBAAM,CAAC,IAAI,CAAC,MAAM,CACxC,OAAO,CAAC,GAAG,CAAC,SAAS,EACrB,OAAO,CAAC,GAAG,CAAC,aAAa,EACzB,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,CAC5B,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,IAAA,wBAAc,EAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAClE,UAAU,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAClC,mBAAM,CAAC,OAAO,CAAC,EAAE,IAAI,EAAG,UAAU,EAAE,CAAC,CAAC;IACtC,OAAO,UAAU,CAAC;AACnB,CAAC;AAhCe,0BAAO"}
|
package/dist/lib/secrets.js
CHANGED
|
@@ -50,9 +50,9 @@ function getSecrets(profile) {
|
|
|
50
50
|
exports.getSecrets = getSecrets;
|
|
51
51
|
async function getCredentials(profile, auth, options) {
|
|
52
52
|
const credentialsFile = (0, paths_1.getCredentialsFile)(profile);
|
|
53
|
-
return (options === null || options === void 0 ? void 0 : options.
|
|
54
|
-
?
|
|
55
|
-
: secrets_1.default.createCredentials(profile, auth);
|
|
53
|
+
return (options === null || options === void 0 ? void 0 : options.temporary)
|
|
54
|
+
? secrets_1.default.createCredentials(profile, auth)
|
|
55
|
+
: (0, jsonLib_1.getJSONAsync)(credentialsFile, () => secrets_1.default.createCredentials(profile, auth));
|
|
56
56
|
}
|
|
57
57
|
exports.getCredentials = getCredentials;
|
|
58
58
|
async function createCredentials(profile, auth) {
|
package/dist/lib/secrets.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"secrets.js","sourceRoot":"","sources":["../../src/lib/secrets.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAwB;AACxB,qDAAuC;AAGvC,uCAAkD;AAClD,qCAAuC;AACvC,mCAA4E;AAE5E,wDAAgC;AAGhC,kBAAe,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,iBAAiB,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,CAAC;AAE3H,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,WAAW,GAAI,oBAAoB,YAAY,gBAAgB,CAAC;AAEtE,SAAS,SAAS;IACjB,MAAM,UAAU,GAAG,IAAA,qBAAa,GAAE,CAAC;IACnC,MAAM,MAAM,GAAO,IAAA,iBAAO,EAAW,UAAU,EAAE,GAAG,EAAE,CAAC,IAAA,cAAK,EAAC,iBAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAU,CAAC,CAAC;IAC3G,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAS,UAAU,CAAC,OAAe;IAClC,MAAM,WAAW,GAAK,IAAA,sBAAc,EAAC,OAAO,CAAC,CAAC;IAC9C,MAAM,aAAa,GAAG,IAAA,iBAAO,EAAU,WAAW,EAAE,GAAG,EAAE,CAAC,IAAA,cAAK,EAAC,iBAAO,CAAC,eAAe,CAAC,OAAO,EAAE,WAAW,CAAC,CAAU,CAAC,CAAC;IACzH,iBAAO,CAAC,YAAY,CAAC,OAAO,EAAE,aAAa,EAAE,WAAW,C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,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QACtD,mCAAmC;QACnC,yBAAyB;QACzB,wBAAwB;QACxB,mCAAmC;QACnC,oDAAoD;QACpD,6CAA6C;QAC7C,+DAA+D;QAC/D,mDAAmD;QACnD,yCAAyC;QACzC,wCAAwC,WAAW,EAAE;QACrD,wBAAwB;QACxB,uEAAuE,OAAO,OAAO;QACrF,8BAA8B;KAC9B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/types/common.ts"],"names":[],"mappings":""}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './common';
|
|
2
2
|
export * from './secrets';
|
package/dist/types/index.js
CHANGED
|
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./common"), exports);
|
|
18
18
|
__exportStar(require("./secrets"), exports);
|
|
19
19
|
//# sourceMappingURL=index.js.map
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,4CAA0B"}
|
package/dist/types/secrets.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -30,6 +30,7 @@ jest.mock('googleapis', () => ({
|
|
|
30
30
|
auth : {
|
|
31
31
|
OAuth2 : jest.fn().mockImplementation(() => googleAuth),
|
|
32
32
|
},
|
|
33
|
+
options : jest.fn(),
|
|
33
34
|
},
|
|
34
35
|
}));
|
|
35
36
|
|
|
@@ -58,15 +59,38 @@ describe('src/lib/auth', () => {
|
|
|
58
59
|
expect(profiles.getProfiles).toBeCalledWith();
|
|
59
60
|
});
|
|
60
61
|
|
|
61
|
-
it('should auth all profiles
|
|
62
|
+
it('should auth all profiles', async () => {
|
|
62
63
|
await original.login();
|
|
63
64
|
|
|
64
65
|
allProfiles.forEach((profile) => {
|
|
65
|
-
expect(auth.getAuth).toBeCalledWith(profile,
|
|
66
|
+
expect(auth.getAuth).toBeCalledWith(profile, undefined);
|
|
66
67
|
});
|
|
67
68
|
});
|
|
68
69
|
|
|
69
|
-
it('should
|
|
70
|
+
it('should auth only specified profile', async () => {
|
|
71
|
+
await original.login('username1');
|
|
72
|
+
|
|
73
|
+
expect(auth.getAuth).toBeCalledWith('username1', undefined);
|
|
74
|
+
expect(auth.getAuth).not.toBeCalledWith('username2', undefined);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should pass temporariness for all profiles', async () => {
|
|
78
|
+
await original.login(undefined, { temporary : true });
|
|
79
|
+
|
|
80
|
+
expect(auth.getAuth).toBeCalledWith('username1', { temporary : true });
|
|
81
|
+
expect(auth.getAuth).toBeCalledWith('username2', { temporary : true });
|
|
82
|
+
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should pass temporariness only for specified profile', async () => {
|
|
86
|
+
await original.login('username1', { temporary : true });
|
|
87
|
+
|
|
88
|
+
expect(auth.getAuth).toBeCalledWith('username1', { temporary : true });
|
|
89
|
+
expect(auth.getAuth).not.toBeCalledWith('username2', { temporary : true });
|
|
90
|
+
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should show auth progress for all profiles by default', async () => {
|
|
70
94
|
await original.login();
|
|
71
95
|
|
|
72
96
|
expect(logger.warn).toBeCalledWith('username1 - logging in...');
|
|
@@ -75,11 +99,18 @@ describe('src/lib/auth', () => {
|
|
|
75
99
|
expect(logger.info).toBeCalledWith('username2 - logged in successfully');
|
|
76
100
|
});
|
|
77
101
|
|
|
78
|
-
it('should auth
|
|
102
|
+
it('should show auth progress for specified profile by default', async () => {
|
|
79
103
|
await original.login('username1');
|
|
80
104
|
|
|
81
|
-
expect(
|
|
82
|
-
expect(
|
|
105
|
+
expect(logger.warn).toBeCalledWith('username1 - logging in...');
|
|
106
|
+
expect(logger.info).toBeCalledWith('username1 - logged in successfully');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should not show auth progress if hidden', async () => {
|
|
110
|
+
await original.login(undefined, { hideProgress : true });
|
|
111
|
+
await original.login('username1', { hideProgress : true });
|
|
112
|
+
|
|
113
|
+
expect(logger.info).not.toBeCalled();
|
|
83
114
|
});
|
|
84
115
|
});
|
|
85
116
|
|
|
@@ -104,15 +135,16 @@ describe('src/lib/auth', () => {
|
|
|
104
135
|
expect(googleAuth.setCredentials).toBeCalledWith(credentials);
|
|
105
136
|
});
|
|
106
137
|
|
|
107
|
-
it('should pass
|
|
108
|
-
await original.getAuth(profile, {
|
|
138
|
+
it('should pass temporariness', async () => {
|
|
139
|
+
await original.getAuth(profile, { temporary : true });
|
|
109
140
|
|
|
110
|
-
expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth, {
|
|
141
|
+
expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth, { temporary : true });
|
|
111
142
|
});
|
|
112
143
|
|
|
113
|
-
it('should
|
|
114
|
-
|
|
115
|
-
|
|
144
|
+
it('should set google auth', async () => {
|
|
145
|
+
await original.getAuth(profile);
|
|
146
|
+
|
|
147
|
+
expect(google.options).toBeCalledWith({ auth : googleAuth });
|
|
116
148
|
});
|
|
117
149
|
});
|
|
118
150
|
});
|
|
@@ -176,27 +176,28 @@ describe('src/lib/secrets', () => {
|
|
|
176
176
|
json = credentialsJSON;
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
-
it('should
|
|
180
|
-
await original.getCredentials(profile, auth
|
|
179
|
+
it('should get json from credentials file by default', async () => {
|
|
180
|
+
await original.getCredentials(profile, auth);
|
|
181
181
|
|
|
182
182
|
expect(getJSONAsyncSpy).toBeCalled();
|
|
183
183
|
expect(getJSONAsyncSpy.mock.calls[0][0]).toEqual(credentialsFile);
|
|
184
184
|
});
|
|
185
185
|
|
|
186
|
-
it('should
|
|
187
|
-
await original.getCredentials(profile, auth, {
|
|
186
|
+
it('should get json from credentials file if temporariness explicitly unset', async () => {
|
|
187
|
+
await original.getCredentials(profile, auth, { temporary : false });
|
|
188
188
|
|
|
189
|
-
expect(getJSONAsyncSpy).
|
|
189
|
+
expect(getJSONAsyncSpy).toBeCalled();
|
|
190
|
+
expect(getJSONAsyncSpy.mock.calls[0][0]).toEqual(credentialsFile);
|
|
190
191
|
});
|
|
191
192
|
|
|
192
|
-
it('should not get json from credentials file if
|
|
193
|
-
await original.getCredentials(profile, auth);
|
|
193
|
+
it('should not get json from credentials file if temporariness set', async () => {
|
|
194
|
+
await original.getCredentials(profile, auth, { temporary : true });
|
|
194
195
|
|
|
195
196
|
expect(getJSONAsyncSpy).not.toBeCalled();
|
|
196
197
|
});
|
|
197
198
|
|
|
198
|
-
it('should fallback to createCredentials
|
|
199
|
-
await original.getCredentials(profile, auth
|
|
199
|
+
it('should fallback to createCredentials by default', async () => {
|
|
200
|
+
await original.getCredentials(profile, auth);
|
|
200
201
|
|
|
201
202
|
const fallback = getJSONAsyncSpy.mock.calls[0][1];
|
|
202
203
|
await fallback();
|
|
@@ -204,32 +205,35 @@ describe('src/lib/secrets', () => {
|
|
|
204
205
|
expect(secrets.createCredentials).toBeCalledWith(profile, auth);
|
|
205
206
|
});
|
|
206
207
|
|
|
207
|
-
it('should call createCredentials directly if
|
|
208
|
-
await original.getCredentials(profile, auth, {
|
|
208
|
+
it('should call createCredentials directly if temporariness explicitly unset', async () => {
|
|
209
|
+
await original.getCredentials(profile, auth, { temporary : false });
|
|
210
|
+
|
|
211
|
+
const fallback = getJSONAsyncSpy.mock.calls[0][1];
|
|
212
|
+
await fallback();
|
|
209
213
|
|
|
210
214
|
expect(secrets.createCredentials).toBeCalledWith(profile, auth);
|
|
211
215
|
});
|
|
212
216
|
|
|
213
|
-
it('should call createCredentials directly if
|
|
214
|
-
await original.getCredentials(profile, auth);
|
|
217
|
+
it('should call createCredentials directly if temporariness set', async () => {
|
|
218
|
+
await original.getCredentials(profile, auth, { temporary : true });
|
|
215
219
|
|
|
216
220
|
expect(secrets.createCredentials).toBeCalledWith(profile, auth);
|
|
217
221
|
});
|
|
218
222
|
|
|
219
|
-
it('should return credentials
|
|
220
|
-
const result = await original.getCredentials(profile, auth
|
|
223
|
+
it('should return credentials by default', async () => {
|
|
224
|
+
const result = await original.getCredentials(profile, auth);
|
|
221
225
|
|
|
222
226
|
expect(result).toEqual(credentialsJSON);
|
|
223
227
|
});
|
|
224
228
|
|
|
225
|
-
it('should return
|
|
226
|
-
const result = await original.getCredentials(profile, auth, {
|
|
229
|
+
it('should return credentials if temporariness explicitly unset', async () => {
|
|
230
|
+
const result = await original.getCredentials(profile, auth, { temporary : false });
|
|
227
231
|
|
|
228
|
-
expect(result).
|
|
232
|
+
expect(result).toEqual(credentialsJSON);
|
|
229
233
|
});
|
|
230
234
|
|
|
231
|
-
it('should return nothing if
|
|
232
|
-
const result = await original.getCredentials(profile, auth);
|
|
235
|
+
it('should return nothing if temporariness set', async () => {
|
|
236
|
+
const result = await original.getCredentials(profile, auth, { temporary : true });
|
|
233
237
|
|
|
234
238
|
expect(result).toBeUndefined();
|
|
235
239
|
});
|
|
@@ -24,10 +24,10 @@ describe('src/lib/api/calendar', () => {
|
|
|
24
24
|
expect(auth.getAuth).toBeCalledWith(profile, undefined);
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
-
it('should pass
|
|
28
|
-
await getAPI(profile, {
|
|
27
|
+
it('should pass temporariness', async () => {
|
|
28
|
+
await getAPI(profile, { temporary : true });
|
|
29
29
|
|
|
30
|
-
expect(auth.getAuth).toBeCalledWith(profile, {
|
|
30
|
+
expect(auth.getAuth).toBeCalledWith(profile, { temporary : true });
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
it('should get calendar api', async () => {
|
|
@@ -64,8 +64,8 @@ describe('src/lib/api/shared', () => {
|
|
|
64
64
|
});
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
it('should output progress
|
|
68
|
-
await original.getItems(api, args
|
|
67
|
+
it('should output progress by default', async () => {
|
|
68
|
+
await original.getItems(api, args);
|
|
69
69
|
|
|
70
70
|
expect(logger.log).toBeCalledTimes(response.length);
|
|
71
71
|
expect(logger.log).toBeCalledWith('Getting items (2 of 4)...');
|
|
@@ -73,8 +73,8 @@ describe('src/lib/api/shared', () => {
|
|
|
73
73
|
expect(logger.log).toBeCalledWith('Getting items (4 of 4)...');
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
it('should not output progress
|
|
77
|
-
await original.getItems(api, args);
|
|
76
|
+
it('should not output progress if hidden', async () => {
|
|
77
|
+
await original.getItems(api, args, { hideProgress : true });
|
|
78
78
|
|
|
79
79
|
expect(logger.log).not.toBeCalled();
|
|
80
80
|
});
|
|
@@ -24,10 +24,10 @@ describe('src/lib/api/youtube', () => {
|
|
|
24
24
|
expect(auth.getAuth).toBeCalledWith(profile, undefined);
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
-
it('should pass
|
|
28
|
-
await getAPI(profile, {
|
|
27
|
+
it('should pass temporariness', async () => {
|
|
28
|
+
await getAPI(profile, { temporary : true });
|
|
29
29
|
|
|
30
|
-
expect(auth.getAuth).toBeCalledWith(profile, {
|
|
30
|
+
expect(auth.getAuth).toBeCalledWith(profile, { temporary : true });
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
it('should get youtube api', async () => {
|
package/src/lib/api/shared.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
-
import type {
|
|
2
|
+
import type { CommonOptions } from '../../types';
|
|
3
3
|
import { log } from '../logger';
|
|
4
4
|
import { sleep } from '../sleep';
|
|
5
5
|
|
|
6
6
|
export { getItems };
|
|
7
7
|
export default { getItems };
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const requestInterval = 300;
|
|
10
|
+
|
|
11
|
+
type CommonApi<TItem> = {
|
|
10
12
|
list: (
|
|
11
|
-
params
|
|
12
|
-
options?: GoogleApis.Common.MethodOptions
|
|
13
|
-
) => Promise<GoogleApis.Common.GaxiosResponse<
|
|
13
|
+
params?: { pageToken: string | undefined },
|
|
14
|
+
options?: GoogleApis.Common.MethodOptions
|
|
15
|
+
) => Promise<GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>>>
|
|
16
|
+
} & {
|
|
17
|
+
list: (callback: (err: Error | null, res?: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>> | null) => void) => void
|
|
14
18
|
};
|
|
15
19
|
|
|
16
20
|
type CommonResponse<TItem> = {
|
|
@@ -21,28 +25,21 @@ type CommonResponse<TItem> = {
|
|
|
21
25
|
nextPageToken?: string | null | undefined
|
|
22
26
|
};
|
|
23
27
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
async function getItems<
|
|
27
|
-
TApi extends CommonApi<TArgs, TResponse>,
|
|
28
|
-
TItem,
|
|
29
|
-
TArgs,
|
|
30
|
-
TResponse extends CommonResponse<TItem>
|
|
31
|
-
>(api: TApi, args: TArgs, options?: GetItemsOptions): Promise<TItem[]> {
|
|
28
|
+
async function getItems<TItem>(api: CommonApi<TItem>, params: any, options?: CommonOptions): Promise<TItem[]> {
|
|
32
29
|
const items: TItem[] = [];
|
|
33
30
|
|
|
34
31
|
let pageToken: string | null | undefined = undefined;
|
|
35
32
|
|
|
36
33
|
do {
|
|
37
|
-
const response: GoogleApis.Common.GaxiosResponse<
|
|
34
|
+
const response: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>> = await api.list({ ...params, pageToken });
|
|
38
35
|
response.data.items?.forEach((item) => items.push(item));
|
|
39
36
|
|
|
40
|
-
if (options?.
|
|
37
|
+
if (!options?.hideProgress) {
|
|
41
38
|
log(`Getting items (${items.length} of ${response.data.pageInfo?.totalResults || 'many'})...`);
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
pageToken = response.data.nextPageToken;
|
|
45
41
|
await sleep(requestInterval);
|
|
42
|
+
pageToken = response.data.nextPageToken;
|
|
46
43
|
} while (pageToken);
|
|
47
44
|
|
|
48
45
|
return items;
|
package/src/lib/auth.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { google } from 'googleapis';
|
|
2
2
|
import type GoogleApis from 'googleapis';
|
|
3
|
-
import type { AuthOptions } from '../types';
|
|
3
|
+
import type { CommonOptions, AuthOptions } from '../types';
|
|
4
4
|
import { info, warn } from './logger';
|
|
5
5
|
import { getProfiles } from './profiles';
|
|
6
6
|
import { getCredentials, getSecrets } from './secrets';
|
|
@@ -10,13 +10,19 @@ import auth from './auth';
|
|
|
10
10
|
export { login, getAuth };
|
|
11
11
|
export default { login, getAuth };
|
|
12
12
|
|
|
13
|
-
async function login(profile?: string): Promise<void> {
|
|
13
|
+
async function login(profile?: string, options?: CommonOptions & AuthOptions): Promise<void> {
|
|
14
14
|
const profiles = getProfiles().filter((p) => !profile || p === profile);
|
|
15
15
|
|
|
16
16
|
for (const profile of profiles) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
if (!options?.hideProgress) {
|
|
18
|
+
warn(`${profile} - logging in...`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
await auth.getAuth(profile, options);
|
|
22
|
+
|
|
23
|
+
if (!options?.hideProgress) {
|
|
24
|
+
info(`${profile} - logged in successfully`);
|
|
25
|
+
}
|
|
20
26
|
}
|
|
21
27
|
}
|
|
22
28
|
|
|
@@ -31,5 +37,6 @@ async function getAuth(profile: string, options?: AuthOptions): Promise<GoogleAp
|
|
|
31
37
|
|
|
32
38
|
const tokens = await getCredentials(profile, googleAuth, options);
|
|
33
39
|
googleAuth.setCredentials(tokens);
|
|
40
|
+
google.options({ auth : googleAuth });
|
|
34
41
|
return googleAuth;
|
|
35
42
|
}
|
package/src/lib/secrets.ts
CHANGED
|
@@ -30,9 +30,9 @@ function getSecrets(profile: string): Secrets {
|
|
|
30
30
|
async function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Client, options?: AuthOptions): Promise<GoogleApis.Auth.Credentials> {
|
|
31
31
|
const credentialsFile = getCredentialsFile(profile);
|
|
32
32
|
|
|
33
|
-
return options?.
|
|
34
|
-
?
|
|
35
|
-
: secrets.createCredentials(profile, auth);
|
|
33
|
+
return options?.temporary
|
|
34
|
+
? secrets.createCredentials(profile, auth)
|
|
35
|
+
: getJSONAsync(credentialsFile, () => secrets.createCredentials(profile, auth));
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
async function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client): Promise<GoogleApis.Auth.Credentials> {
|
package/src/types/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './common';
|
|
2
2
|
export * from './secrets';
|
package/src/types/secrets.ts
CHANGED
package/dist/types/api.d.ts
DELETED
package/dist/types/api.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/types/api.ts"],"names":[],"mappings":""}
|
package/src/types/api.ts
DELETED