@anmiles/google-api-wrapper 4.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 +12 -0
- package/README.md +21 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -26
- package/dist/index.js.map +1 -1
- package/dist/lib/api/calendar.d.ts +2 -13
- package/dist/lib/api/calendar.js +3 -24
- package/dist/lib/api/calendar.js.map +1 -1
- 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/api/youtube.d.ts +2 -9
- package/dist/lib/api/youtube.js +3 -14
- package/dist/lib/api/youtube.js.map +1 -1
- package/dist/lib/auth.d.ts +3 -2
- package/dist/lib/auth.js +11 -6
- package/dist/lib/auth.js.map +1 -1
- package/dist/lib/secrets.d.ts +2 -2
- package/dist/lib/secrets.js +4 -2
- 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 +3 -0
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/lib/__tests__/auth.test.ts +47 -9
- package/src/lib/__tests__/secrets.test.ts +43 -3
- package/src/lib/api/__tests__/calendar.test.ts +16 -135
- package/src/lib/api/__tests__/shared.test.ts +22 -6
- package/src/lib/api/__tests__/youtube.test.ts +16 -78
- package/src/lib/api/calendar.ts +3 -23
- package/src/lib/api/shared.ts +13 -16
- package/src/lib/api/youtube.ts +3 -13
- package/src/lib/auth.ts +14 -6
- package/src/lib/secrets.ts +6 -3
- package/src/types/common.ts +3 -0
- package/src/types/index.ts +1 -1
- package/src/types/secrets.ts +5 -0
- package/dist/types/api.d.ts +0 -3
- package/dist/types/api.js.map +0 -1
- package/src/lib/api/__tests__/apiHelpers.ts +0 -18
- package/src/types/api.ts +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,18 @@ 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
|
+
## [6.0.0](../../tags/v6.0.0) - 2023-03-20
|
|
9
|
+
### Added
|
|
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
|
|
16
|
+
|
|
17
|
+
## Removed
|
|
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.
|
|
19
|
+
|
|
8
20
|
## [4.0.0](../../tags/v4.0.0) - 2023-03-12
|
|
9
21
|
### Changed
|
|
10
22
|
- Silent mode for getting items
|
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ Provides quick interface for getting google API data
|
|
|
12
12
|
> $ node ./videos.js
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
### Authorization
|
|
15
16
|
``` js
|
|
16
17
|
/* auth.js */
|
|
17
18
|
|
|
@@ -22,13 +23,31 @@ login("username");
|
|
|
22
23
|
|
|
23
24
|
```
|
|
24
25
|
|
|
26
|
+
### Example with persisted auth
|
|
27
|
+
``` js
|
|
28
|
+
/* calendar.js */
|
|
29
|
+
|
|
30
|
+
import { getProfiles, getCalendarAPI } from '@anmiles/google-api-wrapper';
|
|
31
|
+
|
|
32
|
+
require('./auth');
|
|
33
|
+
|
|
34
|
+
getProfiles().map(async (profile) => {
|
|
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
|
+
});
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Example with temporary auth
|
|
25
43
|
``` js
|
|
26
44
|
/* videos.js */
|
|
27
45
|
|
|
28
|
-
import { getProfiles,
|
|
46
|
+
import { getProfiles, getYoutubeAPI } from '@anmiles/google-api-wrapper';
|
|
29
47
|
|
|
30
48
|
getProfiles().map(async (profile) => {
|
|
31
|
-
const
|
|
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 });
|
|
32
51
|
videos.forEach((video) => console.log(`Downloaded: ${video.snippet?.title}`));
|
|
33
52
|
});
|
|
34
53
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { getAPI as getCalendarAPI } from './lib/api/calendar';
|
|
2
|
+
export { getAPI as getYoutubeAPI } from './lib/api/youtube';
|
|
3
3
|
export { getItems } from './lib/api/shared';
|
|
4
4
|
export { createProfile, getProfiles } from './lib/profiles';
|
|
5
5
|
export { login, getAuth } from './lib/auth';
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getAuth = exports.login = exports.getProfiles = exports.createProfile = exports.getItems = exports.
|
|
27
|
-
|
|
28
|
-
exports
|
|
3
|
+
exports.getAuth = exports.login = exports.getProfiles = exports.createProfile = exports.getItems = exports.getYoutubeAPI = exports.getCalendarAPI = void 0;
|
|
4
|
+
var calendar_1 = require("./lib/api/calendar");
|
|
5
|
+
Object.defineProperty(exports, "getCalendarAPI", { enumerable: true, get: function () { return calendar_1.getAPI; } });
|
|
6
|
+
var youtube_1 = require("./lib/api/youtube");
|
|
7
|
+
Object.defineProperty(exports, "getYoutubeAPI", { enumerable: true, get: function () { return youtube_1.getAPI; } });
|
|
29
8
|
var shared_1 = require("./lib/api/shared");
|
|
30
9
|
Object.defineProperty(exports, "getItems", { enumerable: true, get: function () { return shared_1.getItems; } });
|
|
31
10
|
var profiles_1 = require("./lib/profiles");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+CAA8D;AAArD,0GAAA,MAAM,OAAkB;AACjC,6CAA4D;AAAnD,wGAAA,MAAM,OAAiB;AAChC,2CAA4C;AAAnC,kGAAA,QAAQ,OAAA;AACjB,2CAA4D;AAAnD,yGAAA,aAAa,OAAA;AAAE,uGAAA,WAAW,OAAA;AACnC,mCAA4C;AAAnC,6FAAA,KAAK,OAAA;AAAE,+FAAA,OAAO,OAAA"}
|
|
@@ -1,14 +1,3 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
-
import type {
|
|
3
|
-
export
|
|
4
|
-
declare const _default: {
|
|
5
|
-
getAPI: typeof getAPI;
|
|
6
|
-
getEvents: typeof getEvents;
|
|
7
|
-
getCalendars: typeof getCalendars;
|
|
8
|
-
setEvent: typeof setEvent;
|
|
9
|
-
};
|
|
10
|
-
export default _default;
|
|
11
|
-
declare function getAPI(profile: string): Promise<GoogleApis.calendar_v3.Calendar>;
|
|
12
|
-
declare function getCalendars(profile: string, args: GoogleApis.calendar_v3.Params$Resource$Calendarlist$List, options?: GetItemsOptions): Promise<GoogleApis.calendar_v3.Schema$CalendarListEntry[]>;
|
|
13
|
-
declare function getEvents(profile: string, args: GoogleApis.calendar_v3.Params$Resource$Events$List, options?: GetItemsOptions): Promise<GoogleApis.calendar_v3.Schema$Event[]>;
|
|
14
|
-
declare function setEvent(profile: string, eventId: string | undefined, args: GoogleApis.calendar_v3.Params$Resource$Events$Update): Promise<void>;
|
|
2
|
+
import type { AuthOptions } from '../../types';
|
|
3
|
+
export declare function getAPI(profile: string, options?: AuthOptions): Promise<GoogleApis.calendar_v3.Calendar>;
|
package/dist/lib/api/calendar.js
CHANGED
|
@@ -1,35 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.getAPI = void 0;
|
|
7
4
|
const googleapis_1 = require("googleapis");
|
|
8
5
|
const auth_1 = require("../auth");
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
exports.default = { getAPI, getEvents, getCalendars, setEvent };
|
|
12
|
-
async function getAPI(profile) {
|
|
13
|
-
const googleAuth = await (0, auth_1.getAuth)(profile);
|
|
6
|
+
async function getAPI(profile, options) {
|
|
7
|
+
const googleAuth = await (0, auth_1.getAuth)(profile, options);
|
|
14
8
|
return googleapis_1.google.calendar({
|
|
15
9
|
version: 'v3',
|
|
16
10
|
auth: googleAuth,
|
|
17
11
|
});
|
|
18
12
|
}
|
|
19
13
|
exports.getAPI = getAPI;
|
|
20
|
-
async function getCalendars(profile, args, options) {
|
|
21
|
-
const api = await calendar_1.default.getAPI(profile);
|
|
22
|
-
return (0, shared_1.getItems)(api.calendarList, args, options);
|
|
23
|
-
}
|
|
24
|
-
exports.getCalendars = getCalendars;
|
|
25
|
-
async function getEvents(profile, args, options) {
|
|
26
|
-
const api = await calendar_1.default.getAPI(profile);
|
|
27
|
-
return (0, shared_1.getItems)(api.events, args, options);
|
|
28
|
-
}
|
|
29
|
-
exports.getEvents = getEvents;
|
|
30
|
-
async function setEvent(profile, eventId, args) {
|
|
31
|
-
const api = await calendar_1.default.getAPI(profile);
|
|
32
|
-
api.events.update({ eventId, ...args });
|
|
33
|
-
}
|
|
34
|
-
exports.setEvent = setEvent;
|
|
35
14
|
//# sourceMappingURL=calendar.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calendar.js","sourceRoot":"","sources":["../../../src/lib/api/calendar.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"calendar.js","sourceRoot":"","sources":["../../../src/lib/api/calendar.ts"],"names":[],"mappings":";;;AAAA,2CAAoC;AAGpC,kCAAkC;AAE3B,KAAK,UAAU,MAAM,CAAC,OAAe,EAAE,OAAqB;IAClE,MAAM,UAAU,GAAG,MAAM,IAAA,cAAO,EAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAEnD,OAAO,mBAAM,CAAC,QAAQ,CAAC;QACtB,OAAO,EAAG,IAAI;QACd,IAAI,EAAM,UAAU;KACpB,CAAC,CAAC;AACJ,CAAC;AAPD,wBAOC"}
|
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"}
|
|
@@ -1,10 +1,3 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
-
import type {
|
|
3
|
-
export
|
|
4
|
-
declare const _default: {
|
|
5
|
-
getAPI: typeof getAPI;
|
|
6
|
-
getPlaylistItems: typeof getPlaylistItems;
|
|
7
|
-
};
|
|
8
|
-
export default _default;
|
|
9
|
-
declare function getAPI(profile: string): Promise<GoogleApis.youtube_v3.Youtube>;
|
|
10
|
-
declare function getPlaylistItems(profile: string, args: GoogleApis.youtube_v3.Params$Resource$Playlistitems$List, options?: GetItemsOptions): Promise<GoogleApis.youtube_v3.Schema$PlaylistItem[]>;
|
|
2
|
+
import type { AuthOptions } from '../../types';
|
|
3
|
+
export declare function getAPI(profile: string, options?: AuthOptions): Promise<GoogleApis.youtube_v3.Youtube>;
|
package/dist/lib/api/youtube.js
CHANGED
|
@@ -1,25 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.getAPI = void 0;
|
|
7
4
|
const googleapis_1 = require("googleapis");
|
|
8
5
|
const auth_1 = require("../auth");
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
exports.default = { getAPI, getPlaylistItems };
|
|
12
|
-
async function getAPI(profile) {
|
|
13
|
-
const googleAuth = await (0, auth_1.getAuth)(profile);
|
|
6
|
+
async function getAPI(profile, options) {
|
|
7
|
+
const googleAuth = await (0, auth_1.getAuth)(profile, options);
|
|
14
8
|
return googleapis_1.google.youtube({
|
|
15
9
|
version: 'v3',
|
|
16
10
|
auth: googleAuth,
|
|
17
11
|
});
|
|
18
12
|
}
|
|
19
13
|
exports.getAPI = getAPI;
|
|
20
|
-
async function getPlaylistItems(profile, args, options) {
|
|
21
|
-
const api = await youtube_1.default.getAPI(profile);
|
|
22
|
-
return (0, shared_1.getItems)(api.playlistItems, args, options);
|
|
23
|
-
}
|
|
24
|
-
exports.getPlaylistItems = getPlaylistItems;
|
|
25
14
|
//# sourceMappingURL=youtube.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"youtube.js","sourceRoot":"","sources":["../../../src/lib/api/youtube.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"youtube.js","sourceRoot":"","sources":["../../../src/lib/api/youtube.ts"],"names":[],"mappings":";;;AAAA,2CAAoC;AAGpC,kCAAkC;AAE3B,KAAK,UAAU,MAAM,CAAC,OAAe,EAAE,OAAqB;IAClE,MAAM,UAAU,GAAG,MAAM,IAAA,cAAO,EAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAEnD,OAAO,mBAAM,CAAC,OAAO,CAAC;QACrB,OAAO,EAAG,IAAI;QACd,IAAI,EAAM,UAAU;KACpB,CAAC,CAAC;AACJ,CAAC;AAPD,wBAOC"}
|
package/dist/lib/auth.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
+
import type { CommonOptions, AuthOptions } from '../types';
|
|
2
3
|
export { login, getAuth };
|
|
3
4
|
declare const _default: {
|
|
4
5
|
login: typeof login;
|
|
5
6
|
getAuth: typeof getAuth;
|
|
6
7
|
};
|
|
7
8
|
export default _default;
|
|
8
|
-
declare function login(profile?: string): Promise<void>;
|
|
9
|
-
declare function getAuth(profile: string): Promise<GoogleApis.Common.OAuth2Client>;
|
|
9
|
+
declare function login(profile?: string, options?: CommonOptions & AuthOptions): Promise<void>;
|
|
10
|
+
declare function getAuth(profile: string, options?: AuthOptions): Promise<GoogleApis.Common.OAuth2Client>;
|
package/dist/lib/auth.js
CHANGED
|
@@ -10,20 +10,25 @@ 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;
|
|
22
|
-
async function getAuth(profile) {
|
|
26
|
+
async function getAuth(profile, options) {
|
|
23
27
|
const secrets = (0, secrets_1.getSecrets)(profile);
|
|
24
28
|
const googleAuth = new googleapis_1.google.auth.OAuth2(secrets.web.client_id, secrets.web.client_secret, secrets.web.redirect_uris[0]);
|
|
25
|
-
const tokens = await (0, secrets_1.getCredentials)(profile, googleAuth);
|
|
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;
|
|
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.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
|
-
import type { Secrets } from '../types';
|
|
2
|
+
import type { Secrets, AuthOptions } from '../types';
|
|
3
3
|
export { getSecrets, getCredentials };
|
|
4
4
|
declare const _default: {
|
|
5
5
|
getScopes: typeof getScopes;
|
|
@@ -13,7 +13,7 @@ declare const _default: {
|
|
|
13
13
|
export default _default;
|
|
14
14
|
declare function getScopes(): string[];
|
|
15
15
|
declare function getSecrets(profile: string): Secrets;
|
|
16
|
-
declare function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Client): Promise<GoogleApis.Auth.Credentials>;
|
|
16
|
+
declare function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Client, options?: AuthOptions): Promise<GoogleApis.Auth.Credentials>;
|
|
17
17
|
declare function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client): Promise<GoogleApis.Auth.Credentials>;
|
|
18
18
|
declare function checkSecrets(profile: string, secretsObject: Secrets, secretsFile: string): true | void;
|
|
19
19
|
declare function getScopesError(scopesFile: string): string;
|
package/dist/lib/secrets.js
CHANGED
|
@@ -48,9 +48,11 @@ function getSecrets(profile) {
|
|
|
48
48
|
return secretsObject;
|
|
49
49
|
}
|
|
50
50
|
exports.getSecrets = getSecrets;
|
|
51
|
-
async function getCredentials(profile, auth) {
|
|
51
|
+
async function getCredentials(profile, auth, options) {
|
|
52
52
|
const credentialsFile = (0, paths_1.getCredentialsFile)(profile);
|
|
53
|
-
return (0
|
|
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));
|
|
54
56
|
}
|
|
55
57
|
exports.getCredentials = getCredentials;
|
|
56
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,CAAC,CAAC;IAC1D,OAAO,aAAa,CAAC;AACtB,CAAC;AAjBQ,gCAAU;AAmBnB,KAAK,UAAU,cAAc,CAAC,OAAe,EAAE,IAAoC;
|
|
1
|
+
{"version":3,"file":"secrets.js","sourceRoot":"","sources":["../../src/lib/secrets.ts"],"names":[],"mappings":"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"}
|
|
@@ -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
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { getAPI as getCalendarAPI } from './lib/api/calendar';
|
|
2
|
+
export { getAPI as getYoutubeAPI } from './lib/api/youtube';
|
|
3
3
|
export { getItems } from './lib/api/shared';
|
|
4
4
|
export { createProfile, getProfiles } from './lib/profiles';
|
|
5
5
|
export { login, getAuth } from './lib/auth';
|
|
@@ -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
|
|
|
@@ -62,11 +63,34 @@ describe('src/lib/auth', () => {
|
|
|
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
|
|
|
@@ -91,7 +122,7 @@ describe('src/lib/auth', () => {
|
|
|
91
122
|
|
|
92
123
|
it('should get credentials', async () => {
|
|
93
124
|
await original.getAuth(profile);
|
|
94
|
-
expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth);
|
|
125
|
+
expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth, undefined);
|
|
95
126
|
});
|
|
96
127
|
|
|
97
128
|
it('should create OAuth2 instance', async () => {
|
|
@@ -104,9 +135,16 @@ describe('src/lib/auth', () => {
|
|
|
104
135
|
expect(googleAuth.setCredentials).toBeCalledWith(credentials);
|
|
105
136
|
});
|
|
106
137
|
|
|
107
|
-
it('should
|
|
108
|
-
|
|
109
|
-
|
|
138
|
+
it('should pass temporariness', async () => {
|
|
139
|
+
await original.getAuth(profile, { temporary : true });
|
|
140
|
+
|
|
141
|
+
expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth, { temporary : true });
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('should set google auth', async () => {
|
|
145
|
+
await original.getAuth(profile);
|
|
146
|
+
|
|
147
|
+
expect(google.options).toBeCalledWith({ auth : googleAuth });
|
|
110
148
|
});
|
|
111
149
|
});
|
|
112
150
|
});
|
|
@@ -176,14 +176,27 @@ describe('src/lib/secrets', () => {
|
|
|
176
176
|
json = credentialsJSON;
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
-
it('should get json from credentials file', async () => {
|
|
179
|
+
it('should get json from credentials file by default', async () => {
|
|
180
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
|
|
186
|
+
it('should get json from credentials file if temporariness explicitly unset', async () => {
|
|
187
|
+
await original.getCredentials(profile, auth, { temporary : false });
|
|
188
|
+
|
|
189
|
+
expect(getJSONAsyncSpy).toBeCalled();
|
|
190
|
+
expect(getJSONAsyncSpy.mock.calls[0][0]).toEqual(credentialsFile);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('should not get json from credentials file if temporariness set', async () => {
|
|
194
|
+
await original.getCredentials(profile, auth, { temporary : true });
|
|
195
|
+
|
|
196
|
+
expect(getJSONAsyncSpy).not.toBeCalled();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should fallback to createCredentials by default', async () => {
|
|
187
200
|
await original.getCredentials(profile, auth);
|
|
188
201
|
|
|
189
202
|
const fallback = getJSONAsyncSpy.mock.calls[0][1];
|
|
@@ -192,11 +205,38 @@ describe('src/lib/secrets', () => {
|
|
|
192
205
|
expect(secrets.createCredentials).toBeCalledWith(profile, auth);
|
|
193
206
|
});
|
|
194
207
|
|
|
195
|
-
it('should
|
|
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();
|
|
213
|
+
|
|
214
|
+
expect(secrets.createCredentials).toBeCalledWith(profile, auth);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should call createCredentials directly if temporariness set', async () => {
|
|
218
|
+
await original.getCredentials(profile, auth, { temporary : true });
|
|
219
|
+
|
|
220
|
+
expect(secrets.createCredentials).toBeCalledWith(profile, auth);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should return credentials by default', async () => {
|
|
196
224
|
const result = await original.getCredentials(profile, auth);
|
|
197
225
|
|
|
198
226
|
expect(result).toEqual(credentialsJSON);
|
|
199
227
|
});
|
|
228
|
+
|
|
229
|
+
it('should return credentials if temporariness explicitly unset', async () => {
|
|
230
|
+
const result = await original.getCredentials(profile, auth, { temporary : false });
|
|
231
|
+
|
|
232
|
+
expect(result).toEqual(credentialsJSON);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should return nothing if temporariness set', async () => {
|
|
236
|
+
const result = await original.getCredentials(profile, auth, { temporary : true });
|
|
237
|
+
|
|
238
|
+
expect(result).toBeUndefined();
|
|
239
|
+
});
|
|
200
240
|
});
|
|
201
241
|
|
|
202
242
|
describe('createCredentials', () => {
|
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
1
|
import { google } from 'googleapis';
|
|
3
2
|
import auth from '../../auth';
|
|
4
|
-
import
|
|
5
|
-
import shared from '../shared';
|
|
6
|
-
import apiHelpers from './apiHelpers';
|
|
7
|
-
|
|
8
|
-
const original = jest.requireActual('../calendar').default as typeof calendar;
|
|
9
|
-
jest.mock<Partial<typeof calendar>>('../calendar', () => ({
|
|
10
|
-
getAPI : jest.fn().mockImplementation(async () => ({ calendarList : calendarsAPI, events : eventsAPI })),
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
jest.mock<Partial<typeof shared>>('../shared', () => ({
|
|
14
|
-
getItems : jest.fn(),
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
jest.mock<Partial<typeof fs>>('fs', () => ({
|
|
18
|
-
writeFileSync : jest.fn(),
|
|
19
|
-
}));
|
|
3
|
+
import { getAPI } from '../calendar';
|
|
20
4
|
|
|
21
5
|
jest.mock('googleapis', () => ({
|
|
22
6
|
google : {
|
|
23
|
-
calendar : jest.fn().mockImplementation(() =>
|
|
7
|
+
calendar : jest.fn().mockImplementation(() => api),
|
|
24
8
|
},
|
|
25
9
|
}));
|
|
26
10
|
|
|
@@ -28,137 +12,34 @@ jest.mock<Partial<typeof auth>>('../../auth', () => ({
|
|
|
28
12
|
getAuth : jest.fn().mockImplementation(() => googleAuth),
|
|
29
13
|
}));
|
|
30
14
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const getItemsOptions = { showProgress : true };
|
|
35
|
-
|
|
36
|
-
const googleAuth = {
|
|
37
|
-
setCredentials : jest.fn(),
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const calendars: Array<{ id?: string | null | undefined, summary?: string, description?: string, hidden?: boolean }> = [
|
|
41
|
-
{ id : 'id1', summary : 'calendar 1', description : 'calendar 1 description', hidden : false },
|
|
42
|
-
{ id : 'id2', summary : 'calendar 2', description : 'calendar 2 description', hidden : undefined },
|
|
43
|
-
{ id : null, summary : 'calendar 3', description : undefined, hidden : true },
|
|
44
|
-
{ id : 'id4', summary : 'calendar 4', description : undefined, hidden : undefined },
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
const calendarsResponse = [
|
|
48
|
-
[ calendars[0], calendars[1] ],
|
|
49
|
-
null,
|
|
50
|
-
[ calendars[2], calendars[3] ],
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const events: Array<{ id?: string | null | undefined, summary?: string, source?: { url?: string, title?: string} }> = [
|
|
54
|
-
{ id : 'id1', summary : 'event 1', source : { title : 'source 1', url : 'https://example.com' } },
|
|
55
|
-
{ id : null, summary : 'event 2', source : { title : 'source 2', url : undefined } },
|
|
56
|
-
{ id : 'id3', summary : 'event 3', source : { title : undefined, url : undefined } },
|
|
57
|
-
{ id : 'id4', summary : 'event 4', source : undefined },
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
const eventsResponse = [
|
|
61
|
-
[ events[0], events[1] ],
|
|
62
|
-
null,
|
|
63
|
-
[ events[2], events[3] ],
|
|
64
|
-
];
|
|
65
|
-
|
|
66
|
-
const pageTokens = [
|
|
67
|
-
undefined,
|
|
68
|
-
'token1',
|
|
69
|
-
'token2',
|
|
70
|
-
];
|
|
71
|
-
|
|
72
|
-
const calendarsAPI = apiHelpers.getAPI(calendarsResponse, pageTokens);
|
|
73
|
-
const eventsAPI = apiHelpers.getAPI(eventsResponse, pageTokens);
|
|
15
|
+
const profile = 'username';
|
|
16
|
+
const api = 'api';
|
|
17
|
+
const googleAuth = 'googleAuth';
|
|
74
18
|
|
|
75
19
|
describe('src/lib/api/calendar', () => {
|
|
76
20
|
describe('getAPI', () => {
|
|
77
21
|
it('should call getAuth', async () => {
|
|
78
|
-
await
|
|
22
|
+
await getAPI(profile);
|
|
79
23
|
|
|
80
|
-
expect(auth.getAuth).toBeCalledWith(profile);
|
|
24
|
+
expect(auth.getAuth).toBeCalledWith(profile, undefined);
|
|
81
25
|
});
|
|
82
26
|
|
|
83
|
-
it('should
|
|
84
|
-
await
|
|
27
|
+
it('should pass temporariness', async () => {
|
|
28
|
+
await getAPI(profile, { temporary : true });
|
|
85
29
|
|
|
86
|
-
expect(
|
|
30
|
+
expect(auth.getAuth).toBeCalledWith(profile, { temporary : true });
|
|
87
31
|
});
|
|
88
32
|
|
|
89
|
-
it('should
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
expect(result).toEqual({ calendarList : calendarsAPI, events : eventsAPI });
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe('getCalendars', () => {
|
|
97
|
-
const args = { showHidden : true };
|
|
98
|
-
|
|
99
|
-
beforeEach(() => {
|
|
100
|
-
getItemsSpy.mockResolvedValue(calendars);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should get api', async () => {
|
|
104
|
-
await original.getCalendars(profile, args, getItemsOptions);
|
|
105
|
-
|
|
106
|
-
expect(calendar.getAPI).toBeCalledWith(profile);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should get items', async () => {
|
|
110
|
-
await original.getCalendars(profile, args, getItemsOptions);
|
|
111
|
-
|
|
112
|
-
expect(getItemsSpy).toBeCalledWith(calendarsAPI, args, getItemsOptions);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('should return calendars', async () => {
|
|
116
|
-
const result = await original.getCalendars(profile, args, getItemsOptions);
|
|
117
|
-
|
|
118
|
-
expect(result).toEqual(calendars);
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
describe('getEvents', () => {
|
|
123
|
-
const args = { timeMin : '2010-01-01T00:00:00', timeMax : '2019-12-31T23:59:59' };
|
|
124
|
-
|
|
125
|
-
beforeEach(() => {
|
|
126
|
-
getItemsSpy.mockResolvedValue(events);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('should get api', async () => {
|
|
130
|
-
await original.getEvents(profile, args, getItemsOptions);
|
|
131
|
-
|
|
132
|
-
expect(calendar.getAPI).toBeCalledWith(profile);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it('should get items', async () => {
|
|
136
|
-
await original.getEvents(profile, args, getItemsOptions);
|
|
137
|
-
|
|
138
|
-
expect(getItemsSpy).toBeCalledWith(eventsAPI, args, getItemsOptions);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('should return events', async () => {
|
|
142
|
-
const result = await original.getEvents(profile, args, getItemsOptions);
|
|
143
|
-
|
|
144
|
-
expect(result).toEqual(events);
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
describe('setEvent', () => {
|
|
149
|
-
const eventId = 'eventId';
|
|
150
|
-
const args = { requestBody : { summary : 'summary' } };
|
|
151
|
-
|
|
152
|
-
it('should get api', async () => {
|
|
153
|
-
await original.setEvent(profile, eventId, args);
|
|
33
|
+
it('should get calendar api', async () => {
|
|
34
|
+
await getAPI(profile);
|
|
154
35
|
|
|
155
|
-
expect(calendar
|
|
36
|
+
expect(google.calendar).toBeCalledWith({ version : 'v3', auth : googleAuth });
|
|
156
37
|
});
|
|
157
38
|
|
|
158
|
-
it('should
|
|
159
|
-
await
|
|
39
|
+
it('should return calendar api', async () => {
|
|
40
|
+
const result = await getAPI(profile);
|
|
160
41
|
|
|
161
|
-
expect(
|
|
42
|
+
expect(result).toEqual(api);
|
|
162
43
|
});
|
|
163
44
|
});
|
|
164
45
|
});
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import logger from '../../logger';
|
|
2
2
|
import sleep from '../../sleep';
|
|
3
3
|
import shared from '../shared';
|
|
4
|
-
import apiHelpers from './apiHelpers';
|
|
5
4
|
|
|
6
5
|
const original = jest.requireActual('../shared').default as typeof shared;
|
|
7
6
|
jest.mock<Partial<typeof shared>>('../shared', () => ({
|
|
@@ -35,7 +34,24 @@ const pageTokens = [
|
|
|
35
34
|
'token2',
|
|
36
35
|
];
|
|
37
36
|
|
|
38
|
-
const
|
|
37
|
+
const getAPI = <T>(items: Array<Array<T> | null>, pageTokens: Array<string | undefined>) => ({
|
|
38
|
+
list : jest.fn().mockImplementation(async ({ pageToken }: {pageToken?: string}) => {
|
|
39
|
+
const index = pageTokens.indexOf(pageToken);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
data : {
|
|
43
|
+
items : items[index],
|
|
44
|
+
nextPageToken : pageTokens[index + 1],
|
|
45
|
+
pageInfo : !items[index] ? null : {
|
|
46
|
+
totalResults : items.reduce((sum, list) => sum + (list?.length || 0), 0),
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}),
|
|
51
|
+
update : jest.fn(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const api = getAPI(response, pageTokens);
|
|
39
55
|
const args = { key : 'value' };
|
|
40
56
|
|
|
41
57
|
describe('src/lib/api/shared', () => {
|
|
@@ -48,8 +64,8 @@ describe('src/lib/api/shared', () => {
|
|
|
48
64
|
});
|
|
49
65
|
});
|
|
50
66
|
|
|
51
|
-
it('should output progress
|
|
52
|
-
await original.getItems(api, args
|
|
67
|
+
it('should output progress by default', async () => {
|
|
68
|
+
await original.getItems(api, args);
|
|
53
69
|
|
|
54
70
|
expect(logger.log).toBeCalledTimes(response.length);
|
|
55
71
|
expect(logger.log).toBeCalledWith('Getting items (2 of 4)...');
|
|
@@ -57,8 +73,8 @@ describe('src/lib/api/shared', () => {
|
|
|
57
73
|
expect(logger.log).toBeCalledWith('Getting items (4 of 4)...');
|
|
58
74
|
});
|
|
59
75
|
|
|
60
|
-
it('should not output progress
|
|
61
|
-
await original.getItems(api, args);
|
|
76
|
+
it('should not output progress if hidden', async () => {
|
|
77
|
+
await original.getItems(api, args, { hideProgress : true });
|
|
62
78
|
|
|
63
79
|
expect(logger.log).not.toBeCalled();
|
|
64
80
|
});
|
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
1
|
import { google } from 'googleapis';
|
|
3
2
|
import auth from '../../auth';
|
|
4
|
-
import
|
|
5
|
-
import shared from '../shared';
|
|
6
|
-
import apiHelpers from './apiHelpers';
|
|
7
|
-
|
|
8
|
-
const original = jest.requireActual('../youtube').default as typeof youtube;
|
|
9
|
-
jest.mock<Partial<typeof youtube>>('../youtube', () => ({
|
|
10
|
-
getAPI : jest.fn().mockImplementation(async () => ({ playlistItems : playlistItemsAPI })),
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
jest.mock<Partial<typeof shared>>('../shared', () => ({
|
|
14
|
-
getItems : jest.fn(),
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
jest.mock<Partial<typeof fs>>('fs', () => ({
|
|
18
|
-
writeFileSync : jest.fn(),
|
|
19
|
-
}));
|
|
3
|
+
import { getAPI } from '../youtube';
|
|
20
4
|
|
|
21
5
|
jest.mock('googleapis', () => ({
|
|
22
6
|
google : {
|
|
23
|
-
youtube : jest.fn().mockImplementation(() =>
|
|
7
|
+
youtube : jest.fn().mockImplementation(() => api),
|
|
24
8
|
},
|
|
25
9
|
}));
|
|
26
10
|
|
|
@@ -28,80 +12,34 @@ jest.mock<Partial<typeof auth>>('../../auth', () => ({
|
|
|
28
12
|
getAuth : jest.fn().mockImplementation(() => googleAuth),
|
|
29
13
|
}));
|
|
30
14
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const getItemsOptions = { showProgress : true };
|
|
35
|
-
|
|
36
|
-
const googleAuth = {
|
|
37
|
-
setCredentials : jest.fn(),
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const playlistItems: Array<{ id?: string | null | undefined, snippet?: { title?: string, resourceId?: { videoId?: string } } }> = [
|
|
41
|
-
{ id : 'id1', snippet : { title : 'video1', resourceId : { videoId : 'video1Id' } } },
|
|
42
|
-
{ id : null, snippet : { title : 'video2', resourceId : { videoId : undefined } } },
|
|
43
|
-
{ id : 'id3', snippet : { title : undefined, resourceId : undefined } },
|
|
44
|
-
{ id : 'id4', snippet : undefined },
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
const playlistItemsResponse = [
|
|
48
|
-
[ playlistItems[0], playlistItems[1] ],
|
|
49
|
-
null,
|
|
50
|
-
[ playlistItems[2], playlistItems[3] ],
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const pageTokens = [
|
|
54
|
-
undefined,
|
|
55
|
-
'token1',
|
|
56
|
-
'token2',
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
const playlistItemsAPI = apiHelpers.getAPI(playlistItemsResponse, pageTokens);
|
|
15
|
+
const profile = 'username';
|
|
16
|
+
const api = 'api';
|
|
17
|
+
const googleAuth = 'googleAuth';
|
|
60
18
|
|
|
61
19
|
describe('src/lib/api/youtube', () => {
|
|
62
20
|
describe('getAPI', () => {
|
|
63
21
|
it('should call getAuth', async () => {
|
|
64
|
-
await
|
|
65
|
-
|
|
66
|
-
expect(auth.getAuth).toBeCalledWith(profile);
|
|
67
|
-
});
|
|
22
|
+
await getAPI(profile);
|
|
68
23
|
|
|
69
|
-
|
|
70
|
-
await original.getAPI(profile);
|
|
71
|
-
|
|
72
|
-
expect(google.youtube).toBeCalledWith({ version : 'v3', auth : googleAuth });
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should return youtube api', async () => {
|
|
76
|
-
const result = await original.getAPI(profile);
|
|
77
|
-
|
|
78
|
-
expect(result).toEqual({ playlistItems : playlistItemsAPI });
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
describe('getPlaylistItems', () => {
|
|
83
|
-
const args = { playlistId : 'LL', part : [ 'snippet' ], maxResults : 50 };
|
|
84
|
-
|
|
85
|
-
beforeEach(() => {
|
|
86
|
-
getItemsSpy.mockResolvedValue(playlistItems);
|
|
24
|
+
expect(auth.getAuth).toBeCalledWith(profile, undefined);
|
|
87
25
|
});
|
|
88
26
|
|
|
89
|
-
it('should
|
|
90
|
-
await
|
|
27
|
+
it('should pass temporariness', async () => {
|
|
28
|
+
await getAPI(profile, { temporary : true });
|
|
91
29
|
|
|
92
|
-
expect(
|
|
30
|
+
expect(auth.getAuth).toBeCalledWith(profile, { temporary : true });
|
|
93
31
|
});
|
|
94
32
|
|
|
95
|
-
it('should
|
|
96
|
-
await
|
|
33
|
+
it('should get youtube api', async () => {
|
|
34
|
+
await getAPI(profile);
|
|
97
35
|
|
|
98
|
-
expect(
|
|
36
|
+
expect(google.youtube).toBeCalledWith({ version : 'v3', auth : googleAuth });
|
|
99
37
|
});
|
|
100
38
|
|
|
101
|
-
it('should return
|
|
102
|
-
const result = await
|
|
39
|
+
it('should return youtube api', async () => {
|
|
40
|
+
const result = await getAPI(profile);
|
|
103
41
|
|
|
104
|
-
expect(result).toEqual(
|
|
42
|
+
expect(result).toEqual(api);
|
|
105
43
|
});
|
|
106
44
|
});
|
|
107
45
|
});
|
package/src/lib/api/calendar.ts
CHANGED
|
@@ -1,33 +1,13 @@
|
|
|
1
1
|
import { google } from 'googleapis';
|
|
2
2
|
import type GoogleApis from 'googleapis';
|
|
3
|
-
import type {
|
|
3
|
+
import type { AuthOptions } from '../../types';
|
|
4
4
|
import { getAuth } from '../auth';
|
|
5
|
-
import { getItems } from './shared';
|
|
6
|
-
import calendar from './calendar';
|
|
7
5
|
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
async function getAPI(profile: string): Promise<GoogleApis.calendar_v3.Calendar> {
|
|
12
|
-
const googleAuth = await getAuth(profile);
|
|
6
|
+
export async function getAPI(profile: string, options?: AuthOptions): Promise<GoogleApis.calendar_v3.Calendar> {
|
|
7
|
+
const googleAuth = await getAuth(profile, options);
|
|
13
8
|
|
|
14
9
|
return google.calendar({
|
|
15
10
|
version : 'v3',
|
|
16
11
|
auth : googleAuth,
|
|
17
12
|
});
|
|
18
13
|
}
|
|
19
|
-
|
|
20
|
-
async function getCalendars(profile: string, args: GoogleApis.calendar_v3.Params$Resource$Calendarlist$List, options?: GetItemsOptions): Promise<GoogleApis.calendar_v3.Schema$CalendarListEntry[]> {
|
|
21
|
-
const api = await calendar.getAPI(profile);
|
|
22
|
-
return getItems(api.calendarList, args, options);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async function getEvents(profile: string, args: GoogleApis.calendar_v3.Params$Resource$Events$List, options?: GetItemsOptions): Promise<GoogleApis.calendar_v3.Schema$Event[]> {
|
|
26
|
-
const api = await calendar.getAPI(profile);
|
|
27
|
-
return getItems(api.events, args, options);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async function setEvent(profile: string, eventId: string | undefined, args: GoogleApis.calendar_v3.Params$Resource$Events$Update) {
|
|
31
|
-
const api = await calendar.getAPI(profile);
|
|
32
|
-
api.events.update({ eventId, ...args });
|
|
33
|
-
}
|
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/api/youtube.ts
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
1
|
import { google } from 'googleapis';
|
|
2
2
|
import type GoogleApis from 'googleapis';
|
|
3
|
-
import type {
|
|
3
|
+
import type { AuthOptions } from '../../types';
|
|
4
4
|
import { getAuth } from '../auth';
|
|
5
|
-
import { getItems } from './shared';
|
|
6
|
-
import youtube from './youtube';
|
|
7
5
|
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
async function getAPI(profile: string): Promise<GoogleApis.youtube_v3.Youtube> {
|
|
12
|
-
const googleAuth = await getAuth(profile);
|
|
6
|
+
export async function getAPI(profile: string, options?: AuthOptions): Promise<GoogleApis.youtube_v3.Youtube> {
|
|
7
|
+
const googleAuth = await getAuth(profile, options);
|
|
13
8
|
|
|
14
9
|
return google.youtube({
|
|
15
10
|
version : 'v3',
|
|
16
11
|
auth : googleAuth,
|
|
17
12
|
});
|
|
18
13
|
}
|
|
19
|
-
|
|
20
|
-
async function getPlaylistItems(profile: string, args: GoogleApis.youtube_v3.Params$Resource$Playlistitems$List, options?: GetItemsOptions): Promise<GoogleApis.youtube_v3.Schema$PlaylistItem[]> {
|
|
21
|
-
const api = await youtube.getAPI(profile);
|
|
22
|
-
return getItems(api.playlistItems, args, options);
|
|
23
|
-
}
|
package/src/lib/auth.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { google } from 'googleapis';
|
|
2
2
|
import type GoogleApis from 'googleapis';
|
|
3
|
+
import type { CommonOptions, AuthOptions } from '../types';
|
|
3
4
|
import { info, warn } from './logger';
|
|
4
5
|
import { getProfiles } from './profiles';
|
|
5
6
|
import { getCredentials, getSecrets } from './secrets';
|
|
@@ -9,17 +10,23 @@ import auth from './auth';
|
|
|
9
10
|
export { login, getAuth };
|
|
10
11
|
export default { login, getAuth };
|
|
11
12
|
|
|
12
|
-
async function login(profile?: string): Promise<void> {
|
|
13
|
+
async function login(profile?: string, options?: CommonOptions & AuthOptions): Promise<void> {
|
|
13
14
|
const profiles = getProfiles().filter((p) => !profile || p === profile);
|
|
14
15
|
|
|
15
16
|
for (const profile of profiles) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
+
}
|
|
19
26
|
}
|
|
20
27
|
}
|
|
21
28
|
|
|
22
|
-
async function getAuth(profile: string): Promise<GoogleApis.Common.OAuth2Client> {
|
|
29
|
+
async function getAuth(profile: string, options?: AuthOptions): Promise<GoogleApis.Common.OAuth2Client> {
|
|
23
30
|
const secrets = getSecrets(profile);
|
|
24
31
|
|
|
25
32
|
const googleAuth = new google.auth.OAuth2(
|
|
@@ -28,7 +35,8 @@ async function getAuth(profile: string): Promise<GoogleApis.Common.OAuth2Client>
|
|
|
28
35
|
secrets.web.redirect_uris[0],
|
|
29
36
|
);
|
|
30
37
|
|
|
31
|
-
const tokens = await getCredentials(profile, googleAuth);
|
|
38
|
+
const tokens = await getCredentials(profile, googleAuth, options);
|
|
32
39
|
googleAuth.setCredentials(tokens);
|
|
40
|
+
google.options({ auth : googleAuth });
|
|
33
41
|
return googleAuth;
|
|
34
42
|
}
|
package/src/lib/secrets.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import http from 'http';
|
|
2
2
|
import * as colorette from 'colorette';
|
|
3
3
|
import type GoogleApis from 'googleapis';
|
|
4
|
-
import type { Secrets } from '../types';
|
|
4
|
+
import type { Secrets, AuthOptions } from '../types';
|
|
5
5
|
import { getJSON, getJSONAsync } from './jsonLib';
|
|
6
6
|
import { info, error } from './logger';
|
|
7
7
|
import { getScopesFile, getSecretsFile, getCredentialsFile } from './paths';
|
|
@@ -27,9 +27,12 @@ function getSecrets(profile: string): Secrets {
|
|
|
27
27
|
return secretsObject;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
async function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Client): Promise<GoogleApis.Auth.Credentials> {
|
|
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?.temporary
|
|
34
|
+
? secrets.createCredentials(profile, auth)
|
|
35
|
+
: getJSONAsync(credentialsFile, () => secrets.createCredentials(profile, auth));
|
|
33
36
|
}
|
|
34
37
|
|
|
35
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":""}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const getAPI = <T>(items: Array<Array<T> | null>, pageTokens: Array<string | undefined>) => ({
|
|
2
|
-
list : jest.fn().mockImplementation(async ({ pageToken }: {pageToken?: string}) => {
|
|
3
|
-
const index = pageTokens.indexOf(pageToken);
|
|
4
|
-
|
|
5
|
-
return {
|
|
6
|
-
data : {
|
|
7
|
-
items : items[index],
|
|
8
|
-
nextPageToken : pageTokens[index + 1],
|
|
9
|
-
pageInfo : !items[index] ? null : {
|
|
10
|
-
totalResults : items.reduce((sum, list) => sum + (list?.length || 0), 0),
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
}),
|
|
15
|
-
update : jest.fn(),
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
export default { getAPI };
|
package/src/types/api.ts
DELETED