mirror of
https://github.com/Steffo99/festa.git
synced 2025-03-11 11:17:45 +00:00
Create some users API routes
This commit is contained in:
parent
a7c8ba72a1
commit
58430bc702
2 changed files with 112 additions and 0 deletions
63
pages/api/users/[id].ts
Normal file
63
pages/api/users/[id].ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { database } from "../../../utils/prismaClient";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { Event, Prisma, User } from "@prisma/client";
|
||||
import { festaAPI } from "../../../utils/api";
|
||||
import { festaNoConfig } from "../../../utils/api/configurator";
|
||||
import { festaBearerAuthOptional, festaNoAuth, FestaToken } from "../../../utils/api/authenticator";
|
||||
import { festaJsonSchemaBody, festaNoBody } from "../../../utils/api/bodyValidator";
|
||||
import { festaRESTSpecific } from "../../../utils/api/executor";
|
||||
import { festaJsonSchemaQuery } from "../../../utils/api/queryValidator";
|
||||
import { Response } from "../../../utils/api/throwables";
|
||||
|
||||
|
||||
type Config = {}
|
||||
|
||||
type Auth = {}
|
||||
|
||||
type Query = {
|
||||
id: string
|
||||
}
|
||||
|
||||
type Body = {}
|
||||
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
await festaAPI(req, res, {
|
||||
|
||||
configurator: festaNoConfig,
|
||||
|
||||
authenticator: festaNoAuth,
|
||||
|
||||
queryValidator: festaJsonSchemaQuery<Query>({
|
||||
type: "object",
|
||||
properties: {
|
||||
id: { type: "string" }
|
||||
},
|
||||
required: [
|
||||
"id",
|
||||
]
|
||||
}),
|
||||
|
||||
bodyValidator: festaNoBody,
|
||||
|
||||
executor: festaRESTSpecific<Config, Auth, Query, Body, User, Prisma.UserDelegate<any>, Prisma.UserFindUniqueArgs, Prisma.UserUpdateArgs>({
|
||||
delegate: database.user,
|
||||
|
||||
getFindArgs: async ({ query }) => {
|
||||
return {
|
||||
where: {
|
||||
id: query.id,
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getUpdateArgs: async ({ body, query }) => {
|
||||
throw Response.error({ status: 405, message: "This route cannot be used to edit users" })
|
||||
},
|
||||
|
||||
getDestroyArgs: async ({ query }) => {
|
||||
throw Response.error({ status: 405, message: "This route cannot be used to destroy users" })
|
||||
}
|
||||
}),
|
||||
})
|
||||
}
|
49
pages/api/users/me.ts
Normal file
49
pages/api/users/me.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
import { database } from "../../../utils/prismaClient";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { Event, Prisma } from "@prisma/client";
|
||||
import { festaNoConfig } from "../../../utils/api/configurator";
|
||||
import { festaBearerAuthRequired, FestaToken } from "../../../utils/api/authenticator";
|
||||
import { festaAPI } from "../../../utils/api";
|
||||
import { festaNoQuery } from "../../../utils/api/queryValidator";
|
||||
import { festaNoBody } from "../../../utils/api/bodyValidator";
|
||||
import { festaRESTGeneric } from "../../../utils/api/executor";
|
||||
import { Response } from "../../../utils/api/throwables";
|
||||
|
||||
|
||||
type Config = {}
|
||||
|
||||
type Auth = FestaToken
|
||||
|
||||
type Query = {}
|
||||
|
||||
type Body = {}
|
||||
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
await festaAPI(req, res, {
|
||||
|
||||
configurator: festaNoConfig,
|
||||
|
||||
authenticator: festaBearerAuthRequired,
|
||||
|
||||
queryValidator: festaNoQuery,
|
||||
|
||||
bodyValidator: festaNoBody,
|
||||
|
||||
executor: festaRESTGeneric<Config, Auth, Query, Body, Event, Prisma.UserDelegate<any>, Prisma.UserFindManyArgs, Prisma.UserCreateArgs>({
|
||||
delegate: database.user,
|
||||
|
||||
getListArgs: async ({ auth }) => {
|
||||
return {
|
||||
where: {
|
||||
id: auth.userId,
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getCreateArgs: async ({ }) => {
|
||||
throw Response.error({ status: 405, message: "This route cannot be used to create new users" })
|
||||
},
|
||||
}),
|
||||
})
|
||||
}
|
Loading…
Add table
Reference in a new issue