All files / reply reply.js

17.78% Statements 16/90
0% Branches 0/31
0% Functions 0/32
19.05% Lines 16/84
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 1641x       1x                         1x                 1x                                             1x                 1x                                             1x             1x             1x             1x             1x                                     1x                   1x                 1x             1x               1x  
const path = require('path');
 
 
 
const Reply = function (name, ...args) {
  if (!(this instanceof Reply)) {
    return new Reply();
  }
  this.stack = [];
  if (name) {
    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('file() expects a path');
 
  if (args.length > 2)
    throw new Error('file() expects a path and options but nothing else');
 
  let [file, opts = {}] = args;
  if (!path.isAbsolute(file)) {
    file = path.resolve(process.cwd(), file);
  }
 
  this.stack.push(ctx => new Promise((resolve, reject) => {
    ctx.res.download(file, opts, err => err ? reject(err) : resolve());
  }));
 
  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');
 
  if (args.length > 2)
    throw new Error('file() expects a path and options but nothing else');
 
  let [file, opts = {}] = args;
  if (!path.isAbsolute(file)) {
    file = path.resolve(process.cwd(), file);
  }
 
  this.stack.push(ctx => new Promise((resolve, reject) => {
    ctx.res.sendFile(file, opts, err => err ? reject(err) : resolve());
  }));
 
  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('file() expects a path');
 
  if (args.length > 2)
    throw new Error('file() expects a path and options but nothing else');
 
  let [file, opts = {}] = args;
 
  this.stack.push(ctx => new Promise((resolve, reject) => {
    // Note: if callback is provided, it does not send() automatically
    const cb = (err, html) => err ? reject(err) : resolve(ctx.res.send(html));
    ctx.res.render(file, opts, cb);
  }));
  return this;
};
 
Reply.prototype.send = function (...args) {
  this.stack.push(ctx => {
    if (args[0].req) {
      throw new Error('You should not return res()');
    }
    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;