Added schma to validate
This commit is contained in:
parent
bb09b731aa
commit
cfc49f3ec4
|
@ -31,26 +31,36 @@ fastify.get('/:id', async (req, res) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
//Create new urls
|
//Create new urls
|
||||||
fastify.post('/new', async (req, res) => {
|
const newUrlSchema = {
|
||||||
|
body: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
target: { type: 'string' },
|
||||||
|
shortcode: { type: 'string' },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fastify.post('/new', { newUrlSchema }, async (req, res) => {
|
||||||
const target = req.body?.target;
|
const target = req.body?.target;
|
||||||
let shortcode = req.body?.shortcode;
|
let shortcode = req.body?.shortcode;
|
||||||
if (!req.body?.target) {
|
if (!target) {
|
||||||
res.statusCode = 400;
|
res.statusCode = 400;
|
||||||
return "Missing target";
|
return "Missing target";
|
||||||
}
|
}
|
||||||
if (!shortcode) {
|
if (!shortcode) {
|
||||||
shortcode = nanoid(12);
|
shortcode = nanoid(12);
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
try {
|
try {
|
||||||
const exists = await db.get(shortcode);
|
const exists = await db.get(shortcode);
|
||||||
if(exists){
|
if (exists) {
|
||||||
res.statusCode = 400;
|
res.statusCode = 400;
|
||||||
return "Shortcode already exists, please choose another code";
|
return "Shortcode already exists, please choose another code";
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if(!error.notFound){
|
if (!error.notFound) {
|
||||||
res.statusCode=500;
|
res.statusCode = 500;
|
||||||
return "Internal Server Error."
|
return "Internal Server Error."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue