Skip to content

Commit 0165ee0

Browse files
authored
docs: update metadata example (#198)
1 parent 09ec5b4 commit 0165ee0

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

apps/content/docs/metadata.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@ oRPC procedures support metadata, simple key-value pairs that provide extra info
1111

1212
```ts twoslash
1313
import { os } from '@orpc/server'
14+
declare const db: Map<string, unknown>
1415
// ---cut---
1516
interface ORPCMetadata {
1617
cache?: boolean
1718
}
1819

1920
const base = os
2021
.$meta<ORPCMetadata>({}) // require define initial context [!code highlight]
21-
.use(async ({ procedure, next }) => {
22-
const result = await next()
23-
22+
.use(async ({ procedure, next, path }, input, output) => {
2423
if (!procedure['~orpc'].meta.cache) {
25-
return result
24+
return await next()
2625
}
2726

28-
// Process the result when caching is enabled
27+
const cacheKey = path.join('/') + JSON.stringify(input)
28+
29+
if (db.has(cacheKey)) {
30+
return output(db.get(cacheKey))
31+
}
32+
33+
const result = await next()
34+
35+
db.set(cacheKey, result.output)
2936

3037
return result
3138
})

0 commit comments

Comments
 (0)