refactor: update trpc entry style
This commit is contained in:
parent
044fd94e30
commit
d20aedda3f
@ -1,16 +1,6 @@
|
|||||||
import * as trpcExpress from '@trpc/server/adapters/express';
|
import * as trpcExpress from '@trpc/server/adapters/express';
|
||||||
import { createContext, router } from './trpc';
|
import { createContext, router } from './trpc';
|
||||||
import { notificationRouter } from './routers/notification';
|
import { appRouter } from './routers';
|
||||||
import { websiteRouter } from './routers/website';
|
|
||||||
import { monitorRouter } from './routers/monitor';
|
|
||||||
|
|
||||||
const appRouter = router({
|
|
||||||
website: websiteRouter,
|
|
||||||
notification: notificationRouter,
|
|
||||||
monitor: monitorRouter,
|
|
||||||
});
|
|
||||||
|
|
||||||
export type AppRouter = typeof appRouter;
|
|
||||||
|
|
||||||
export const trpcExpressMiddleware = trpcExpress.createExpressMiddleware({
|
export const trpcExpressMiddleware = trpcExpress.createExpressMiddleware({
|
||||||
router: appRouter,
|
router: appRouter,
|
||||||
|
12
src/server/trpc/routers/index.ts
Normal file
12
src/server/trpc/routers/index.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { router } from '../trpc';
|
||||||
|
import { notificationRouter } from './notification';
|
||||||
|
import { websiteRouter } from './website';
|
||||||
|
import { monitorRouter } from './monitor';
|
||||||
|
|
||||||
|
export const appRouter = router({
|
||||||
|
website: websiteRouter,
|
||||||
|
notification: notificationRouter,
|
||||||
|
monitor: monitorRouter,
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AppRouter = typeof appRouter;
|
@ -1,25 +1,16 @@
|
|||||||
import { initTRPC, inferAsyncReturnType, TRPCError } from '@trpc/server';
|
import { initTRPC, inferAsyncReturnType, TRPCError } from '@trpc/server';
|
||||||
import * as trpcExpress from '@trpc/server/adapters/express';
|
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { jwtVerify } from '../middleware/auth';
|
import { jwtVerify } from '../middleware/auth';
|
||||||
import { getWorkspaceUser } from '../model/workspace';
|
import { getWorkspaceUser } from '../model/workspace';
|
||||||
import { ROLES, SYSTEM_ROLES } from '../utils/const';
|
import { ROLES, SYSTEM_ROLES } from '../utils/const';
|
||||||
|
import type { IncomingMessage } from 'http';
|
||||||
|
|
||||||
export function createContext({
|
export function createContext({ req }: { req: IncomingMessage }) {
|
||||||
req,
|
|
||||||
res,
|
|
||||||
}: trpcExpress.CreateExpressContextOptions) {
|
|
||||||
const authorization = req.headers['authorization'] ?? '';
|
const authorization = req.headers['authorization'] ?? '';
|
||||||
const token = authorization.replace('Bearer ', '');
|
const token = authorization.replace('Bearer ', '');
|
||||||
|
|
||||||
try {
|
return { token };
|
||||||
const user = jwtVerify(token);
|
|
||||||
|
|
||||||
return { user };
|
|
||||||
} catch (err) {
|
|
||||||
throw new TRPCError({ code: 'UNAUTHORIZED' });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Context = inferAsyncReturnType<typeof createContext>;
|
type Context = inferAsyncReturnType<typeof createContext>;
|
||||||
@ -29,8 +20,31 @@ export const middleware = t.middleware;
|
|||||||
export const router = t.router;
|
export const router = t.router;
|
||||||
export const publicProcedure = t.procedure;
|
export const publicProcedure = t.procedure;
|
||||||
|
|
||||||
const isSystemAdmin = middleware(async (opts) => {
|
const isUser = middleware(async (opts) => {
|
||||||
|
const token = opts.ctx.token;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'NoToken' });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = jwtVerify(token);
|
||||||
|
|
||||||
|
return opts.next({
|
||||||
|
ctx: {
|
||||||
|
user,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'TokenInvalid' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const protectProedure = t.procedure.use(isUser);
|
||||||
|
|
||||||
|
const isSystemAdmin = isUser.unstable_pipe(async (opts) => {
|
||||||
const { ctx, input } = opts;
|
const { ctx, input } = opts;
|
||||||
|
|
||||||
if (ctx.user.role !== SYSTEM_ROLES.admin) {
|
if (ctx.user.role !== SYSTEM_ROLES.admin) {
|
||||||
throw new TRPCError({ code: 'FORBIDDEN' });
|
throw new TRPCError({ code: 'FORBIDDEN' });
|
||||||
}
|
}
|
||||||
@ -39,14 +53,14 @@ const isSystemAdmin = middleware(async (opts) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const systemAdminProcedure = t.procedure.use(isSystemAdmin);
|
export const systemAdminProcedure = t.procedure.use(isSystemAdmin);
|
||||||
export const workspaceProcedure = t.procedure
|
export const workspaceProcedure = protectProedure
|
||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
workspaceId: z.string().uuid(),
|
workspaceId: z.string().uuid(),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.use(createWorkspacePermissionMiddleware());
|
.use(createWorkspacePermissionMiddleware());
|
||||||
export const workspaceOwnerProcedure = t.procedure
|
export const workspaceOwnerProcedure = protectProedure
|
||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
workspaceId: z.string().uuid(),
|
workspaceId: z.string().uuid(),
|
||||||
@ -58,7 +72,7 @@ export const workspaceOwnerProcedure = t.procedure
|
|||||||
* Create a trpc middleware which help user check workspace permission
|
* Create a trpc middleware which help user check workspace permission
|
||||||
*/
|
*/
|
||||||
function createWorkspacePermissionMiddleware(roles: ROLES[] = []) {
|
function createWorkspacePermissionMiddleware(roles: ROLES[] = []) {
|
||||||
return middleware(async (opts) => {
|
return isUser.unstable_pipe(async (opts) => {
|
||||||
const { ctx, input } = opts;
|
const { ctx, input } = opts;
|
||||||
|
|
||||||
const workspaceId = _.get(input, 'workspaceId', '');
|
const workspaceId = _.get(input, 'workspaceId', '');
|
||||||
|
Loading…
Reference in New Issue
Block a user