Skip to content

Commit

Permalink
refactor(server): Renamed .func() to .handler()
Browse files Browse the repository at this point in the history
  • Loading branch information
unteifu committed Dec 27, 2024
1 parent 11a25ed commit a7cb1b0
Show file tree
Hide file tree
Showing 76 changed files with 281 additions and 281 deletions.
2 changes: 1 addition & 1 deletion apps/content/content/docs/client/react-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const router = {
user: {
list: os
.input(z.object({ cursor: z.number(), limit: z.number() }))
.func((input) => ({
.handler((input) => ({
nextCursor: input.cursor + input.limit,
users: [], // Fetch your actual data here
})),
Expand Down
2 changes: 1 addition & 1 deletion apps/content/content/docs/client/react.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const router = {
user: {
list: os
.input(z.object({ cursor: z.number(), limit: z.number() }))
.func((input) => {
.handler((input) => {
return {
nextCursor: input.cursor + input.limit,
users: []
Expand Down
2 changes: 1 addition & 1 deletion apps/content/content/docs/client/vue-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const router = {
user: {
list: os
.input(z.object({ cursor: z.number(), limit: z.number() }))
.func((input) => ({
.handler((input) => ({
nextCursor: input.cursor + input.limit,
users: [], // Fetch your actual data here
})),
Expand Down
6 changes: 3 additions & 3 deletions apps/content/content/docs/contract-first.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,23 @@ export const authed /** require authed */ = os
.contract(contract)

export const router = pub.router({
getting: pub.getting.func((input, context, meta) => {
getting: pub.getting.handler((input, context, meta) => {
return {
message: `Hello, ${input.name}!`,
}
}),

post: {
find: pub.post.find
.func((input, context, meta) => {
.handler((input, context, meta) => {
return {
id: 'example',
title: 'example',
description: 'example',
}
}),

create: authed.post.create.func((input, context, meta) => {
create: authed.post.create.handler((input, context, meta) => {
return {
id: 'example',
title: input.title,
Expand Down
6 changes: 3 additions & 3 deletions apps/content/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const router = pub.router({
name: z.string(),
}),
)
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
return {
message: `Hello, ${input.name}!`,
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export const router = pub.router({

return result
})
.func((input, context, meta) => {
.handler((input, context, meta) => {
return {
id: 'example',
title: 'example',
Expand All @@ -121,7 +121,7 @@ export const router = pub.router({
thumb: oz.file().type('image/*'),
}),
)
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
input.thumb // file upload out of the box

return {
Expand Down
6 changes: 3 additions & 3 deletions apps/content/content/docs/openapi/generator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const router = pub.router({
name: z.string(),
}),
)
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
return {
message: `Hello, ${input.name}!`,
}
Expand All @@ -53,7 +53,7 @@ export const router = pub.router({
description: z.string(),
}),
)
.func((input, context, meta) => {
.handler((input, context, meta) => {
return {
id: 'example',
title: 'example',
Expand All @@ -69,7 +69,7 @@ export const router = pub.router({
thumb: oz.file().type('image/*'),
}),
)
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
input.thumb // file upload out of the box

return {
Expand Down
10 changes: 5 additions & 5 deletions apps/content/content/docs/server/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import { os, createProcedureClient } from '@orpc/server'
import { z } from 'zod'

// ❌ Cannot call this procedure directly because undefined is not assignable to 'Context'
const e1 = os.context<{ auth: boolean }>().func(() => 'pong')
const e1 = os.context<{ auth: boolean }>().handler(() => 'pong')
// @errors: 2349
e1() // Error: Procedure 'e1' cannot be called directly

// ✅ Can call this procedure directly because undefined is assignable to 'Context'
const e2 = os.context<{ auth: boolean } | undefined>().func(() => 'pong')
const e2 = os.context<{ auth: boolean } | undefined>().handler(() => 'pong')
const o2 = await e2() // Ok, output is 'pong'

// ✅ Can call this procedure directly because undefined is assignable to 'Context'
const getting = os.input(z.object({ name: z.string() })).func(({ name }) => `Hello, ${name}!`)
const getting = os.input(z.object({ name: z.string() })).handler(({ name }) => `Hello, ${name}!`)

const router = os.router({
getting
Expand All @@ -44,7 +44,7 @@ import { os, createProcedureClient } from '@orpc/server'

type Context = { user?: { id: string } }

const getting = os.context<Context>().func(() => 'pong')
const getting = os.context<Context>().handler(() => 'pong')

const gettingClient = createProcedureClient({
procedure: getting,
Expand All @@ -68,7 +68,7 @@ To call multiple procedures with shared context, use a `Router Client`.
import { os, createRouterClient } from '@orpc/server'

const router = os.router({
ping: os.func(() => 'pong')
ping: os.handler(() => 'pong')
})

const client = createRouterClient({
Expand Down
6 changes: 3 additions & 3 deletions apps/content/content/docs/server/context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const pub = os.context<ORPCContext>().use(async (input, context, meta) => {
})

export const router = pub.router({
getting: pub.func((input, context, meta) => {
getting: pub.handler((input, context, meta) => {
// ^ context is combined from `initial context` and `middleware context`
}),
})
Expand Down Expand Up @@ -74,7 +74,7 @@ const authMid = base.middleware(async (input, context, meta) => {
export const router = base.router({
getting: base
.use(authMid)
.func((input, context, meta) => {
.handler((input, context, meta) => {
// ^ context is fully typed
}),
})
Expand Down Expand Up @@ -123,7 +123,7 @@ const authMid = base.middleware((input, context, meta) => {
export const router = base.router({
getting: base
.use(authMid)
.func((input, context, meta) => {
.handler((input, context, meta) => {
// ^ context is fully typed
}),
})
Expand Down
6 changes: 3 additions & 3 deletions apps/content/content/docs/server/contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ export const authed /** require authed */ = base
.contract(contract)

export const router = pub.router({
getting: pub.getting.func((input, context, meta) => {
getting: pub.getting.handler((input, context, meta) => {
return {
message: `Hello, ${input.name}!`,
}
}),

post: {
find: pub.post.find
.func((input, context, meta) => {
.handler((input, context, meta) => {
return {
id: 'example',
title: 'example',
description: 'example',
}
}),

create: authed.post.create.func((input, context, meta) => {
create: authed.post.create.handler((input, context, meta) => {
return {
id: 'example',
title: input.title,
Expand Down
2 changes: 1 addition & 1 deletion apps/content/content/docs/server/error-handling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ping = os
// do something on finish
}
})
.func((input, context, meta) => {
.handler((input, context, meta) => {
throw new ORPCError({
code: 'NOT_FOUND',
message: 'Not found',
Expand Down
4 changes: 2 additions & 2 deletions apps/content/content/docs/server/file-upload.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { z } from 'zod'

export const uploadFile = os.input(
z.object({ file: z.instanceof(File) })
).func(async (input) => {
).handler(async (input) => {
const file: File = input.file
// Handle the file as needed, e.g., save to storage, process, etc.
})
Expand All @@ -45,7 +45,7 @@ import { z } from 'zod'

export const uploadFile = os.input(
z.object({ file: oz.file().type('image/*') })
).func(async (input) => {
).handler(async (input) => {
const image: File = input.file
// Process the image file as needed
})
Expand Down
2 changes: 1 addition & 1 deletion apps/content/content/docs/server/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const authed = pub
}
})
})
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {

const _expect1: NonNullable<typeof context['user']> = context.user
const _expect2: string = context.say
Expand Down
2 changes: 1 addition & 1 deletion apps/content/content/docs/server/procedure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const findUser = pub
})

// Define handler with business logic
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
// Implement your business logic here

return {
Expand Down
2 changes: 1 addition & 1 deletion apps/content/content/docs/server/router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A router is a collection of procedures with utilities that help reduce code dupl
```ts twoslash
import { os } from '@orpc/server'

const findUser = os.route({path: '/'}).func(() => {})
const findUser = os.route({path: '/'}).handler(() => {})

export const appRouter = os.router({
user: {
Expand Down
4 changes: 2 additions & 2 deletions apps/content/content/docs/server/server-action.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const updateUser = os
}),
}),
)
.func((input, context, meta) => {
.handler((input, context, meta) => {
// ^ context.user is automatically injected
redirect('/some-where') // or return some thing
})
Expand Down Expand Up @@ -189,7 +189,7 @@ type Context = { user?: { id: string } }
const getting = os
.context<Context>()
.input(z.object({ name: z.string() }))
.func(({ name }) => `Hello, ${name}!`)
.handler(({ name }) => `Hello, ${name}!`)

// @errors: 2349
getting({ name: 'Unnoq' }) // ❌ cannot call this procedure directly, and cannot be used as a server action
Expand Down
8 changes: 4 additions & 4 deletions apps/content/content/home/landing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export const getting = os
}))
.use(canMiddleware, (i) => i.id) // permission check by id
.output(z.string()) // validate output
.func(async (input) => 'Name and Avatar has been updated')
.handler(async (input) => 'Name and Avatar has been updated')
```

> Only the `.func` method is required. All other chain methods are optional.
> Only the `.handler` method is required. All other chain methods are optional.
>
With [Middleware](/docs/server/middleware) and the [Procedure Builder](/docs/server/procedure),
Expand Down Expand Up @@ -143,7 +143,7 @@ const getting = pub
}
})
})
.func((input, context, meta) => {
.handler((input, context, meta) => {
// ^ context.user is now guaranteed to be defined
})
```
Expand All @@ -160,7 +160,7 @@ const gettingContract = oc

const getting = os
.contract(gettingContract)
.func(async () => 'Worked')
.handler(async () => 'Worked')
```

With [oRPC's Contract First Development](/docs/server/contract), you can easily separate the procedure's definition
Expand Down
6 changes: 3 additions & 3 deletions apps/content/examples/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const authed /** require authed */ = base
.contract(contract)

export const router = pub.router({
getting: pub.getting.func((input, context, meta) => {
getting: pub.getting.handler((input, context, meta) => {
return {
message: `Hello, ${input.name}!`,
}
Expand All @@ -95,15 +95,15 @@ export const router = pub.router({

return result
})
.func((input, context, meta) => {
.handler((input, context, meta) => {
return {
id: 'example',
title: 'example',
description: 'example',
}
}),

create: authed.post.create.func((input, context, meta) => {
create: authed.post.create.handler((input, context, meta) => {
return {
id: 'example',
title: input.title,
Expand Down
2 changes: 1 addition & 1 deletion apps/content/examples/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const editPost = authed
const _output = result.output
return result
})
.func(() => 'Edited')
.handler(() => 'Edited')

//
//
2 changes: 1 addition & 1 deletion apps/content/examples/server-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const updateUser = os
}),
}),
)
.func((input, context, meta) => {
.handler((input, context, meta) => {
// ^ context.user is automatically injected
// do something
})
Expand Down
6 changes: 3 additions & 3 deletions apps/content/examples/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const router = pub.router({
name: z.string(),
}),
)
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
return {
message: `Hello, ${input.name}!`,
}
Expand Down Expand Up @@ -57,7 +57,7 @@ export const router = pub.router({

return result
})
.func((input, context, meta) => {
.handler((input, context, meta) => {
return {
id: 'example',
title: 'example',
Expand All @@ -73,7 +73,7 @@ export const router = pub.router({
thumb: oz.file().type('image/*'),
}),
)
.func(async (input, context, meta) => {
.handler(async (input, context, meta) => {
const _thumb = input.thumb // file upload out of the box

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/router-fetch-client.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('router fetch client', () => {
},
})

const ping = os.contract(pingContract).func(name => `ping ${name}`)
const pong = os.contract(pongContract).func(num => `pong ${num}`)
const ping = os.contract(pingContract).handler(name => `ping ${name}`)
const pong = os.contract(pongContract).handler(num => `pong ${num}`)

const router = os.contract(contractRouter).router({
ping,
Expand Down
Loading

0 comments on commit a7cb1b0

Please sign in to comment.