@friggframework/core 0.2.31-v1-alpha.6 → 1.0.1-canary.278.7b20420.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 (156) hide show
  1. package/assertions/CHANGELOG.md +87 -0
  2. package/assertions/LICENSE.md +9 -0
  3. package/assertions/README.md +3 -0
  4. package/assertions/bump.txt +1 -0
  5. package/assertions/get.js +139 -0
  6. package/assertions/index.js +19 -0
  7. package/assertions/is-equal.js +17 -0
  8. package/associations/LICENSE.md +9 -0
  9. package/associations/README.md +3 -0
  10. package/associations/association.js +78 -0
  11. package/associations/bump3.txt +0 -0
  12. package/associations/jest.config.js +5 -0
  13. package/associations/model.js +54 -0
  14. package/bump0.txt +1 -0
  15. package/core/.eslintrc.json +3 -0
  16. package/{Delegate.js → core/Delegate.js} +1 -1
  17. package/core/LICENSE.md +9 -0
  18. package/{Worker.js → core/Worker.js} +2 -2
  19. package/core/bump3.txt +0 -0
  20. package/{create-handler.js → core/create-handler.js} +2 -2
  21. package/core/index.js +6 -0
  22. package/core/jest.config.js +5 -0
  23. package/database/.eslintrc.json +3 -0
  24. package/database/CHANGELOG.md +97 -0
  25. package/database/LICENSE.md +9 -0
  26. package/database/README.md +3 -0
  27. package/database/bump3.txt +0 -0
  28. package/database/index.js +23 -0
  29. package/database/jest.config.js +5 -0
  30. package/database/models/IndividualUser.js +76 -0
  31. package/database/models/OrganizationUser.js +29 -0
  32. package/database/models/State.js +9 -0
  33. package/database/models/Token.js +70 -0
  34. package/database/models/UserModel.js +7 -0
  35. package/database/mongo.js +45 -0
  36. package/database/mongoose.js +5 -0
  37. package/encrypt/.eslintrc.json +3 -0
  38. package/encrypt/CHANGELOG.md +78 -0
  39. package/encrypt/Cryptor.js +236 -0
  40. package/encrypt/Cryptor.test.js +32 -0
  41. package/encrypt/LICENSE.md +9 -0
  42. package/encrypt/README.md +13 -0
  43. package/encrypt/aes.js +27 -0
  44. package/encrypt/bump3.txt +0 -0
  45. package/encrypt/encrypt.js +132 -0
  46. package/encrypt/encrypt.test.js +1069 -0
  47. package/encrypt/index.js +4 -0
  48. package/encrypt/jest.config.js +5 -0
  49. package/encrypt/test-encrypt.js +107 -0
  50. package/errors/.eslintrc.json +3 -0
  51. package/errors/CHANGELOG.md +44 -0
  52. package/errors/LICENSE.md +9 -0
  53. package/errors/README.md +3 -0
  54. package/errors/base-error.js +23 -0
  55. package/errors/base-error.test.js +32 -0
  56. package/errors/bump.txt +1 -0
  57. package/errors/bump3.txt +0 -0
  58. package/errors/fetch-error.js +76 -0
  59. package/errors/fetch-error.test.js +79 -0
  60. package/errors/halt-error.js +10 -0
  61. package/errors/halt-error.test.js +11 -0
  62. package/errors/index.js +15 -0
  63. package/errors/jest.config.js +5 -0
  64. package/errors/validation-errors.js +23 -0
  65. package/errors/validation-errors.test.js +120 -0
  66. package/index.js +123 -5
  67. package/integrations/.eslintrc.json +3 -0
  68. package/integrations/CHANGELOG.md +244 -0
  69. package/integrations/LICENSE.md +9 -0
  70. package/integrations/README.md +3 -0
  71. package/integrations/bump3.txt +0 -0
  72. package/integrations/create-frigg-backend.js +31 -0
  73. package/integrations/index.js +19 -0
  74. package/integrations/integration-base.js +162 -0
  75. package/integrations/integration-factory.js +166 -0
  76. package/integrations/integration-mapping.js +43 -0
  77. package/integrations/integration-model.js +42 -0
  78. package/integrations/integration-router.js +367 -0
  79. package/integrations/integration-user.js +144 -0
  80. package/integrations/jest-setup.js +2 -0
  81. package/integrations/jest-teardown.js +2 -0
  82. package/integrations/jest.config.js +12 -0
  83. package/integrations/options.js +54 -0
  84. package/integrations/test/integration-base.test.js +143 -0
  85. package/lambda/README.md +3 -0
  86. package/lambda/TimeoutCatcher.js +43 -0
  87. package/lambda/TimeoutCatcher.test.js +68 -0
  88. package/lambda/bump3.txt +0 -0
  89. package/lambda/index.js +3 -0
  90. package/lambda/jest.config.js +3 -0
  91. package/logs/.eslintrc.json +3 -0
  92. package/logs/CHANGELOG.md +57 -0
  93. package/logs/LICENSE.md +9 -0
  94. package/logs/README.md +3 -0
  95. package/logs/bump3.txt +0 -0
  96. package/logs/index.js +7 -0
  97. package/logs/jest.config.js +5 -0
  98. package/logs/logger.js +69 -0
  99. package/logs/logger.test.js +76 -0
  100. package/module-plugin/.eslintrc.json +3 -0
  101. package/module-plugin/CHANGELOG.md +224 -0
  102. package/module-plugin/LICENSE.md +9 -0
  103. package/module-plugin/ModuleConstants.js +10 -0
  104. package/module-plugin/README.md +3 -0
  105. package/module-plugin/auther.js +342 -0
  106. package/module-plugin/bump3.txt +0 -0
  107. package/module-plugin/credential.js +22 -0
  108. package/module-plugin/entity-manager.js +70 -0
  109. package/module-plugin/entity.js +46 -0
  110. package/module-plugin/index.js +25 -0
  111. package/module-plugin/jest-setup.js +3 -0
  112. package/module-plugin/jest-teardown.js +2 -0
  113. package/module-plugin/jest.config.js +20 -0
  114. package/module-plugin/manager.js +169 -0
  115. package/module-plugin/module-factory.js +60 -0
  116. package/module-plugin/requester/api-key.js +36 -0
  117. package/module-plugin/requester/basic.js +43 -0
  118. package/module-plugin/requester/oauth-2.js +219 -0
  119. package/module-plugin/requester/requester.js +151 -0
  120. package/module-plugin/requester/requester.test.js +28 -0
  121. package/module-plugin/test/auther.test.js +97 -0
  122. package/module-plugin/test/mock-api/api.js +29 -0
  123. package/module-plugin/test/mock-api/definition.js +48 -0
  124. package/module-plugin/test/mock-api/mocks/hubspot.js +43 -0
  125. package/package.json +28 -13
  126. package/syncs/README.md +3 -0
  127. package/syncs/bump3.txt +0 -0
  128. package/syncs/jest.config.js +5 -0
  129. package/syncs/manager.js +464 -0
  130. package/syncs/model.js +62 -0
  131. package/syncs/sync.js +114 -0
  132. package/types/CHANGELOG.md +49 -0
  133. package/types/README.md +24 -0
  134. package/types/assertions/index.d.ts +83 -0
  135. package/types/associations/index.d.ts +74 -0
  136. package/types/bump3.txt +0 -0
  137. package/types/core/index.d.ts +54 -0
  138. package/types/database/index.d.ts +3 -0
  139. package/types/encrypt/index.d.ts +5 -0
  140. package/types/errors/index.d.ts +66 -0
  141. package/types/eslint-config/index.d.ts +41 -0
  142. package/types/index.d.ts +14 -0
  143. package/types/integrations/index.d.ts +191 -0
  144. package/types/lambda/index.d.ts +31 -0
  145. package/types/logs/index.d.ts +5 -0
  146. package/types/module-plugin/index.d.ts +293 -0
  147. package/types/prettier-config/index.d.ts +6 -0
  148. package/types/syncs/index.d.ts +128 -0
  149. package/types/test-environment/index.d.ts +17 -0
  150. package/types/tsconfig.json +103 -0
  151. /package/{.eslintrc.json → assertions/.eslintrc.json} +0 -0
  152. /package/{bump3.txt → assertions/bump3.txt} +0 -0
  153. /package/{jest.config.js → assertions/jest.config.js} +0 -0
  154. /package/{CHANGELOG.md → core/CHANGELOG.md} +0 -0
  155. /package/{README.md → core/README.md} +0 -0
  156. /package/{load-installed-modules.js → core/load-installed-modules.js} +0 -0
@@ -0,0 +1,219 @@
1
+ const { Requester } = require('./requester');
2
+ const { get } = require('../../assertions');
3
+ const { ModuleConstants } = require('../ModuleConstants');
4
+
5
+ class OAuth2Requester extends Requester {
6
+
7
+ static requesterType = ModuleConstants.authType.oauth2;
8
+
9
+ constructor(params) {
10
+ super(params);
11
+ this.DLGT_TOKEN_UPDATE = 'TOKEN_UPDATE';
12
+ this.DLGT_TOKEN_DEAUTHORIZED = 'TOKEN_DEAUTHORIZED';
13
+
14
+ this.delegateTypes.push(this.DLGT_TOKEN_UPDATE);
15
+ this.delegateTypes.push(this.DLGT_TOKEN_DEAUTHORIZED);
16
+
17
+ this.grant_type = get(params, 'grant_type', 'authorization_code');
18
+ this.client_id = get(params, 'client_id', null);
19
+ this.client_secret = get(params, 'client_secret', null);
20
+ this.redirect_uri = get(params, 'redirect_uri', null);
21
+ this.scope = get(params, 'scope', null);
22
+ this.authorizationUri = get(params, 'authorizationUri', null);
23
+ this.baseURL = get(params, 'baseURL', null);
24
+ this.access_token = get(params, 'access_token', null);
25
+ this.refresh_token = get(params, 'refresh_token', null);
26
+ this.accessTokenExpire = get(params, 'accessTokenExpire', null);
27
+ this.refreshTokenExpire = get(params, 'refreshTokenExpire', null);
28
+ this.audience = get(params, 'audience', null);
29
+ this.username = get(params, 'username', null);
30
+ this.password = get(params, 'password', null);
31
+ this.state = get(params, 'state', null);
32
+
33
+ this.isRefreshable = true;
34
+ }
35
+
36
+ async setTokens(params) {
37
+ this.access_token = get(params, 'access_token');
38
+ this.refresh_token = get(params, 'refresh_token', null);
39
+ const accessExpiresIn = get(params, 'expires_in', null);
40
+ const refreshExpiresIn = get(
41
+ params,
42
+ 'x_refresh_token_expires_in',
43
+ null
44
+ );
45
+
46
+ this.accessTokenExpire = new Date(Date.now() + accessExpiresIn * 1000);
47
+ this.refreshTokenExpire = new Date(Date.now() + refreshExpiresIn * 1000);
48
+
49
+ await this.notify(this.DLGT_TOKEN_UPDATE);
50
+ }
51
+
52
+ getAuthorizationUri() {
53
+ return this.authorizationUri;
54
+ }
55
+
56
+ getAuthorizationRequirements() {
57
+ return {
58
+ url: this.getAuthorizationUri(),
59
+ type: 'oauth2',
60
+ };
61
+ }
62
+
63
+ // this.client_id, this.client_secret, this.redirect_uri, and this.tokenUri
64
+ // will need to be defined in the child class before super(params)
65
+ async getTokenFromCode(code) {
66
+ const params = new URLSearchParams();
67
+ params.append('grant_type', 'authorization_code');
68
+ params.append('client_id', this.client_id);
69
+ params.append('client_secret', this.client_secret);
70
+ params.append('redirect_uri', this.redirect_uri);
71
+ params.append('scope', this.scope);
72
+ params.append('code', code);
73
+ const options = {
74
+ body: params,
75
+ headers: {
76
+ 'Content-Type': 'application/x-www-form-urlencoded',
77
+ },
78
+ url: this.tokenUri,
79
+ };
80
+ const response = await this._post(options, false);
81
+ await this.setTokens(response);
82
+ return response;
83
+ }
84
+
85
+ // REPLACE getTokenFromCode IN THE CHILD IF NEEDED
86
+ // this.client_id, this.client_secret, this.redirect_uri, and this.tokenUri
87
+ // will need to be defined in the child class before super(params)
88
+ async getTokenFromCodeBasicAuthHeader(code) {
89
+ const params = new URLSearchParams();
90
+ params.append('grant_type', 'authorization_code');
91
+ params.append('client_id', this.client_id);
92
+ params.append('redirect_uri', this.redirect_uri);
93
+ params.append('code', code);
94
+
95
+ const options = {
96
+ body: params,
97
+ headers: {
98
+ 'Content-Type': 'application/x-www-form-urlencoded',
99
+ Authorization: `Basic ${Buffer.from(
100
+ `${this.client_id}:${this.client_secret}`
101
+ ).toString('base64')}`,
102
+ },
103
+ url: this.tokenUri,
104
+ };
105
+
106
+ const response = await this._post(options, false);
107
+ await this.setTokens(response);
108
+ return response;
109
+ }
110
+
111
+ // this.client_id, this.client_secret, this.redirect_uri, and this.tokenUri
112
+ // will need to be defined in the child class before super(params)
113
+ async refreshAccessToken(refreshTokenObject) {
114
+ this.access_token = undefined;
115
+ const params = new URLSearchParams();
116
+ params.append('grant_type', 'refresh_token');
117
+ params.append('client_id', this.client_id);
118
+ params.append('client_secret', this.client_secret);
119
+ params.append('refresh_token', refreshTokenObject.refresh_token);
120
+ params.append('redirect_uri', this.redirect_uri);
121
+
122
+ const options = {
123
+ body: params,
124
+ url: this.tokenUri,
125
+ headers: {
126
+ 'Content-Type': 'application/x-www-form-urlencoded',
127
+ },
128
+ };
129
+ const response = await this._post(options, false);
130
+ await this.setTokens(response);
131
+ return response;
132
+ }
133
+
134
+ async addAuthHeaders(headers) {
135
+ if (this.access_token) {
136
+ headers.Authorization = `Bearer ${this.access_token}`;
137
+ }
138
+
139
+ return headers;
140
+ }
141
+
142
+ isAuthenticated() {
143
+ return (
144
+ this.accessToken !== null &&
145
+ this.refreshToken !== null &&
146
+ this.accessTokenExpire &&
147
+ this.refreshTokenExpire
148
+ );
149
+ }
150
+
151
+ async refreshAuth() {
152
+ try {
153
+ if (this.grantType !== 'client_credentials') {
154
+ await this.refreshAccessToken({
155
+ refresh_token: this.refresh_token,
156
+ });
157
+ } else {
158
+ await this.getTokenFromClientCredentials();
159
+ }
160
+ } catch {
161
+ await this.notify(this.DLGT_INVALID_AUTH);
162
+ }
163
+ }
164
+
165
+ async getTokenFromUsernamePassword() {
166
+ try {
167
+ const url = this.tokenUri;
168
+
169
+ const body = {
170
+ username: this.username,
171
+ password: this.password,
172
+ grant_type: 'password',
173
+ };
174
+ const headers = {
175
+ 'Content-Type': 'application/json',
176
+ };
177
+
178
+ const tokenRes = await this._post({
179
+ url,
180
+ body,
181
+ headers,
182
+ });
183
+
184
+ await this.setTokens(tokenRes);
185
+ return tokenRes;
186
+ } catch {
187
+ await this.notify(this.DLGT_INVALID_AUTH);
188
+ }
189
+ }
190
+
191
+ async getTokenFromClientCredentials() {
192
+ try {
193
+ const url = this.tokenUri;
194
+
195
+ const body = {
196
+ audience: this.audience,
197
+ client_id: this.client_id,
198
+ client_secret: this.client_secret,
199
+ grant_type: 'client_credentials',
200
+ };
201
+ const headers = {
202
+ 'Content-Type': 'application/json',
203
+ };
204
+
205
+ const tokenRes = await this._post({
206
+ url,
207
+ body,
208
+ headers,
209
+ });
210
+
211
+ await this.setTokens(tokenRes);
212
+ return tokenRes;
213
+ } catch {
214
+ await this.notify(this.DLGT_INVALID_AUTH);
215
+ }
216
+ }
217
+ }
218
+
219
+ module.exports = { OAuth2Requester };
@@ -0,0 +1,151 @@
1
+ const fetch = require('node-fetch');
2
+ const { Delegate } = require('../../core');
3
+ const { FetchError } = require('../../errors');
4
+ const { get } = require('../../assertions');
5
+
6
+ class Requester extends Delegate {
7
+ constructor(params) {
8
+ super(params);
9
+ this.backOff = get(params, 'backOff', [1, 3, 10, 30, 60, 180]);
10
+ this.isRefreshable = false;
11
+ this.refreshCount = 0;
12
+ this.DLGT_INVALID_AUTH = 'INVALID_AUTH';
13
+ this.delegateTypes.push(this.DLGT_INVALID_AUTH);
14
+ this.agent = get(params, 'agent', null);
15
+
16
+ // Allow passing in the fetch function
17
+ // Instance methods can use this.fetch without differentiating
18
+ this.fetch = get(params, 'fetch', fetch);
19
+ }
20
+
21
+ parsedBody = async (resp) => {
22
+ const contentType = resp.headers.get('Content-Type') || '';
23
+
24
+ if (
25
+ contentType.match(/^application\/json/) ||
26
+ contentType.match(/^application\/vnd.api\+json/) ||
27
+ contentType.match(/^application\/hal\+json/)
28
+ ) {
29
+ return resp.json();
30
+ }
31
+
32
+ return resp.text();
33
+ };
34
+
35
+ async _request(url, options, i = 0) {
36
+ let encodedUrl = encodeURI(url);
37
+ if (options.query) {
38
+ let queryBuild = '?';
39
+ for (const key in options.query) {
40
+ queryBuild += `${encodeURIComponent(key)}=${encodeURIComponent(
41
+ options.query[key]
42
+ )}&`;
43
+ }
44
+ encodedUrl += queryBuild.slice(0, -1);
45
+ }
46
+
47
+ options.headers = await this.addAuthHeaders(options.headers);
48
+
49
+ if (this.agent) options.agent = this.agent;
50
+
51
+ const response = await this.fetch(encodedUrl, options);
52
+ const { status } = response;
53
+
54
+ // If the status is retriable and there are back off requests left, retry the request
55
+ if ((status === 429 || status >= 500) && i < this.backOff.length) {
56
+ const delay = this.backOff[i] * 1000;
57
+ await new Promise((resolve) => setTimeout(resolve, delay));
58
+ return this._request(url, options, i + 1);
59
+ } else if (status === 401) {
60
+ if (!this.isRefreshable || this.refreshCount > 0) {
61
+ await this.notify(this.DLGT_INVALID_AUTH);
62
+ } else {
63
+ this.refreshCount++;
64
+ await this.refreshAuth();
65
+ return this._request(url, options, i + 1); // Retries
66
+ }
67
+ }
68
+
69
+ // If the error wasn't retried, throw.
70
+ if (status >= 400) {
71
+ throw await FetchError.create({
72
+ resource: encodedUrl,
73
+ init: options,
74
+ response,
75
+ });
76
+ }
77
+
78
+ return options.returnFullRes
79
+ ? response
80
+ : await this.parsedBody(response);
81
+ }
82
+
83
+ async _get(options) {
84
+ const fetchOptions = {
85
+ method: 'GET',
86
+ credentials: 'include',
87
+ headers: options.headers || {},
88
+ query: options.query || {},
89
+ returnFullRes: options.returnFullRes || false,
90
+ };
91
+
92
+ const res = await this._request(options.url, fetchOptions);
93
+ return res;
94
+ }
95
+
96
+ async _post(options, stringify = true) {
97
+ const fetchOptions = {
98
+ method: 'POST',
99
+ credentials: 'include',
100
+ headers: options.headers || {},
101
+ query: options.query || {},
102
+ body: stringify ? JSON.stringify(options.body) : options.body,
103
+ returnFullRes: options.returnFullRes || false,
104
+ };
105
+ const res = await this._request(options.url, fetchOptions);
106
+ return res;
107
+ }
108
+
109
+ async _patch(options, stringify = true) {
110
+ const fetchOptions = {
111
+ method: 'PATCH',
112
+ credentials: 'include',
113
+ headers: options.headers || {},
114
+ query: options.query || {},
115
+ body: stringify ? JSON.stringify(options.body) : options.body,
116
+ returnFullRes: options.returnFullRes || false,
117
+ };
118
+ const res = await this._request(options.url, fetchOptions);
119
+ return res;
120
+ }
121
+
122
+ async _put(options, stringify = true) {
123
+ const fetchOptions = {
124
+ method: 'PUT',
125
+ credentials: 'include',
126
+ headers: options.headers || {},
127
+ query: options.query || {},
128
+ body: stringify ? JSON.stringify(options.body) : options.body,
129
+ returnFullRes: options.returnFullRes || false,
130
+ };
131
+ const res = await this._request(options.url, fetchOptions);
132
+ return res;
133
+ }
134
+
135
+ async _delete(options) {
136
+ const fetchOptions = {
137
+ method: 'DELETE',
138
+ credentials: 'include',
139
+ headers: options.headers || {},
140
+ query: options.query || {},
141
+ returnFullRes: options.returnFullRes || true,
142
+ };
143
+ return this._request(options.url, fetchOptions);
144
+ }
145
+
146
+ async refreshAuth() {
147
+ throw new Error('refreshAuth not yet defined in child of Requester');
148
+ }
149
+ }
150
+
151
+ module.exports = { Requester };
@@ -0,0 +1,28 @@
1
+ const { Requester } = require('./requester');
2
+
3
+ describe('429 and 5xx testing', () => {
4
+ let backOffArray = [1, 1, 1];
5
+ let requester = new Requester({ backOff: backOffArray });
6
+ let sum = backOffArray.reduce((a, b) => {
7
+ return a + b;
8
+ }, 0);
9
+ it.skip("should retry with 'exponential' back off due to 429", async () => {
10
+ let startTime = await Date.now();
11
+ let res = await requester._get({
12
+ url: 'https://70e18ff0-1967-4fb5-8f96-10477ab6bb9e.mock.pstmn.io//429',
13
+ });
14
+ let endTime = await Date.now();
15
+ let difference = endTime - startTime;
16
+ expect(difference).toBeGreaterThan(sum * 1000);
17
+ });
18
+
19
+ it.skip("should retry with 'exponential' back off due to 500", async () => {
20
+ let startTime = await Date.now();
21
+ let res = await requester._get({
22
+ url: 'https://70e18ff0-1967-4fb5-8f96-10477ab6bb9e.mock.pstmn.io//5xx',
23
+ });
24
+ let endTime = await Date.now();
25
+ let difference = endTime - startTime;
26
+ expect(difference).toBeGreaterThan(sum * 1000);
27
+ });
28
+ });
@@ -0,0 +1,97 @@
1
+ const {Api} = require('./mock-api/api');
2
+ const hubspotMocks = require('./mock-api/mocks/hubspot');
3
+
4
+ const { Definition } = require('./mock-api/definition');
5
+ const { Auther } = require('../auther');
6
+ const { mongoose } = require('../../database/mongoose');
7
+
8
+
9
+
10
+ const getModule = async (params) => {
11
+ const module = await Auther.getInstance({
12
+ definition: Definition,
13
+ userId: new mongoose.Types.ObjectId(),
14
+ ...params,
15
+ });
16
+ module.api.getTokenFromCode = async function(code) {
17
+ await this.setTokens(hubspotMocks.tokenResponse);
18
+ return hubspotMocks.tokenResponse;
19
+ }
20
+ module.api.getUserDetails = async function() {
21
+ return hubspotMocks.userDetailsResponse;
22
+ }
23
+ return module
24
+ }
25
+
26
+
27
+ describe('HubSpot Module Tests', () => {
28
+ let module, authUrl;
29
+ beforeAll(async () => {
30
+ await mongoose.connect(process.env.MONGO_URI);
31
+ module = await getModule();
32
+ });
33
+
34
+ afterAll(async () => {
35
+ await mongoose.disconnect();
36
+ });
37
+
38
+ describe('getAuthorizationRequirements() test', () => {
39
+ it('should return auth requirements', async () => {
40
+ const requirements = module.getAuthorizationRequirements();
41
+ expect(requirements).toBeDefined();
42
+ expect(requirements.type).toEqual('oauth2');
43
+ expect(requirements.url).toBeDefined();
44
+ authUrl = requirements.url;
45
+ });
46
+ });
47
+
48
+ describe('Authorization requests', () => {
49
+ let firstRes;
50
+ it('processAuthorizationCallback()', async () => {
51
+ const response = hubspotMocks.authorizeResponse;
52
+ firstRes = await module.processAuthorizationCallback({
53
+ data: {
54
+ code: response.data.code,
55
+ },
56
+ });
57
+ expect(firstRes).toBeDefined();
58
+ expect(firstRes.entity_id).toBeDefined();
59
+ expect(firstRes.credential_id).toBeDefined();
60
+ });
61
+ it('retrieves existing entity on subsequent calls', async () =>{
62
+ const response = hubspotMocks.authorizeResponse;
63
+ const res = await module.processAuthorizationCallback({
64
+ data: {
65
+ code: response.data.code,
66
+ },
67
+ });
68
+ expect(res).toEqual(firstRes);
69
+ });
70
+ });
71
+ describe('Test credential retrieval and module instantiation', () => {
72
+ it('retrieve by entity id', async () => {
73
+ const newModule = await getModule({
74
+ userId: module.userId,
75
+ entityId: module.entity.id,
76
+ definition: Definition,
77
+ });
78
+ expect(newModule).toBeDefined();
79
+ expect(newModule.entity).toBeDefined();
80
+ expect(newModule.credential).toBeDefined();
81
+ expect(await newModule.testAuth()).toBeTruthy();
82
+
83
+ });
84
+
85
+ it('retrieve by credential id', async () => {
86
+ const newModule = await getModule({
87
+ userId: module.userId,
88
+ credentialId: module.credential.id,
89
+ definition: Definition,
90
+ });
91
+ expect(newModule).toBeDefined();
92
+ expect(newModule.credential).toBeDefined();
93
+ expect(await newModule.testAuth()).toBeTruthy();
94
+
95
+ });
96
+ });
97
+ });
@@ -0,0 +1,29 @@
1
+ const { get } = require('../../assertions');
2
+ const { OAuth2Requester } = require('../../module-plugin');
3
+
4
+ class Api extends OAuth2Requester {
5
+ constructor(params) {
6
+ super(params);
7
+ this.baseUrl = 'https://api.anapi.com';
8
+
9
+ this.URLs = {
10
+ authorization: '/oauth/authorize',
11
+ access_token: '/oauth/v1/token',
12
+ };
13
+
14
+ this.authorizationUri = encodeURI(
15
+ `https://app.anapi.com/oauth/authorize?client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&scope=${this.scope}&state=${this.state}`
16
+ );
17
+ this.tokenUri = 'https://api.anapi.com/oauth/v1/token';
18
+
19
+ this.access_token = get(params, 'access_token', null);
20
+ this.refresh_token = get(params, 'refresh_token', null);
21
+ }
22
+ getAuthUri() {
23
+ return this.authorizationUri;
24
+ }
25
+
26
+
27
+ }
28
+
29
+ module.exports = { Api };
@@ -0,0 +1,48 @@
1
+ require('dotenv').config();
2
+ const {Api} = require('./api');
3
+ const {get} = require('../../assertions');
4
+ const config = {name: 'anapi'}
5
+
6
+ const Definition = {
7
+ API: Api,
8
+ getName: function() {return config.name},
9
+ moduleName: config.name,
10
+ modelName: 'AnApi',
11
+ requiredAuthMethods: {
12
+ getToken: async function(api, params){
13
+ const code = get(params.data, 'code');
14
+ return api.getTokenFromCode(code);
15
+ },
16
+ getEntityDetails: async function(api, callbackParams, tokenResponse, userId) {
17
+ const userDetails = await api.getUserDetails();
18
+ return {
19
+ identifiers: { externalId: userDetails.portalId, user: userId },
20
+ details: { name: userDetails.hub_domain },
21
+ }
22
+ },
23
+ apiPropertiesToPersist: {
24
+ credential: [
25
+ 'access_token', 'refresh_token'
26
+ ],
27
+ entity: [],
28
+ },
29
+ getCredentialDetails: async function(api, userId) {
30
+ const userDetails = await api.getUserDetails();
31
+ return {
32
+ identifiers: { externalId: userDetails.portalId, user: userId },
33
+ details: {}
34
+ };
35
+ },
36
+ testAuthRequest: async function(api){
37
+ return api.getUserDetails()
38
+ },
39
+ },
40
+ env: {
41
+ client_id: 'test',
42
+ client_secret: 'test',
43
+ scope: 'test',
44
+ redirect_uri: `http://localhost:3000/redirect/anapi`,
45
+ }
46
+ };
47
+
48
+ module.exports = { Definition };
@@ -0,0 +1,43 @@
1
+ const authorizeResponse = {
2
+ "base": "/redirect/hubspot",
3
+ "data": {
4
+ "code": "test-code",
5
+ "state": "null"
6
+ }
7
+ }
8
+
9
+ const tokenResponse = {
10
+ "token_type": "bearer",
11
+ "refresh_token": "test-refresh-token",
12
+ "access_token": "test-access-token",
13
+ "expires_in": 1800
14
+ }
15
+
16
+ const userDetailsResponse = {
17
+ "portalId": 111111111,
18
+ "timeZone": "US/Eastern",
19
+ "accountType": "DEVELOPER_TEST",
20
+ "currency": "USD",
21
+ "utcOffset": "-05:00",
22
+ "utcOffsetMilliseconds": -18000000,
23
+ "token": "test-token",
24
+ "user": "projectteam@lefthook.co",
25
+ "hub_domain": "Testing Object Things-dev-44613847.com",
26
+ "scopes": [
27
+ "content",
28
+ "oauth",
29
+ "crm.objects.contacts.read",
30
+ "crm.objects.contacts.write",
31
+ "crm.objects.companies.write",
32
+ "crm.objects.companies.read",
33
+ "crm.objects.deals.read",
34
+ "crm.schemas.deals.read"
35
+ ],
36
+ "hub_id": 111111111,
37
+ "app_id": 22222222,
38
+ "expires_in": 1704,
39
+ "user_id": 33333333,
40
+ "token_type": "access"
41
+ }
42
+
43
+ module.exports = { authorizeResponse, tokenResponse, userDetailsResponse }
package/package.json CHANGED
@@ -1,12 +1,35 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "0.2.31-v1-alpha.6",
4
+ "version": "1.0.1-canary.278.7b20420.0",
5
+ "dependencies": {
6
+ "@hapi/boom": "^10.0.1",
7
+ "aws-sdk": "^2.1200.0",
8
+ "bcryptjs": "^2.4.3",
9
+ "common-tags": "^1.8.2",
10
+ "express": "^4.18.2",
11
+ "express-async-handler": "^1.2.0",
12
+ "lodash": "^4.17.21",
13
+ "lodash.get": "^4.4.2",
14
+ "mongoose": "6.11.6",
15
+ "node-fetch": "^2.6.7"
16
+ },
5
17
  "devDependencies": {
6
- "@friggframework/eslint-config": "^1.0.21-v1-alpha.4",
7
- "eslint": "^8.22.0",
18
+ "@friggframework/devtools": "1.0.1-canary.278.7b20420.0",
19
+ "@types/lodash": "^4.14.191",
20
+ "@typescript-eslint/eslint-plugin": "^5.55.0",
21
+ "chai": "^4.3.6",
22
+ "eslint": "^8.36.0",
23
+ "eslint-config-standard-with-typescript": "^34.0.1",
24
+ "eslint-plugin-import": "^2.27.5",
25
+ "eslint-plugin-n": "^15.6.1",
26
+ "eslint-plugin-promise": "^6.1.1",
8
27
  "jest": "^28.1.3",
9
- "prettier": "^2.7.1"
28
+ "jest-runner-groups": "^2.2.0",
29
+ "mongodb-memory-server": "^8.9.0",
30
+ "prettier": "^2.8.5",
31
+ "sinon": "^14.0.0",
32
+ "typescript": "^5.0.2"
10
33
  },
11
34
  "scripts": {
12
35
  "lint:fix": "prettier --write --loglevel error . && eslint . --fix",
@@ -23,14 +46,6 @@
23
46
  "url": "https://github.com/friggframework/frigg/issues"
24
47
  },
25
48
  "homepage": "https://github.com/friggframework/frigg#readme",
26
- "dependencies": {
27
- "@friggframework/assertions": "^1.0.22-v1-alpha.6",
28
- "@friggframework/errors": "^1.1.19-v1-alpha.5",
29
- "aws-sdk": "^2.1200.0",
30
- "lodash": "^4.17.21",
31
- "moment": "^2.29.4",
32
- "node-fetch": "^2.6.7"
33
- },
34
49
  "description": "",
35
- "gitHead": "b5ae7333eb660a8014a7c27a08b5c8cb1938ff1c"
50
+ "gitHead": "7b20420ab945a52ffaf7d8caaf4c45d97a10204c"
36
51
  }
@@ -0,0 +1,3 @@
1
+ # syncs
2
+
3
+ This package exports the `syncs` classes, models, and functions used in [Frigg](https://friggframework.org). You can find its documentation [on Frigg's website](https://docs.friggframework.org/packages/syncs).
File without changes
@@ -0,0 +1,5 @@
1
+ module.exports = async () => {
2
+ return {
3
+ preset: "@friggframework/test-environment",
4
+ };
5
+ };