Skip to content

Commit

Permalink
fix: update return for useRequestEvent to include undefined (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
wattanx authored Jan 30, 2024
1 parent 039b8b4 commit dd79c81
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/bridge/src/runtime/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:
const nuxtApp = useNuxtApp()
const writeFinalCookieValue = () => {
if (opts.readonly || isEqual(cookie.value, cookies[name])) { return }
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts as CookieOptions<any>)
writeServerCookie(useRequestEvent(nuxtApp)!, name, cookie.value, opts as CookieOptions<any>)
}

// @ts-expect-error
const unhook = nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue)
nuxtApp.hooks.hookOnce('app:error', () => {
unhook() // don't write cookie subsequently when app:rendered is called
Expand All @@ -110,7 +111,7 @@ export function useCookie<T = string | null | undefined> (name: string, _opts?:

function readRawCookies (opts: CookieOptions = {}): Record<string, string> {
if (process.server) {
return parse(getRequestHeader(useRequestEvent(), 'cookie') || '', opts)
return parse(getRequestHeader(useRequestEvent()!, 'cookie') || '', opts)
} else if (process.client) {
return parse(document.cookie, opts)
}
Expand Down
5 changes: 4 additions & 1 deletion packages/bridge/src/runtime/composables/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export function useRequestHeaders (include?: any[]) {
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
}

export function useRequestEvent (nuxtApp: NuxtAppCompat = useNuxtApp()): H3Event {
export function useRequestEvent (nuxtApp: NuxtAppCompat = useNuxtApp()): H3Event | undefined {
if (process.client || !nuxtApp.ssrContext) {
return undefined
}
// check if H3 event is available
if (nuxtApp.ssrContext?.event) { return nuxtApp.ssrContext.event }
// Check if we created H3 event manually before
Expand Down

0 comments on commit dd79c81

Please sign in to comment.