Skip to content

Commit 2f1a6fa

Browse files
committed
feat: add useContext function
1 parent a82ef43 commit 2f1a6fa

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

README.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ import { decodeAndVerifyJwtToken } from '~/somewhere/in/utils'
6969
export async function addTodo(todo: Todo) {
7070
const event = useEvent()
7171

72-
const authorization = getRequestHeader(event, 'authorization')
73-
const user = await decodeAndVerifyJwtToken(authorization.split(' ')[1])
72+
async function getUserFromHeader() {
73+
const authorization = getRequestHeader(event, 'authorization')
74+
if (authorization) {
75+
const user = await decodeAndVerifyJwtToken(authorization.split(' ')[1])
76+
return user
77+
}
78+
return null
79+
}
80+
81+
const user = await getUserFromHeader()
7482

7583
if (!user) {
7684
throw createError({ statusCode: 401 })
@@ -89,6 +97,37 @@ export async function addTodo(todo: Todo) {
8997

9098
You can use all built-in [h3 utilities](https://github.com/unjs/h3#built-in) inside your exported functions.
9199

100+
## createContext
101+
102+
Each `.server.` file can also export a `createContext` function that is called for each incoming request:
103+
104+
```ts
105+
export function createContext() {
106+
const event = useEvent()
107+
108+
async function getUserFromHeader() {
109+
const authorization = getRequestHeader(event, 'authorization')
110+
if (authorization) {
111+
const user = await decodeAndVerifyJwtToken(authorization.split(' ')[1])
112+
return user
113+
}
114+
return null
115+
}
116+
117+
event.context.user = await getUserFromHeader()
118+
}
119+
120+
export async function addTodo(todo: Todo) {
121+
const event = useEvent()
122+
123+
if (!event.context.user) {
124+
throw createError({ statusCode: 401 })
125+
}
126+
127+
// addTodo logic
128+
}
129+
```
130+
92131
## useAsyncData
93132

94133
`nuxt-remote-fn` can work seamlessly with [`useAsyncData`](https://nuxt.com/docs/api/composables/use-async-data/):

playground/lib/todo.server.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ export function getTodo (id: number) {
1515
}
1616

1717
export async function toggleTodo (id: number) {
18-
const event = useEvent()
19-
console.log(event.context.params)
20-
2118
const todo = await getTodo(id)
2219
return prisma.todo.update({
2320
where: { id },
@@ -38,3 +35,8 @@ export function addTodo ({ title, content }: { title: string; content: string })
3835
}
3936
})
4037
}
38+
39+
export function createContext() {
40+
const event = useEvent()
41+
// console.log('event.context.params', event.context.params)
42+
}

src/runtime/server.ts

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export function createRemoteFnHandler<
3434
statusMessage: `[nuxt-remote-fn]: ${functionName as string} is not a function.`
3535
})
3636
}
37+
38+
if ('createContext' in functions[moduleId]) {
39+
await functions[moduleId]['createContext'].apply(event)
40+
}
3741

3842
const result = functions[moduleId][functionName].apply(event, body.args)
3943
return result

0 commit comments

Comments
 (0)