Ejemplo n.º 1
0
module.exports = (content, data) => {
  Joi.assert(content, Joi.alternatives().try(Joi.func(), Joi.string()).required(), `render action`);
  Joi.assert(data, Joi.object(), `render action`);
  return args => {
    return (utils.getType(content) === 'function' ?
        Promise.resolve({}).then(content) : Promise.resolve({content})).
      then(result => {
        const message = `render template${result.path ? ` ${basename(result.path)}` : ``}`;
        args._ctx = args._ctx || {stack: []};
        args._ctx.stack.push(message);
        logger.debug(message);
        return compile(result.content, data || {});
      }).
      then(result => {
        logger.debug(`template rendered:\n${result}`);
        args.content = result;
        return args;
      });
  };
};
Ejemplo n.º 2
0
'use strict';

const _ = require(`lodash`);
const Joi = require(`joi`);
const express = require(`express`);
const libxml = require(`libxml-xsd`).libxmljs;
const bodyParser = require(`body-parser`);
const expect = require(`chai`).expect;
const logger = require(`../utils/logger`)(`act:listen`);

// schema to enforce incoming options
const schema = Joi.object().keys({
  port: Joi.number().required(),
  url: Joi.string().required().regex(/^\//),
  method: Joi.string().valid(`GET`, `POST`, `PUT`, `HEAD`, `DELETE`),
  body: Joi.alternatives(Joi.string(), Joi.object(), Joi.func()),
  headers: Joi.object(),
  code: Joi.number()
});

// TODO timeout

/**
 * Starts an HTTP server to listen a given url.
 * Acceptable method can be configured, has well as response body and headers.
 *
 * If a JSON body is passed, set default response content-type to 'application/json'.
 * If a libXML.js Document body is pased, set default response content-type to 'application/xml'.
 * You can still override the response content-type if needed.
 *
 * If body is given as a function, it must return a promise fulfilled