Format files

This commit is contained in:
oitsjustjose 2021-06-21 20:31:41 -05:00
commit 175f29eeb7
12 changed files with 1102 additions and 124 deletions

10
backend/.eslintignore Normal file
View file

@ -0,0 +1,10 @@
# /node_modules/* in the project root is ignored by default
# build artefacts
dist/*
coverage/*
# data definition files
**/*.d.ts
# 3rd party libs
/src/public/
# custom definition files
/src/types/

33
backend/.eslintrc Normal file
View file

@ -0,0 +1,33 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"eol-last": [
"error",
"always"
],
"semi": [
"error",
"always"
],
"quotes": [
"error",
"double"
],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": 1,
"@typescript-eslint/no-inferrable-types": [
"warn",
{
"ignoreParameters": true
}
],
"@typescript-eslint/no-unused-vars": "warn"
}
}

File diff suppressed because it is too large Load diff

View file

@ -25,7 +25,10 @@
"mongoose": "^5.12.14", "mongoose": "^5.12.14",
"multer": "^1.4.2", "multer": "^1.4.2",
"request-ip": "^2.1.3", "request-ip": "^2.1.3",
"shortid": "^2.2.15" "shortid": "^2.2.15",
"@typescript-eslint/eslint-plugin": "^2.3.1",
"@typescript-eslint/parser": "^2.3.1",
"eslint": "^6.4.0"
}, },
"devDependencies": { "devDependencies": {
"@types/body-parser": "^1.19.0", "@types/body-parser": "^1.19.0",
@ -35,7 +38,8 @@
"@types/multer": "^1.4.4", "@types/multer": "^1.4.4",
"@types/request-ip": "0.0.35", "@types/request-ip": "0.0.35",
"@types/shortid": "0.0.29", "@types/shortid": "0.0.29",
"typescript": "^3.9.5" "typescript": "^3.9.5",
"@types/eslint": "^6.1.1"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",

View file

@ -1,29 +1,29 @@
import { Request, Response } from 'express' import { Request, Response } from "express";
import Pastes from '../../db/txt.schema' import Pastes from "../../db/txt.schema";
import moment from 'moment-timezone' import moment from "moment-timezone";
export default async (req: Request, res: Response) => { export default async (req: Request, res: Response) => {
try { try {
const paste = await Pastes.findById(req.params.slug) const paste = await Pastes.findById(req.params.slug);
if (paste) { if (paste) {
// Check for an expired link // Check for an expired link
if (moment().isAfter(moment(paste.expiresAt))) { if (moment().isAfter(moment(paste.expiresAt))) {
await Pastes.findByIdAndDelete(paste._id) await Pastes.findByIdAndDelete(paste._id);
return res.status(404).send("PASTE EXPIRED") return res.status(404).send("PASTE EXPIRED");
} }
return res.json({ return res.json({
paste: paste.paste, paste: paste.paste,
syntax: paste.syntax syntax: paste.syntax,
}) });
} else { } else {
return res.status(404).end() return res.status(404).end();
}
} catch (ex) {
console.error(ex)
return res.status(500).send({
error: ex
})
} }
} } catch (ex) {
console.error(ex);
return res.status(500).send({
error: ex,
});
}
};

View file

@ -1,20 +1,20 @@
import { Request, Response } from 'express' import { Request, Response } from "express";
import Pastes from '../../db/txt.schema' import Pastes from "../../db/txt.schema";
import moment from 'moment-timezone' import moment from "moment-timezone";
export default async (req: Request, res: Response) => { export default async (req: Request, res: Response) => {
try { try {
const paste = new Pastes() const paste = new Pastes();
paste.paste = req.body.paste paste.paste = req.body.paste;
paste.syntax = req.body.syntax paste.syntax = req.body.syntax;
paste.expiresAt = moment(req.body.expiresAt).toDate(); paste.expiresAt = moment(req.body.expiresAt).toDate();
await paste.save() await paste.save();
return res.status(200).send(paste._id) return res.status(200).send(paste._id);
} catch (ex) { } catch (ex) {
console.error(ex) console.error(ex);
return res.status(500).send({ return res.status(500).send({
error: ex error: ex,
}) });
} }
} };

View file

@ -1,19 +1,19 @@
import { Request, Response } from 'express' import { Request, Response } from "express";
import URLs from '../../db/url.schema' import URLs from "../../db/url.schema";
export default async (req: Request, res: Response) => { export default async (req: Request, res: Response) => {
try { try {
const url = await URLs.findById(req.params.slug) const url = await URLs.findById(req.params.slug);
if (url) { if (url) {
res.redirect(url.url); res.redirect(url.url);
} else { } else {
res.status(404).send() res.status(404).send();
}
} catch (ex) {
console.error(ex)
return res.status(500).send({
error: ex
})
} }
} } catch (ex) {
console.error(ex);
return res.status(500).send({
error: ex,
});
}
};

View file

@ -1,25 +1,25 @@
import { Request, Response } from 'express' import { Request, Response } from "express";
import URLs from '../../db/url.schema' import URLs from "../../db/url.schema";
const isUrlValid = (url: string) => { const isUrlValid = (url: string) => {
return url.startsWith("http") || url.startsWith("www") return url.startsWith("http") || url.startsWith("www");
} };
export default async (req: Request, res: Response) => { export default async (req: Request, res: Response) => {
try { try {
if (!isUrlValid(req.body.url)) { if (!isUrlValid(req.body.url)) {
return res.status(422).send("INVALID URL") return res.status(422).send("INVALID URL");
}
const url = new URLs()
url.url = req.body.url
await url.save()
return res.status(200).send(url._id)
} catch (ex) {
console.error(ex)
return res.status(500).send({
error: ex
})
} }
}
const url = new URLs();
url.url = req.body.url;
await url.save();
return res.status(200).send(url._id);
} catch (ex) {
console.error(ex);
return res.status(500).send({
error: ex,
});
}
};

View file

@ -1,31 +1,34 @@
import { Schema, model, Document } from 'mongoose' import { Schema, model, Document } from "mongoose";
import shortid from 'shortid' import shortid from "shortid";
const PasteSchema = new Schema({ const PasteSchema = new Schema(
{
_id: { _id: {
type: String, type: String,
default: shortid.generate default: shortid.generate,
}, },
paste: { type: String }, paste: { type: String },
syntax: { syntax: {
type: String, type: String,
default: null default: null,
}, },
expiresAt: { expiresAt: {
type: Date, type: Date,
expires: 0, expires: 0,
default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
}, },
}, { },
timestamps: true {
}) timestamps: true,
}
);
export type PasteModel = Document & { export type PasteModel = Document & {
_id: string, _id: string;
paste: string, paste: string;
syntax: string, syntax: string;
expiresAt: Date, expiresAt: Date;
timestamps: Date timestamps: Date;
} };
export default model<PasteModel>('Paste', PasteSchema) export default model<PasteModel>("Paste", PasteSchema);

View file

@ -1,17 +1,17 @@
import mongoose, { Document, Schema, Types } from 'mongoose' import mongoose, { Document, Schema, Types } from "mongoose";
import shortid from 'shortid' import shortid from "shortid";
const UrlSchema: Schema = new Schema({ const UrlSchema: Schema = new Schema({
_id: { _id: {
type: String, type: String,
default: shortid.generate default: shortid.generate,
}, },
url: String url: String,
}) });
export type UrlModel = Document & { export type UrlModel = Document & {
_id: string, _id: string;
url: string url: string;
} };
export default mongoose.model<UrlModel>('urls', UrlSchema, 'urls') export default mongoose.model<UrlModel>("urls", UrlSchema, "urls");

View file

@ -1,9 +1,9 @@
import mongoose from 'mongoose' import mongoose from "mongoose";
export default async () => { export default async () => {
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Utils`, { await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Utils`, {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
useCreateIndex: true useCreateIndex: true,
}) });
} };

View file

@ -1,13 +1,13 @@
import { config as LoadDotenv } from 'dotenv' import { config as LoadDotenv } from "dotenv";
import app from './app' import app from "./app";
import connectDB from './db' import connectDB from "./db";
if(process.env.NODE_ENV !== "production") { if (process.env.NODE_ENV !== "production") {
LoadDotenv() LoadDotenv();
} }
connectDB() connectDB();
const port = process.env.PORT || 3000 const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening at http://127.0.0.1:${port}`)) app.listen(port, () => console.log(`Listening at http://127.0.0.1:${port}`));