1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-10-16 15:07:27 +00:00
festa/utils/restInPeace.ts

320 lines
8.5 KiB
TypeScript
Raw Normal View History

2022-05-30 03:19:49 +00:00
import { NextApiRequest, NextApiResponse } from "next";
import { ApiError, ApiResult } from "../types/api";
2022-05-31 03:03:48 +00:00
// I don't know what the typing of a Prisma model is.
2022-05-31 10:00:55 +00:00
export type Model = any
2022-05-31 03:03:48 +00:00
type RestInPeaceOptions<T> = {
2022-05-30 03:19:49 +00:00
/**
* Prisma delegate to operate on.
*/
2022-05-31 03:03:48 +00:00
model: Model,
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "head" operation.
2022-05-30 03:19:49 +00:00
*/
2022-05-31 03:03:48 +00:00
head?: HeadOptions<T>
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "options" operation.
2022-05-30 03:19:49 +00:00
*/
2022-05-31 03:03:48 +00:00
options?: OptionsOptions<T>
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "list" operation.
2022-05-30 03:19:49 +00:00
*/
2022-05-31 03:03:48 +00:00
list?: ListOptions<T>
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "retrieve" operation.
2022-05-30 03:19:49 +00:00
*/
2022-05-31 03:03:48 +00:00
retrieve?: RetrieveOptions<T>
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "create" operation.
2022-05-30 03:19:49 +00:00
*/
2022-05-31 03:03:48 +00:00
create?: CreateOptions<T>
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "upsert" operation.
2022-05-30 03:19:49 +00:00
*/
2022-05-31 03:03:48 +00:00
upsert?: UpsertOptions<T>
2022-05-30 03:19:49 +00:00
/**
2022-05-31 03:03:48 +00:00
* Options for the "update" operation.
*/
update?: UpdateOptions<T>
/**
* Options for the "destroy" operation.
*/
destroy?: DestroyOptions<T>
2022-05-30 03:19:49 +00:00
}
/**
* Handle an API route in a [REST](https://en.wikipedia.org/wiki/Representational_state_transfer)ful way.
*/
2022-05-31 03:03:48 +00:00
export async function restInPeace<T>(req: NextApiRequest, res: NextApiResponse<ApiResult<T>>, options: RestInPeaceOptions<T>) {
2022-05-30 03:19:49 +00:00
// Handle HEAD by returning an empty body
2022-05-31 03:03:48 +00:00
if (options.head && req.method === "HEAD") {
return await handleHead(res, options.model, options.head)
2022-05-30 03:19:49 +00:00
}
// Same thing for OPTIONS, but beware of weird CORS things!
2022-05-31 03:03:48 +00:00
else if (options.options && req.method === "OPTIONS") {
return await handleOptions(res, options.model, options.options)
2022-05-30 03:19:49 +00:00
}
// GET can be both "list" and "retrieve"
2022-05-31 03:03:48 +00:00
else if (options.list && options.list.where && req.method === "GET") {
return await handleList(res, options.model, options.list)
}
else if(options.retrieve && options.retrieve.which && req.method === "GET") {
return await handleRetrieve(res, options.model, options.retrieve)
2022-05-30 03:19:49 +00:00
}
// POST is always "create"
2022-05-31 03:03:48 +00:00
else if (options.create && req.method === "POST") {
return await handleCreate(res, options.model, options.create)
2022-05-30 03:19:49 +00:00
}
// PUT is always "upsert"
2022-05-31 03:03:48 +00:00
else if (options.upsert && options.upsert.which && req.method === "PUT") {
return await handleUpsert(res, options.model, options.upsert)
2022-05-30 03:19:49 +00:00
}
// PATCH is always "update"
2022-05-31 03:03:48 +00:00
else if (options.update && options.update.which && req.method === "PATCH") {
return await handleUpdate(res, options.model, options.update)
2022-05-30 03:19:49 +00:00
}
// DELETE is always "destroy"
2022-05-31 03:03:48 +00:00
else if (options.destroy && options.destroy.which && req.method === "DELETE") {
return await handleDestroy(res, options.model, options.destroy)
2022-05-30 03:19:49 +00:00
}
// What kind of weird HTTP methods are you using?!
else {
2022-05-31 03:03:48 +00:00
return res.status(405).json({ error: "Method not allowed" })
2022-05-30 03:19:49 +00:00
}
}
2022-05-31 03:03:48 +00:00
interface OperationOptions<T> {
2022-05-31 10:00:55 +00:00
before?: (model: T, obj?: any) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T, obj?: any) => Promise<any>,
2022-05-30 03:19:49 +00:00
}
2022-05-31 03:03:48 +00:00
// === HEAD ===
2022-05-30 03:19:49 +00:00
2022-05-31 03:03:48 +00:00
interface HeadOptions<T> extends OperationOptions<T> {
2022-05-31 10:00:55 +00:00
before?: (model: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T) => Promise<void>,
2022-05-30 03:19:49 +00:00
}
/**
* Handle an `HEAD` HTTP request.
*/
2022-05-31 03:03:48 +00:00
async function handleHead<T>(res: NextApiResponse<"">, model: Model, options: HeadOptions<T>) {
await options.before?.(model)
await options.after?.(model)
2022-05-30 03:19:49 +00:00
return res.status(200).send("")
}
2022-05-31 03:03:48 +00:00
// === OPTIONS ===
interface OptionsOptions<T> extends OperationOptions<T> {
2022-05-31 10:00:55 +00:00
before?: (model: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T) => Promise<void>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle an `OPTIONS` HTTP request.
*/
2022-05-31 03:03:48 +00:00
async function handleOptions<T>(res: NextApiResponse<"">, model: Model, options: OptionsOptions<T>) {
await options.before?.(model)
await options.after?.(model)
2022-05-30 03:19:49 +00:00
return res.status(200).send("")
}
2022-05-31 03:03:48 +00:00
// === LIST ===
interface ListOptions<T> extends OperationOptions<T> {
/**
* Prisma Where clause used to list objects available in a API route.
*/
where?: object,
2022-05-31 10:00:55 +00:00
before?: (model: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T, obj: T[]) => Promise<T[]>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle a `GET` HTTP request where a list of items is requested.
*/
2022-05-31 03:03:48 +00:00
async function handleList<T>(res: NextApiResponse<ApiResult<T>>, model: Model, options: ListOptions<T>) {
await options.before?.(model)
const objs = await model.findMany({ where: options.where })
const mutatedObjs = await options.after?.(model, objs) ?? objs
2022-05-30 03:19:49 +00:00
return res.status(200).json(mutatedObjs)
}
2022-05-31 03:03:48 +00:00
// === RETRIEVE ===
interface RetrieveOptions<T> extends OperationOptions<T> {
/**
* Prisma Where clause used to select the object to display.
*
* See also `findUnique`.
*/
which?: object,
2022-05-31 10:00:55 +00:00
before?: (model: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T, obj: T) => Promise<T>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle a `GET` HTTP request where a single item is requested.
*/
2022-05-31 03:03:48 +00:00
async function handleRetrieve<T>(res: NextApiResponse<ApiResult<T>>, model: Model, options: RetrieveOptions<T>) {
await options.before?.(model)
const obj = await model.findUnique({ where: options.which })
const mutatedObj = await options.after?.(model, obj) ?? obj
if (!mutatedObj) {
2022-05-30 03:19:49 +00:00
return res.status(404).json({ error: "Not found" })
}
return res.status(200).json(mutatedObj)
}
2022-05-31 03:03:48 +00:00
// === CREATE ===
interface CreateOptions<T> extends OperationOptions<T> {
/**
* Prisma Create clause used to create the object.
*/
create: object,
2022-05-31 10:00:55 +00:00
before?: (model: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T, obj: T) => Promise<T>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle a `POST` HTTP request where a single item is created.
*/
2022-05-31 03:03:48 +00:00
async function handleCreate<T>(res: NextApiResponse<ApiResult<T>>, model: Model, options: CreateOptions<T>) {
await options.before?.(model)
const obj = await model.create({ data: options.create })
const mutatedObj = await options.after?.(model, obj) ?? obj
2022-05-30 03:19:49 +00:00
return res.status(200).json(mutatedObj)
}
2022-05-31 03:03:48 +00:00
// === UPSERT ===
interface UpsertOptions<T> extends OperationOptions<T> {
/**
* Prisma Where clause used to select the object to operate on.
*
* See also `findUnique`.
*/
which?: object,
/**
* Prisma Create clause used to create the object if it doesn't exist.
*/
create: object,
/**
* Prisma Update clause used to update the object if it exists.
*/
update: object,
2022-05-31 10:00:55 +00:00
before?: (model: T, obj?: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T, obj: T) => Promise<T>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle a `PUT` HTTP request where a single item is either created or updated.
*/
2022-05-31 03:03:48 +00:00
async function handleUpsert<T>(res: NextApiResponse<ApiResult<T>>, model: Model, options: UpsertOptions<T>) {
2022-05-31 10:00:55 +00:00
const initialObj = await model.findUnique({ where: options.which })
await options.before?.(model, initialObj)
2022-05-31 03:03:48 +00:00
const obj = await model.upsert({
where: options.which,
2022-05-30 03:19:49 +00:00
create: options.create,
update: options.update,
})
2022-05-31 03:03:48 +00:00
const mutatedObj = await options.after?.(model, obj) ?? obj
2022-05-30 03:19:49 +00:00
return res.status(200).json(mutatedObj)
}
2022-05-31 03:03:48 +00:00
// === UPDATE ===
interface UpdateOptions<T> extends OperationOptions<T> {
/**
* Prisma Where clause used to select the object to operate on.
*
* See also `findUnique`.
*/
which?: object,
/**
* Prisma Update clause used to update the object if it exists.
*/
update: object,
2022-05-31 10:00:55 +00:00
before?: (model: T, obj?: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T, obj: T) => Promise<T>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle a `PATCH` HTTP request where a single item is updated.
*/
2022-05-31 03:03:48 +00:00
async function handleUpdate<T>(res: NextApiResponse<ApiResult<T>>, model: Model, options: UpdateOptions<T>) {
2022-05-31 10:00:55 +00:00
const initialObj = await model.findUnique({ where: options.which })
await options.before?.(model, initialObj)
2022-05-31 03:03:48 +00:00
const obj = await model.update({
where: options.which,
2022-05-30 03:19:49 +00:00
data: options.update,
})
2022-05-31 03:03:48 +00:00
const mutatedObj = await options.after?.(model, obj) ?? obj
2022-05-30 03:19:49 +00:00
return res.status(200).json(mutatedObj)
}
2022-05-31 03:03:48 +00:00
// === DESTROY ===
interface DestroyOptions<T> extends OperationOptions<T> {
/**
* Prisma Where clause used to select the object to operate on.
*
* See also `findUnique`.
*/
which?: object,
2022-05-31 10:00:55 +00:00
before?: (model: T, obj?: T) => Promise<void>,
2022-05-31 03:03:48 +00:00
after?: (model: T) => Promise<void>,
}
2022-05-30 03:19:49 +00:00
/**
* Handle a `DELETE` HTTP request where a single item is destroyed.
*/
2022-05-31 03:03:48 +00:00
async function handleDestroy<T>(res: NextApiResponse<ApiResult<T>>, model: Model, options: DestroyOptions<T>) {
2022-05-31 10:00:55 +00:00
const initialObj = await model.findUnique({ where: options.which })
await options.before?.(model, initialObj)
2022-05-31 03:03:48 +00:00
await model.delete({
where: options.which,
2022-05-30 03:19:49 +00:00
})
2022-05-31 03:03:48 +00:00
await options.after?.(model)
2022-05-30 03:19:49 +00:00
return res.status(204).send("")
2022-05-31 03:03:48 +00:00
}