@flowerforce/flowerbase 1.2.0 → 1.2.1-beta.2
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/dist/auth/controller.d.ts.map +1 -1
- package/dist/auth/controller.js +3 -0
- package/dist/auth/providers/custom-function/controller.d.ts.map +1 -1
- package/dist/auth/providers/custom-function/controller.js +5 -2
- package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -1
- package/dist/auth/providers/local-userpass/controller.js +7 -10
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/auth/utils.js +3 -2
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +5 -1
- package/dist/features/functions/controller.d.ts.map +1 -1
- package/dist/features/functions/controller.js +28 -2
- package/dist/features/rules/utils.d.ts.map +1 -1
- package/dist/features/rules/utils.js +11 -2
- package/dist/features/triggers/utils.d.ts.map +1 -1
- package/dist/features/triggers/utils.js +52 -2
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -9
- package/dist/services/mongodb-atlas/index.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/index.js +540 -483
- package/dist/services/mongodb-atlas/utils.d.ts +9 -2
- package/dist/services/mongodb-atlas/utils.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/utils.js +113 -23
- package/dist/shared/handleUserRegistration.d.ts.map +1 -1
- package/dist/shared/handleUserRegistration.js +1 -0
- package/dist/shared/models/handleUserRegistration.model.d.ts +6 -2
- package/dist/shared/models/handleUserRegistration.model.d.ts.map +1 -1
- package/dist/utils/context/helpers.d.ts +6 -5
- package/dist/utils/context/helpers.d.ts.map +1 -1
- package/dist/utils/context/helpers.js +3 -0
- package/dist/utils/context/index.d.ts.map +1 -1
- package/dist/utils/context/index.js +2 -0
- package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -1
- package/dist/utils/initializer/exposeRoutes.js +11 -4
- package/dist/utils/initializer/registerPlugins.d.ts +3 -1
- package/dist/utils/initializer/registerPlugins.d.ts.map +1 -1
- package/dist/utils/initializer/registerPlugins.js +9 -6
- package/dist/utils/roles/helpers.js +9 -2
- package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -1
- package/dist/utils/roles/machines/commonValidators.js +10 -6
- package/dist/utils/roles/machines/read/B/validators.d.ts +4 -0
- package/dist/utils/roles/machines/read/B/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/B/validators.js +8 -0
- package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/C/index.js +10 -7
- package/dist/utils/roles/machines/read/C/validators.d.ts +5 -0
- package/dist/utils/roles/machines/read/C/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/C/validators.js +29 -0
- package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/D/index.js +13 -11
- package/dist/utils/rules.d.ts +1 -1
- package/dist/utils/rules.d.ts.map +1 -1
- package/dist/utils/rules.js +26 -17
- package/jest.config.ts +2 -12
- package/jest.setup.ts +28 -0
- package/package.json +1 -1
- package/src/auth/controller.ts +3 -0
- package/src/auth/providers/custom-function/controller.ts +5 -2
- package/src/auth/providers/local-userpass/controller.ts +13 -10
- package/src/auth/utils.ts +6 -3
- package/src/constants.ts +7 -2
- package/src/fastify.d.ts +32 -15
- package/src/features/functions/controller.ts +36 -2
- package/src/features/rules/utils.ts +11 -2
- package/src/features/triggers/utils.ts +59 -2
- package/src/index.ts +21 -8
- package/src/services/mongodb-atlas/__tests__/utils.test.ts +141 -0
- package/src/services/mongodb-atlas/index.ts +143 -90
- package/src/services/mongodb-atlas/utils.ts +158 -22
- package/src/shared/handleUserRegistration.ts +3 -3
- package/src/shared/models/handleUserRegistration.model.ts +8 -3
- package/src/types/fastify-raw-body.d.ts +22 -0
- package/src/utils/__tests__/STEP_B_STATES.test.ts +1 -1
- package/src/utils/__tests__/STEP_C_STATES.test.ts +1 -1
- package/src/utils/__tests__/STEP_D_STATES.test.ts +2 -2
- package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +9 -4
- package/src/utils/__tests__/registerPlugins.test.ts +16 -1
- package/src/utils/context/helpers.ts +3 -0
- package/src/utils/context/index.ts +1 -0
- package/src/utils/initializer/exposeRoutes.ts +15 -8
- package/src/utils/initializer/registerPlugins.ts +15 -7
- package/src/utils/roles/helpers.ts +20 -3
- package/src/utils/roles/machines/commonValidators.ts +10 -5
- package/src/utils/roles/machines/read/B/validators.ts +8 -0
- package/src/utils/roles/machines/read/C/index.ts +11 -7
- package/src/utils/roles/machines/read/C/validators.ts +21 -0
- package/src/utils/roles/machines/read/D/index.ts +22 -12
- package/src/utils/rules.ts +31 -22
- package/tsconfig.spec.json +7 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/auth/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAOzC;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/auth/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAOzC;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,eAAe,iBA8ExD"}
|
package/dist/auth/controller.js
CHANGED
|
@@ -33,6 +33,9 @@ function authController(app) {
|
|
|
33
33
|
*/
|
|
34
34
|
app.get(utils_1.AUTH_ENDPOINTS.PROFILE, function (req) {
|
|
35
35
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
if (req.user.typ !== 'access') {
|
|
37
|
+
throw new Error('Access token required');
|
|
38
|
+
}
|
|
36
39
|
const user = yield db
|
|
37
40
|
.collection(authCollection)
|
|
38
41
|
.findOne({ _id: bson_1.ObjectId.createFromHexString(req.user.id) });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../../src/auth/providers/custom-function/controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAezC;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../../src/auth/providers/custom-function/controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAezC;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,iBAiFlE"}
|
|
@@ -68,17 +68,20 @@ function customFunctionController(app) {
|
|
|
68
68
|
});
|
|
69
69
|
if (res.id) {
|
|
70
70
|
const user = yield (0, handleUserRegistration_1.default)(app, { run_as_system: true, skipUserCheck: true, provider: handleUserRegistration_model_1.PROVIDER.CUSTOM_FUNCTION })({ email: res.id, password: (0, utils_1.generatePassword)() });
|
|
71
|
+
if (!(user === null || user === void 0 ? void 0 : user.insertedId)) {
|
|
72
|
+
throw new Error('Failed to register custom user');
|
|
73
|
+
}
|
|
71
74
|
const currentUserData = {
|
|
72
75
|
_id: user.insertedId,
|
|
73
76
|
user_data: {
|
|
74
|
-
_id: user.insertedId
|
|
77
|
+
_id: user.insertedId
|
|
75
78
|
}
|
|
76
79
|
};
|
|
77
80
|
return {
|
|
78
81
|
access_token: this.createAccessToken(currentUserData),
|
|
79
82
|
refresh_token: this.createRefreshToken(currentUserData),
|
|
80
83
|
device_id: '',
|
|
81
|
-
user_id: user.insertedId.toString()
|
|
84
|
+
user_id: user.insertedId.toString()
|
|
82
85
|
};
|
|
83
86
|
}
|
|
84
87
|
throw new Error("Authentication Failed");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../../src/auth/providers/local-userpass/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAuBzC;;;;GAIG;AACH,wBAAsB,uBAAuB,CAAC,GAAG,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../../src/auth/providers/local-userpass/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAuBzC;;;;GAIG;AACH,wBAAsB,uBAAuB,CAAC,GAAG,EAAE,eAAe,iBAqOjE"}
|
|
@@ -44,8 +44,12 @@ function localUserPassController(app) {
|
|
|
44
44
|
schema: utils_1.REGISTRATION_SCHEMA
|
|
45
45
|
}, (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
46
46
|
const result = yield (0, handleUserRegistration_1.default)(app, { run_as_system: true, provider: handleUserRegistration_model_1.PROVIDER.LOCAL_USERPASS })({ email: req.body.email.toLowerCase(), password: req.body.password });
|
|
47
|
+
if (!(result === null || result === void 0 ? void 0 : result.insertedId)) {
|
|
48
|
+
res === null || res === void 0 ? void 0 : res.status(500);
|
|
49
|
+
throw new Error('Failed to register user');
|
|
50
|
+
}
|
|
47
51
|
res === null || res === void 0 ? void 0 : res.status(201);
|
|
48
|
-
return { userId: result
|
|
52
|
+
return { userId: result.insertedId.toString() };
|
|
49
53
|
}));
|
|
50
54
|
/**
|
|
51
55
|
* Endpoint for user login.
|
|
@@ -74,7 +78,7 @@ function localUserPassController(app) {
|
|
|
74
78
|
.findOne({ [user_id_field]: authUser._id.toString() })
|
|
75
79
|
: {};
|
|
76
80
|
authUser === null || authUser === void 0 ? true : delete authUser.password;
|
|
77
|
-
const userWithCustomData = Object.assign(Object.assign({}, authUser), { user_data: user, id: authUser._id.toString() });
|
|
81
|
+
const userWithCustomData = Object.assign(Object.assign({}, authUser), { user_data: Object.assign(Object.assign({}, (user || {})), { _id: authUser._id }), data: { email: authUser.email }, id: authUser._id.toString() });
|
|
78
82
|
if (authUser && authUser.status === 'pending') {
|
|
79
83
|
try {
|
|
80
84
|
yield (db === null || db === void 0 ? void 0 : db.collection(authCollection).updateOne({ _id: authUser._id }, {
|
|
@@ -93,14 +97,7 @@ function localUserPassController(app) {
|
|
|
93
97
|
functionsList[on_user_creation_function_name]) {
|
|
94
98
|
try {
|
|
95
99
|
yield (0, context_1.GenerateContext)({
|
|
96
|
-
args: [
|
|
97
|
-
{
|
|
98
|
-
operationType: 'CREATE',
|
|
99
|
-
providers: 'local-userpass',
|
|
100
|
-
user: userWithCustomData,
|
|
101
|
-
time: new Date().getTime()
|
|
102
|
-
}
|
|
103
|
-
],
|
|
100
|
+
args: [userWithCustomData],
|
|
104
101
|
app,
|
|
105
102
|
rules: {},
|
|
106
103
|
user: userWithCustomData,
|
package/dist/auth/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/auth/utils.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,YAAY;;;;;;;;;;;;;CASxB,CAAA;AAED,eAAO,MAAM,YAAY;;;;;;;;;;;;;CASxB,CAAA;AAED,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;CAUhC,CAAA;AAED,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;CAS/B,CAAA;AAED,oBAAY,cAAc;IACxB,KAAK,WAAW;IAChB,YAAY,cAAc;IAC1B,OAAO,aAAa;IACpB,OAAO,aAAa;IACpB,KAAK,gBAAgB;IACrB,aAAa,WAAW;IACxB,UAAU,sBAAsB;CACjC;AAED,oBAAY,WAAW;IACrB,mBAAmB,wBAAwB;IAC3C,aAAa,mCAAmC;IAChD,oBAAoB,sCAAsC;CAC3D;AAED,MAAM,WAAW,UAAU;IACzB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,gBAAgB,EAAE,aAAa,CAAA;IAC/B,iBAAiB,EAAE,cAAc,CAAA;CAClC;AAED,UAAU,MAAM;IACd,IAAI,EAA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B,aAAa,EAAE,MAAM,CAAA;IACrB,eAAe,EAAE,MAAM,CAAA;IACvB,aAAa,EAAE,MAAM,CAAA;IACrB,8BAA8B,EAAE,MAAM,CAAA;CACvC;AAMD;;;GAGG;AACH,eAAO,MAAM,cAAc,QAAO,UAGjC,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,kBAAkB,QAAO,oBAGrC,CAAA;AAED,eAAO,MAAM,aAAa,GACxB,qBAAqB,MAAM,EAC3B,OAAO,MAAM,EACb,SAAS,MAAM;;;;;CA2DhB,CAAA;AAMD,eAAO,MAAM,gBAAgB,GAAI,eAAW,WAG3C,CAAA"}
|
package/dist/auth/utils.js
CHANGED
|
@@ -65,12 +65,13 @@ var AUTH_ERRORS;
|
|
|
65
65
|
AUTH_ERRORS["INVALID_TOKEN"] = "Invalid refresh token provided";
|
|
66
66
|
AUTH_ERRORS["INVALID_RESET_PARAMS"] = "Invalid token or tokenId provided";
|
|
67
67
|
})(AUTH_ERRORS || (exports.AUTH_ERRORS = AUTH_ERRORS = {}));
|
|
68
|
+
const resolveAppPath = () => { var _a, _b, _c; return (_c = (_a = process.env.FLOWERBASE_APP_PATH) !== null && _a !== void 0 ? _a : (_b = require.main) === null || _b === void 0 ? void 0 : _b.path) !== null && _c !== void 0 ? _c : process.cwd(); };
|
|
68
69
|
/**
|
|
69
70
|
* > Loads the auth config json file
|
|
70
71
|
* @testable
|
|
71
72
|
*/
|
|
72
73
|
const loadAuthConfig = () => {
|
|
73
|
-
const authPath = path_1.default.join(
|
|
74
|
+
const authPath = path_1.default.join(resolveAppPath(), 'auth/providers.json');
|
|
74
75
|
return JSON.parse(fs_1.default.readFileSync(authPath, 'utf-8'));
|
|
75
76
|
};
|
|
76
77
|
exports.loadAuthConfig = loadAuthConfig;
|
|
@@ -79,7 +80,7 @@ exports.loadAuthConfig = loadAuthConfig;
|
|
|
79
80
|
* @testable
|
|
80
81
|
*/
|
|
81
82
|
const loadCustomUserData = () => {
|
|
82
|
-
const userDataPath = path_1.default.join(
|
|
83
|
+
const userDataPath = path_1.default.join(resolveAppPath(), 'auth/custom_user_data.json');
|
|
83
84
|
return JSON.parse(fs_1.default.readFileSync(userDataPath, 'utf-8'));
|
|
84
85
|
};
|
|
85
86
|
exports.loadCustomUserData = loadCustomUserData;
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ALLOWED_METHODS } from './';
|
|
1
2
|
export declare const DEFAULT_CONFIG: {
|
|
2
3
|
PORT: number;
|
|
3
4
|
MONGODB_URL: string;
|
|
@@ -6,6 +7,10 @@ export declare const DEFAULT_CONFIG: {
|
|
|
6
7
|
HTTPS_SCHEMA: string;
|
|
7
8
|
HOST: string;
|
|
8
9
|
ENABLE_LOGGER: string | undefined;
|
|
10
|
+
CORS_OPTIONS: {
|
|
11
|
+
origin: string;
|
|
12
|
+
methods: ALLOWED_METHODS[];
|
|
13
|
+
};
|
|
9
14
|
};
|
|
10
15
|
export declare const API_VERSION: string;
|
|
11
16
|
export declare const HTTPS_SCHEMA: string;
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,IAAI,CAAA;AAUpC,eAAO,MAAM,cAAc;;;;;;;;;;iBAUsB,eAAe,EAAE;;CAEjE,CAAA;AACD,eAAO,MAAM,WAAW,QAA8C,CAAA;AACtE,eAAO,MAAM,YAAY,QAA8B,CAAA;AACvD,eAAO,MAAM,OAAO,QAAgB,CAAA;AAGpC,eAAO,MAAM,WAAW;;;;;;;;;;;;CAUvB,CAAA;AAID,eAAO,MAAM,SAAS;;;CAGrB,CAAA"}
|
package/dist/constants.js
CHANGED
|
@@ -23,7 +23,11 @@ exports.DEFAULT_CONFIG = {
|
|
|
23
23
|
API_VERSION: process.env.API_VERSION || 'v2.0',
|
|
24
24
|
HTTPS_SCHEMA: process.env.HTTPS_SCHEMA || 'https',
|
|
25
25
|
HOST: process.env.HOST || '0.0.0.0',
|
|
26
|
-
ENABLE_LOGGER: process.env.ENABLE_LOGGER
|
|
26
|
+
ENABLE_LOGGER: process.env.ENABLE_LOGGER,
|
|
27
|
+
CORS_OPTIONS: {
|
|
28
|
+
origin: "*",
|
|
29
|
+
methods: ["GET", "POST", "PUT", "DELETE"]
|
|
30
|
+
}
|
|
27
31
|
};
|
|
28
32
|
exports.API_VERSION = `/api/client/${exports.DEFAULT_CONFIG.API_VERSION}`;
|
|
29
33
|
exports.HTTPS_SCHEMA = exports.DEFAULT_CONFIG.HTTPS_SCHEMA;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../src/features/functions/controller.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../src/features/functions/controller.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AA6BhD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,EAAE,kBA6HjC,CAAA"}
|
|
@@ -15,6 +15,23 @@ const services_1 = require("../../services");
|
|
|
15
15
|
const state_1 = require("../../state");
|
|
16
16
|
const context_1 = require("../../utils/context");
|
|
17
17
|
const utils_1 = require("./utils");
|
|
18
|
+
const normalizeUser = (payload) => {
|
|
19
|
+
var _a, _b, _c, _d, _e, _f;
|
|
20
|
+
if (!payload)
|
|
21
|
+
return undefined;
|
|
22
|
+
const nestedUser = (_c = (_b = (_a = payload.data) !== null && _a !== void 0 ? _a : payload.user_data) !== null && _b !== void 0 ? _b : payload.custom_data) !== null && _c !== void 0 ? _c : payload;
|
|
23
|
+
const flattened = typeof nestedUser === 'object' && nestedUser !== null ? nestedUser : {};
|
|
24
|
+
return Object.assign(Object.assign(Object.assign({}, payload), flattened), { custom_data: (_d = payload.custom_data) !== null && _d !== void 0 ? _d : flattened, user_data: (_e = payload.user_data) !== null && _e !== void 0 ? _e : flattened, data: (_f = payload.data) !== null && _f !== void 0 ? _f : flattened });
|
|
25
|
+
};
|
|
26
|
+
const getRequestUser = (req) => {
|
|
27
|
+
const candidate = req.user;
|
|
28
|
+
return normalizeUser(candidate);
|
|
29
|
+
};
|
|
30
|
+
const logFunctionCall = (method, user, args) => {
|
|
31
|
+
if (process.env.DEBUG_FUNCTIONS !== 'true')
|
|
32
|
+
return;
|
|
33
|
+
console.log('[functions-debug]', method, user ? { id: user.id, role: user.role, email: user.email } : 'no-user', args);
|
|
34
|
+
};
|
|
18
35
|
/**
|
|
19
36
|
* > Creates a pre handler for every query
|
|
20
37
|
* @param app -> the fastify instance
|
|
@@ -25,7 +42,10 @@ const functionsController = (app_1, _a) => __awaiter(void 0, [app_1, _a], void 0
|
|
|
25
42
|
app.addHook('preHandler', app.jwtAuthentication);
|
|
26
43
|
const streams = {};
|
|
27
44
|
app.post('/call', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
-
const
|
|
45
|
+
const user = getRequestUser(req);
|
|
46
|
+
if (!user || user.typ !== 'access') {
|
|
47
|
+
throw new Error('Access token required');
|
|
48
|
+
}
|
|
29
49
|
const { name: method, arguments: args } = req.body;
|
|
30
50
|
if ('service' in req.body) {
|
|
31
51
|
const serviceFn = services_1.services[req.body.service];
|
|
@@ -37,6 +57,7 @@ const functionsController = (app_1, _a) => __awaiter(void 0, [app_1, _a], void 0
|
|
|
37
57
|
const currentMethod = serviceFn(app, { rules, user })
|
|
38
58
|
.db(database)
|
|
39
59
|
.collection(collection)[method];
|
|
60
|
+
logFunctionCall(`service:${req.body.service}:${method}`, user, args);
|
|
40
61
|
const operatorsByType = yield (0, utils_1.executeQuery)({
|
|
41
62
|
currentMethod,
|
|
42
63
|
query,
|
|
@@ -55,6 +76,7 @@ const functionsController = (app_1, _a) => __awaiter(void 0, [app_1, _a], void 0
|
|
|
55
76
|
if (currentFunction.private) {
|
|
56
77
|
throw new Error(`Function "${req.body.name}" is private`);
|
|
57
78
|
}
|
|
79
|
+
logFunctionCall(`function:${method}`, user, args);
|
|
58
80
|
const result = yield (0, context_1.GenerateContext)({
|
|
59
81
|
args: req.body.arguments,
|
|
60
82
|
app,
|
|
@@ -68,7 +90,11 @@ const functionsController = (app_1, _a) => __awaiter(void 0, [app_1, _a], void 0
|
|
|
68
90
|
return JSON.stringify(result);
|
|
69
91
|
}));
|
|
70
92
|
app.get('/call', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
-
const { query
|
|
93
|
+
const { query } = req;
|
|
94
|
+
const user = getRequestUser(req);
|
|
95
|
+
if (!user || user.typ !== 'access') {
|
|
96
|
+
throw new Error('Access token required');
|
|
97
|
+
}
|
|
72
98
|
const { baas_request, stitch_request } = query;
|
|
73
99
|
const config = JSON.parse(Buffer.from(baas_request || stitch_request || '', 'base64').toString('utf8'));
|
|
74
100
|
const [{ database, collection }] = config.arguments;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/rules/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAe,MAAM,aAAa,CAAA;AAEhD,eAAO,MAAM,SAAS,GAAU,gBAAuB,KAAG,OAAO,CAAC,KAAK,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/rules/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAe,MAAM,aAAa,CAAA;AAEhD,eAAO,MAAM,SAAS,GAAU,gBAAuB,KAAG,OAAO,CAAC,KAAK,CAuBtE,CAAA"}
|
|
@@ -18,10 +18,19 @@ const node_path_1 = __importDefault(require("node:path"));
|
|
|
18
18
|
const utils_1 = require("../../utils");
|
|
19
19
|
const loadRules = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (rootDir = process.cwd()) {
|
|
20
20
|
const rulesRoot = node_path_1.default.join(rootDir, 'data_sources', 'mongodb-atlas');
|
|
21
|
-
const
|
|
21
|
+
const recursivelyCollectFiles = (dir) => {
|
|
22
|
+
return fs_1.default.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
23
|
+
const fullPath = node_path_1.default.join(dir, entry.name);
|
|
24
|
+
if (entry.isDirectory()) {
|
|
25
|
+
return recursivelyCollectFiles(fullPath);
|
|
26
|
+
}
|
|
27
|
+
return entry.isFile() ? [fullPath] : [];
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
const files = recursivelyCollectFiles(rulesRoot);
|
|
22
31
|
const rulesFiles = files.filter((x) => x.endsWith('rules.json'));
|
|
23
32
|
const rulesByCollection = rulesFiles.reduce((acc, rulesFile) => {
|
|
24
|
-
const filePath =
|
|
33
|
+
const filePath = rulesFile;
|
|
25
34
|
const collectionRules = (0, utils_1.readJsonContent)(filePath);
|
|
26
35
|
acc[collectionRules.collection] = collectionRules;
|
|
27
36
|
return acc;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;AAqC9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAU,gBAAuB,KAAG,OAAO,CAAC,QAAQ,CAkB5E,CAAA;AAkLD,eAAO,MAAM,gBAAgB;0EA9J1B,aAAa;yEAyGb,aAAa;+EApFb,aAAa;CA6If,CAAA"}
|
|
@@ -31,6 +31,40 @@ const constants_1 = require("../../constants");
|
|
|
31
31
|
const state_1 = require("../../state");
|
|
32
32
|
const utils_1 = require("../../utils");
|
|
33
33
|
const context_1 = require("../../utils/context");
|
|
34
|
+
const registerOnClose = (app, handler, label) => {
|
|
35
|
+
if (app.server) {
|
|
36
|
+
app.server.once('close', () => {
|
|
37
|
+
Promise.resolve(handler()).catch((error) => {
|
|
38
|
+
console.error(`${label} close error`, error);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
app.addHook('onClose', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
+
try {
|
|
46
|
+
yield handler();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.error(`${label} close error`, error);
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error(`${label} hook registration error`, error);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const shouldIgnoreStreamError = (error) => {
|
|
58
|
+
var _a, _b;
|
|
59
|
+
const err = error;
|
|
60
|
+
if ((err === null || err === void 0 ? void 0 : err.name) === 'MongoClientClosedError')
|
|
61
|
+
return true;
|
|
62
|
+
if ((_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.includes('client was closed'))
|
|
63
|
+
return true;
|
|
64
|
+
if ((_b = err === null || err === void 0 ? void 0 : err.message) === null || _b === void 0 ? void 0 : _b.includes('Client is closed'))
|
|
65
|
+
return true;
|
|
66
|
+
return false;
|
|
67
|
+
};
|
|
34
68
|
/**
|
|
35
69
|
* Loads trigger files from the specified directory and returns them as an array of objects.
|
|
36
70
|
* Each object contains the file name and the parsed JSON content.
|
|
@@ -70,7 +104,7 @@ exports.loadTriggers = loadTriggers;
|
|
|
70
104
|
* @param {Object} params.app - The app instance for context.
|
|
71
105
|
*/
|
|
72
106
|
const handleCronTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ config, triggerHandler, functionsList, services, app }) {
|
|
73
|
-
node_cron_1.default.schedule(config.schedule, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
107
|
+
const task = node_cron_1.default.schedule(config.schedule, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
108
|
yield (0, context_1.GenerateContext)({
|
|
75
109
|
args: [],
|
|
76
110
|
app,
|
|
@@ -81,6 +115,7 @@ const handleCronTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ c
|
|
|
81
115
|
services
|
|
82
116
|
});
|
|
83
117
|
}));
|
|
118
|
+
registerOnClose(app, () => task.stop(), 'Scheduled trigger');
|
|
84
119
|
});
|
|
85
120
|
const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ config, triggerHandler, functionsList, services, app }) {
|
|
86
121
|
const { database } = config;
|
|
@@ -97,6 +132,11 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
|
|
|
97
132
|
.watch(pipeline, {
|
|
98
133
|
fullDocument: 'whenAvailable'
|
|
99
134
|
});
|
|
135
|
+
changeStream.on('error', (error) => {
|
|
136
|
+
if (shouldIgnoreStreamError(error))
|
|
137
|
+
return;
|
|
138
|
+
console.error('Authentication trigger change stream error', error);
|
|
139
|
+
});
|
|
100
140
|
changeStream.on('change', function (change) {
|
|
101
141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
102
142
|
const document = change['fullDocument']; //TODO -> define user type
|
|
@@ -122,6 +162,9 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
|
|
|
122
162
|
}
|
|
123
163
|
});
|
|
124
164
|
});
|
|
165
|
+
registerOnClose(app, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
166
|
+
yield changeStream.close();
|
|
167
|
+
}), 'Authentication trigger');
|
|
125
168
|
});
|
|
126
169
|
/**
|
|
127
170
|
* Handles a database trigger by watching changes in a specified collection and triggering the appropriate handler.
|
|
@@ -160,6 +203,11 @@ const handleDataBaseTrigger = (_a) => __awaiter(void 0, [_a], void 0, function*
|
|
|
160
203
|
? 'whenAvailable'
|
|
161
204
|
: undefined
|
|
162
205
|
});
|
|
206
|
+
changeStream.on('error', (error) => {
|
|
207
|
+
if (shouldIgnoreStreamError(error))
|
|
208
|
+
return;
|
|
209
|
+
console.error('Database trigger change stream error', error);
|
|
210
|
+
});
|
|
163
211
|
changeStream.on('change', function (_a) {
|
|
164
212
|
return __awaiter(this, void 0, void 0, function* () {
|
|
165
213
|
var { clusterTime } = _a, change = __rest(_a, ["clusterTime"]);
|
|
@@ -174,7 +222,9 @@ const handleDataBaseTrigger = (_a) => __awaiter(void 0, [_a], void 0, function*
|
|
|
174
222
|
});
|
|
175
223
|
});
|
|
176
224
|
});
|
|
177
|
-
|
|
225
|
+
registerOnClose(app, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
226
|
+
yield changeStream.close();
|
|
227
|
+
}), 'Database trigger');
|
|
178
228
|
});
|
|
179
229
|
exports.TRIGGER_HANDLERS = {
|
|
180
230
|
SCHEDULED: handleCronTrigger,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import 'dotenv/config';
|
|
2
2
|
export * from './model';
|
|
3
|
+
export type ALLOWED_METHODS = "GET" | "POST" | "PUT" | "DELETE";
|
|
4
|
+
export type CorsConfig = {
|
|
5
|
+
origin: string;
|
|
6
|
+
methods: ALLOWED_METHODS[];
|
|
7
|
+
};
|
|
3
8
|
export type InitializeConfig = {
|
|
4
9
|
projectId: string;
|
|
5
10
|
mongodbUrl?: string;
|
|
6
11
|
jwtSecret?: string;
|
|
7
12
|
port?: number;
|
|
8
13
|
host?: string;
|
|
14
|
+
corsConfig?: CorsConfig;
|
|
15
|
+
basePath?: string;
|
|
9
16
|
};
|
|
10
17
|
/**
|
|
11
18
|
* > Used to initialize fastify app
|
|
@@ -15,5 +22,5 @@ export type InitializeConfig = {
|
|
|
15
22
|
* @param port -> the serve port number
|
|
16
23
|
* @param mongodbUrl -> the database connection string
|
|
17
24
|
*/
|
|
18
|
-
export declare function initialize({ projectId, host, jwtSecret, port, mongodbUrl }: InitializeConfig): Promise<void>;
|
|
25
|
+
export declare function initialize({ projectId, host, jwtSecret, port, mongodbUrl, corsConfig, basePath }: InitializeConfig): Promise<void>;
|
|
19
26
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AActB,cAAc,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AActB,cAAc,SAAS,CAAA;AAGvB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAA;AAE/D,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,eAAe,EAAE,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,EAC/B,SAAS,EACT,IAA0B,EAC1B,SAAqC,EACrC,IAA0B,EAC1B,UAAuC,EACvC,UAAwC,EACxC,QAAQ,EACT,EAAE,gBAAgB,iBAyElB"}
|
package/dist/index.js
CHANGED
|
@@ -73,22 +73,22 @@ __exportStar(require("./model"), exports);
|
|
|
73
73
|
* @param mongodbUrl -> the database connection string
|
|
74
74
|
*/
|
|
75
75
|
function initialize(_a) {
|
|
76
|
-
return __awaiter(this, arguments, void 0, function* ({ projectId, host = constants_1.DEFAULT_CONFIG.HOST, jwtSecret = constants_1.DEFAULT_CONFIG.JWT_SECRET, port = constants_1.DEFAULT_CONFIG.PORT, mongodbUrl = constants_1.DEFAULT_CONFIG.MONGODB_URL }) {
|
|
77
|
-
var _b;
|
|
76
|
+
return __awaiter(this, arguments, void 0, function* ({ projectId, host = constants_1.DEFAULT_CONFIG.HOST, jwtSecret = constants_1.DEFAULT_CONFIG.JWT_SECRET, port = constants_1.DEFAULT_CONFIG.PORT, mongodbUrl = constants_1.DEFAULT_CONFIG.MONGODB_URL, corsConfig = constants_1.DEFAULT_CONFIG.CORS_OPTIONS, basePath }) {
|
|
77
|
+
var _b, _c;
|
|
78
|
+
const resolvedBasePath = (_c = basePath !== null && basePath !== void 0 ? basePath : (_b = require.main) === null || _b === void 0 ? void 0 : _b.path) !== null && _c !== void 0 ? _c : process.cwd();
|
|
78
79
|
const fastify = (0, fastify_1.default)({
|
|
79
80
|
logger: !!constants_1.DEFAULT_CONFIG.ENABLE_LOGGER
|
|
80
81
|
});
|
|
81
|
-
|
|
82
|
-
console.log("BASE PATH", basePath);
|
|
82
|
+
console.log("BASE PATH", resolvedBasePath);
|
|
83
83
|
console.log("CURRENT PORT", port);
|
|
84
84
|
console.log("CURRENT HOST", host);
|
|
85
|
-
const functionsList = yield (0, utils_2.loadFunctions)(
|
|
85
|
+
const functionsList = yield (0, utils_2.loadFunctions)(resolvedBasePath);
|
|
86
86
|
console.log("Functions LOADED");
|
|
87
|
-
const triggersList = yield (0, utils_4.loadTriggers)(
|
|
87
|
+
const triggersList = yield (0, utils_4.loadTriggers)(resolvedBasePath);
|
|
88
88
|
console.log("Triggers LOADED");
|
|
89
|
-
const endpointsList = yield (0, utils_1.loadEndpoints)(
|
|
89
|
+
const endpointsList = yield (0, utils_1.loadEndpoints)(resolvedBasePath);
|
|
90
90
|
console.log("Endpoints LOADED");
|
|
91
|
-
const rulesList = yield (0, utils_3.loadRules)(
|
|
91
|
+
const rulesList = yield (0, utils_3.loadRules)(resolvedBasePath);
|
|
92
92
|
console.log("Rules LOADED");
|
|
93
93
|
const stateConfig = {
|
|
94
94
|
functions: functionsList,
|
|
@@ -119,7 +119,8 @@ function initialize(_a) {
|
|
|
119
119
|
register: fastify.register,
|
|
120
120
|
mongodbUrl,
|
|
121
121
|
jwtSecret,
|
|
122
|
-
functionsList
|
|
122
|
+
functionsList,
|
|
123
|
+
corsConfig
|
|
123
124
|
});
|
|
124
125
|
console.log('Plugins registration COMPLETED');
|
|
125
126
|
yield (0, exposeRoutes_1.exposeRoutes)(fastify);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/mongodb-atlas/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/mongodb-atlas/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAyC,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAwsBrF,QAAA,MAAM,YAAY,EAAE,oBAsBlB,CAAA;AAEF,eAAe,YAAY,CAAA"}
|