Skip to content

Commit

Permalink
feat: add trpc package
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 21, 2022
1 parent 773a222 commit 5aebe86
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 6 deletions.
3 changes: 2 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dependencies": {
"@egodb/core": "^0.0.0",
"@egodb/domain": "^0.0.0",
"@egodb/trpc": "workspace:^0.0.0",
"@nestjs/common": "^9.2.0",
"@nestjs/core": "^9.2.0",
"@nestjs/cqrs": "^9.0.1",
Expand Down Expand Up @@ -59,7 +60,7 @@
"private": true,
"scripts": {
"build": "nest build",
"dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch",
"dev": "nest start --watch --preserveWatchOutput",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"prebuild": "rimraf dist",
"start": "nest start",
Expand Down
18 changes: 18 additions & 0 deletions apps/backend/src/modules/table/ports/trpc/table-trpc.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AppRouter, createExpressMiddleware, createRouter } from '@egodb/trpc'
import { Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common'
import { CommandBus } from '@nestjs/cqrs'
import { NextFunction, Request, Response } from 'express'

@Injectable()
export class TableTrpcMiddleware implements OnModuleInit, NestMiddleware {
constructor(private readonly commandBus: CommandBus) {}

private appRouter!: AppRouter
onModuleInit() {
this.appRouter = createRouter(this.commandBus)
}

use(req: Request, res: Response, next: NextFunction) {
return createExpressMiddleware(this.appRouter)(req, res, next)
}
}
9 changes: 7 additions & 2 deletions apps/backend/src/modules/table/table.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Module } from '@nestjs/common'
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import { CqrsModule } from '@nestjs/cqrs'
import { dbAdapters } from './adapters'
import { commandHandlers } from './commands'
import { restfulControllers } from './ports/restful'
import { TableTrpcMiddleware } from './ports/trpc/table-trpc.middleware'

@Module({
imports: [CqrsModule],
controllers: [...restfulControllers],
providers: [...commandHandlers, ...dbAdapters],
})
export class TableModule {}
export class TableModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(TableTrpcMiddleware).forRoutes('/trpc')
}
}
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './commands'
export * from './repository'
export * from './table'
export * from './table.command-bus'
3 changes: 3 additions & 0 deletions packages/core/table.command-bus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ICommandBus } from '@egodb/domain'

export type ITableCommandBus = ICommandBus
5 changes: 5 additions & 0 deletions packages/domain/command-bus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Command } from './command'

export interface ICommandBus<TCommand extends Command = Command, TResult = any> {
execute(command: TCommand): Promise<TResult>
}
1 change: 1 addition & 0 deletions packages/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './command'
export * from './command-bus'
export * from './command-handler'
export * from './date.vo'
export * from './id.vo'
Expand Down
3 changes: 0 additions & 3 deletions packages/domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
"tsup": "^6.5.0",
"typescript": "^4.9.3"
},
"files": [
"dist"
],
"keywords": [],
"main": "./dist/index",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions packages/trpc/express-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { inferAsyncReturnType } from '@trpc/server'
import * as trpcExpress from '@trpc/server/adapters/express'
import { type Handler } from 'express'
import { AppRouter } from './router'

const createContext = ({ req, res }: trpcExpress.CreateExpressContextOptions) => ({}) // no context
type Context = inferAsyncReturnType<typeof createContext>

export const createExpressMiddleware = (appRouter: AppRouter): Handler => {
return trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
})
}
2 changes: 2 additions & 0 deletions packages/trpc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './express-context'
export * from './router'
28 changes: 28 additions & 0 deletions packages/trpc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@egodb/trpc",
"version": "0.0.0",
"description": "",
"files": [
"dist"
],
"keywords": [],
"main": "./dist/index",
"scripts": {
"build": "tsc",
"dev": "tsup --watch --onSuccess \"tsc --emitDeclarationOnly --declaration\"",
"prebuild": "rimraf dist",
"test": "echo \"Error: no test specified\" && exit 1"
},
"types": "./dist/index.d",
"dependencies": {
"@egodb/core": "workspace:^0.0.0",
"@trpc/server": "10.0.0-rc.8",
"zod": "^3.19.1"
},
"devDependencies": {
"@types/express": "^4.17.14",
"tsconfig": "workspace:^0.0.0",
"tsup": "^6.5.0",
"typescript": "^4.9.3"
}
}
13 changes: 13 additions & 0 deletions packages/trpc/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { router } from './trpc'

import { ITableCommandBus } from '@egodb/core/dist'
import { createTableRouter } from './table'

export const createRouter = (commandBus: ITableCommandBus) => {
const appRouter = router({
table: createTableRouter(commandBus),
})
return appRouter
}

export type AppRouter = ReturnType<typeof createRouter>
27 changes: 27 additions & 0 deletions packages/trpc/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CreateTableCommand, ITableCommandBus } from '@egodb/core/dist'
import { z } from 'zod'
import { publicProcedure, router } from './trpc'

export const createTableRouter = (commandBus: ITableCommandBus) =>
router({
create: publicProcedure
.input(
z.object({
name: z.string(),
}),
)
.mutation(({ input }) => {
const cmd = new CreateTableCommand({ name: input.name })
return commandBus.execute(cmd)
}),
get: publicProcedure
.input(
z.object({
id: z.string(),
}),
)
.query(({ input }) => {
const cmd = new CreateTableCommand({ name: input.id })
return commandBus.execute(cmd)
}),
})
6 changes: 6 additions & 0 deletions packages/trpc/trpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { initTRPC } from '@trpc/server'
const t = initTRPC.create()

export const middleware = t.middleware
export const router = t.router
export const publicProcedure = t.procedure
9 changes: 9 additions & 0 deletions packages/trpc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "tsconfig/lib.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "."
}
}
10 changes: 10 additions & 0 deletions packages/trpc/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['index.ts'],
outDir: 'dist',
splitting: true,
sourcemap: true,
clean: true,
dts: true,
})
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5aebe86

Please sign in to comment.