Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt): useCookie with defaults should return non-null value (#9449
Browse files Browse the repository at this point in the history
)
  • Loading branch information
danielroe committed Jan 21, 2023
1 parent bcfaaf0 commit c5e3934
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/nuxt/src/app/composables/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export interface CookieOptions<T = any> extends _CookieOptions {
default?: () => T | Ref<T>
}

export interface CookieRef<T> extends Ref<T | null> {}
export interface CookieRef<T> extends Ref<T> {}

const CookieDefaults: CookieOptions<any> = {
path: '/',
decode: val => destr(decodeURIComponent(val)),
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
}

export function useCookie <T = string> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
export function useCookie <T = string | null> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
const opts = { ...CookieDefaults, ..._opts }
const cookies = readRawCookies(opts) || {}

Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/basic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ describe('composables', () => {
expectTypeOf(useState('test', () => ref('hello'))).toEqualTypeOf<Ref<string>>()
expectTypeOf(useState('test', () => 'hello')).toEqualTypeOf<Ref<string>>()

expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf<Ref<number | null>>()
expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf<Ref<number | null>>()
useCookie('test').value = null
expectTypeOf(useCookie('test', { default: () => ref(500) })).toEqualTypeOf<Ref<number>>()
expectTypeOf(useCookie('test', { default: () => 500 })).toEqualTypeOf<Ref<number>>()
useCookie<number | null>('test').value = null

expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => ref(500) }).data).toEqualTypeOf<Ref<number | null>>()
expectTypeOf(useAsyncData('test', () => Promise.resolve(500), { default: () => 500 }).data).toEqualTypeOf<Ref<number | null>>()
Expand Down

0 comments on commit c5e3934

Please sign in to comment.