UNPKG

2.2 kBJavaScriptView Raw
1// const etag = require('etag');
2const moment = require('moment');
3
4const statusCodeHelper = (res) => (code) => (body = {}) => {
5 // res.set('ETag', etag(JSON.stringify(body)));
6 res.status(code).json(body);
7};
8
9const removeEmpty = (obj = {}) =>
10 Object.entries(obj).reduce((a, [key, value]) => {
11 const copy = { ...a };
12 if (value !== null && value !== undefined)
13 copy[key] = value;
14 return copy;
15 }, {});
16
17/*
18const getLastModifiedDate = (arr) =>
19 moment(
20 arr.reduce((a, c) => {
21 const d = typeof c === 'object' ? c.updatedAt : null;
22
23 if (!moment(d).isValid()) return a;
24 return moment(a).isAfter(d) ? a : d;
25 }, ''),
26 ).toISOString(); */
27
28const stripMongoDBProps = (i) => {
29 try {
30 if (typeof i !== 'object') return i;
31
32 const json = JSON.parse(JSON.stringify(i));
33 const cleaned = removeEmpty(json);
34
35 delete cleaned._id;
36 delete cleaned.__v;
37 delete cleaned.password;
38 delete cleaned.secret;
39
40 return Object.entries(cleaned).reduce(
41 (a, [k, v]) =>
42 Object.assign(a, {
43 [k]: Array.isArray(v)
44 ? v.map(stripMongoDBProps)
45 : stripMongoDBProps(v),
46 }),
47 cleaned,
48 );
49 } catch (e) {
50 return JSON.parse(JSON.stringify(i));
51 }
52};
53
54const decorateResponse = (req, res, next) => {
55 const dispatch = statusCodeHelper(res);
56
57 req.marshal = (o) => {
58 if (Array.isArray(o)) {
59 // res.set('Last-Modified', getLastModifiedDate(o));
60 return o.map(stripMongoDBProps);
61 }
62
63 // res.set('Last-Modified', o.updatedAt);
64 return stripMongoDBProps(o);
65 };
66
67 req.isFresh = (d) => {
68 const unmod =
69 req.headers['If-Unmodified-Since'] ||
70 req.headers['if-unmodified-since'];
71
72 return moment(unmod, moment.ISO_8601, true).isValid() &&
73 moment(d, moment.ISO_8601, true).isValid() &&
74 moment(d).isAfter(new Date(unmod).toISOString())
75 ? res.status(412).send()
76 : true;
77 };
78
79 res.acknowledge = dispatch(204);
80 res.ok = dispatch(200);
81 res.update = dispatch(200);
82 res.create = dispatch(201);
83 next();
84};
85
86module.exports = decorateResponse;