-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
54 lines (49 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import path from 'path';
import express from 'express';
import kebab from 'lodash/kebabCase';
import { generateRequest } from 'api/requestGenerators';
import { mongoConnector } from '../database/mongo';
import { customApisMapper, REQUEST_TYPES } from 'api/customApisMapper';
import customRoutes from 'server/api/routes';
import { getModelFiles, isTestEnv } from 'utils';
import { registerSwagger } from 'utils/swagUtils';
/* istanbul ignore next */
if (!isTestEnv()) {
mongoConnector();
}
export default app => {
autoGenerateApisFromModels(app);
// Custom api
app.use('/', customRoutes);
registerSwagger(app);
};
const autoGenerateApisFromModels = app => {
let modelsFolderPath = path.join(
__dirname,
'../../server/database/models/'
);
const fileArray = getModelFiles(modelsFolderPath);
fileArray.forEach(f => {
// eslint-disable-next-line prefer-template
const { model } = require(`server/database/models/` + f);
const name = f.split('.')[0];
apiGeneratorFactory(app, name, model);
});
};
const apiGeneratorFactory = (app, name, model) => {
const router = express.Router();
Object.values(REQUEST_TYPES).forEach(type => {
if (!customApisMapper[name]?.methods.map(m => m.type).includes(type)) {
// auto generate api
generateRequest(type, router, model);
} else {
const customApi = customApisMapper[name].methods.find(
m => m.type === type
);
if (customApi.handler) {
customApi.handler(router, model, customApi.validator);
}
}
});
app.use(`/${kebab(name)}`, router);
};