Skip to content

Commit

Permalink
docs: update global os var names
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Nov 20, 2024
1 parent d5b07fc commit 58313c5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 53 deletions.
11 changes: 6 additions & 5 deletions apps/content/content/docs/contract-first.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,18 @@ With your contract defined, implement the server logic:
import { os, ORPCError } from '@orpc/server'
import { contract } from 'examples/contract'

export const osw /** os with ... */ = os.contract(contract) // Ensure every implement must be match contract
export const pub /** public access */ = os.contract(contract) // Ensure every implement must be match contract
export const authed /** require authed */ = os.use(() => {/* auth logic */}).contract(contract)

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

post: {
find: osw.post.find
find: pub.post.find
.handler((input, context, meta) => {
return {
id: 'example',
Expand All @@ -105,7 +106,7 @@ export const router = osw.router({
}
}),

create: osw.post.create.handler((input, context, meta) => {
create: authed.post.create.handler((input, context, meta) => {
return {
id: 'example',
title: input.title,
Expand Down
12 changes: 7 additions & 5 deletions apps/content/content/docs/contract/builder.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ import { os } from '@orpc/server'
import { contract } from 'examples/contract'

export type Context = { user?: { id: string } }
export const osw /** os with ... */ = os.context<Context>().contract(contract) // Ensure every implement must be match contract
export const base = os.context<Context>()
export const pub /** public access */ = base.contract(contract) // Ensure every implement must be match contract
export const authed /** require authed */ = base.use(() => {/* auth logic */}).contract(contract)

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

post: {
find: osw.post.find
find: pub.post.find
.handler((input, context, meta) => {
return {
id: 'example',
Expand All @@ -100,7 +102,7 @@ export const router = osw.router({
}
}),

create: osw.post.create.handler((input, context, meta) => {
create: authed.post.create.handler((input, context, meta) => {
return {
id: 'example',
title: input.title,
Expand Down
11 changes: 6 additions & 5 deletions apps/content/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ import { z } from 'zod'

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

// global osw completely optional, needed when you want to use context
export const osw /** os with ... */ = os.context<Context>()
// global pub, authed completely optional
export const pub /** public access */ = os.context<Context>()
export const authed /** require authed */ = pub.use(() => {/* auth logic */})

export const router = osw.router({
export const router = pub.router({
getting: os
.input(
z.object({
Expand All @@ -74,7 +75,7 @@ export const router = osw.router({
}),

post: {
find: osw
find: pub
.input(
z.object({
id: z.string({}),
Expand Down Expand Up @@ -112,7 +113,7 @@ export const router = osw.router({
}
}),

create: osw
create: authed
.input(
z.object({
title: z.string(),
Expand Down
13 changes: 7 additions & 6 deletions apps/content/content/docs/openapi/generator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import { z } from 'zod'

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

// global osw completely optional, needed when you want to use context
export const osw /** os with ... */ = os.context<Context>()
// global pub, authed completely optional
export const pub /** public access */ = os.context<Context>()
export const authed /** require authed */ = pub.use(() => {/* auth logic */})

export const router = osw.router({
export const router = pub.router({
getting: os
.input(
z.object({
Expand All @@ -34,8 +35,8 @@ export const router = osw.router({
}
}),

post: osw.prefix('/posts').router({
find: osw
post: pub.prefix('/posts').router({
find: pub
.route({
path: '/{id}', // custom your OpenAPI
method: 'GET',
Expand All @@ -60,7 +61,7 @@ export const router = osw.router({
}
}),

create: osw
create: authed
.input(
z.object({
title: z.string(),
Expand Down
13 changes: 6 additions & 7 deletions apps/content/content/docs/server/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import { os, ORPCError } from '@orpc/server'

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

// global osw completely optional, needed when you want to use context
export const osw /** os with ... */ = os.context<Context>()
export const pub /** public access */ = os.context<Context>()

const authMiddleware = osw.middleware(async (input, context, meta) => {
const authMiddleware = pub.middleware(async (input, context, meta) => {
meta.onSuccess(async (output) => {
// Invoked after successful handler execution
// Other callbacks: onError, onFinish
Expand All @@ -36,7 +35,7 @@ const authMiddleware = osw.middleware(async (input, context, meta) => {
})

// Now every procedure or router defined in this oRPC will be protected by authMiddleware
const authOS = osw.use(authMiddleware)
export const authed /** require authed */ = pub.use(authMiddleware)
```

## Typed Input
Expand Down Expand Up @@ -87,10 +86,10 @@ type Context = {
}
}

const osw = os.context<Context>()
export const pub = os.context<Context>()

// Any procedure using this middleware will infer context.user as NonNullable<typeof context['user']>
const authMiddleware = osw
const authMiddleware = pub
.middleware(async (input, context, meta) => {
if (!context.user) {
throw new ORPCError({ code: 'UNAUTHORIZED' })
Expand All @@ -103,7 +102,7 @@ const authMiddleware = osw
}
})

const procedure = osw
export const authed = pub
.use(authMiddleware)
.use((input, context, meta) => {

Expand Down
4 changes: 2 additions & 2 deletions apps/content/content/docs/server/procedure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { z } from 'zod'
import { os, ORPCError } from '@orpc/server'

// Define context type for full type inference
const osw = os.context<{user?: {id: string}}>()
const pub /* public access */ = os.context<{user?: {id: string}}>()

const findUser = osw
const findUser = pub
.route({ method: 'GET', path: '/{id}' }) // Optional: if you want custom api under OpenAPI Specifications
.input(z.object({ id: z.string() })) // Optional
.output(z.object({ id: z.string(), name: z.string() })) // Optional
Expand Down
18 changes: 12 additions & 6 deletions apps/content/examples/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,23 @@ export const contract = oc.router({
import { os, ORPCError } from '@orpc/server'

export type Context = { user?: { id: string } }
export const osw /** os with ... */ = os.context<Context>().contract(contract) // Ensure every implement must be match contract

export const router = osw.router({
getting: osw.getting.handler((input, context, meta) => {
export const base = os.context<Context>()
export const pub /** os with ... */ = base.contract(contract) // Ensure every implement must be match contract
export const authed /** require authed */ = base
.use(() => {
/* auth logic */
})
.contract(contract)

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

post: {
find: osw.post.find
find: pub.post.find
.use((input, context, meta) => {
if (!context.user) {
throw new ORPCError({
Expand All @@ -96,7 +102,7 @@ export const router = osw.router({
}
}),

create: osw.post.create.handler((input, context, meta) => {
create: authed.post.create.handler((input, context, meta) => {
return {
id: 'example',
title: input.title,
Expand Down
24 changes: 13 additions & 11 deletions apps/content/examples/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { z } from 'zod'

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

const osw = os.context<Context>().use((input, context, meta) => {
// This middleware will apply to everything create from osw
const start = Date.now()
export const pub /** public access */ = os
.context<Context>()
.use((input, context, meta) => {
// This middleware will apply to everything create from pub
const start = Date.now()

meta.onFinish((output, error) => {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(`middleware cost ${Date.now() - start}ms`)
meta.onFinish((output, error) => {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(`middleware cost ${Date.now() - start}ms`)
})
})
})

export const authMiddleware = osw.middleware(async (input, context, meta) => {
export const authMid = pub.middleware(async (input, context, meta) => {
if (!context.user) {
throw new ORPCError({ code: 'UNAUTHORIZED' })
}
Expand All @@ -34,9 +36,9 @@ export const authMiddleware = osw.middleware(async (input, context, meta) => {
}
})

export const authOS = osw.use(authMiddleware) // any procedure compose from authOS will be protected
export const authed = pub.use(authMid) // any procedure compose from authOS will be protected

export const canEditPost = authMiddleware.concat(
export const canEditPost = authMid.concat(
// Now you expect to have id in input
async (input: { id: string }, context, meta) => {
if (context.user.id !== input.id) {
Expand All @@ -45,7 +47,7 @@ export const canEditPost = authMiddleware.concat(
},
)

const editPost = authOS
export const editPost = authed
.input(z.object({ id: z.string() }))
.output(z.string())
.use(canEditPost) // if input not match, will throw type error
Expand Down
15 changes: 9 additions & 6 deletions apps/content/examples/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { z } from 'zod'

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

// global osw completely optional, needed when you want to use context
export const osw /** os with ... */ = os.context<Context>()
// global pub, authed completely optional
export const pub /** public access */ = os.context<Context>()
export const authed /** require authed */ = pub.use(() => {
/* auth logic */
})

export const router = osw.router({
export const router = pub.router({
getting: os
.input(
z.object({
Expand All @@ -20,8 +23,8 @@ export const router = osw.router({
}
}),

post: osw.prefix('/posts').router({
find: osw
post: pub.prefix('/posts').router({
find: pub
.route({
path: '/{id}', // custom your OpenAPI
method: 'GET',
Expand Down Expand Up @@ -63,7 +66,7 @@ export const router = osw.router({
}
}),

create: osw
create: authed
.input(
z.object({
title: z.string(),
Expand Down

0 comments on commit 58313c5

Please sign in to comment.