-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental composition api via
useEvent()
ans async context…
… support (#1546)
- Loading branch information
Showing
13 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { AsyncLocalStorage } from "node:async_hooks"; | ||
import { H3Event, createError } from "h3"; | ||
import { getContext } from "unctx"; | ||
|
||
export interface NitroAsyncContext { | ||
event: H3Event; | ||
} | ||
|
||
export const nitroAsyncContext = getContext<NitroAsyncContext>("nitro-app", { | ||
asyncContext: import.meta._asyncContext, | ||
AsyncLocalStorage: import.meta._asyncContext ? AsyncLocalStorage : undefined, | ||
}); | ||
|
||
/** | ||
* | ||
* Access to the current Nitro request event. | ||
* | ||
* @experimental | ||
* - Requires `experimental.asyncContext: true` config to work. | ||
* - Works in Node.js and limited runtimes only | ||
* | ||
*/ | ||
export function useEvent(): H3Event { | ||
try { | ||
return nitroAsyncContext.use().event; | ||
} catch { | ||
const hint = import.meta._asyncContext | ||
? "Note: This is an experimental feature and might be broken on non-Node.js environments." | ||
: "Enable the experimental flag using `experimental.asyncContext: true`."; | ||
throw createError({ | ||
message: `Nitro request context is not available. ${hint}`, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,5 +82,6 @@ export default defineNitroConfig({ | |
}, | ||
experimental: { | ||
openAPI: true, | ||
asyncContext: true, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default defineEventHandler(async () => { | ||
await Promise.resolve(setTimeout(() => {}, 10)); | ||
return await useTest(); | ||
}); | ||
|
||
function useTest() { | ||
return { | ||
context: { | ||
path: useEvent().path, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters