Smol changes
This commit is contained in:
parent
658a783f6a
commit
8a6645f560
9 changed files with 68 additions and 23 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
import shortid from "shortid";
|
||||||
import URLs from "../../db/url.schema";
|
import URLs from "../../db/url.schema";
|
||||||
|
|
||||||
const isUrlValid = (url: string) => {
|
const isUrlValid = (url: string) => {
|
||||||
|
|
@ -8,11 +9,33 @@ const isUrlValid = (url: string) => {
|
||||||
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({ error: "URL Provided is Invalid" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate that there isn't a conflict
|
||||||
|
if (req.body.slug) {
|
||||||
|
const conflict = await URLs.findById(req.body.slug);
|
||||||
|
if (!!conflict) {
|
||||||
|
return res
|
||||||
|
.status(409)
|
||||||
|
.send({ error: `Slug '${req.body.slug}' is already registered.` });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Only return existing matches if the slug isn't custom
|
||||||
|
// Custom ones can continue to conflict :)
|
||||||
|
const existing = await URLs.findOne({
|
||||||
|
url: req.body.url,
|
||||||
|
isSlugCustom: false,
|
||||||
|
});
|
||||||
|
if (existing) {
|
||||||
|
return res.status(200).send(existing._id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = new URLs();
|
const url = new URLs();
|
||||||
url.url = req.body.url;
|
url.url = req.body.url;
|
||||||
|
url.isSlugCustom = !!req.body.slug;
|
||||||
|
url._id = req.body.slug || shortid.generate();
|
||||||
await url.save();
|
await url.save();
|
||||||
|
|
||||||
return res.status(200).send(url._id);
|
return res.status(200).send(url._id);
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,16 @@ const UrlSchema: Schema = new Schema({
|
||||||
default: shortid.generate,
|
default: shortid.generate,
|
||||||
},
|
},
|
||||||
url: String,
|
url: String,
|
||||||
|
isSlugCustom: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export type UrlModel = Document & {
|
export type UrlModel = Document & {
|
||||||
_id: string;
|
_id: string;
|
||||||
url: string;
|
url: string;
|
||||||
|
isSlugCustom: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default mongoose.model<UrlModel>("urls", UrlSchema, "urls");
|
export default mongoose.model<UrlModel>("urls", UrlSchema, "urls");
|
||||||
|
|
|
||||||
1
frontend/.gitignore
vendored
1
frontend/.gitignore
vendored
|
|
@ -21,3 +21,4 @@
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
|
**/.DS_Store
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 343 KiB After Width: | Height: | Size: 478 KiB |
|
|
@ -5,13 +5,10 @@
|
||||||
<link rel="icon" href="%PUBLIC_URL%/app-icon.png" />
|
<link rel="icon" href="%PUBLIC_URL%/app-icon.png" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<meta name="theme-color" content="#17a2b8" />
|
<meta name="theme-color" content="#17a2b8" />
|
||||||
<meta
|
<meta name="description" content="🧰 Tools for Devs 👨💻" />
|
||||||
name="description"
|
|
||||||
content="Utils for Devs!"
|
|
||||||
/>
|
|
||||||
<link rel="apple-touch-icon" href="%PUBLIC_URL%/app-icon.png" />
|
<link rel="apple-touch-icon" href="%PUBLIC_URL%/app-icon.png" />
|
||||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||||
<title>dv2ls - Utils for Devs!</title>
|
<title>dv2ls: 🛠 for 👨💻</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
# https://www.robotstxt.org/robotstxt.html
|
# https://www.robotstxt.org/robotstxt.html
|
||||||
User-agent: *
|
User-agent: *
|
||||||
Disallow: /paste?id=*
|
Disallow: /paste?id=*
|
||||||
|
Disallow: /code?id=*
|
||||||
|
Disallow: /f
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
export default (url) => axios.put('/api/url', { url }).then((resp) => resp.data);
|
export default (url, slug) => axios.put('/api/url', { url, slug }).then((resp) => resp.data);
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import putUrlHandler from '../axios/putUrlHandler';
|
||||||
export default () => {
|
export default () => {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
url: null,
|
url: null,
|
||||||
|
slug: null,
|
||||||
loading: false,
|
loading: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: false,
|
error: false,
|
||||||
|
|
@ -17,7 +18,9 @@ export default () => {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
if (state.url) {
|
if (state.url) {
|
||||||
setState({ ...state, loading: true });
|
setState({ ...state, loading: true });
|
||||||
putUrlHandler(state.url).then((data) => {
|
putUrlHandler(
|
||||||
|
state.url, state.slug,
|
||||||
|
).then((data) => {
|
||||||
setState({
|
setState({
|
||||||
...state,
|
...state,
|
||||||
result: `${window.location.origin}/${data}`,
|
result: `${window.location.origin}/${data}`,
|
||||||
|
|
@ -25,8 +28,13 @@ export default () => {
|
||||||
error: false,
|
error: false,
|
||||||
});
|
});
|
||||||
}).catch((ex) => {
|
}).catch((ex) => {
|
||||||
if (ex.response && ex.response.data === 'INVALID URL') {
|
if (ex.response?.data?.error) {
|
||||||
setState({ ...state, error: true });
|
setState({
|
||||||
|
...state,
|
||||||
|
result: null,
|
||||||
|
loading: false,
|
||||||
|
error: ex.response.data.error,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -35,8 +43,7 @@ export default () => {
|
||||||
return (
|
return (
|
||||||
<CSSTransition classNames="react-router" appear in timeout={300}>
|
<CSSTransition classNames="react-router" appear in timeout={300}>
|
||||||
<Container className="perfect-width">
|
<Container className="perfect-width">
|
||||||
<h2 className="text-center mt-10 py-3">URL Shortener</h2>
|
<h2 className="text-center mt-10 py-3">Link Shortener</h2>
|
||||||
|
|
||||||
<Form onSubmit={submit} className="perfect-width">
|
<Form onSubmit={submit} className="perfect-width">
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<InputGroup>
|
<InputGroup>
|
||||||
|
|
@ -47,27 +54,37 @@ export default () => {
|
||||||
onChange={(evt) => setState({ ...state, url: evt.target.value })}
|
onChange={(evt) => setState({ ...state, url: evt.target.value })}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
<InputGroup.Append>
|
||||||
|
<Form.Control
|
||||||
|
type="text"
|
||||||
|
style={{ width: '8rem', borderRadius: '0' }}
|
||||||
|
value={state.slug}
|
||||||
|
placeholder="Optional Slug"
|
||||||
|
onChange={(evt) => setState({ ...state, slug: evt.target.value })}
|
||||||
|
/>
|
||||||
|
</InputGroup.Append>
|
||||||
<InputGroup.Append>
|
<InputGroup.Append>
|
||||||
<Button type="submit" disabled={state.loading || !state.url}>
|
<Button type="submit" disabled={state.loading || !state.url}>
|
||||||
{state.loading ? 'Loading...' : 'Submit'}
|
{state.loading ? 'Loading...' : 'Submit'}
|
||||||
</Button>
|
</Button>
|
||||||
</InputGroup.Append>
|
</InputGroup.Append>
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
{state.error && (
|
|
||||||
<Form.Text className="text-danger">
|
|
||||||
Please enter a valid URL
|
|
||||||
</Form.Text>
|
|
||||||
)}
|
|
||||||
<Form.Text className="danger" />
|
<Form.Text className="danger" />
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
|
{state.error && (
|
||||||
|
<h5 className="text-center text-danger">
|
||||||
|
{state.error}
|
||||||
|
</h5>
|
||||||
|
)}
|
||||||
|
|
||||||
{state.result && (
|
{state.result && (
|
||||||
<h5 className="text-center">
|
<h5 className="text-center">
|
||||||
<a rel="noopener noreferrer" href={state.result} target="_blank">
|
<a rel="noopener noreferrer" href={state.result} target="_blank">
|
||||||
{state.result}
|
{state.result}
|
||||||
</a>
|
</a>
|
||||||
</h5>
|
</h5>
|
||||||
)}
|
)}
|
||||||
</Container>
|
</Container>
|
||||||
</CSSTransition>
|
</CSSTransition>
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import NavLinkItem from './NavLinkItem';
|
||||||
|
|
||||||
export default () => (
|
export default () => (
|
||||||
<Navbar expand="lg" collapseOnSelect bg="dark" variant="dark" fixed="top">
|
<Navbar expand="lg" collapseOnSelect bg="dark" variant="dark" fixed="top">
|
||||||
<Navbar.Brand>{window.location.origin.substr(window.location.origin.indexOf('//') + 2)}</Navbar.Brand>
|
<Navbar.Brand>dv2ls: Dev Tools</Navbar.Brand>
|
||||||
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
|
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
|
||||||
<Navbar.Collapse>
|
<Navbar.Collapse>
|
||||||
<Nav className="mr-auto">
|
<Nav className="mr-auto">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue