mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 07:43:16 +10:00
api architecture
This commit is contained in:
@ -7,7 +7,10 @@ const nextConfig = {
|
||||
distDir: "build",
|
||||
};
|
||||
|
||||
const withTM = require("next-transpile-modules")(["@documenso/prisma"]);
|
||||
const withTM = require("next-transpile-modules")([
|
||||
"@documenso/prisma",
|
||||
"@documenso/lib",
|
||||
]);
|
||||
const plugins = [];
|
||||
plugins.push(withTM);
|
||||
|
||||
|
||||
@ -1,49 +1 @@
|
||||
import PrismaClient from "@documenso/prisma";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function userHandler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const { method, body } = req;
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// Check Session
|
||||
|
||||
switch (method) {
|
||||
case "POST":
|
||||
if (!body.userId) {
|
||||
res.status(400).end("Owner ID cannot be empty.");
|
||||
}
|
||||
|
||||
try {
|
||||
const newDocument: any = await prisma.document
|
||||
.create({
|
||||
data: { userId: body.userId, document: body.document },
|
||||
})
|
||||
.then(async () => {
|
||||
await prisma.$disconnect();
|
||||
res.status(200).send(newDocument);
|
||||
});
|
||||
} catch (error) {
|
||||
await prisma.$disconnect();
|
||||
res.status(500).end("An error has occured.");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "GET":
|
||||
// GET all docs for user in session
|
||||
let documents = await prisma.document.findMany({
|
||||
where: {
|
||||
userId: body.userId,
|
||||
},
|
||||
});
|
||||
res.status(200).send(documents);
|
||||
break;
|
||||
|
||||
default:
|
||||
res.setHeader("Allow", ["GET", "POST"]);
|
||||
res.status(405).end(`Method ${method} Not Allowed`);
|
||||
}
|
||||
}
|
||||
export {};
|
||||
|
||||
@ -1,12 +1,19 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
type Data = {
|
||||
import { defaultHandler, defaultResponder } from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
|
||||
type responseData = {
|
||||
status: string;
|
||||
};
|
||||
|
||||
export default function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<Data>
|
||||
) {
|
||||
res.status(200).json({ status: "Api up and running :)" });
|
||||
// Return a healthy 200 status code for uptime monitoring and render.com zero-downtime-deploy
|
||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
// A generic database access to make sure the service is healthy.
|
||||
const users = await prisma.user.findFirst();
|
||||
res.status(200).json({ message: "Api up and running :)" });
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
GET: Promise.resolve({ default: defaultResponder(getHandler) }),
|
||||
});
|
||||
|
||||
@ -1,42 +1,31 @@
|
||||
// POST to create
|
||||
import PrismaClient from "@documenso/prisma";
|
||||
import User from "@documenso/prisma";
|
||||
import {
|
||||
defaultHandler,
|
||||
defaultResponder,
|
||||
HttpError,
|
||||
} from "@documenso/lib/server";
|
||||
import prisma from "@documenso/prisma";
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { json } from "stream/consumers";
|
||||
|
||||
export default async function userHandler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<User>
|
||||
) {
|
||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { method, body } = req;
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
switch (method) {
|
||||
case "POST":
|
||||
if (!body.email) {
|
||||
res.status(400).end("Email cannot be empty.");
|
||||
}
|
||||
|
||||
try {
|
||||
let newUser: any;
|
||||
newUser = await prisma.user
|
||||
.create({
|
||||
data: { email: body.email },
|
||||
})
|
||||
.then(async () => {
|
||||
await prisma.$disconnect();
|
||||
res.status(200).send(newUser);
|
||||
});
|
||||
} catch (error) {
|
||||
await prisma.$disconnect();
|
||||
res.status(500).end("An error has occured. Error: " + error);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
res.status(405).end(`Method ${method} Not Allowed`);
|
||||
if (!body.email) {
|
||||
return res.status(400).json({ message: "Email cannot be empty." });
|
||||
}
|
||||
|
||||
let newUser: any;
|
||||
newUser = await prisma.user
|
||||
.create({
|
||||
data: { email: body.email },
|
||||
})
|
||||
.then(async () => {
|
||||
return res.status(201).send(newUser);
|
||||
});
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user