This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.js
85 lines (73 loc) · 3.31 KB
/
api.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
require('./db.js')();
// TODO: Eventually this will be the main entry point, includable as api middleware into other applications as a node module.
module.exports = function(config){
var express = require('express'),
bodyParser = require('body-parser'),
_ = require('underscore'),
router = new express.Router(),
Request = require('./models/request.js'),
Mapping = require('./models/mapping.js'),
requester = require('./requester'),
transformations = require('./default_transformations'),
previousRouterStackLength;
if (config.transformations){
_.each(config.transformations, function(transformation, name){
transformations._set(name, transformation);
});
}
router.use(bodyParser.json());
// Disable require cache for development or the test runner fails on node 0.10 and 0.12
router.get('/', require('./routes/frontend.js'));
router.get('/requests', require('./routes/frontend.js'));
router.get('/requests/*', require('./routes/frontend.js'));
router.use('/try', require('./routes/try.js')());
router.use('/api/requests', require('./routes/requests.js')());
router.get('/api/transformations', require('./routes/transformations.js'));
// this is what we will restore the router stack to when tearing down any stale mounted request handlers
previousRouterStackLength = router.stack.length;
// Adds a single route into the router from a request definition in the DB
function populateSingleRoute(request){
var method = request.method,
mountPath = request.mountPath;
if (!mountPath){
console.warn('WARNING: Request found with no mount path - probably from a previous version of REST client. Offending request: ');
console.log(request);
return;
}
router[method.toLowerCase()](mountPath, function(req, res){
// Handle the mounted route using our requester library
return requester(request, false, function(err, requestResult){
if (err){
return res.status(500).json(err);
}
var status = requestResult.response.statusCode || 500;
if (!requestResult || !requestResult.response || !requestResult.response.body){
return res.status(status).end();
}
var responseBody = requestResult.response.mapped || requestResult.response.body;
if (typeof responseBody !== 'object'){
return res.status(status).end(responseBody);
}
return res.status(status).json(responseBody);
});
});
}
// Finds all our requests, clears out the router of any old request definitions, then adds these routes to the DB
function populateAllRoutes(){
Request.find({}).populate('headers mapping').exec(function(err, requestsList){
if (err){
console.error("Failed to find routes on startup: " + JSON.stringify(err));
}
// Restore our router to it's previous state before we had mounted any user routes
router.stack.splice(previousRouterStackLength, router.stack.length);
// Now, re-mount every route to ensure we're up to date
requestsList.forEach(populateSingleRoute);
});
}
Request.schema.on('update', populateAllRoutes);
Request.schema.on('save', populateAllRoutes);
Mapping.schema.on('update', populateAllRoutes);
Mapping.schema.on('save', populateAllRoutes);
populateAllRoutes();
return router;
};