@anmiles/google-api-wrapper 11.0.0 → 13.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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/lib/api.d.ts +10 -10
- package/dist/lib/api.js +8 -8
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/lib/__tests__/api.test.ts +13 -13
- package/src/lib/api.ts +12 -12
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,7 @@ 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
|
+
## [13.0.0](../../tags/v13.0.0) - 2023-05-31
|
|
9
9
|
### Added
|
|
10
10
|
- Credentials can be revoked. Useful after working with temporary credentials to not let re-use them
|
|
11
11
|
- All existing google APIs are now available
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAuth = exports.login = exports.getProfiles = exports.createProfile = exports.
|
|
3
|
+
exports.getAuth = exports.login = exports.getProfiles = exports.createProfile = exports.getAPI = void 0;
|
|
4
4
|
var api_1 = require("./lib/api");
|
|
5
|
-
Object.defineProperty(exports, "
|
|
5
|
+
Object.defineProperty(exports, "getAPI", { enumerable: true, get: function () { return api_1.getAPI; } });
|
|
6
6
|
var profiles_1 = require("./lib/profiles");
|
|
7
7
|
Object.defineProperty(exports, "createProfile", { enumerable: true, get: function () { return profiles_1.createProfile; } });
|
|
8
8
|
Object.defineProperty(exports, "getProfiles", { enumerable: true, get: function () { return profiles_1.getProfiles; } });
|
package/dist/lib/api.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type GoogleApis from 'googleapis';
|
|
2
2
|
import type { AuthOptions, CommonOptions } from '../types';
|
|
3
|
-
type
|
|
3
|
+
type CommonAPI<TItem> = {
|
|
4
4
|
list: {
|
|
5
5
|
(params?: {
|
|
6
6
|
pageToken: string | undefined;
|
|
@@ -15,19 +15,19 @@ type CommonResponse<TItem> = {
|
|
|
15
15
|
};
|
|
16
16
|
nextPageToken?: string | null | undefined;
|
|
17
17
|
};
|
|
18
|
-
declare class
|
|
19
|
-
api: ReturnType<typeof
|
|
18
|
+
declare class API<TKey extends keyof typeof allAPIs> {
|
|
19
|
+
api: ReturnType<typeof allAPIs[TKey]>;
|
|
20
20
|
private auth;
|
|
21
21
|
private apiName;
|
|
22
22
|
private profile;
|
|
23
23
|
private authOptions?;
|
|
24
|
-
constructor(apiName:
|
|
24
|
+
constructor(apiName: TKey, profile: string, authOptions?: AuthOptions);
|
|
25
25
|
init(): Promise<void>;
|
|
26
|
-
getItems<TItem>(selectAPI: (api: ReturnType<typeof
|
|
26
|
+
getItems<TItem>(selectAPI: (api: ReturnType<typeof allAPIs[TKey]>) => CommonAPI<TItem>, params: any, options?: CommonOptions): Promise<TItem[]>;
|
|
27
27
|
revoke(): Promise<GoogleApis.Common.GaxiosResponse<import("google-auth-library/build/src/auth/oauth2client").RevokeCredentialsResult>>;
|
|
28
28
|
}
|
|
29
|
-
declare function
|
|
30
|
-
declare const
|
|
29
|
+
declare function getAPI<TKey extends keyof typeof allAPIs>(apiName: TKey, profile: string, authOptions?: AuthOptions): Promise<API<TKey>>;
|
|
30
|
+
declare const allAPIs: {
|
|
31
31
|
readonly abusiveexperiencereport: (auth: GoogleApis.Common.OAuth2Client) => GoogleApis.abusiveexperiencereport_v1.Abusiveexperiencereport;
|
|
32
32
|
readonly acceleratedmobilepageurl: (auth: GoogleApis.Common.OAuth2Client) => GoogleApis.acceleratedmobilepageurl_v1.Acceleratedmobilepageurl;
|
|
33
33
|
readonly accessapproval: (auth: GoogleApis.Common.OAuth2Client) => GoogleApis.accessapproval_v1.Accessapproval;
|
|
@@ -298,9 +298,9 @@ declare const allApis: {
|
|
|
298
298
|
readonly youtubeAnalytics: (auth: GoogleApis.Common.OAuth2Client) => GoogleApis.youtubeAnalytics_v1.Youtubeanalytics;
|
|
299
299
|
readonly youtubereporting: (auth: GoogleApis.Common.OAuth2Client) => GoogleApis.youtubereporting_v1.Youtubereporting;
|
|
300
300
|
};
|
|
301
|
-
export {
|
|
301
|
+
export { getAPI };
|
|
302
302
|
declare const _default: {
|
|
303
|
-
|
|
304
|
-
|
|
303
|
+
getAPI: typeof getAPI;
|
|
304
|
+
API: typeof API;
|
|
305
305
|
};
|
|
306
306
|
export default _default;
|
package/dist/lib/api.js
CHANGED
|
@@ -3,14 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.getAPI = void 0;
|
|
7
7
|
const googleapis_1 = require("googleapis");
|
|
8
8
|
const logger_1 = require("@anmiles/logger");
|
|
9
9
|
const sleep_1 = __importDefault(require("@anmiles/sleep"));
|
|
10
10
|
const auth_1 = require("./auth");
|
|
11
11
|
const secrets_1 = require("./secrets");
|
|
12
12
|
const requestInterval = 300;
|
|
13
|
-
class
|
|
13
|
+
class API {
|
|
14
14
|
constructor(apiName, profile, authOptions) {
|
|
15
15
|
this.apiName = apiName;
|
|
16
16
|
this.profile = profile;
|
|
@@ -18,7 +18,7 @@ class Api {
|
|
|
18
18
|
}
|
|
19
19
|
async init() {
|
|
20
20
|
this.auth = await (0, auth_1.getAuth)(this.profile, this.authOptions);
|
|
21
|
-
this.api =
|
|
21
|
+
this.api = allAPIs[this.apiName](this.auth);
|
|
22
22
|
}
|
|
23
23
|
async getItems(selectAPI, params, options) {
|
|
24
24
|
var _a, _b;
|
|
@@ -53,14 +53,14 @@ class Api {
|
|
|
53
53
|
return this.auth.revokeCredentials();
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
async function
|
|
57
|
-
const instance = new
|
|
56
|
+
async function getAPI(apiName, profile, authOptions) {
|
|
57
|
+
const instance = new API(apiName, profile, authOptions);
|
|
58
58
|
await instance.init();
|
|
59
59
|
return instance;
|
|
60
60
|
}
|
|
61
|
-
exports.
|
|
61
|
+
exports.getAPI = getAPI;
|
|
62
62
|
/* istanbul ignore next */
|
|
63
|
-
const
|
|
63
|
+
const allAPIs = {
|
|
64
64
|
abusiveexperiencereport: (auth) => googleapis_1.google.abusiveexperiencereport({ version: 'v1', auth }),
|
|
65
65
|
acceleratedmobilepageurl: (auth) => googleapis_1.google.acceleratedmobilepageurl({ version: 'v1', auth }),
|
|
66
66
|
accessapproval: (auth) => googleapis_1.google.accessapproval({ version: 'v1', auth }),
|
|
@@ -332,5 +332,5 @@ const allApis = {
|
|
|
332
332
|
youtubeAnalytics: (auth) => googleapis_1.google.youtubeAnalytics({ version: 'v1', auth }),
|
|
333
333
|
youtubereporting: (auth) => googleapis_1.google.youtubereporting({ version: 'v1', auth }),
|
|
334
334
|
};
|
|
335
|
-
exports.default = {
|
|
335
|
+
exports.default = { getAPI, API };
|
|
336
336
|
//# sourceMappingURL=api.js.map
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -50,7 +50,7 @@ const getAPI = <T>(items: Array<Array<T> | null>, pageTokens: Array<string | und
|
|
|
50
50
|
const args = { key : 'value' };
|
|
51
51
|
|
|
52
52
|
const profile = 'username1';
|
|
53
|
-
const
|
|
53
|
+
const calendarAPI = {
|
|
54
54
|
calendarList : getAPI(response, pageTokens),
|
|
55
55
|
};
|
|
56
56
|
|
|
@@ -68,7 +68,7 @@ beforeEach(() => {
|
|
|
68
68
|
|
|
69
69
|
jest.mock('googleapis', () => ({
|
|
70
70
|
google : {
|
|
71
|
-
calendar : jest.fn().mockImplementation(() =>
|
|
71
|
+
calendar : jest.fn().mockImplementation(() => calendarAPI),
|
|
72
72
|
},
|
|
73
73
|
}));
|
|
74
74
|
|
|
@@ -87,42 +87,42 @@ jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
|
|
|
87
87
|
jest.mock<Partial<typeof sleep>>('@anmiles/sleep', () => jest.fn());
|
|
88
88
|
|
|
89
89
|
describe('src/lib/api', () => {
|
|
90
|
-
describe('
|
|
90
|
+
describe('getAPI', () => {
|
|
91
91
|
it('should call getAuth', async () => {
|
|
92
|
-
await api.
|
|
92
|
+
await api.getAPI('calendar', profile);
|
|
93
93
|
|
|
94
94
|
expect(auth.getAuth).toHaveBeenCalledWith(profile, undefined);
|
|
95
95
|
});
|
|
96
96
|
|
|
97
97
|
it('should pass temporariness and scopes', async () => {
|
|
98
|
-
await api.
|
|
98
|
+
await api.getAPI('calendar', profile, { scopes, temporary : true });
|
|
99
99
|
|
|
100
100
|
expect(auth.getAuth).toHaveBeenCalledWith(profile, { scopes, temporary : true });
|
|
101
101
|
});
|
|
102
102
|
|
|
103
103
|
it('should get google api', async () => {
|
|
104
|
-
await api.
|
|
104
|
+
await api.getAPI('calendar', profile);
|
|
105
105
|
|
|
106
106
|
expect(google.calendar).toHaveBeenCalledWith({ version : 'v3', auth : googleAuth });
|
|
107
107
|
});
|
|
108
108
|
|
|
109
109
|
it('should return instance wrapper for google api', async () => {
|
|
110
|
-
const instance = await api.
|
|
110
|
+
const instance = await api.getAPI('calendar', profile, { scopes, temporary : true });
|
|
111
111
|
|
|
112
|
-
expect(instance).toEqual({ apiName : 'calendar', profile, authOptions : { scopes, temporary : true }, api :
|
|
112
|
+
expect(instance).toEqual({ apiName : 'calendar', profile, authOptions : { scopes, temporary : true }, api : calendarAPI, auth : googleAuth });
|
|
113
113
|
});
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
-
describe('
|
|
117
|
-
let instance: InstanceType<typeof api.
|
|
116
|
+
describe('API', () => {
|
|
117
|
+
let instance: InstanceType<typeof api.API<'calendar'>>;
|
|
118
118
|
|
|
119
119
|
beforeEach(async () => {
|
|
120
|
-
instance = await api.
|
|
120
|
+
instance = await api.getAPI('calendar', profile);
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
describe('constructor', () => {
|
|
124
124
|
it('should return instance', async () => {
|
|
125
|
-
const instance = new api.
|
|
125
|
+
const instance = new api.API('calendar', profile, { scopes, temporary : true });
|
|
126
126
|
|
|
127
127
|
expect(instance).toEqual({ apiName : 'calendar', profile, authOptions : { scopes, temporary : true } });
|
|
128
128
|
});
|
|
@@ -133,7 +133,7 @@ describe('src/lib/api', () => {
|
|
|
133
133
|
await instance.getItems((api) => api.calendarList, args);
|
|
134
134
|
|
|
135
135
|
pageTokens.forEach((pageToken) => {
|
|
136
|
-
expect(
|
|
136
|
+
expect(calendarAPI.calendarList.list).toHaveBeenCalledWith({ ...args, pageToken });
|
|
137
137
|
});
|
|
138
138
|
});
|
|
139
139
|
|
package/src/lib/api.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { deleteCredentials } from './secrets';
|
|
|
8
8
|
|
|
9
9
|
const requestInterval = 300;
|
|
10
10
|
|
|
11
|
-
type
|
|
11
|
+
type CommonAPI<TItem> = {
|
|
12
12
|
list: {
|
|
13
13
|
(params?: { pageToken: string | undefined }, options?: GoogleApis.Common.MethodOptions): Promise<GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>>>;
|
|
14
14
|
(callback: (err: Error | null, res?: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>> | null) => void): void;
|
|
@@ -23,15 +23,15 @@ type CommonResponse<TItem> = {
|
|
|
23
23
|
nextPageToken?: string | null | undefined
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
class
|
|
27
|
-
api: ReturnType<typeof
|
|
26
|
+
class API<TKey extends keyof typeof allAPIs> {
|
|
27
|
+
api: ReturnType<typeof allAPIs[TKey]>;
|
|
28
28
|
private auth: GoogleApis.Common.OAuth2Client;
|
|
29
29
|
|
|
30
|
-
private apiName:
|
|
30
|
+
private apiName: TKey;
|
|
31
31
|
private profile: string;
|
|
32
32
|
private authOptions?: AuthOptions;
|
|
33
33
|
|
|
34
|
-
constructor(apiName:
|
|
34
|
+
constructor(apiName: TKey, profile: string, authOptions?: AuthOptions) {
|
|
35
35
|
this.apiName = apiName;
|
|
36
36
|
this.profile = profile;
|
|
37
37
|
this.authOptions = authOptions;
|
|
@@ -39,10 +39,10 @@ class Api<TApi extends keyof typeof allApis> {
|
|
|
39
39
|
|
|
40
40
|
async init() {
|
|
41
41
|
this.auth = await getAuth(this.profile, this.authOptions);
|
|
42
|
-
this.api =
|
|
42
|
+
this.api = allAPIs[this.apiName](this.auth) as ReturnType<typeof allAPIs[TKey]>;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
async getItems<TItem>(selectAPI: (api: ReturnType<typeof
|
|
45
|
+
async getItems<TItem>(selectAPI: (api: ReturnType<typeof allAPIs[TKey]>) => CommonAPI<TItem>, params: any, options?: CommonOptions): Promise<TItem[]> {
|
|
46
46
|
const items: TItem[] = [];
|
|
47
47
|
|
|
48
48
|
let pageToken: string | null | undefined = undefined;
|
|
@@ -81,14 +81,14 @@ class Api<TApi extends keyof typeof allApis> {
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
async function
|
|
85
|
-
const instance = new
|
|
84
|
+
async function getAPI<TKey extends keyof typeof allAPIs>(apiName: TKey, profile: string, authOptions?: AuthOptions): Promise<API<TKey>> {
|
|
85
|
+
const instance = new API<TKey>(apiName, profile, authOptions);
|
|
86
86
|
await instance.init();
|
|
87
87
|
return instance;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/* istanbul ignore next */
|
|
91
|
-
const
|
|
91
|
+
const allAPIs = {
|
|
92
92
|
abusiveexperiencereport : (auth: GoogleApis.Common.OAuth2Client) => google.abusiveexperiencereport({ version : 'v1', auth }),
|
|
93
93
|
acceleratedmobilepageurl : (auth: GoogleApis.Common.OAuth2Client) => google.acceleratedmobilepageurl({ version : 'v1', auth }),
|
|
94
94
|
accessapproval : (auth: GoogleApis.Common.OAuth2Client) => google.accessapproval({ version : 'v1', auth }),
|
|
@@ -361,5 +361,5 @@ const allApis = {
|
|
|
361
361
|
youtubereporting : (auth: GoogleApis.Common.OAuth2Client) => google.youtubereporting({ version : 'v1', auth }),
|
|
362
362
|
} as const;
|
|
363
363
|
|
|
364
|
-
export {
|
|
365
|
-
export default {
|
|
364
|
+
export { getAPI };
|
|
365
|
+
export default { getAPI, API };
|