@vulkano/core 0.1.0 → 0.5.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/.nvmrc CHANGED
@@ -1 +1 @@
1
- v18
1
+ v20
@@ -24,8 +24,10 @@ const AllControllers = require('include-all')({
24
24
  filter: /(.+Controller)\.js$/,
25
25
  optional: true
26
26
  });
27
+
27
28
  const responses = require('./responses');
28
- const JWT = require(`${CORE_PATH}/libs/Jwt`);
29
+
30
+ const JWT = require('../libs/Jwt');
29
31
 
30
32
  module.exports = {
31
33
 
@@ -23,7 +23,6 @@ const appLibs = require('include-all')({
23
23
  optional: true
24
24
  });
25
25
 
26
-
27
26
  const allServices = {
28
27
  ...coreLibs,
29
28
  ...appServices,
@@ -79,7 +79,6 @@ module.exports = function loadModelsApplication() {
79
79
  ...Callbacks,
80
80
  ...scaffold,
81
81
  ...custom,
82
- ...ActiveRecord,
83
82
  ...Current
84
83
  };
85
84
 
package/init.js CHANGED
@@ -17,27 +17,27 @@ global._ = _;
17
17
  global.Promise = Promise;
18
18
 
19
19
  if (!global.ABS_PATH) {
20
- global.ABS_PATH = path.resolve(__dirname, '');
20
+ global.ABS_PATH = path.resolve(__dirname, './');
21
21
  }
22
22
 
23
23
  if (!global.APP_PATH) {
24
- global.APP_PATH = path.join(__dirname, '../app');
24
+ global.APP_PATH = path.join(__dirname, './');
25
25
  }
26
26
 
27
27
  if (!global.PUBLIC_PATH) {
28
- global.PUBLIC_PATH = path.join(__dirname, '../public');
28
+ global.PUBLIC_PATH = path.join(__dirname, './');
29
29
  }
30
30
 
31
- global.CORE_PATH = path.join(__dirname, '');
31
+ global.CORE_PATH = path.join(__dirname, './');
32
32
 
33
33
  if (!fs.existsSync(APP_PATH)) {
34
- console.log('the global var APP_PATH or directory not found');
35
- global.APP_PATH = path.resolve(__dirname, '');
34
+ console.log('the global var APP_PATH or directory not found', APP_PATH);
35
+ global.APP_PATH = path.resolve(__dirname, './');
36
36
  }
37
37
 
38
38
  if (!fs.existsSync(PUBLIC_PATH)) {
39
- console.log('the global var PUBLIC_PATH or directory not found');
40
- global.PUBLIC_PATH = path.resolve(__dirname, '');
39
+ console.log('the global var PUBLIC_PATH or directory not found', PUBLIC_PATH);
40
+ global.PUBLIC_PATH = path.resolve(__dirname, './');
41
41
  }
42
42
 
43
43
  // Read Dontenv config
@@ -201,10 +201,11 @@ function startVulkano() {
201
201
  const nodeVersion = process.version.match(/^v(\d+\.\d+\.\d+)/)[1];
202
202
  const portText = String(app.server.get('port') || 8000).padEnd(nodeVersion.length, ' ');
203
203
  const socketText = (sockets.enabled ? 'YES' : 'NO').padEnd(nodeVersion.length - 3, ' ');
204
+ const redisText = (redis && redis.enabled ? 'YES' : 'NO').padEnd(5, ' ');
204
205
 
205
206
  serverConfig.push(` PORT: ${colors.fg.green}${portText}${colors.reset}`);
206
207
  serverConfig.push(' | ');
207
- serverConfig.push(` ENV: ${app.PRODUCTION ? colors.fg.red : colors.fg.green}${env}${colors.reset}`);
208
+ serverConfig.push(` ENV: ${app.PRODUCTION ? colors.fg.red : colors.fg.green}${NODE_ENV}${colors.reset}`);
208
209
 
209
210
  console.log(serverConfig.join(''));
210
211
 
@@ -218,20 +219,16 @@ function startVulkano() {
218
219
  console.log(nodeConfig.join(''));
219
220
 
220
221
  const startUpConfig = [];
221
- if (sockets.redis && redis && redis.enabled) {
222
- startUpConfig.push(' SOCKETS: ', `${colors.fg.green}${socketText}${colors.reset}`);
223
- } else {
224
- startUpConfig.push(' SOCKETS: ', `${colors.fg.green}${socketText}${colors.reset}`);
225
- }
222
+ startUpConfig.push(' SOCKETS: ', `${colors.fg.green}${socketText}${colors.reset}`);
226
223
  startUpConfig.push(' | ');
227
224
  startUpConfig.push(` STARTUP: ${colors.fg.green}${moment(moment().diff(global.START_TIME)).format('ss.SSS')} sec${colors.reset}`);
228
225
  console.log(startUpConfig.join(''));
229
226
 
230
227
  const dbConfig = [];
231
228
  if (redis && redis.enabled) {
232
- dbConfig.push(' REDIS: ', `${colors.fg.green}YES ${colors.reset}`);
229
+ dbConfig.push(' REDIS: ', `${colors.fg.green}${redisText}${colors.reset}`);
233
230
  } else {
234
- dbConfig.push(' REDIS: ', `${colors.fg.green}NO ${colors.reset}`);
231
+ dbConfig.push(' REDIS: ', `${colors.fg.green}${redisText}${colors.reset}`);
235
232
  }
236
233
 
237
234
  dbConfig.push(' | ');
@@ -0,0 +1,160 @@
1
+ const http = require('http');
2
+ const https = require('https');
3
+ const axios = require('axios');
4
+
5
+ module.exports = {
6
+
7
+ /**
8
+ * Method to make a GET request
9
+ *
10
+ * @param {String} url
11
+ * @param {Object} props { headers }
12
+ * @returns {Promise}
13
+ */
14
+ get(url, props) {
15
+
16
+ return this.send({
17
+ url,
18
+ ...props,
19
+ method: 'GET'
20
+ });
21
+
22
+ },
23
+
24
+ /**
25
+ * Method to make a POST request
26
+ *
27
+ * @param {String} url
28
+ * @param {Object} body
29
+ * @param {Object} props { headers }
30
+ * @returns {Promise}
31
+ */
32
+ post(url, body, props) {
33
+
34
+ return this.send({
35
+ url,
36
+ body,
37
+ ...props,
38
+ method: 'POST'
39
+ });
40
+
41
+ },
42
+
43
+ /**
44
+ * Method to make a PUT request
45
+ *
46
+ * @param {String} url
47
+ * @param {Object} body
48
+ * @param {Object} props { headers }
49
+ * @returns {Promise}
50
+ */
51
+ put(url, body, props) {
52
+
53
+ return this.send({
54
+ url,
55
+ body,
56
+ ...props,
57
+ method: 'PUT'
58
+ });
59
+
60
+ },
61
+
62
+ /**
63
+ * Method to make a DELETE request
64
+ *
65
+ * @param {String} url
66
+ * @param {Object} body
67
+ * @param {Object} props { headers }
68
+ * @returns {Promise}
69
+ */
70
+ delete(url, props) {
71
+
72
+ return this.send({
73
+ url,
74
+ ...props,
75
+ method: 'DELETE'
76
+ });
77
+
78
+ },
79
+
80
+ /**
81
+ * Method to send a request
82
+ *
83
+ * @param {Object} props
84
+ * @returns {Promise}
85
+ */
86
+ send(props) {
87
+
88
+ const {
89
+ url,
90
+ body,
91
+ method,
92
+ responseType,
93
+ headers
94
+ } = typeof props === 'string'
95
+ ? { path: props, method: 'get' }
96
+ : (props || {});
97
+
98
+ const optHeaders = {
99
+ 'Content-Type': 'application/json',
100
+ Accept: 'application/json'
101
+ };
102
+
103
+ const options = {
104
+ url,
105
+ method: (method || 'GET').toLowerCase(),
106
+ headers: Object.assign(optHeaders, headers || {}),
107
+ httpAgent: new http.Agent({ keepAlive: true, rejectUnauthorized: false }),
108
+ httpsAgent: new https.Agent({ keepAlive: true, rejectUnauthorized: false }),
109
+ responseType: responseType || 'json'
110
+ };
111
+
112
+ if (body) {
113
+ // convert body into just one line of json.
114
+ options.data = JSON.parse(JSON.stringify(body || {}));
115
+ }
116
+
117
+ return axios(options)
118
+ .then( (response) => {
119
+
120
+ const {
121
+ data
122
+ } = response;
123
+
124
+ return data || {};
125
+
126
+ })
127
+ .catch( (err) => {
128
+
129
+ const {
130
+ response
131
+ } = err || {};
132
+
133
+ const {
134
+ data: errorData,
135
+ status: statusCode
136
+ } = response || {};
137
+
138
+ const target = options.baseURL ? `${options.method} ${options.baseURL}/${options.url}` : `${options.method} ${options.url}`;
139
+
140
+ console.log('');
141
+ console.log('');
142
+ console.log('---------------------');
143
+ console.log('ApiClient', target);
144
+ console.log('---------------------');
145
+
146
+ const {
147
+ msg,
148
+ message: errorMessage,
149
+ error: errorMessage2
150
+ } = errorData || {};
151
+
152
+ const message = msg || errorMessage || errorMessage2 || 'Unable to connect to the Request Service';
153
+
154
+ return VSError.reject(message, statusCode || 500);
155
+
156
+ });
157
+
158
+ }
159
+
160
+ };
@@ -0,0 +1,276 @@
1
+ const _ = require('underscore');
2
+
3
+ module.exports = {
4
+
5
+ serializeQuery(_props, _query) {
6
+
7
+ const props = typeof _props === 'object'
8
+ ? _props
9
+ : { sort: null, search: [] };
10
+
11
+ const query = _query || {};
12
+
13
+ const page = query.page || 1;
14
+ const perPage = Number(query.per_page) || Number(query.perPage) || 30;
15
+ const fields = query.fields || props.fields || [];
16
+ const sort = query.sort || props.sort || null;
17
+ const search = query.search || props.search || null;
18
+ const searchType = (query.searchType || '').toLowerCase().replace('-', '');
19
+
20
+ const result = _.omit({
21
+ page,
22
+ perPage,
23
+ fields,
24
+ sort,
25
+ search
26
+ }, (value) => !value );
27
+
28
+ // Filter by search
29
+ const searchBy = props.searchBy || [];
30
+
31
+ const itemsToSearch = [];
32
+
33
+ if (search && Array.isArray(searchBy) && searchBy.length > 0 ) {
34
+
35
+ searchBy.forEach( (item) => {
36
+
37
+ let type = String;
38
+
39
+ if (typeof item === 'object') {
40
+ type = item?.type || String;
41
+ }
42
+
43
+ const row = {};
44
+
45
+ if (type === String) {
46
+
47
+ if (searchType === 'startwith' || searchType === 'start') {
48
+ row[item] = new RegExp(['^', Utils.accentToRegex(search), '*.'].join(''), 'i');
49
+ } else if (searchType === 'endwith' || searchType === 'end') {
50
+ row[item] = new RegExp(['.*', Utils.accentToRegex(search)].join(''), 'i');
51
+ } else {
52
+ row[item] = new RegExp(['.*', Utils.accentToRegex(search), '*.'].join(''), 'i');
53
+ }
54
+
55
+ } else if (type === Number) {
56
+
57
+ row[item] = search;
58
+
59
+ }
60
+
61
+ itemsToSearch.push(row);
62
+
63
+ });
64
+
65
+ }
66
+
67
+ if (props.filterByOr) {
68
+ if (Array.isArray(props.filterByOr)) {
69
+ props.filterByOr.forEach( (i) => {
70
+ itemsToSearch.push(i);
71
+ });
72
+ } else {
73
+ itemsToSearch.push(props.filterByOr);
74
+ }
75
+ }
76
+
77
+ let newSearch = {};
78
+
79
+ const currentFilters = {
80
+ ...(props.filter || {})
81
+ };
82
+
83
+ const hasFilters = Object.keys( props.filter || {} ).length > 0 ? true : false;
84
+ const hasItems = itemsToSearch.length > 0 ? true : false;
85
+
86
+ if (hasItems && hasFilters) {
87
+
88
+ newSearch.$and = [];
89
+
90
+ // AND
91
+ newSearch.$and.push(currentFilters);
92
+
93
+ // OR
94
+ newSearch.$and.push({ $or: itemsToSearch });
95
+
96
+ } else if (hasItems && !hasFilters) {
97
+
98
+ newSearch.$or = itemsToSearch;
99
+
100
+ } else if (!hasItems && hasFilters) {
101
+
102
+ newSearch = { ...currentFilters };
103
+
104
+ }
105
+
106
+ return {
107
+ ...result,
108
+ search: newSearch
109
+ };
110
+
111
+ },
112
+
113
+ getPopulatedCollections(populate) {
114
+
115
+ const optPopulate = [];
116
+
117
+ if (populate.length > 0) {
118
+
119
+ populate.forEach( (item) => {
120
+
121
+ const {
122
+ virtual,
123
+ collection,
124
+ path,
125
+ model
126
+ } = item;
127
+
128
+ let populateProps = null;
129
+
130
+ if (virtual) {
131
+ populateProps = virtual;
132
+ } else {
133
+
134
+ populateProps = {
135
+ path: collection || path || model
136
+ };
137
+
138
+ if (item.fields) {
139
+ populateProps.select = item.fields;
140
+ } else if (item.select) {
141
+ populateProps.select = item.select;
142
+ }
143
+
144
+ if (item.match) {
145
+ populateProps.match = item.match;
146
+ }
147
+
148
+ if (item.populate) {
149
+ populateProps.populate = item.populate;
150
+ }
151
+
152
+ }
153
+
154
+ optPopulate.push(populateProps);
155
+
156
+ });
157
+
158
+ }
159
+
160
+ return optPopulate;
161
+
162
+ },
163
+
164
+ // Convert records to paginate
165
+ get(Model, query, hasPopulate) {
166
+
167
+ const populate = hasPopulate || [];
168
+
169
+ let criteria = query || {};
170
+
171
+ // Setup
172
+ const page = criteria.page === 'all' ? 'all' : ( parseInt(criteria.page, 10) || 1);
173
+ const perPage = +criteria.per_page || +criteria.perPage || 50;
174
+
175
+ delete criteria.page;
176
+ delete criteria.per_page;
177
+ delete criteria.perPage; // Fallback
178
+
179
+ const fields = [];
180
+ if (Array.isArray(criteria.fields)) {
181
+ criteria.fields.forEach( (f) => {
182
+ fields.push(f);
183
+ });
184
+ } else if (typeof criteria.fields === 'string') {
185
+ const tmpFields = (criteria.fields) ? criteria.fields.split(',') : [];
186
+ tmpFields.forEach( (f) => {
187
+ fields.push(f);
188
+ });
189
+ }
190
+ delete criteria.fields;
191
+
192
+ const tmpSort = (criteria.sort || '').split(',');
193
+ const sort = { sort: {} };
194
+ tmpSort.forEach( (_part) => {
195
+ const part = (_part || '').split('|');
196
+ if (part.length > 1) {
197
+ const desc = part[1].trim().toLowerCase() === 'descending' ? 'desc' : '';
198
+ const asc = part[1].trim().toLowerCase() === 'ascending' ? 'asc' : '';
199
+ sort.sort[part[0].trim()] = asc || desc || part[1].trim().toLowerCase();
200
+ }
201
+ });
202
+
203
+ delete criteria.sort;
204
+
205
+ if (criteria.search) {
206
+ criteria = _.extend(criteria, criteria.search);
207
+ delete criteria.search;
208
+ }
209
+
210
+ const optPopulate = this.getPopulatedCollections(populate || []);
211
+ const queryModel = _.extend(criteria, {});
212
+
213
+ if (page === 'all') {
214
+
215
+ const optsModel = _.extend(sort, { populate: optPopulate });
216
+ return Model.find(queryModel, fields.join(' '), optsModel);
217
+
218
+ }
219
+
220
+ return Model
221
+ .countDocuments(criteria)
222
+ .then( (total) => {
223
+
224
+ const opts = {
225
+ page,
226
+ limit: perPage
227
+ };
228
+
229
+ if (fields.length > 0) {
230
+ opts.select = fields.join(' ');
231
+ }
232
+
233
+ opts.populate = optPopulate;
234
+
235
+ const optsModel = _.extend(opts, sort);
236
+
237
+ return Model
238
+ .paginate(queryModel, optsModel)
239
+ .then( (data) => this._set(total, data.docs, page, perPage) );
240
+
241
+ });
242
+
243
+ },
244
+
245
+ _set(total, items, _page, _perPage) {
246
+
247
+ const page = _page || 1;
248
+ const perPage = _perPage || 30;
249
+
250
+ let cursor = (page > 1) ? ((page * perPage) - (perPage - 1)) : 1;
251
+
252
+ const tmpNext = (((perPage * (page - 1)) + perPage) <= total);
253
+ const next = tmpNext ? (page + 1) : false;
254
+ let prev = (page > 1) ? (page - 1) : false;
255
+ const totalPages = Math.ceil(total / perPage);
256
+
257
+ if ( (totalPages < page) && (total > 0) ) {
258
+ prev = false;
259
+ }
260
+
261
+ cursor = (total >= cursor) ? cursor : 1;
262
+
263
+ return {
264
+ items,
265
+ cursor,
266
+ page,
267
+ perPage,
268
+ next,
269
+ prev,
270
+ totalPages,
271
+ totalItems: total
272
+ };
273
+
274
+ }
275
+
276
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vulkano/core",
3
- "version": "0.1.0",
3
+ "version": "0.5.0",
4
4
  "description": "A MVC framework using Express 4",
5
5
  "license": "MIT",
6
6
  "author": "Vulkano Team",
@@ -15,7 +15,7 @@
15
15
  "homepage": "https://github.com/vulkanojs/vulkano-core",
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "git+https://github.com/vulkanojs/vulkano-core"
18
+ "url": "git+https://github.com/vulkanojs/vulkano-core.git"
19
19
  },
20
20
  "keywords": [
21
21
  "vulkano",
@@ -29,6 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@socket.io/redis-adapter": "^8.3.0",
32
+ "axios": "^1.6.8",
32
33
  "bluebird": "^3.7.2",
33
34
  "body-parser": "^1.20.2",
34
35
  "compression": "^1.7.4",
@@ -49,7 +50,7 @@
49
50
  "jwt-simple": "^0.5.6",
50
51
  "moment": "^2.30.1",
51
52
  "moment-timezone": "^0.5.45",
52
- "mongoose": "^8.2.4",
53
+ "mongoose": "^7.4.0",
53
54
  "mongoose-paginate": "^5.0.3",
54
55
  "morgan": "^1.10.0",
55
56
  "multer": "^1.4.5-lts.1",
@@ -63,5 +64,20 @@
63
64
  "underscore": "^1.13.6",
64
65
  "webp-middleware": "^0.4.0",
65
66
  "yargs": "^17.7.2"
67
+ },
68
+ "devDependencies": {
69
+ "@babel/core": "^7.24.3",
70
+ "@babel/eslint-parser": "^7.24.1",
71
+ "@babel/plugin-syntax-dynamic-import": "^7.8.3",
72
+ "@babel/plugin-syntax-jsx": "^7.24.1",
73
+ "@babel/preset-env": "^7.24.3",
74
+ "@babel/preset-react": "^7.24.1",
75
+ "@babel/preset-stage-1": "^7.8.3",
76
+ "eslint": "^8.57.0",
77
+ "eslint-config-airbnb": "^19.0.4",
78
+ "eslint-plugin-import": "^2.29.1",
79
+ "eslint-plugin-jsx-a11y": "^6.8.0",
80
+ "eslint-plugin-react": "^7.34.1",
81
+ "eslint-plugin-react-hooks": "^4.6.0"
66
82
  }
67
83
  }