mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-15 20:54:53 +00:00
Initial commit 🚀
This commit is contained in:
53
4-natours/after-section-10/utils/apiFeatures.js
Normal file
53
4-natours/after-section-10/utils/apiFeatures.js
Normal 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;
|
||||
13
4-natours/after-section-10/utils/appError.js
Normal file
13
4-natours/after-section-10/utils/appError.js
Normal 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;
|
||||
5
4-natours/after-section-10/utils/catchAsync.js
Normal file
5
4-natours/after-section-10/utils/catchAsync.js
Normal file
@@ -0,0 +1,5 @@
|
||||
module.exports = fn => {
|
||||
return (req, res, next) => {
|
||||
fn(req, res, next).catch(next);
|
||||
};
|
||||
};
|
||||
27
4-natours/after-section-10/utils/email.js
Normal file
27
4-natours/after-section-10/utils/email.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
const sendEmail = async options => {
|
||||
// 1) Create a transporter
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.EMAIL_HOST,
|
||||
port: process.env.EMAIL_PORT,
|
||||
auth: {
|
||||
user: process.env.EMAIL_USERNAME,
|
||||
pass: process.env.EMAIL_PASSWORD
|
||||
}
|
||||
});
|
||||
|
||||
// 2) Define the email options
|
||||
const mailOptions = {
|
||||
from: 'Jonas Schmedtmann <hello@jonas.io>',
|
||||
to: options.email,
|
||||
subject: options.subject,
|
||||
text: options.message
|
||||
// html:
|
||||
};
|
||||
|
||||
// 3) Actually send the email
|
||||
await transporter.sendMail(mailOptions);
|
||||
};
|
||||
|
||||
module.exports = sendEmail;
|
||||
Reference in New Issue
Block a user