|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var JSONScript = require('jsonscript-js') |
| 4 | + , request = require('request') |
| 5 | + , Ajv = require('ajv') |
| 6 | + , defineKeywords = require('ajv-keywords') |
| 7 | + , _ = require('lodash') |
| 8 | + , optionsSchema = require('./config_schema.json') |
| 9 | + , validateOptions = getValidator(); |
| 10 | + |
| 11 | +module.exports = jsonscriptProxy; |
| 12 | + |
| 13 | + |
| 14 | +var METHODS = ['get', 'post', 'put', 'delete']; |
| 15 | +function jsonscriptProxy(options) { |
| 16 | + if (!validateOptions(options)) { |
| 17 | + console.log('Invalid options:\n', validateOptions.errors); |
| 18 | + throw new Error('Invalid options'); |
| 19 | + } |
| 20 | + |
| 21 | + var js = JSONScript(options.jsonscript || { strict: true }); |
| 22 | + for (var key in options.services) |
| 23 | + js.addExecutor(key, getExecutor(options.services[key])); |
| 24 | + evaluator.js = js; |
| 25 | + |
| 26 | + return evaluator; |
| 27 | + |
| 28 | + |
| 29 | + function evaluator(req, res) { |
| 30 | + var script = req.body.script; |
| 31 | + var data = req.body.data; |
| 32 | + var valid = js.validate(script); |
| 33 | + if (valid) { |
| 34 | + js.evaluate(script, data) |
| 35 | + .then(function (value) { |
| 36 | + res.json(value); |
| 37 | + }, function (err) { |
| 38 | + res.status(err.errors ? 400 : err.statusCode || 500) |
| 39 | + .send({ |
| 40 | + error: err.message, |
| 41 | + errors: err.errors |
| 42 | + }); |
| 43 | + }); |
| 44 | + } else { |
| 45 | + res.status(400) |
| 46 | + .send({ |
| 47 | + error: 'script is invalid', |
| 48 | + errors: js.validate.errors |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + function getExecutor(service) { |
| 55 | + var processResponse = processResponseFunc(service.processResponse || options.processResponse); |
| 56 | + addExecutorMethods(); |
| 57 | + return execRouter; |
| 58 | + |
| 59 | + function execRouter(args) { |
| 60 | + var opts = { |
| 61 | + uri: service.basePath + args.path, |
| 62 | + headers: args.headers, |
| 63 | + json: args.body || true |
| 64 | + }; |
| 65 | + |
| 66 | + return new (options.Promise || Promise)(function (resolve, reject) { |
| 67 | + request[args.method](opts, function (err, resp) { |
| 68 | + if (err) return reject(err); |
| 69 | + resolve(processResponse(resp, args)); |
| 70 | + }); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + function addExecutorMethods() { |
| 75 | + METHODS.forEach(function (method) { |
| 76 | + execRouter[method] = function(args) { |
| 77 | + if (args.method && args.method != method) { |
| 78 | + console.warn('method specified in args (' + args.method + |
| 79 | + ') is different from $method in instruction (' + method + '), used ' + method); |
| 80 | + } |
| 81 | + args.method = method; |
| 82 | + return execRouter(args); |
| 83 | + }; |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +function getValidator() { |
| 91 | + var ajv = Ajv({ allErrors: true }); |
| 92 | + defineKeywords(ajv, 'typeof'); |
| 93 | + return ajv.compile(optionsSchema); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +function processResponseFunc(processResponse) { |
| 98 | + return processResponse == 'body' |
| 99 | + ? bodyProcessResponse |
| 100 | + : typeof processResponse == 'function' |
| 101 | + ? processResponse |
| 102 | + : defaultProcessResponse; |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +function bodyProcessResponse(resp) { |
| 107 | + if (resp.statusCode < 300) return resp.body; |
| 108 | + throw new HttpError(resp); |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +function defaultProcessResponse(resp, args) { |
| 113 | + resp = _.pick(resp, 'statusCode', 'headers', 'body'); |
| 114 | + resp.request = args; |
| 115 | + return resp; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +function HttpError(resp) { |
| 120 | + this.message = resp.body ? JSON.stringify(resp.body) : 'Error'; |
| 121 | + this.statusCode = resp.statusCode; |
| 122 | +} |
| 123 | + |
| 124 | +HttpError.prototype = Object.create(Error.prototype); |
| 125 | +HttpError.prototype.constructor = HttpError; |
0 commit comments