-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce tRPC server middleware (#36)
* feat: introduce tRPC server middleware * chore: add changeset
- Loading branch information
Showing
16 changed files
with
238 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hono/trpc-server': patch | ||
--- | ||
|
||
Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: ci-trpc-server | ||
on: | ||
push: | ||
branches: [main] | ||
paths: | ||
- 'packages/trpc-server/**' | ||
pull_request: | ||
branches: ['*'] | ||
paths: | ||
- 'packages/trpc-server/**' | ||
|
||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./packages/trpc-server | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: 18.x | ||
- run: yarn install --frozen-lockfile | ||
- run: yarn build | ||
- run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# tRPC Server Middleware for Hono | ||
|
||
tRPC Server Middleware adapts a [tRPC](https://trpc.io) server as middleware for Hono. | ||
Hono works on almost any JavaScript runtime, including Cloudflare Workers, Deno, and Bun. So, with this middleware, the same code will run as tRPC server. | ||
|
||
## Install | ||
|
||
``` | ||
npm install @hono/trpc-server | ||
``` | ||
|
||
## Usage | ||
|
||
Router: | ||
|
||
```ts | ||
import { initTRPC } from '@trpc/server' | ||
import { z } from 'zod' | ||
|
||
const t = initTRPC.create() | ||
|
||
const publicProcedure = t.procedure | ||
const router = t.router | ||
|
||
export const appRouter = router({ | ||
hello: publicProcedure.input(z.string().nullish()).query(({ input }) => { | ||
return `Hello ${input ?? 'World'}!` | ||
}), | ||
}) | ||
|
||
export type AppRouter = typeof appRouter | ||
``` | ||
Hono app using tRPC Server Middleware: | ||
```ts | ||
import { Hono } from 'hono' | ||
import { trpcServer } from '@hono/trpc-server' // Deno 'npm:@hono/trpc-server' | ||
import { appRouter } from './router' | ||
|
||
const app = new Hono() | ||
|
||
app.use( | ||
'/trpc/*', | ||
trpcServer({ | ||
router: appRouter, | ||
}) | ||
) | ||
|
||
export default app | ||
``` | ||
|
||
Client: | ||
|
||
```ts | ||
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client' | ||
import type { AppRouter } from './router' | ||
|
||
const client = createTRPCProxyClient<AppRouter>({ | ||
links: [ | ||
httpBatchLink({ | ||
url: 'http://localhost:8787/trpc', | ||
}), | ||
], | ||
}) | ||
|
||
console.log(await client.hello.query('Hono')) | ||
``` | ||
|
||
## Author | ||
|
||
Yusuke Wada <https://github.com/yusukebe> | ||
|
||
## License | ||
|
||
MIT |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@hono/trpc-server", | ||
"version": "0.0.0", | ||
"description": "tRPC Server Middleware for Hono", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/esm/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"test": "jest", | ||
"build:cjs": "tsc -p tsconfig.cjs.json", | ||
"build:esm": "tsc -p tsconfig.esm.json", | ||
"build": "rimraf dist && yarn build:cjs && yarn build:esm", | ||
"prerelease": "yarn build && yarn test", | ||
"release": "yarn publish" | ||
}, | ||
"license": "MIT", | ||
"private": false, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org", | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/honojs/middleware.git" | ||
}, | ||
"homepage": "https://honojs.dev", | ||
"peerDependencies": { | ||
"@trpc/server": "^10.10.0", | ||
"hono": "^2.7.5" | ||
}, | ||
"devDependencies": { | ||
"@trpc/server": "^10.10.0", | ||
"hono": "^2.7.5", | ||
"zod": "^3.20.2" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { AnyRouter } from '@trpc/server' | ||
import { fetchRequestHandler } from '@trpc/server/adapters/fetch' | ||
import type { MiddlewareHandler } from 'hono' | ||
|
||
type tRPCOptions = { | ||
endpoint?: string | ||
router: AnyRouter | ||
} | ||
|
||
export const trpcServer = ({ router, endpoint = '/trpc' }: tRPCOptions): MiddlewareHandler => { | ||
return async (c) => { | ||
const res = fetchRequestHandler({ | ||
endpoint: endpoint, | ||
req: c.req, | ||
router: router, | ||
}) | ||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { initTRPC } from '@trpc/server' | ||
import { Hono } from 'hono' | ||
import { z } from 'zod' | ||
import { trpcServer } from '../src' | ||
|
||
describe('tRPC Adapter Middleware', () => { | ||
const t = initTRPC.create() | ||
|
||
const publicProcedure = t.procedure | ||
const router = t.router | ||
|
||
const appRouter = router({ | ||
hello: publicProcedure.input(z.string().nullish()).query(({ input }) => { | ||
return `Hello ${input ?? 'World'}` | ||
}), | ||
}) | ||
|
||
const app = new Hono() | ||
|
||
app.use( | ||
'/trpc/*', | ||
trpcServer({ | ||
router: appRouter, | ||
}) | ||
) | ||
|
||
it('Should return 200 response', async () => { | ||
const searchParams = new URLSearchParams({ | ||
input: JSON.stringify({ '0': 'Hono' }), | ||
batch: '1', | ||
}) | ||
const req = new Request(`http://localhost/trpc/hello?${searchParams.toString()}`) | ||
const res = await app.request(req) | ||
expect(res.status).toBe(200) | ||
expect(await res.json()).toEqual([ | ||
{ | ||
result: { | ||
data: 'Hello Hono', | ||
}, | ||
}, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"declaration": false, | ||
"outDir": "./dist/cjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"declaration": true, | ||
"outDir": "./dist/esm" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters