@anmiles/google-api-wrapper 7.0.6 → 8.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.
Files changed (60) hide show
  1. package/.eslintrc.js +1 -86
  2. package/.gitlab-ci.yml +0 -1
  3. package/.vscode/settings.json +0 -4
  4. package/CHANGELOG.md +11 -0
  5. package/README.md +5 -2
  6. package/dist/index.d.ts +5 -5
  7. package/dist/index.js +15 -15
  8. package/dist/lib/api/calendar.d.ts +3 -3
  9. package/dist/lib/api/calendar.js +13 -13
  10. package/dist/lib/api/shared.d.ts +23 -23
  11. package/dist/lib/api/shared.js +26 -23
  12. package/dist/lib/api/shared.js.map +1 -1
  13. package/dist/lib/api/youtube.d.ts +3 -3
  14. package/dist/lib/api/youtube.js +13 -13
  15. package/dist/lib/auth.d.ts +10 -10
  16. package/dist/lib/auth.js +34 -34
  17. package/dist/lib/auth.js.map +1 -1
  18. package/dist/lib/paths.d.ts +12 -16
  19. package/dist/lib/paths.js +28 -45
  20. package/dist/lib/paths.js.map +1 -1
  21. package/dist/lib/profiles.d.ts +11 -10
  22. package/dist/lib/profiles.js +33 -33
  23. package/dist/lib/profiles.js.map +1 -1
  24. package/dist/lib/secrets.d.ts +26 -22
  25. package/dist/lib/secrets.js +152 -146
  26. package/dist/lib/secrets.js.map +1 -1
  27. package/dist/types/common.d.ts +3 -3
  28. package/dist/types/common.js +2 -2
  29. package/dist/types/index.d.ts +2 -2
  30. package/dist/types/index.js +18 -18
  31. package/dist/types/secrets.d.ts +15 -15
  32. package/dist/types/secrets.js +2 -2
  33. package/package.json +18 -15
  34. package/src/lib/__tests__/auth.test.ts +23 -23
  35. package/src/lib/__tests__/paths.test.ts +1 -63
  36. package/src/lib/__tests__/profiles.test.ts +25 -21
  37. package/src/lib/__tests__/secrets.test.ts +100 -94
  38. package/src/lib/api/__tests__/calendar.test.ts +3 -3
  39. package/src/lib/api/__tests__/shared.test.ts +12 -14
  40. package/src/lib/api/__tests__/youtube.test.ts +3 -3
  41. package/src/lib/api/shared.ts +2 -2
  42. package/src/lib/auth.ts +1 -1
  43. package/src/lib/paths.ts +2 -20
  44. package/src/lib/profiles.ts +5 -5
  45. package/src/lib/secrets.ts +24 -17
  46. package/dist/lib/jsonLib.d.ts +0 -14
  47. package/dist/lib/jsonLib.js +0 -56
  48. package/dist/lib/jsonLib.js.map +0 -1
  49. package/dist/lib/logger.d.ts +0 -12
  50. package/dist/lib/logger.js +0 -46
  51. package/dist/lib/logger.js.map +0 -1
  52. package/dist/lib/sleep.d.ts +0 -6
  53. package/dist/lib/sleep.js +0 -11
  54. package/dist/lib/sleep.js.map +0 -1
  55. package/src/lib/__tests__/jsonLib.test.ts +0 -253
  56. package/src/lib/__tests__/logger.test.ts +0 -57
  57. package/src/lib/__tests__/sleep.test.ts +0 -17
  58. package/src/lib/jsonLib.ts +0 -57
  59. package/src/lib/logger.ts +0 -21
  60. package/src/lib/sleep.ts +0 -8
@@ -1,6 +1,6 @@
1
1
  import { google } from 'googleapis';
2
2
  import type GoogleApis from 'googleapis';
3
- import logger from '../logger';
3
+ import logger from '@anmiles/logger';
4
4
  import profiles from '../profiles';
5
5
  import secrets from '../secrets';
6
6
 
@@ -11,7 +11,7 @@ jest.mock<typeof auth>('../auth', () => ({
11
11
  getAuth : jest.fn().mockImplementation(async () => googleAuth),
12
12
  }));
13
13
 
14
- jest.mock<Partial<typeof logger>>('../logger', () => ({
14
+ jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
15
15
  info : jest.fn(),
16
16
  warn : jest.fn(),
17
17
  }));
@@ -56,95 +56,95 @@ describe('src/lib/auth', () => {
56
56
  describe('login', () => {
57
57
  it('should get profiles', async () => {
58
58
  await original.login();
59
- expect(profiles.getProfiles).toBeCalledWith();
59
+ expect(profiles.getProfiles).toHaveBeenCalledWith();
60
60
  });
61
61
 
62
62
  it('should auth all profiles', async () => {
63
63
  await original.login();
64
64
 
65
65
  allProfiles.forEach((profile) => {
66
- expect(auth.getAuth).toBeCalledWith(profile, undefined);
66
+ expect(auth.getAuth).toHaveBeenCalledWith(profile, undefined);
67
67
  });
68
68
  });
69
69
 
70
70
  it('should auth only specified profile', async () => {
71
71
  await original.login('username1');
72
72
 
73
- expect(auth.getAuth).toBeCalledWith('username1', undefined);
74
- expect(auth.getAuth).not.toBeCalledWith('username2', undefined);
73
+ expect(auth.getAuth).toHaveBeenCalledWith('username1', undefined);
74
+ expect(auth.getAuth).not.toHaveBeenCalledWith('username2', undefined);
75
75
  });
76
76
 
77
77
  it('should pass temporariness for all profiles', async () => {
78
78
  await original.login(undefined, { temporary : true });
79
79
 
80
- expect(auth.getAuth).toBeCalledWith('username1', { temporary : true });
81
- expect(auth.getAuth).toBeCalledWith('username2', { temporary : true });
80
+ expect(auth.getAuth).toHaveBeenCalledWith('username1', { temporary : true });
81
+ expect(auth.getAuth).toHaveBeenCalledWith('username2', { temporary : true });
82
82
 
83
83
  });
84
84
 
85
85
  it('should pass temporariness only for specified profile', async () => {
86
86
  await original.login('username1', { temporary : true });
87
87
 
88
- expect(auth.getAuth).toBeCalledWith('username1', { temporary : true });
89
- expect(auth.getAuth).not.toBeCalledWith('username2', { temporary : true });
88
+ expect(auth.getAuth).toHaveBeenCalledWith('username1', { temporary : true });
89
+ expect(auth.getAuth).not.toHaveBeenCalledWith('username2', { temporary : true });
90
90
 
91
91
  });
92
92
 
93
93
  it('should show auth progress for all profiles by default', async () => {
94
94
  await original.login();
95
95
 
96
- expect(logger.warn).toBeCalledWith('username1 - logging in...');
97
- expect(logger.warn).toBeCalledWith('username2 - logging in...');
98
- expect(logger.info).toBeCalledWith('username1 - logged in successfully');
99
- expect(logger.info).toBeCalledWith('username2 - logged in successfully');
96
+ expect(logger.warn).toHaveBeenCalledWith('username1 - logging in...');
97
+ expect(logger.warn).toHaveBeenCalledWith('username2 - logging in...');
98
+ expect(logger.info).toHaveBeenCalledWith('username1 - logged in successfully');
99
+ expect(logger.info).toHaveBeenCalledWith('username2 - logged in successfully');
100
100
  });
101
101
 
102
102
  it('should show auth progress for specified profile by default', async () => {
103
103
  await original.login('username1');
104
104
 
105
- expect(logger.warn).toBeCalledWith('username1 - logging in...');
106
- expect(logger.info).toBeCalledWith('username1 - logged in successfully');
105
+ expect(logger.warn).toHaveBeenCalledWith('username1 - logging in...');
106
+ expect(logger.info).toHaveBeenCalledWith('username1 - logged in successfully');
107
107
  });
108
108
 
109
109
  it('should not show auth progress if hidden', async () => {
110
110
  await original.login(undefined, { hideProgress : true });
111
111
  await original.login('username1', { hideProgress : true });
112
112
 
113
- expect(logger.info).not.toBeCalled();
113
+ expect(logger.info).not.toHaveBeenCalled();
114
114
  });
115
115
  });
116
116
 
117
117
  describe('getAuth', () => {
118
118
  it('should get secrets', async () => {
119
119
  await original.getAuth(profile);
120
- expect(secrets.getSecrets).toBeCalledWith(profile);
120
+ expect(secrets.getSecrets).toHaveBeenCalledWith(profile);
121
121
  });
122
122
 
123
123
  it('should get credentials', async () => {
124
124
  await original.getAuth(profile);
125
- expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth, undefined);
125
+ expect(secrets.getCredentials).toHaveBeenCalledWith(profile, googleAuth, undefined);
126
126
  });
127
127
 
128
128
  it('should create OAuth2 instance', async () => {
129
129
  await original.getAuth(profile);
130
- expect(google.auth.OAuth2).toBeCalledWith(secretsObject.web.client_id, secretsObject.web.client_secret, secretsObject.web.redirect_uris[0]);
130
+ expect(google.auth.OAuth2).toHaveBeenCalledWith(secretsObject.web.client_id, secretsObject.web.client_secret, secretsObject.web.redirect_uris[0]);
131
131
  });
132
132
 
133
133
  it('should set credentials', async () => {
134
134
  await original.getAuth(profile);
135
- expect(googleAuth.setCredentials).toBeCalledWith(credentials);
135
+ expect(googleAuth.setCredentials).toHaveBeenCalledWith(credentials);
136
136
  });
137
137
 
138
138
  it('should pass temporariness', async () => {
139
139
  await original.getAuth(profile, { temporary : true });
140
140
 
141
- expect(secrets.getCredentials).toBeCalledWith(profile, googleAuth, { temporary : true });
141
+ expect(secrets.getCredentials).toHaveBeenCalledWith(profile, googleAuth, { temporary : true });
142
142
  });
143
143
 
144
144
  it('should set google auth', async () => {
145
145
  await original.getAuth(profile);
146
146
 
147
- expect(google.options).toBeCalledWith({ auth : googleAuth });
147
+ expect(google.options).toHaveBeenCalledWith({ auth : googleAuth });
148
148
  });
149
149
  });
150
150
  });
@@ -4,8 +4,6 @@ import path from 'path';
4
4
  import paths from '../paths';
5
5
  const original = jest.requireActual('../paths').default as typeof paths;
6
6
  jest.mock<typeof paths>('../paths', () => ({
7
- ensureDir : jest.fn().mockImplementation((dirPath) => dirPath),
8
- ensureFile : jest.fn().mockImplementation((filePath) => filePath),
9
7
  getProfilesFile : jest.fn().mockImplementation(() => profilesFile),
10
8
  getScopesFile : jest.fn().mockImplementation(() => scopesFile),
11
9
  getSecretsFile : jest.fn().mockImplementation(() => secretsFile),
@@ -23,10 +21,7 @@ jest.mock<Partial<typeof path>>('path', () => ({
23
21
  dirname : jest.fn().mockImplementation((arg) => arg.split('/').slice(0, -1).join('/')),
24
22
  }));
25
23
 
26
- const profile = 'username';
27
- const dirPath = 'dirPath';
28
- const filePath = 'parentDir/filePath';
29
-
24
+ const profile = 'username';
30
25
  const profilesFile = 'input/profiles.json';
31
26
  const scopesFile = 'scopes.json';
32
27
  const secretsFile = 'secrets/username.json';
@@ -35,63 +30,6 @@ const credentialsFile = 'secrets/username.credentials.json';
35
30
  let exists: boolean;
36
31
 
37
32
  describe('src/lib/paths', () => {
38
-
39
- describe('ensureDir', () => {
40
- it('should create empty dir if not exists', () => {
41
- exists = false;
42
-
43
- original.ensureDir(dirPath);
44
-
45
- expect(fs.mkdirSync).toBeCalledWith(dirPath, { recursive : true });
46
- });
47
-
48
- it('should not create empty dir if already exists', () => {
49
- exists = true;
50
-
51
- original.ensureDir(dirPath);
52
-
53
- expect(fs.writeFileSync).not.toBeCalled();
54
- });
55
-
56
- it('should return dirPath', () => {
57
- const result = original.ensureDir(dirPath);
58
-
59
- expect(result).toEqual(dirPath);
60
- });
61
- });
62
-
63
- describe('ensureFile', () => {
64
- it('should ensure parent dir', () => {
65
- exists = false;
66
-
67
- original.ensureFile(filePath);
68
-
69
- expect(paths.ensureDir).toBeCalledWith('parentDir');
70
- });
71
-
72
- it('should create empty file if not exists', () => {
73
- exists = false;
74
-
75
- original.ensureFile(filePath);
76
-
77
- expect(fs.writeFileSync).toBeCalledWith(filePath, '');
78
- });
79
-
80
- it('should not create empty file if already exists', () => {
81
- exists = true;
82
-
83
- original.ensureFile(filePath);
84
-
85
- expect(fs.writeFileSync).not.toBeCalled();
86
- });
87
-
88
- it('should return filePath', () => {
89
- const result = original.ensureFile(filePath);
90
-
91
- expect(result).toEqual(filePath);
92
- });
93
- });
94
-
95
33
  describe('getProfilesFile', () => {
96
34
  it('should return profiles file', () => {
97
35
  const result = original.getProfilesFile();
@@ -1,6 +1,5 @@
1
1
  import fs from 'fs';
2
- import jsonLib from '../jsonLib';
3
- import logger from '../logger';
2
+ import logger from '@anmiles/logger';
4
3
  import paths from '../paths';
5
4
 
6
5
  import profiles from '../profiles';
@@ -18,17 +17,9 @@ jest.mock<Partial<typeof fs>>('fs', () => ({
18
17
  existsSync : jest.fn().mockImplementation((file) => existingFiles.includes(file)),
19
18
  }));
20
19
 
21
- jest.mock<Partial<typeof jsonLib>>('../jsonLib', () => ({
22
- getJSON : jest.fn().mockImplementation(() => json),
23
- writeJSON : jest.fn(),
24
- }));
25
-
26
- jest.mock<Partial<typeof logger>>('../logger', () => ({
27
- log : jest.fn(),
28
- warn : jest.fn(),
29
- error : jest.fn().mockImplementation((error) => {
30
- throw error;
31
- }) as jest.Mock<never, any>,
20
+ jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
21
+ log : jest.fn(),
22
+ warn : jest.fn(),
32
23
  }));
33
24
 
34
25
  jest.mock<Partial<typeof paths>>('../paths', () => ({
@@ -44,19 +35,32 @@ const allProfiles = [ profile1, profile2 ];
44
35
 
45
36
  let existingFiles: string[] = [];
46
37
 
38
+ let getJSONSpy: jest.SpyInstance;
39
+ let writeJSONSpy: jest.SpyInstance;
40
+
41
+ beforeAll(() => {
42
+ getJSONSpy = jest.spyOn(fs, 'getJSON');
43
+ writeJSONSpy = jest.spyOn(fs, 'writeJSON');
44
+ });
45
+
47
46
  beforeEach(() => {
47
+ getJSONSpy.mockImplementation(() => json);
48
+ writeJSONSpy.mockImplementation();
48
49
  existingFiles = [];
49
50
  });
50
51
 
52
+ afterAll(() => {
53
+ getJSONSpy.mockRestore();
54
+ writeJSONSpy.mockRestore();
55
+ });
56
+
51
57
  describe('src/lib/profiles', () => {
52
58
 
53
59
  describe('getProfiles', () => {
54
- const getJSONSpy = jest.spyOn(jsonLib, 'getJSON');
55
-
56
60
  it('should get json from profiles file', () => {
57
61
  original.getProfiles();
58
62
 
59
- expect(getJSONSpy).toBeCalled();
63
+ expect(getJSONSpy).toHaveBeenCalled();
60
64
  expect(getJSONSpy.mock.calls[0][0]).toEqual(profilesFile);
61
65
  });
62
66
 
@@ -79,7 +83,7 @@ describe('src/lib/profiles', () => {
79
83
  it('should write json to profiles file', () => {
80
84
  original.setProfiles(allProfiles);
81
85
 
82
- expect(jsonLib.writeJSON).toBeCalledWith(profilesFile, allProfiles);
86
+ expect(writeJSONSpy).toHaveBeenCalledWith(profilesFile, allProfiles);
83
87
  });
84
88
  });
85
89
 
@@ -87,7 +91,7 @@ describe('src/lib/profiles', () => {
87
91
  it('should output error and do nothing if profile is falsy', () => {
88
92
  const func = () => original.createProfile('');
89
93
 
90
- expect(func).toThrowError('Usage: `npm run create <profile>` where `profile` - is any profile name you want');
94
+ expect(func).toThrow('Usage: `npm run create <profile>` where `profile` - is any profile name you want');
91
95
  });
92
96
 
93
97
  it('should get profiles', () => {
@@ -95,7 +99,7 @@ describe('src/lib/profiles', () => {
95
99
 
96
100
  original.createProfile(newProfile);
97
101
 
98
- expect(profiles.getProfiles).toBeCalledWith();
102
+ expect(profiles.getProfiles).toHaveBeenCalledWith();
99
103
  });
100
104
 
101
105
  it('should not save profiles if profile already exists', () => {
@@ -103,7 +107,7 @@ describe('src/lib/profiles', () => {
103
107
 
104
108
  original.createProfile(newProfile);
105
109
 
106
- expect(profiles.setProfiles).not.toBeCalled();
110
+ expect(profiles.setProfiles).not.toHaveBeenCalled();
107
111
  });
108
112
 
109
113
  it('should add new profile if not exists', () => {
@@ -111,7 +115,7 @@ describe('src/lib/profiles', () => {
111
115
 
112
116
  original.createProfile(newProfile);
113
117
 
114
- expect(profiles.setProfiles).toBeCalledWith([ 'username1', 'username2', 'newProfile' ]);
118
+ expect(profiles.setProfiles).toHaveBeenCalledWith([ 'username1', 'username2', 'newProfile' ]);
115
119
  });
116
120
  });
117
121
  });