-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
154 lines (122 loc) · 3.42 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
/* globals require, module, console */
'use strict';
var _ = require('lodash');
var express = require('express');
var cors = require('cors');
var chokidar = require('chokidar');
var ramlMocker = require('raml-mocker');
var options;
var requestsMap = {};
var app;
var defaults = {
port: 3030,
path: 'raml',
prefix: '',
prioritizeBy: 'schema', // example, todo:order
// watch: true, // watching
// debug: true, // shows logs
};
var colors = {default: '\x1b[0m', green: '\x1b[32m', qyan: '\x1b[36m'};
/**
* Initializing RAML mocker server
* @param {Function} cb callback executed af
* @param {Object} prefs configuration object
* @return {[type]} [description]
*/
function init (prefs, callback) {
options = _.extend(defaults, prefs);
app = options.app || express();
app.use(cors());
ramlMocker.generate(options, process(function(requestsToMock){
requestsToMock.forEach(function(reqToMock){
addRoute(reqToMock);
});
log(new Array(30).join('='));
log('%s[%s]%s API routes generated', colors.qyan, requestsToMock.length, colors.default);
if(typeof callback === 'function'){
callback(app);
}
}));
var watcher = watch();
var server = launchServer(app);
function close () {
if (watcher) { watcher.close(); }
if (server) { server.close(); }
}
return {
close: close,
server: server,
watcher: watcher
};
}
function log (){
if(options.debug){
console.log.apply(console, arguments);
}
}
function process (callback) {
return function (requestsToMock) {
requestsToMock.forEach(function(reqToMock){
requestsMap[reqToMock.method + '!' + reqToMock.uri] = reqToMock;
});
if(typeof callback === 'function'){
callback(requestsToMock);
}
};
}
function launchServer (app){
// intercept OPTIONS method
app.options('*', function(req, res) {
res.status(200).send();
});
if (!options.app) {
// set static location
if(options.staticPath){
app.use(express.static(options.staticPath));
}
// CORS middleware
if(options.cors){
app.use(function(req, res) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
});
}
console.log('Listening on port ' + options.port);
return app.listen(options.port);
}
}
function watch (){
if(options.watch){
var watcher = chokidar.watch(options.path, {ignored: /[\/\\]\./, persistent: true});
watcher.on('change', function(path) {
log('File modified: ', path);
ramlMocker.generate(options, process());
});
return watcher;
}
}
function addRoute (reqToMock){
var prefixes = _.isArray(options.prefix) ? options.prefix : [options.prefix];
var method = reqToMock.method;
log(colors.qyan + method + colors.default, '\t', reqToMock.uri);
prefixes.forEach(function(prefix){
var uri = prefix + reqToMock.uri;
app[method](uri, function(req,res){
var mockObj = requestsMap[method + '!' + reqToMock.uri];
if(mockObj){
var mock = mockObj.mock && mockObj.mock();
var example = mockObj.example && mockObj.example();
var response = options.prioritizeBy === 'example' ?
example || mock :
mock || example || '';
res.setHeader('content-type', 'application/json');
res.status(mockObj.defaultCode || 200).send(response);
} else {
res.status(404).send();
}
log(colors.green + method + colors.default, '\t', uri);
});
});
}
module.exports = init;