Last active
January 13, 2022 09:49
-
-
Save siddharthborderwala/18704060e0179d37f0fe8ce4a4690840 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const headers = { | |
// bearer-token authorization header | |
'Authorization': `Bearer ${process.env.NOTION_API_SECRET}`, | |
// the payload content-type | |
'Content-Type': 'application/json', | |
// specify the version of the API | |
'Notion-Version': '2021-08-16', | |
}; | |
const handler = async (req, res) => { | |
const method = req.method.toLowerCase(); | |
if (method === 'post') { | |
// validate the data | |
const { name, email, message } = req.body.data; | |
// if we don't have a name | |
if (!name) { | |
res.status(400).send('`name` field required'); | |
return; | |
} | |
// if the type of name is not string | |
if (typeof name !== 'string') { | |
res.status(400).send('`name` field must be a string'); | |
return; | |
} | |
// if after stripping out whitespaces, the name is empty | |
if (name.trim().length === 0) { | |
res.status(400).send('`name` field must not be empty'); | |
return; | |
} | |
// if we don't have an email | |
if (!email) { | |
res.status(400).send('`email` field required'); | |
return; | |
} | |
// if the type of email is not string | |
if (typeof email !== 'string') { | |
res.status(400).send('`email` field must be a string'); | |
return; | |
} | |
// if after stripping out whitespaces, the email is empty | |
if (email.trim().length === 0) { | |
res.status(400).send('`email` field must not be empty'); | |
return; | |
} | |
// if the email is invalid, this check is made using a regular expression | |
// Want to learn more about regular expressions, check out this amazing youtube video | |
// https://www.youtube.com/watch?v=VrT3TRDDE4M | |
if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) { | |
res.status(400).send('`email` field invalid'); | |
return; | |
} | |
// if we don't have a message | |
if (!message) { | |
res.status(400).send('`message` field required'); | |
return; | |
} | |
// if the type of message is not a string | |
if (typeof message !== 'string') { | |
res.status(400).send('`message` field must be a string'); | |
return; | |
} | |
// if after stripping out whitespaces, the email is empty | |
if (message.trim().length === 0) { | |
res.status(400).send('`message` field must not be empty'); | |
return; | |
} | |
// store it in notion | |
const payload = { | |
parent: { | |
database_id: process.env.NOTION_DB_ID, | |
}, | |
properties: { | |
name: [ | |
{ | |
text: { | |
content: name, | |
}, | |
}, | |
], | |
email: email, | |
date: { | |
start: new Date().toISOString(), | |
end: null, | |
}, | |
}, | |
children: [ | |
{ | |
object: 'block', | |
type: 'paragraph', | |
paragraph: { | |
text: [ | |
{ | |
type: 'text', | |
text: { | |
content: message, | |
}, | |
}, | |
], | |
}, | |
}, | |
], | |
}; | |
try { | |
const response = axios.post( | |
'https://api.notion.com/v1/pages', | |
payload, | |
{ headers } | |
); | |
// the request was successful, send a 200 OK response | |
res.status(200).send('OK'); | |
} catch (error) { | |
if (error.response && error.response.status === 400) { | |
// some error in the request data | |
res.status(400).send('Bad request'); | |
} else { | |
// some error on our part | |
res.status(500).send('Internal Server Error'); | |
} | |
} | |
} else { | |
res.set('Allow', 'POST'); | |
res.status(405).send(`${method} not allowed`); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment