@anmiles/google-api-wrapper 9.0.0 → 11.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.
@@ -1,93 +0,0 @@
1
- import logger from '@anmiles/logger';
2
- import sleep from '@anmiles/sleep';
3
- import shared from '../shared';
4
-
5
- const original = jest.requireActual('../shared').default as typeof shared;
6
- jest.mock<Partial<typeof shared>>('../shared', () => ({
7
- getItems : jest.fn().mockImplementation(async () => items),
8
- }));
9
-
10
- jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
11
- log : jest.fn(),
12
- }));
13
-
14
- jest.mock<Partial<typeof sleep>>('@anmiles/sleep', () => jest.fn());
15
-
16
- const items: Array<{ data: string}> = [
17
- { data : 'first' },
18
- { data : 'second' },
19
- { data : 'third' },
20
- { data : 'forth' },
21
- ];
22
-
23
- const response = [
24
- [ items[0], items[1] ],
25
- null,
26
- [ items[2], items[3] ],
27
- ];
28
-
29
- const pageTokens = [
30
- undefined,
31
- 'token1',
32
- 'token2',
33
- ];
34
-
35
- const getAPI = <T>(items: Array<Array<T> | null>, pageTokens: Array<string | undefined>) => ({
36
- list : jest.fn().mockImplementation(async ({ pageToken }: {pageToken?: string}) => {
37
- const index = pageTokens.indexOf(pageToken);
38
-
39
- return {
40
- data : {
41
- items : items[index],
42
- nextPageToken : pageTokens[index + 1],
43
- pageInfo : !items[index] ? null : {
44
- totalResults : items.reduce((sum, list) => sum + (list?.length || 0), 0),
45
- },
46
- },
47
- };
48
- }),
49
- update : jest.fn(),
50
- });
51
-
52
- const api = getAPI(response, pageTokens);
53
- const args = { key : 'value' };
54
-
55
- describe('src/lib/api/shared', () => {
56
- describe('getItems', () => {
57
- it('should call API list method for each page', async () => {
58
- await original.getItems(api, args);
59
-
60
- pageTokens.forEach((pageToken) => {
61
- expect(api.list).toHaveBeenCalledWith({ ...args, pageToken });
62
- });
63
- });
64
-
65
- it('should output progress by default', async () => {
66
- await original.getItems(api, args);
67
-
68
- expect(logger.log).toHaveBeenCalledTimes(response.length);
69
- expect(logger.log).toHaveBeenCalledWith('Getting items (2 of 4)...');
70
- expect(logger.log).toHaveBeenCalledWith('Getting items (2 of many)...');
71
- expect(logger.log).toHaveBeenCalledWith('Getting items (4 of 4)...');
72
- });
73
-
74
- it('should not output progress if hidden', async () => {
75
- await original.getItems(api, args, { hideProgress : true });
76
-
77
- expect(logger.log).not.toHaveBeenCalled();
78
- });
79
-
80
- it('should sleep after reach request', async () => {
81
- await original.getItems(api, args);
82
-
83
- expect(sleep).toHaveBeenCalledTimes(response.length);
84
- expect(sleep).toHaveBeenCalledWith(300);
85
- });
86
-
87
- it('should return items data', async () => {
88
- const items = await original.getItems(api, args);
89
-
90
- expect(items).toEqual(items);
91
- });
92
- });
93
- });
@@ -1,45 +0,0 @@
1
- import { google } from 'googleapis';
2
- import auth from '../../auth';
3
- import { getAPI } from '../youtube';
4
-
5
- jest.mock('googleapis', () => ({
6
- google : {
7
- youtube : jest.fn().mockImplementation(() => api),
8
- },
9
- }));
10
-
11
- jest.mock<Partial<typeof auth>>('../../auth', () => ({
12
- getAuth : jest.fn().mockImplementation(() => googleAuth),
13
- }));
14
-
15
- const profile = 'username';
16
- const api = 'api';
17
- const googleAuth = 'googleAuth';
18
-
19
- describe('src/lib/api/youtube', () => {
20
- describe('getAPI', () => {
21
- it('should call getAuth', async () => {
22
- await getAPI(profile);
23
-
24
- expect(auth.getAuth).toHaveBeenCalledWith(profile, undefined);
25
- });
26
-
27
- it('should pass temporariness', async () => {
28
- await getAPI(profile, { temporary : true });
29
-
30
- expect(auth.getAuth).toHaveBeenCalledWith(profile, { temporary : true });
31
- });
32
-
33
- it('should get youtube api', async () => {
34
- await getAPI(profile);
35
-
36
- expect(google.youtube).toHaveBeenCalledWith({ version : 'v3', auth : googleAuth });
37
- });
38
-
39
- it('should return youtube api', async () => {
40
- const result = await getAPI(profile);
41
-
42
- expect(result).toEqual(api);
43
- });
44
- });
45
- });
@@ -1,13 +0,0 @@
1
- import { google } from 'googleapis';
2
- import type GoogleApis from 'googleapis';
3
- import type { AuthOptions } from '../../types';
4
- import { getAuth } from '../auth';
5
-
6
- export async function getAPI(profile: string, options?: AuthOptions): Promise<GoogleApis.calendar_v3.Calendar> {
7
- const googleAuth = await getAuth(profile, options);
8
-
9
- return google.calendar({
10
- version : 'v3',
11
- auth : googleAuth,
12
- });
13
- }
@@ -1,44 +0,0 @@
1
- import type GoogleApis from 'googleapis';
2
- import { log } from '@anmiles/logger';
3
- import sleep from '@anmiles/sleep';
4
- import type { CommonOptions } from '../../types';
5
-
6
- export { getItems };
7
- export default { getItems };
8
-
9
- const requestInterval = 300;
10
-
11
- type CommonApi<TItem> = {
12
- list: {
13
- (params?: { pageToken: string | undefined }, options?: GoogleApis.Common.MethodOptions): Promise<GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>>>;
14
- (callback: (err: Error | null, res?: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>> | null) => void): void;
15
- }
16
- };
17
-
18
- type CommonResponse<TItem> = {
19
- items?: TItem[],
20
- pageInfo?: {
21
- totalResults?: number | null | undefined
22
- },
23
- nextPageToken?: string | null | undefined
24
- };
25
-
26
- async function getItems<TItem>(api: CommonApi<TItem>, params: any, options?: CommonOptions): Promise<TItem[]> {
27
- const items: TItem[] = [];
28
-
29
- let pageToken: string | null | undefined = undefined;
30
-
31
- do {
32
- const response: GoogleApis.Common.GaxiosResponse<CommonResponse<TItem>> = await api.list({ ...params, pageToken });
33
- response.data.items?.forEach((item) => items.push(item));
34
-
35
- if (!options?.hideProgress) {
36
- log(`Getting items (${items.length} of ${response.data.pageInfo?.totalResults || 'many'})...`);
37
- }
38
-
39
- await sleep(requestInterval);
40
- pageToken = response.data.nextPageToken;
41
- } while (pageToken);
42
-
43
- return items;
44
- }
@@ -1,13 +0,0 @@
1
- import { google } from 'googleapis';
2
- import type GoogleApis from 'googleapis';
3
- import type { AuthOptions } from '../../types';
4
- import { getAuth } from '../auth';
5
-
6
- export async function getAPI(profile: string, options?: AuthOptions): Promise<GoogleApis.youtube_v3.Youtube> {
7
- const googleAuth = await getAuth(profile, options);
8
-
9
- return google.youtube({
10
- version : 'v3',
11
- auth : googleAuth,
12
- });
13
- }