Skip to content

Instantly share code, notes, and snippets.

@lexyblazy
Last active October 31, 2019 00:47
Show Gist options
  • Save lexyblazy/4349453289a2b0a2ea90e74999a82074 to your computer and use it in GitHub Desktop.
Save lexyblazy/4349453289a2b0a2ea90e74999a82074 to your computer and use it in GitHub Desktop.
API routes
import * as firebaseAdmin from "firebase-admin";
import * as functions from "firebase-functions";
import express from "express";
import nanoid from "nanoid"; // we use this npm package to generate unique ids for our documents.
import * as serviceAccount from "./serviceAccount.json";
const app = express();
const firebase = firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert({
projectId: serviceAccount.project_id,
clientEmail: serviceAccount.client_email,
privateKey: serviceAccount.private_key
}),
databaseURL: <YOUR_DATABASE_URL>
});
const db = firebase.firestore(); // it feels inefficient to write firebase.firestore() everytime :(
// GET /users - returns all the users in the database
app.get("/users", async (req: express.Request, res: express.Response) => {
try {
const usersCollectionReference = db.collection("users");
const usersSnapshot = await usersCollectionReference.get();
const users = usersSnapshot.docs.map(doc => ({
id: doc.id,
...doc.data()
}));
res.status(200).send({ users });
} catch (error) {
res.send(error);
}
});
// POST /users - adds a new user to the database
app.post("/users", async (req: express.Request, res: express.Response) => {
try {
const { firstName, lastName, email } = req.body;
await db
.collection("users")
.doc(nanoid())
.set({
firstName,
lastName,
email
});
res.status(201).send({ message: "Created a new User", success: true });
} catch (error) {
res.send(error);
}
});
// GET /users/:id - returns a specific user by id
app.get("/users/:id", async (req: express.Request, res: express.Response) => {
try {
const { id } = req.params;
const userDocumentReference = db.collection("users").doc(id);
const userDocumentSnapshot = await userDocumentReference.get();
const userDocument = {
id: userDocumentSnapshot.id,
...userDocumentSnapshot.data()
};
res.status(200).send({ user: userDocument });
} catch (error) {
res.send(error);
}
});
// PATCH /users/:id - updates a specific user
app.patch("/users/:id", async (req: express.Request, res: express.Response) => {
try {
const { id } = req.params;
const userDocumentReference = db.collection("users").doc(id);
await userDocumentReference.update({ ...req.body });
res.status(200).send({ message: "User updated", success: true });
} catch (error) {
res.send(error);
}
});
// DELETE /users/:id - deletes a specific user
app.delete(
"/users/:id",
async (req: express.Request, res: express.Response) => {
try {
const { id } = req.params;
const userDocumentReference = db.collection("users").doc(id);
await userDocumentReference.delete();
res.status(200).send({ message: "User Deleted", success: true });
} catch (error) {
res.send(error);
}
}
);
export const v1 = functions.https.onRequest(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment