Skip to content

Instantly share code, notes, and snippets.

View Niweera's full-sized avatar
🚁
________Apache_Helicopter_______

Nipuna Weerasekara Niweera

🚁
________Apache_Helicopter_______
View GitHub Profile
@Niweera
Niweera / services.index.js
Created February 20, 2022 15:20
Services index.js file
import { AuthenticationError } from "../errors";
import config from "../keys";
import crypto from "crypto";
export default class Service {
getHandler({ crc_token: crcToken }) {
if (crcToken) {
return {
response_token: this.createCrcResponseToken(crcToken),
};
@Niweera
Niweera / errors.index.js
Created February 20, 2022 15:04
Custom errors
export const AccessDeniedError = class AccessDeniedError {
constructor(message) {
this.message = message;
}
};
export const AuthenticationError = class AuthenticationError {
constructor(message) {
this.message = message;
}
@Niweera
Niweera / middleware.error-handling.js
Created February 20, 2022 15:02
Error handling middleware
import {
ValidationError,
AuthenticationError,
AccessDeniedError,
NotFoundError,
} from "../errors";
import chalk from "chalk";
const errorLogger = (err, req, res, next) => {
if (err.message) {
@Niweera
Niweera / middleware.twitter-validator.js
Last active February 26, 2022 05:27
Twitter Validator Middleware
import config from "../keys";
import bodyParser from "body-parser";
import crypto from "crypto";
import { AuthenticationError } from "../errors";
const verifyGetRequest = (req, res, next) => {
const { crc_token, nonce } = req?.query || {};
const hmac = crypto
.createHmac("sha256", config.TWITTER_CONSUMER_SECRET)
.update(`crc_token=${crc_token}&nonce=${nonce}`)
@Niweera
Niweera / middleware.index.js
Created February 20, 2022 14:10
Middlewares
import bodyParser from "body-parser";
import morgan from "morgan";
import cors from "cors";
import helmet from "helmet";
import TwitterValidator from "./twitter-validator";
const CommonMiddleware = (app) => {
app.use(bodyParser.json());
app.use(morgan("dev"));
app.use(cors());
@Niweera
Niweera / models.index.js
Created February 20, 2022 12:20
Models index file
import Joi from "joi";
const PostValidationSchema = Joi.object({
for_user_id: Joi.string().required(),
});
export default {
PostValidationSchema,
};
@Niweera
Niweera / middleware.validator.js
Created February 20, 2022 12:18
Validator middleware
import Main from "../models";
import { ValidationError } from "../errors";
const validators = {
Main: {
scopes: {
post: Main.PostValidationSchema,
},
},
};
@Niweera
Niweera / async-wrapper.d.ts
Created February 20, 2022 12:14
Async Wrapper data type declaration
import { RequestHandler } from "express";
export default AsyncWrapper;
declare function AsyncWrapper(
fn: RequestHandler
): (req: any, res: any, next: any) => any;
@Niweera
Niweera / utilities.async-wrapper.js
Created February 20, 2022 12:10
Utilities Async Wrapper
const AsyncWrapper = (fn) => {
return (req, res, next) => {
return fn(req, res).catch(next);
};
};
export default AsyncWrapper;
@Niweera
Niweera / controllers.index.js
Created February 20, 2022 12:03
Controllers index file
import { Router } from "express";
import asyncWrapper from "../utilities/async-wrapper";
import Service from "../services";
import validator from "../middleware/validator";
const service = new Service();
const router = Router();
/** @route GET /callback
* @desc Get CRC Check