Initial commit 🚀

This commit is contained in:
Jonas Schmedtmann
2019-06-13 15:43:15 +01:00
commit 7f81af0ddf
1052 changed files with 2123177 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
class APIFeatures {
constructor(query, queryString) {
this.query = query;
this.queryString = queryString;
}
filter() {
const queryObj = { ...this.queryString };
const excludedFields = ['page', 'sort', 'limit', 'fields'];
excludedFields.forEach(el => delete queryObj[el]);
// 1B) Advanced filtering
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, match => `$${match}`);
this.query = this.query.find(JSON.parse(queryStr));
return this;
}
sort() {
if (this.queryString.sort) {
const sortBy = this.queryString.sort.split(',').join(' ');
this.query = this.query.sort(sortBy);
} else {
this.query = this.query.sort('-createdAt');
}
return this;
}
limitFields() {
if (this.queryString.fields) {
const fields = this.queryString.fields.split(',').join(' ');
this.query = this.query.select(fields);
} else {
this.query = this.query.select('-__v');
}
return this;
}
paginate() {
const page = this.queryString.page * 1 || 1;
const limit = this.queryString.limit * 1 || 100;
const skip = (page - 1) * limit;
this.query = this.query.skip(skip).limit(limit);
return this;
}
}
module.exports = APIFeatures;

View File

@@ -0,0 +1,13 @@
class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = AppError;

View File

@@ -0,0 +1,5 @@
module.exports = fn => {
return (req, res, next) => {
fn(req, res, next).catch(next);
};
};

View File

@@ -0,0 +1,67 @@
const nodemailer = require('nodemailer');
const pug = require('pug');
const htmlToText = require('html-to-text');
module.exports = class Email {
constructor(user, url) {
this.to = user.email;
this.firstName = user.name.split(' ')[0];
this.url = url;
this.from = `Jonas Schmedtmann <${process.env.EMAIL_FROM}>`;
}
newTransport() {
if (process.env.NODE_ENV === 'production') {
// Sendgrid
return nodemailer.createTransport({
service: 'SendGrid',
auth: {
user: process.env.SENDGRID_USERNAME,
pass: process.env.SENDGRID_PASSWORD
}
});
}
return nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
}
// Send the actual email
async send(template, subject) {
// 1) Render HTML based on a pug template
const html = pug.renderFile(`${__dirname}/../views/email/${template}.pug`, {
firstName: this.firstName,
url: this.url,
subject
});
// 2) Define email options
const mailOptions = {
from: this.from,
to: this.to,
subject,
html,
text: htmlToText.fromString(html)
};
// 3) Create a transport and send email
await this.newTransport().sendMail(mailOptions);
}
async sendWelcome() {
await this.send('welcome', 'Welcome to the Natours Family!');
}
async sendPasswordReset() {
await this.send(
'passwordReset',
'Your password reset token (valid for only 10 minutes)'
);
}
};