-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
160 lines (134 loc) · 4.98 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const router = require('express').Router();
const consts = require('./consts.js');
const Endpoint = require('./objects/Endpoint.js');
var logger;
const defaultMiddleware = (res, req, next) => next();
const clearConsoleAndScrollbackBuffer = () => {
process.stdout.write('\u001b[3J\u001b[2J\u001b[1J');
console.clear();
};
const createLogger = () => {
const winston = require('winston');
const logFormat = winston.format.printf(function (info) {
if (info.message === '\n') {
return info.message;
}
return `[totoro] [${info.level}] ${info.message}`;
});
return winston.createLogger({
level: consts.LOG_LEVEL,
format: winston.format.combine(winston.format.colorize(), logFormat),
transports: [
new winston.transports.Console(),
// new winston.transports.File({ filename: 'logfile.log' }),
],
});
};
const rain = (apiConfig, loggerInstance = undefined, clearConsole = false) => {
// maybe use TS next time?
try {
logger =
loggerInstance && typeof loggerInstance['log'] === 'function' ? loggerInstance : createLogger();
} catch (err) {
// idk when this will be called. maybe at nuclear apocalypse when winston stops working
logger = console;
logger.log(err);
}
clearConsole && clearConsoleAndScrollbackBuffer();
const versions = {};
let previousApiVersion = null;
for (let apiVersion in apiConfig) {
if (apiConfig.hasOwnProperty(apiVersion)) {
let apiVersionConfig = apiConfig[apiVersion];
let apiVersionActive = apiVersionConfig.active;
// use default value if not found
if (apiVersionActive == null) apiVersionActive = true;
let apiVersionDeprecated = apiVersionConfig.deprecated;
// use default value if not found
if (apiVersionDeprecated == null) apiVersionDeprecated = false;
delete apiVersionConfig.active;
delete apiVersionConfig.deprecated;
versions[apiVersion] = [];
// copy over endpoints from previous version if needed
inheritEndpoints(versions, previousApiVersion, apiVersion);
// set previous api version number
previousApiVersion = apiVersion;
for (let i = 0; i < apiVersionConfig.endpoints.length; i++) {
let endpointActive = apiVersionConfig.endpoints[i].active;
// use default value if not found
if (endpointActive == null) endpointActive = true;
let endpointDeprecated = apiVersionConfig.endpoints[i].deprecated;
// use default value if not found
if (endpointDeprecated == null) endpointDeprecated = false;
apiVersionConfig.endpoints[i].active = endpointActive && apiVersionActive;
apiVersionConfig.endpoints[i].deprecated = endpointDeprecated || apiVersionDeprecated;
let endpoint = new Endpoint(apiVersion, apiVersionConfig.endpoints[i]);
endpoint.config.middleware = [...[defaultMiddleware], ...(endpoint.config.middleware || [])];
// add new endpoint to the list or replace if it exists already
pushOrReplaceRoute(versions[apiVersion], endpoint);
}
}
}
return populateRouter(versions);
};
function inheritEndpoints(versions, previousApiVersion, apiVersion) {
if (previousApiVersion == null) {
return;
}
for (let i = 0; i < versions[previousApiVersion].length; i++) {
if (!versions[previousApiVersion][i].config.deprecated) {
let endpointCopy = { ...versions[previousApiVersion][i] };
endpointCopy.apiVersion = apiVersion;
endpointCopy.config.active = true;
versions[apiVersion].push(endpointCopy);
}
}
}
function pushOrReplaceRoute(endpoints, endpoint) {
let replaced = false;
for (let i = 0; i < endpoints.length; i++) {
if (
endpoints[i].config.route == endpoint.config.route &&
endpoints[i].config.method == endpoint.config.method
) {
endpoints[i] = endpoint;
replaced = true;
}
}
if (!replaced) {
endpoints.push(endpoint);
}
}
function populateRouter(versions) {
for (let apiVersion in versions) {
if (versions.hasOwnProperty(apiVersion)) {
logger.log('debug', `API version ${apiVersion}`);
for (let i = 0; i < versions[apiVersion].length; i++) {
if (versions[apiVersion][i].config.active) {
constructRoute(versions[apiVersion][i]);
}
}
logger.log('debug', '\n');
// logger.log('debug', `End of API version ${apiVersion}\n`);
}
}
return router;
}
function constructRoute(endpoint) {
const endpointURL = `/${endpoint.apiVersion}${endpoint.config.route}`;
if (!consts.HTTP_METHODS.includes(endpoint.config.method)) {
logger.log('error', `HTTP Method not recognised! '${endpoint.config.method} ${endpointURL}'`);
return;
}
logger.log('debug', `[Endpoint] : '${endpoint.config.method} ${endpointURL}'`);
router[endpoint.config.method.toLowerCase()](
endpointURL,
endpoint.config.middleware,
async (req, res, next) => {
return await endpoint.config.implementation(endpoint.apiVersion, req, res, next);
}
);
}
module.exports = {
rain: rain,
};