All files / spirit-common/lib defaults.js

97.62% Statements 41/42
75.86% Branches 22/29
88.89% Functions 8/9
100% Lines 37/37
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    25x   2x   2x   2x   2x     2x 2x   2x 11x     2x 11x                                                   11x 11x 11x 9x 6x     2x     2x   2x 12x     2x 9x 48x     48x   48x 25x   25x 7x     25x 25x     48x       2x 5x 5x 5x 5x       2x            
"use strict";
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
 
var _require = require("spirit");
 
var compose = _require.compose;
 
var exp_compat = require("spirit-express");
 
var middleware = require("./middleware");
 
// express middleware
var body_parser = require("body-parser");
var express_session = require("express-session");
 
var random_string = function random_string() {
  return Date.now().toString() + Math.random().toString();
};
 
var config = function config(key) {
  var cfg = {
    // every config uses these
    api: {
      log: true,
      proxy: true,
      ifmod: true,
      body: {
        json: { strict: true },
        urlencoded: { extended: true },
        text: false
      }
    },
 
    // site config (+ api)
    site: {
      session: {
        secret: random_string(), // user should pass their own
        //secure: true, // ssl sites should enable this
        //store: ...,   // NOTE user should pass in this always
        resave: false,
        saveUninitialized: true,
        httpOnly: true
      }
    }
  };
 
  key = key || "site";
  var keylc = key.toLowerCase();
  if (cfg[keylc]) {
    if (keylc === "api") return cfg.api;
    return mixin(cfg.api, cfg[keylc]);
  }
 
  throw new Error("No such configuration for defaults()", key);
};
 
var middleware_list = [{ name: "log", fn: middleware.log }, { name: "proxy", fn: middleware.proxy }, { name: "ifmod", fn: middleware.ifmod }, { name: "body", x: "json", fn: body_parser.json, express: true }, { name: "body", x: "urlencoded", fn: body_parser.urlencoded, express: true }, { name: "body", x: "text", fn: body_parser.text, express: true }, { name: "session", fn: express_session, express: true }];
 
var mixin = function mixin(base, user) {
  return Object.assign(base, user);
};
 
var generate = function generate(cfg, mwlist) {
  return mwlist.reduce(function (gen, mw_item) {
    var opts = cfg[mw_item.name];
 
    // switch to sub key
    if (opts && mw_item.x) opts = opts[mw_item.x];
 
    if (opts) {
      var _middleware = mw_item.fn;
      // init options (usually express)
      if ((typeof opts === "undefined" ? "undefined" : _typeof(opts)) === "object" && mw_item.express) {
        _middleware = _middleware(opts);
      }
      // if express wrap with spirit-express
      if (mw_item.express) _middleware = exp_compat(_middleware);
      gen.push(_middleware);
    }
 
    return gen;
  }, []);
};
 
var defaults = function defaults(str, user_cfg) {
  var cfg = mixin(config(str), user_cfg);
  var mw = generate(cfg, middleware_list);
  return function (handler) {
    return compose(handler, mw);
  };
};
 
module.exports = {
  defaults: defaults,
  generate: generate,
  mixin: mixin,
  config: config,
  middleware_list: middleware_list
};