Skip to content

Commit 911bdd9

Browse files
authored
feat(server, openapi)!: rewrite ORPCHandler, OpenAPIHandler (#74)
* wip * wip * orpc node * openapi * playgrounds * fixes * fix tsconfig * own fetch server * fix * update examples * update docs * update docs
1 parent 2528e17 commit 911bdd9

58 files changed

Lines changed: 698 additions & 1320 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/content/content/docs/client/react-query.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { ORPCLink } from '@orpc/client/fetch';
2020
import type { router } from 'examples/server';
2121

2222
const orpcLink = new ORPCLink({
23-
url: 'http://localhost:3000/api',
23+
url: 'http://localhost:3000/rpc',
2424
// fetch: optional override for the default fetch function
2525
// headers: provide additional headers
2626
})

apps/content/content/docs/client/vanilla.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ORPCLink } from '@orpc/client/fetch'
1919
import type { router } from 'examples/server'
2020

2121
const orpcLink = new ORPCLink({
22-
url: 'http://localhost:3000/api',
22+
url: 'http://localhost:3000/rpc',
2323
// fetch: optional override for the default fetch function
2424
// headers: provide additional headers
2525
})
@@ -51,7 +51,7 @@ type ClientContext = { cache?: RequestCache } | undefined
5151
// if context is not undefinable, it will require you pass context in every call
5252

5353
const orpcLink = new ORPCLink<ClientContext>({
54-
url: 'http://localhost:3000/api',
54+
url: 'http://localhost:3000/rpc',
5555
// headers: provide additional headers
5656
fetch: (input, init, context) => globalThis.fetch(input, {
5757
...init,
@@ -90,7 +90,7 @@ import { createORPCClient, DynamicLink, ORPCError } from '@orpc/client'
9090
import { ORPCLink } from '@orpc/client/fetch'
9191

9292
const orpcLink1 = new ORPCLink({
93-
url: 'http://localhost:3000/api',
93+
url: 'http://localhost:3000/rpc',
9494
// headers: provide additional headers
9595
})
9696

apps/content/content/docs/client/vue-colada.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ORPCLink } from '@orpc/client/fetch';
1818
import type { router } from 'examples/server';
1919

2020
const orpcLink = new ORPCLink({
21-
url: 'http://localhost:3000/api',
21+
url: 'http://localhost:3000/rpc',
2222
// fetch: optional override for the default fetch function
2323
// headers: provide additional headers
2424
});

apps/content/content/docs/client/vue-query.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ORPCLink } from '@orpc/client/fetch';
1818
import type { router } from 'examples/server';
1919

2020
const orpcLink = new ORPCLink({
21-
url: 'http://localhost:3000/api',
21+
url: 'http://localhost:3000/rpc',
2222
// fetch: optional override for the default fetch function
2323
// headers: provide additional headers
2424
});

apps/content/content/docs/contract-first.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ import { ORPCLink } from '@orpc/client/fetch'
139139
import type { contract } from 'examples/contract'
140140

141141
const orpcLink = new ORPCLink({
142-
url: 'https://localhost:3000/prefix',
142+
url: 'https://localhost:3000/rpc',
143143
// fetch: optional override for the default fetch function
144144
// headers: provide additional headers
145145
})

apps/content/content/docs/index.mdx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ In oRPC middleware is very useful and fully typed you can find more info [here](
146146
## Start Your Server
147147
148148
```ts twoslash
149-
import { ORPCHandler, CompositeHandler } from '@orpc/server/node'
149+
import { ORPCHandler } from '@orpc/server/node'
150150
import { OpenAPIServerlessHandler } from '@orpc/openapi/node'
151151
import { createServer } from 'node:http'
152152
import { router } from 'examples/server'
@@ -157,15 +157,30 @@ const openapiHandler = new OpenAPIServerlessHandler(router, {
157157
new ZodCoercer(),
158158
],
159159
})
160+
160161
const orpcHandler = new ORPCHandler(router)
161-
const compositeHandler = new CompositeHandler([openapiHandler, orpcHandler])
162162

163163
const server = createServer(async (req, res) => {
164164
if (req.url?.startsWith('/api')) {
165-
return compositeHandler.handle(req, res, {
165+
const { matched } = await openapiHandler.handle(req, res, {
166166
prefix: '/api',
167167
context: {},
168168
})
169+
170+
if(matched){
171+
return
172+
}
173+
}
174+
175+
if(req.url?.startsWith('/rpc')){
176+
const { matched } = await orpcHandler.handle(req, res, {
177+
prefix: '/rpc',
178+
context: {},
179+
})
180+
181+
if(matched){
182+
return
183+
}
169184
}
170185

171186
res.statusCode = 404
@@ -190,7 +205,7 @@ import { ORPCLink } from '@orpc/client/fetch'
190205
import type { router } from 'examples/server'
191206

192207
const orpcLink = new ORPCLink({
193-
url: 'http://localhost:3000/api',
208+
url: 'http://localhost:3000/rpc',
194209
// fetch: optional override for the default fetch function
195210
// headers: provide additional headers
196211
})

apps/content/content/docs/server/context.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ import { OpenAPIServerlessHandler, OpenAPIServerHandler } from '@orpc/openapi/fe
8787

8888
const orpcHandler = new ORPCHandler(router)
8989

90-
export function fetch(request: Request) {
90+
export async function fetch(request: Request) {
9191
// No need to pass context; middleware handles it
92-
return orpcHandler.fetch(request)
92+
const { response } = await orpcHandler.handle(request)
93+
94+
return response ?? new Response('Not found', { status: 404 })
9395
}
9496
```
9597

@@ -130,12 +132,14 @@ export const router = base.router({
130132

131133
const orpcHandler = new ORPCHandler(router)
132134

133-
export function fetch(request: Request) {
135+
export async function fetch(request: Request) {
134136
// Initialize context explicitly for each request
135137
const db = 'fake-db' as const
136138
const user = request.headers.get('Authorization') ? { id: 'example' } : undefined
137139

138-
return orpcHandler.fetch(request, { context: { db, user } })
140+
const { response } = await orpcHandler.handle(request, { context: { db, user } })
141+
142+
return response ?? new Response('Not Found', { status: 404 })
139143
}
140144

141145
// If you want to call this procedure or use as server action

apps/content/content/docs/server/file-upload.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import { createORPCClient } from '@orpc/client'
6767
import { ORPCLink } from '@orpc/client/fetch'
6868

6969
const orpcLink = new ORPCLink({
70-
url: 'http://localhost:3000/api',
70+
url: 'http://localhost:3000/rpc',
7171
// fetch: optional override for the default fetch function
7272
// headers: provide additional headers
7373
})

0 commit comments

Comments
 (0)