Skip to content

Commit

Permalink
chore: todos for save function
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmord committed Jan 28, 2020
1 parent fa54539 commit 8b45bbb
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/poolbase-app/src/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import next from 'next'

/* TODO: refactor and improve
- [ ] put firebase admin initialization in singleton module
- [ ] create request validation function
- [ ] create data validation function
- [ ] create save function that is backend agnostic
*/
admin.initializeApp();

export const savePage = functions.https.onRequest(async (request, response) => {
export const savePage = functions.https.onRequest(async (req, res) => {
// minimal requirement is URL
if(!req.body.url) {
res.status(400).send('No url data provided');
}
const requestData: { title: string; url: string } = {
...request.body,
...req.body,
};
// Push the new message into Cloud Firestore using the Firebase Admin SDK.
const writeResult = await admin
.firestore()
.collection('pages')
.add(requestData);
// Send back a message that we've successfully written the message
response.json({ result: `Page with url: ${writeResult.id} added.` });
res.json({ result: `Page with url: ${writeResult.id} added.` });
});

const dev = process.env.NODE_ENV !== 'production';
Expand Down

0 comments on commit 8b45bbb

Please sign in to comment.