@friggframework/core 2.0.0--canary.396.b9f7d5a.0 → 2.0.0--canary.396.d50d765.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.
@@ -0,0 +1,38 @@
1
+ const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
2
+ const path = require('node:path');
3
+ const fs = require('fs-extra');
4
+
5
+ /**
6
+ * Loads the App definition from the nearest backend package
7
+ * @function loadAppDefinition
8
+ * @description Searches for the nearest backend package.json, loads the corresponding index.js file,
9
+ * and extracts the application definition containing integrations and user configuration.
10
+ * @returns {{integrations: Array<object>, userConfig: object | null}} An object containing the application definition.
11
+ * @throws {Error} Throws error if backend package.json cannot be found.
12
+ * @throws {Error} Throws error if index.js file cannot be found in the backend directory.
13
+ * @example
14
+ * const { integrations, userConfig } = loadAppDefinition();
15
+ * console.log(`Found ${integrations.length} integrations`);
16
+ */
17
+ function loadAppDefinition() {
18
+ const backendPath = findNearestBackendPackageJson();
19
+ if (!backendPath) {
20
+ throw new Error('Could not find backend package.json');
21
+ }
22
+
23
+ const backendDir = path.dirname(backendPath);
24
+ const backendFilePath = path.join(backendDir, 'index.js');
25
+ if (!fs.existsSync(backendFilePath)) {
26
+ throw new Error('Could not find index.js');
27
+ }
28
+
29
+ const backendJsFile = require(backendFilePath);
30
+ const appDefinition = backendJsFile.Definition;
31
+
32
+ const { integrations = [], user: userConfig = null } = appDefinition;
33
+ return { integrations, userConfig };
34
+ }
35
+
36
+ module.exports = {
37
+ loadAppDefinition,
38
+ };
@@ -1,41 +1,6 @@
1
- const { findNearestBackendPackageJson } = require('@friggframework/core/utils');
2
- const path = require('node:path');
3
- const fs = require('fs-extra');
4
1
  const { Router } = require('express');
5
2
  const { Worker } = require('@friggframework/core');
6
3
 
7
-
8
- /**
9
- * Loads the App definition from the nearest backend package
10
- * @function loadAppDefinition
11
- * @description Searches for the nearest backend package.json, loads the corresponding index.js file,
12
- * and extracts the application definition containing integrations and user configuration.
13
- * @returns {{integrations: Array<object>, userConfig: object | null}} An object containing the application definition.
14
- * @throws {Error} Throws error if backend package.json cannot be found.
15
- * @throws {Error} Throws error if index.js file cannot be found in the backend directory.
16
- * @example
17
- * const { integrations, userConfig } = loadAppDefinition();
18
- * console.log(`Found ${integrations.length} integrations`);
19
- */
20
- function loadAppDefinition() {
21
- const backendPath = findNearestBackendPackageJson();
22
- if (!backendPath) {
23
- throw new Error('Could not find backend package.json');
24
- }
25
-
26
- const backendDir = path.dirname(backendPath);
27
- const backendFilePath = path.join(backendDir, 'index.js');
28
- if (!fs.existsSync(backendFilePath)) {
29
- throw new Error('Could not find index.js');
30
- }
31
-
32
- const backendJsFile = require(backendFilePath);
33
- const appDefinition = backendJsFile.Definition;
34
-
35
- const { integrations = [], user: userConfig = null } = appDefinition;
36
- return { integrations, userConfig };
37
- }
38
-
39
4
  const loadRouterFromObject = (IntegrationClass, routerObject) => {
40
5
  const router = Router();
41
6
  const { path, method, event } = routerObject;
@@ -100,5 +65,4 @@ const createQueueWorker = (integrationClass, integrationFactory) => {
100
65
  module.exports = {
101
66
  loadRouterFromObject,
102
67
  createQueueWorker,
103
- loadAppDefinition,
104
68
  };
@@ -2,7 +2,7 @@ const { createIntegrationRouter } = require('@friggframework/core');
2
2
  const { createAppHandler } = require('./../app-handler-helpers');
3
3
  const {
4
4
  loadAppDefinition,
5
- } = require('./../backend-utils');
5
+ } = require('../app-definition-loader');
6
6
  const { UserRepository } = require('../../user/user-repository');
7
7
  const { IntegrationFactory } = require('../../integrations/integration-factory');
8
8
  const { GetUserFromBearerToken } = require('../../user/use-cases/get-user-from-bearer-token');
@@ -2,7 +2,7 @@ const { createAppHandler } = require('./../app-handler-helpers');
2
2
  const {
3
3
  loadAppDefinition,
4
4
  loadRouterFromObject,
5
- } = require('./../backend-utils');
5
+ } = require('../app-definition-loader');
6
6
  const { Router } = require('express');
