All files / server/reply reply.js

35.96% Statements 32/89
10.34% Branches 3/29
23.08% Functions 6/26
35.96% Lines 32/89
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 18012x 12x 12x     12x 96x 96x 96x         12x                 12x                                                               12x                 12x                                                       12x             12x             12x             12x             12x                                           12x     96x       96x 96x   96x     12x 87x   87x 87x   87x     12x             12x 96x 183x   89x       12x  
const path = require('path');
const fs = require('mz/fs');
const { promisify } = require('util');
 
 
const Reply = function (name, ...args) {
  this.stack = [];
  this[name](...args);
  return this;
};
 
 
 
Reply.prototype.cookie = function (...args) {
  this.stack.push(ctx => {
    ctx.res.cookie(...args);
  });
  return this;
};
 
 
 
Reply.prototype.download = function (...args) {
 
  // Guard clauses
  if (args.length < 1) {
    throw new Error('download() expects a path as the first argument');
  }
 
  if (args.length < 2) {
    throw new Error('download() expects a filename as the second argument');
  }
 
  if (args.length > 2) {
    throw new Error('download() only expects two arguments, path and filename. The rest of them will be ignored');
  }
 
  let [file, opts] = args;
  if (!path.isAbsolute(file)) {
    file = path.resolve(process.cwd(), file);
  }
 
  this.stack.push(async ({ res }) => {
    if (!await fs.exists(file)) {
      throw new Error(`The file "${file}" does not exist. Make sure that you set an absolute path or a relative path to the root of your project`);
    }
    return promisify(res.download.bind(res))(file, opts);
  });
 
  return this;
};
 
 
 
Reply.prototype.end = function () {
  this.stack.push(ctx => {
    ctx.res.end();
  });
  return this;
};
 
 
 
Reply.prototype.file = function (...args) {
 
  // Guard clauses
  if (args.length < 1) {
    throw new Error('file() expects a path as the first argument');
  }
 
  if (args.length > 2) {
    throw new Error(`file() only expects two arguments, the path and options, but ${args.length} were provided.`);
  }
 
  let [file, opts = {}] = args;
  if (!path.isAbsolute(file)) {
    file = path.resolve(process.cwd(), file);
  }
 
  this.stack.push(async ({ res }) => {
    if (!await fs.exists(file)) {
      throw new Error(`The file "${file}" does not exist. Make sure that you set an absolute path or a relative path to the root of your project`);
    }
    return promisify(res.sendFile.bind(res))(file, opts);
  });
 
  return this;
};
 
 
 
Reply.prototype.header = function (...args) {
  this.stack.push(ctx => {
    ctx.res.header(...args);
  });
  return this;
};
 
Reply.prototype.json = function (...args) {
  this.stack.push(ctx => {
    ctx.res.json(...args);
  });
  return this;
};
 
Reply.prototype.jsonp = function (...args) {
  this.stack.push(ctx => {
    ctx.res.jsonp(...args);
  });
  return this;
};
 
Reply.prototype.redirect = function (...args) {
  this.stack.push(ctx => {
    ctx.res.redirect(...args);
  });
  return this;
};
 
Reply.prototype.render = function (...args) {
 
  // Guard clauses
  if (args.length < 1) {
    throw new Error('render() expects a path');
  }
 
  if (args.length > 2) {
    throw new Error('render() expects a path and options but nothing else');
  }
 
  let [file, opts = {}] = args;
 
 
  this.stack.push(async ({ res }) => {
    const html = await promisify(res.render.bind(res))(file, opts);
    res.send(html);
  });
 
  return this;
};
 
Reply.prototype.send = function (...args) {
 
  // If we are trying to send the context
  Iif (args[0] && args[0].close && args[0].close instanceof Function) {
    throw new Error('Never send the context, request or response as those are a security risk');
  }
 
  this.stack.push(ctx => {
    ctx.res.send(...args);
  });
  return this;
};
 
Reply.prototype.status = function (...args) {
  this.stack.push(ctx => {
    // In case there is no response, it'll respond with the status
    ctx.res.explicitStatus = true;
    ctx.res.status(...args);
  });
  return this;
};
 
Reply.prototype.type = function (...args) {
  this.stack.push(ctx => {
    ctx.res.type(...args);
  });
  return this;
};
 
Reply.prototype.exec = async function (ctx) {
  for (let cb of this.stack) {
    await cb(ctx);
  }
  this.stack = [];
};
 
// This will make that the first time a function is called it starts a new stack
module.exports = Reply;