@friggframework/core 2.0.0--canary.393.f4a61c1.0 → 2.0.0--canary.395.d07514c.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/handlers/app-handler-helpers.js +0 -3
- package/handlers/backend-utils.js +34 -16
- package/handlers/routers/auth.js +18 -9
- package/handlers/routers/integration-defined-routers.js +7 -2
- package/handlers/routers/user.js +25 -5
- package/handlers/workers/integration-defined-workers.js +5 -1
- package/index.js +0 -2
- package/integrations/index.js +0 -2
- package/integrations/integration-factory.js +5 -6
- package/integrations/integration-router.js +133 -43
- package/module-plugin/auther.js +3 -2
- package/module-plugin/test/mock-api/api.js +8 -3
- package/module-plugin/test/mock-api/definition.js +12 -8
- package/package.json +5 -5
- package/user/tests/doubles/test-user-repository.js +115 -0
- package/user/tests/use-cases/create-individual-user.test.js +24 -0
- package/user/tests/use-cases/create-organization-user.test.js +28 -0
- package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
- package/user/tests/use-cases/login-user.test.js +148 -0
- package/user/use-cases/create-individual-user.js +52 -0
- package/user/use-cases/create-organization-user.js +39 -0
- package/user/use-cases/create-token-for-user-id.js +30 -0
- package/user/use-cases/get-user-from-bearer-token.js +50 -0
- package/user/use-cases/login-user.js +88 -0
- package/user/user-repository.js +97 -0
- package/user/user.js +77 -0
- package/handlers/routers/middleware/loadUser.js +0 -15
- package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
- package/integrations/create-frigg-backend.js +0 -31
- package/integrations/integration-user.js +0 -144
|
@@ -3,7 +3,6 @@ const express = require('express');
|
|
|
3
3
|
const bodyParser = require('body-parser');
|
|
4
4
|
const cors = require('cors');
|
|
5
5
|
const Boom = require('@hapi/boom');
|
|
6
|
-
const loadUserManager = require('./routers/middleware/loadUser');
|
|
7
6
|
const serverlessHttp = require('serverless-http');
|
|
8
7
|
|
|
9
8
|
const createApp = (applyMiddleware) => {
|
|
@@ -20,8 +19,6 @@ const createApp = (applyMiddleware) => {
|
|
|
20
19
|
})
|
|
21
20
|
);
|
|
22
21
|
|
|
23
|
-
app.use(loadUserManager);
|
|
24
|
-
|
|
25
22
|
if (applyMiddleware) applyMiddleware(app);
|
|
26
23
|
|
|
27
24
|
// Handle sending error response and logging server errors to console
|
|
@@ -1,24 +1,40 @@
|
|
|
1
|
-
const { createFriggBackend, Worker } = require('@friggframework/core');
|
|
2
1
|
const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
|
|
3
2
|
const path = require('node:path');
|
|
4
3
|
const fs = require('fs-extra');
|
|
4
|
+
const { Router } = require('express');
|
|
5
5
|
|
|
6
|
-
const backendPath = findNearestBackendPackageJson();
|
|
7
|
-
if (!backendPath) {
|
|
8
|
-
throw new Error('Could not find backend package.json');
|
|
9
|
-
}
|
|
10
6
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Loads the App definition from the nearest backend package
|
|
9
|
+
* @function loadAppDefinition
|
|
10
|
+
* @description Searches for the nearest backend package.json, loads the corresponding index.js file,
|
|
11
|
+
* and extracts the application definition containing integrations and user configuration.
|
|
12
|
+
* @returns {{integrations: Array<object>, userConfig: object | null}} An object containing the application definition.
|
|
13
|
+
* @throws {Error} Throws error if backend package.json cannot be found.
|
|
14
|
+
* @throws {Error} Throws error if index.js file cannot be found in the backend directory.
|
|
15
|
+
* @example
|
|
16
|
+
* const { integrations, userConfig } = loadAppDefinition();
|
|
17
|
+
* console.log(`Found ${integrations.length} integrations`);
|
|
18
|
+
*/
|
|
19
|
+
function loadAppDefinition() {
|
|
20
|
+
const backendPath = findNearestBackendPackageJson();
|
|
21
|
+
if (!backendPath) {
|
|
22
|
+
throw new Error('Could not find backend package.json');
|
|
23
|
+
}
|
|
16
24
|
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
25
|
+
const backendDir = path.dirname(backendPath);
|
|
26
|
+
const backendFilePath = path.join(backendDir, 'index.js');
|
|
27
|
+
if (!fs.existsSync(backendFilePath)) {
|
|
28
|
+
throw new Error('Could not find index.js');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const backendJsFile = require(backendFilePath);
|
|
32
|
+
const appDefinition = backendJsFile.Definition;
|
|
33
|
+
|
|
34
|
+
const { integrations = [], user: userConfig = null } = appDefinition;
|
|
35
|
+
return { integrations, userConfig };
|
|
36
|
+
}
|
|
20
37
|
|
|
21
|
-
const backend = createFriggBackend(appDefinition);
|
|
22
38
|
const loadRouterFromObject = (IntegrationClass, routerObject) => {
|
|
23
39
|
const router = Router();
|
|
24
40
|
const { path, method, event } = routerObject;
|
|
@@ -30,7 +46,7 @@ const loadRouterFromObject = (IntegrationClass, routerObject) => {
|
|
|
30
46
|
const integration = new IntegrationClass({});
|
|
31
47
|
await integration.loadModules();
|
|
32
48
|
await integration.registerEventHandlers();
|
|
33
|
-
const result = await integration.send(event, {req, res, next});
|
|
49
|
+
const result = await integration.send(event, { req, res, next });
|
|
34
50
|
res.json(result);
|
|
35
51
|
} catch (error) {
|
|
36
52
|
next(error);
|
|
@@ -39,6 +55,8 @@ const loadRouterFromObject = (IntegrationClass, routerObject) => {
|
|
|
39
55
|
|
|
40
56
|
return router;
|
|
41
57
|
};
|
|
58
|
+
|
|
59
|
+
//todo: this should be in a use case class
|
|
42
60
|
const createQueueWorker = (integrationClass, integrationFactory) => {
|
|
43
61
|
class QueueWorker extends Worker {
|
|
44
62
|
async _run(params, context) {
|
|
@@ -79,7 +97,7 @@ const createQueueWorker = (integrationClass, integrationFactory) => {
|
|
|
79
97
|
};
|
|
80
98
|
|
|
81
99
|
module.exports = {
|
|
82
|
-
...backend,
|
|
83
100
|
loadRouterFromObject,
|
|
84
101
|
createQueueWorker,
|
|
102
|
+
loadAppDefinition,
|
|
85
103
|
};
|
package/handlers/routers/auth.js
CHANGED
|
@@ -1,26 +1,35 @@
|
|
|
1
1
|
const { createIntegrationRouter } = require('@friggframework/core');
|
|
2
2
|
const { createAppHandler } = require('./../app-handler-helpers');
|
|
3
|
-
const { requireLoggedInUser } = require('./middleware/requireLoggedInUser');
|
|
4
3
|
const {
|
|
5
|
-
|
|
6
|
-
integrationFactory,
|
|
7
|
-
IntegrationHelper,
|
|
4
|
+
loadAppDefinition,
|
|
8
5
|
} = require('./../backend-utils');
|
|
6
|
+
const { UserRepository } = require('../../user/user-repository');
|
|
7
|
+
const { IntegrationFactory, IntegrationHelper } = require('../../integrations/integration-factory');
|
|
8
|
+
const { GetUserFromBearerToken } = require('../../user/use-cases/get-user-from-bearer-token');
|
|
9
|
+
|
|
10
|
+
const { integrations, userConfig } = loadAppDefinition();
|
|
11
|
+
const integrationFactory = new IntegrationFactory(integrations);
|
|
12
|
+
const userRepository = new UserRepository({ userConfig });
|
|
13
|
+
const getUserFromBearerToken = new GetUserFromBearerToken({ userRepository });
|
|
9
14
|
|
|
10
15
|
const router = createIntegrationRouter({
|
|
11
|
-
factory: {
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
factory: {
|
|
17
|
+
moduleFactory: integrationFactory.moduleFactory,
|
|
18
|
+
integrationFactory,
|
|
19
|
+
IntegrationHelper
|
|
20
|
+
},
|
|
21
|
+
getUserFromBearerToken,
|
|
14
22
|
});
|
|
15
23
|
|
|
24
|
+
//todo: what is this route doing here?
|
|
16
25
|
router.route('/redirect/:appId').get((req, res) => {
|
|
17
26
|
res.redirect(
|
|
18
|
-
`${process.env.FRONTEND_URI}/redirect/${
|
|
19
|
-
req.params.appId
|
|
27
|
+
`${process.env.FRONTEND_URI}/redirect/${req.params.appId
|
|
20
28
|
}?${new URLSearchParams(req.query)}`
|
|
21
29
|
);
|
|
22
30
|
});
|
|
23
31
|
|
|
24
32
|
const handler = createAppHandler('HTTP Event: Auth', router);
|
|
25
33
|
|
|
34
|
+
// todo: I can not find where router is used, do we need to export it?
|
|
26
35
|
module.exports = { handler, router };
|
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
const { createAppHandler } = require('./../app-handler-helpers');
|
|
2
2
|
const {
|
|
3
|
-
|
|
3
|
+
loadAppDefinition,
|
|
4
4
|
loadRouterFromObject,
|
|
5
5
|
} = require('./../backend-utils');
|
|
6
6
|
const { Router } = require('express');
|
|
7
|
+
const { IntegrationFactory } = require('../../integrations/integration-factory');
|
|
7
8
|
|
|
8
9
|
const handlers = {};
|
|
10
|
+
const { integrations } = loadAppDefinition();
|
|
11
|
+
const integrationFactory = new IntegrationFactory(integrations);
|
|
12
|
+
|
|
13
|
+
//todo: this should be in a use case class
|
|
9
14
|
for (const IntegrationClass of integrationFactory.integrationClasses) {
|
|
10
15
|
const router = Router();
|
|
11
16
|
const basePath = `/api/${IntegrationClass.Definition.name}-integration`;
|
|
12
|
-
|
|
17
|
+
|
|
13
18
|
console.log(`\n│ Configuring routes for ${IntegrationClass.Definition.name} Integration:`);
|
|
14
19
|
|
|
15
20
|
for (const routeDef of IntegrationClass.Definition.routes) {
|
package/handlers/routers/user.js
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
2
|
const { createAppHandler } = require('../app-handler-helpers');
|
|
3
3
|
const { checkRequiredParams } = require('@friggframework/core');
|
|
4
|
-
const {
|
|
4
|
+
const { UserRepository } = require('../../user/user-repository');
|
|
5
|
+
const {
|
|
6
|
+
CreateIndividualUser,
|
|
7
|
+
} = require('../../user/use-cases/create-individual-user');
|
|
8
|
+
const { LoginUser } = require('../../user/use-cases/login-user');
|
|
9
|
+
const {
|
|
10
|
+
CreateTokenForUserId,
|
|
11
|
+
} = require('../../user/use-cases/create-token-for-user-id');
|
|
5
12
|
const catchAsyncError = require('express-async-handler');
|
|
13
|
+
const { loadAppDefinition } = require('../backend-utils');
|
|
6
14
|
|
|
7
15
|
const router = express();
|
|
16
|
+
const { userConfig } = loadAppDefinition();
|
|
17
|
+
const userRepository = new UserRepository({ userConfig });
|
|
18
|
+
const createIndividualUser = new CreateIndividualUser({
|
|
19
|
+
userRepository,
|
|
20
|
+
userConfig,
|
|
21
|
+
});
|
|
22
|
+
const loginUser = new LoginUser({
|
|
23
|
+
userRepository,
|
|
24
|
+
userConfig,
|
|
25
|
+
});
|
|
26
|
+
const createTokenForUserId = new CreateTokenForUserId({ userRepository });
|
|
8
27
|
|
|
9
28
|
// define the login endpoint
|
|
10
29
|
router.route('/user/login').post(
|
|
@@ -13,8 +32,8 @@ router.route('/user/login').post(
|
|
|
13
32
|
'username',
|
|
14
33
|
'password',
|
|
15
34
|
]);
|
|
16
|
-
const user = await
|
|
17
|
-
const token = await user.
|
|
35
|
+
const user = await loginUser.execute({ username, password });
|
|
36
|
+
const token = await createTokenForUserId.execute(user.getId(), 120);
|
|
18
37
|
res.status(201);
|
|
19
38
|
res.json({ token });
|
|
20
39
|
})
|
|
@@ -26,11 +45,12 @@ router.route('/user/create').post(
|
|
|
26
45
|
'username',
|
|
27
46
|
'password',
|
|
28
47
|
]);
|
|
29
|
-
|
|
48
|
+
|
|
49
|
+
const user = await createIndividualUser.execute({
|
|
30
50
|
username,
|
|
31
51
|
password,
|
|
32
52
|
});
|
|
33
|
-
const token = await user.
|
|
53
|
+
const token = await createTokenForUserId.execute(user.getId(), 120);
|
|
34
54
|
res.status(201);
|
|
35
55
|
res.json({ token });
|
|
36
56
|
})
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
const { createHandler } = require('@friggframework/core');
|
|
2
|
-
const {
|
|
2
|
+
const { loadAppDefinition, createQueueWorker } = require('../backend-utils');
|
|
3
|
+
const { IntegrationFactory } = require('../../integrations/integration-factory');
|
|
3
4
|
|
|
4
5
|
const handlers = {};
|
|
6
|
+
const { integrations } = loadAppDefinition();
|
|
7
|
+
const integrationFactory = new IntegrationFactory(integrations);
|
|
8
|
+
|
|
5
9
|
integrationFactory.integrationClasses.forEach((IntegrationClass) => {
|
|
6
10
|
const defaultQueueWorker = createQueueWorker(IntegrationClass, integrationFactory);
|
|
7
11
|
|
package/index.js
CHANGED
|
@@ -42,7 +42,6 @@ const {
|
|
|
42
42
|
IntegrationHelper,
|
|
43
43
|
createIntegrationRouter,
|
|
44
44
|
checkRequiredParams,
|
|
45
|
-
createFriggBackend,
|
|
46
45
|
} = require('./integrations/index');
|
|
47
46
|
const { TimeoutCatcher } = require('./lambda/index');
|
|
48
47
|
const { debug, initDebugLog, flushDebugLog } = require('./logs/index');
|
|
@@ -113,7 +112,6 @@ module.exports = {
|
|
|
113
112
|
IntegrationHelper,
|
|
114
113
|
checkRequiredParams,
|
|
115
114
|
createIntegrationRouter,
|
|
116
|
-
createFriggBackend,
|
|
117
115
|
|
|
118
116
|
// lambda
|
|
119
117
|
TimeoutCatcher,
|
package/integrations/index.js
CHANGED
|
@@ -4,7 +4,6 @@ const { Options } = require('./options');
|
|
|
4
4
|
const { IntegrationMapping } = require('./integration-mapping');
|
|
5
5
|
const { IntegrationFactory, IntegrationHelper } = require('./integration-factory');
|
|
6
6
|
const { createIntegrationRouter, checkRequiredParams } = require('./integration-router');
|
|
7
|
-
const { createFriggBackend } = require('./create-frigg-backend');
|
|
8
7
|
|
|
9
8
|
module.exports = {
|
|
10
9
|
IntegrationBase,
|
|
@@ -15,5 +14,4 @@ module.exports = {
|
|
|
15
14
|
IntegrationHelper,
|
|
16
15
|
createIntegrationRouter,
|
|
17
16
|
checkRequiredParams,
|
|
18
|
-
createFriggBackend
|
|
19
17
|
};
|
|
@@ -89,14 +89,13 @@ class IntegrationFactory {
|
|
|
89
89
|
return moduleTypesAndKeys;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
async getInstanceFromIntegrationId(
|
|
92
|
+
async getInstanceFromIntegrationId({ integrationId, userId }) {
|
|
93
93
|
const integrationRecord = await IntegrationHelper.getIntegrationById(
|
|
94
|
-
|
|
94
|
+
integrationId
|
|
95
95
|
);
|
|
96
|
-
let { userId } = params;
|
|
97
96
|
if (!integrationRecord) {
|
|
98
97
|
throw new Error(
|
|
99
|
-
`No integration found by the ID of ${
|
|
98
|
+
`No integration found by the ID of ${integrationId}`
|
|
100
99
|
);
|
|
101
100
|
}
|
|
102
101
|
|
|
@@ -104,8 +103,7 @@ class IntegrationFactory {
|
|
|
104
103
|
userId = integrationRecord.user._id.toString();
|
|
105
104
|
} else if (userId.toString() !== integrationRecord.user.toString()) {
|
|
106
105
|
throw new Error(
|
|
107
|
-
`Integration ${
|
|
108
|
-
params.integrationId
|
|
106
|
+
`Integration ${params.integrationId
|
|
109
107
|
} does not belong to User ${userId}, ${integrationRecord.user.toString()}`
|
|
110
108
|
);
|
|
111
109
|
}
|
|
@@ -189,6 +187,7 @@ class IntegrationFactory {
|
|
|
189
187
|
}
|
|
190
188
|
}
|
|
191
189
|
|
|
190
|
+
// todo: this should be split into use case classes
|
|
192
191
|
const IntegrationHelper = {
|
|
193
192
|
getFormattedIntegration: async function (integrationRecord) {
|
|
194
193
|
const integrationObj = {
|