7
7
  const { IntegrationFactory } = require('../../integrations/integration-factory');
8
8
 
@@ -10,7 +10,7 @@ const {
10
10
  CreateTokenForUserId,
11
11
  } = require('../../user/use-cases/create-token-for-user-id');
12
12
  const catchAsyncError = require('express-async-handler');
13
- const { loadAppDefinition } = require('../backend-utils');
13
+ const { loadAppDefinition } = require('../app-definition-loader');
14
14
 
15
15
  const router = express();
16
16
  const { userConfig } = loadAppDefinition();
@@ -1,5 +1,5 @@
1
1
  const { createHandler } = require('@friggframework/core');
2
- const { loadAppDefinition, createQueueWorker } = require('../backend-utils');
2
+ const { loadAppDefinition, createQueueWorker } = require('../app-definition-loader');
3
3
  const { IntegrationFactory } = require('../../integrations/integration-factory');
4
4
 
5
5
  const handlers = {};
@@ -12,38 +12,9 @@ const { GetCredentialForUser } = require('../credential/use-cases/get-credential
12
12
  const { CreateIntegration } = require('./use-cases/create-integration');
13
13
  const { ModuleService } = require('../module-plugin/module-service');
14
14
  const { ModuleRepository } = require('../module-plugin/module-repository');
15
- const { loadAppDefinition } = require('../handlers/backend-utils');
16
-
17
-
18
- const { integrations } = loadAppDefinition();
19
- const moduleRepository = new ModuleRepository();
20
- const integrationRepository = new IntegrationRepository();
21
- const credentialRepository = new CredentialRepository();
22
- const moduleService = new ModuleService({ moduleRepository, moduleDefinitions: { ...getModules() } });
23
- const deleteIntegrationForUser = new DeleteIntegrationForUser({ integrationRepository });
24
- const getIntegrationForUser = new GetIntegrationForUser({ integrationRepository });
25
- const getIntegrationsForUser = new GetIntegrationsForUser({ integrationRepository });
26
- const getCredentialForUser = new GetCredentialForUser({ credentialRepository });
27
- const createIntegration = new CreateIntegration({
28
- integrationRepository,
29
- integrationClasses: integrations,
30
- moduleService,
31
- });
32
-
33
- // todo: move this into a utils file
34
- const getModules = () => {
35
- return [
36
- ...new Set(
37
- integrations
38
- .map((integration) =>
39
- Object.values(integration.Definition.modules).map(
40
- (module) => module.definition
41
- )
42
- )
43
- .flat()
44
- ),
45
- ];
46
- }
15
+ const {
16
+ loadAppDefinition,
17
+ } = require('../handlers/app-definition-loader');
47
18
 
48
19
  // todo: dont send moduleFactory and integrationFactory as a factory object, instead send them as separate params.
49
20
  // todo: this could be a use case class
@@ -58,12 +29,59 @@ const getModules = () => {
58
29
  * @returns {express.Router} Configured Express router with integration and entity routes
59
30
  */
60
31
  function createIntegrationRouter(params) {
32
+ const { integrations } = loadAppDefinition();
33
+ const moduleRepository = new ModuleRepository();
34
+ const integrationRepository = new IntegrationRepository();
35
+ const credentialRepository = new CredentialRepository();
36
+
37
+ // todo: move this into a utils file
38
+ const getModules = () => {
39
+ return [
40
+ ...new Set(
41
+ integrations
42
+ .map((integration) =>
43
+ Object.values(integration.Definition.modules).map(
44
+ (module) => module.definition
45
+ )
46
+ )
47
+ .flat()
48
+ ),
49
+ ];
50
+ };
51
+ const moduleService = new ModuleService({
52
+ moduleRepository,
53
+ moduleDefinitions: getModules(),
54
+ });
55
+ const deleteIntegrationForUser = new DeleteIntegrationForUser({
56
+ integrationRepository,
57
+ });
58
+ const getIntegrationForUser = new GetIntegrationForUser({
59
+ integrationRepository,
60
+ });
61
+ const getIntegrationsForUser = new GetIntegrationsForUser({
62
+ integrationRepository,
63
+ });
64
+ const getCredentialForUser = new GetCredentialForUser({
65
+ credentialRepository,
66
+ });
67
+ const createIntegration = new CreateIntegration({
68
+ integrationRepository,
69
+ integrationClasses: integrations,
70
+ moduleService,
71
+ });
72
+
61
73
  const router = get(params, 'router', express());
62
74
  const factory = get(params, 'factory');
63
75
  const getUserFromBearerToken = get(params, 'getUserFromBearerToken');
64
76
 
65
- setIntegrationRoutes(router, factory, getUserFromBearerToken);
66
- setEntityRoutes(router, factory, getUserFromBearerToken);
77
+ setIntegrationRoutes(router, factory, getUserFromBearerToken, {
78
+ createIntegration,
79
+ deleteIntegrationForUser,
80
+ getIntegrationsForUser,
81
+ });
82
+ setEntityRoutes(router, factory, getUserFromBearerToken, {
83
+ getCredentialForUser,
84
+ });
67
85
  return router;
68
86
  }
69
87
 
@@ -97,8 +115,13 @@ function checkRequiredParams(params, requiredKeys) {
97
115
  * @param {Object} factory.integrationFactory - Factory for creating and managing integrations
98
116
  * @param {import('../user/use-cases/get-user-from-bearer-token').GetUserFromBearerToken} getUserFromBearerToken - Use case for retrieving a user from a bearer token
99
117
  */
100
- function setIntegrationRoutes(router, factory, getUserFromBearerToken) {
118
+ function setIntegrationRoutes(router, factory, getUserFromBearerToken, useCases) {
101
119
  const { moduleFactory, integrationFactory } = factory;
120
+ const {
121
+ createIntegration,
122
+ deleteIntegrationForUser,
123
+ getIntegrationsForUser,
124
+ } = useCases;
102
125
 
103
126
  router.route('/api/integrations').get(
104
127
  catchAsyncError(async (req, res) => {
@@ -402,8 +425,9 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken) {
402
425
  * @param {Object} factory - Factory object containing moduleFactory
403
426
  * @param {import('../user/use-cases/get-user-from-bearer-token').GetUserFromBearerToken} getUserFromBearerToken - Use case for retrieving a user from a bearer token
404
427
  */
405
- function setEntityRoutes(router, factory, getUserFromBearerToken) {
428
+ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
406
429
  const { moduleFactory } = factory;
430
+ const { getCredentialForUser } = useCases;
407
431
  const getModuleInstance = async (userId, entityType) => {
408
432
  if (!moduleFactory.checkIsValidType(entityType)) {
409
433
  throw Boom.badRequest(
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.396.b9f7d5a.0",
4
+ "version": "2.0.0--canary.396.d50d765.0",
5
5
  "dependencies": {
6
6
  "@hapi/boom": "^10.0.1",
7
7
  "aws-sdk": "^2.1200.0",
@@ -22,9 +22,9 @@
22
22
  "uuid": "^9.0.1"
23
23
  },
24
24
  "devDependencies": {
25
- "@friggframework/eslint-config": "2.0.0--canary.396.b9f7d5a.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.396.b9f7d5a.0",
27
- "@friggframework/test": "2.0.0--canary.396.b9f7d5a.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.396.d50d765.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.396.d50d765.0",
27
+ "@friggframework/test": "2.0.0--canary.396.d50d765.0",
28
28
  "@types/lodash": "4.17.15",
29
29
  "@typescript-eslint/eslint-plugin": "^8.0.0",
30
30
  "chai": "^4.3.6",
@@ -53,5 +53,5 @@
53
53
  },
54
54
  "homepage": "https://github.com/friggframework/frigg#readme",
55
55
  "description": "",
56
- "gitHead": "b9f7d5af4da61a0775ab275e823dbb63247be3b6"
56
+ "gitHead": "d50d765cf88cd18a604ded97837926ad2ab0bc87"
57
57
  }