2021-09-18 17:01:48 +00:00
|
|
|
import { VercelResponse } from '@vercel/node'
|
|
|
|
import axios from 'axios'
|
|
|
|
|
2021-10-03 12:55:49 +00:00
|
|
|
import blacklist from './blacklist.json'
|
2021-09-18 17:01:48 +00:00
|
|
|
import dbConnect from '../dbConnect'
|
|
|
|
import URLModel from '../schema'
|
|
|
|
|
|
|
|
export default async function addURL(url: string, response: VercelResponse) {
|
2021-10-03 12:55:49 +00:00
|
|
|
blacklist.forEach(b => {
|
2021-09-21 17:37:56 +00:00
|
|
|
if (url.includes(b)) {
|
|
|
|
return response.status(400).json({
|
|
|
|
success: false,
|
2022-02-25 17:46:12 +00:00
|
|
|
message: 'URL is on blacklist'
|
2021-09-21 17:37:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-18 17:01:48 +00:00
|
|
|
// fetch url (e.g. check domain exist)
|
|
|
|
try {
|
|
|
|
await axios.get(url)
|
|
|
|
} catch (err) {
|
|
|
|
return response.status(400).json({
|
|
|
|
success: false,
|
|
|
|
message: 'Invalid URL'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await dbConnect()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const dbURL = new URLModel({ url })
|
|
|
|
|
|
|
|
await dbURL.save()
|
|
|
|
|
|
|
|
return response.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message: `Added ${url}`
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return response.status(400).json({
|
|
|
|
success: false,
|
|
|
|
message: 'Already exist?'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|