β¬ οΈ Back to Table of Contents
π useCookies¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 10 |
| π¦ Imports | 4 |
| π Type Aliases | 1 |
π Table of Contents¶
π οΈ File Location:¶
π packages/integrations/useCookies/index.ts
π¦ Imports¶
| Name | Source |
|---|---|
IncomingMessage |
node:http |
tryOnScopeDispose |
@vueuse/shared |
Cookie |
universal-cookie |
shallowRef |
vue |
Functions¶
createCookies(req: IncomingMessage): (dependencies?: string[] | null, { doNotParse, autoUpdateDe⦶
Creates a new function
Parameters:
reqany: incoming http request (for SSR)
See: https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie universal-cookie
Tags: @description Creates universal-cookie instance using request (default is window.document.cookie) and returns useCookies function with provided universal-cookie instance
Raw JSDoc
/**
* Creates a new {@link useCookies} function
* @param req - incoming http request (for SSR)
* @see https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie universal-cookie
* @description Creates universal-cookie instance using request (default is window.document.cookie) and returns {@link useCookies} function with provided universal-cookie instance
*/
Calls:
useCookies
Code
export function createCookies(req?: IncomingMessage) {
const universalCookie = new Cookie(req ? req.headers.cookie : null)
return (
dependencies?: string[] | null,
{ doNotParse = false, autoUpdateDependencies = false } = {},
) => useCookies(dependencies, { doNotParse, autoUpdateDependencies }, universalCookie)
}
useCookies(dependencies: string[] | null, { doNotParse = false, autoUpdatβ¦: any, cookies: any): { get: <T = any>(...args: Parameters<Cookie["get"]>) => any⦶
Reactive methods to work with cookies (use method instead if you are using SSR)
Parameters:
dependenciesany: array of watching cookie's names. Pass empty array if don't want to watch cookies changes.optionsany: No descriptionoptions.doNotParseany: don't try parse value as JSONoptions.autoUpdateDependenciesany: automatically update watching dependenciescookiesany: universal-cookie instance
Tags: @__NO_SIDE_EFFECTS__
Raw JSDoc
/**
* Reactive methods to work with cookies (use {@link createCookies} method instead if you are using SSR)
* @param dependencies - array of watching cookie's names. Pass empty array if don't want to watch cookies changes.
* @param options
* @param options.doNotParse - don't try parse value as JSON
* @param options.autoUpdateDependencies - automatically update watching dependencies
* @param cookies - universal-cookie instance
*
* @__NO_SIDE_EFFECTS__
*/
Calls:
cookies.getAllshallowRef (from vue)shouldUpdatecookies.addChangeListenertryOnScopeDispose (from @vueuse/shared)cookies.removeChangeListenerwatchingDependencies.includeswatchingDependencies.pushcookies.getcookies.setcookies.remove
Internal Comments:
/**
* Adds reactivity to get/getAll methods
*/ (x2)
/**
* Reactive get cookie by name. If **autoUpdateDependencies = true** then it will update watching dependencies
*/ (x2)
/**
* Auto update watching dependencies if needed
*/
// eslint-disable-next-line ts/no-unused-expressions (x6)
/**
* Reactive get all cookies
*/ (x2)
Code
export function useCookies(
dependencies?: string[] | null,
{ doNotParse = false, autoUpdateDependencies = false } = {},
cookies = new Cookie(),
) {
const watchingDependencies = autoUpdateDependencies ? [...dependencies || []] : dependencies
let previousCookies = cookies.getAll<RawCookies>({ doNotParse: true })
/**
* Adds reactivity to get/getAll methods
*/
const touches = shallowRef(0)
const onChange = () => {
const newCookies = cookies.getAll({ doNotParse: true })
if (
shouldUpdate(
watchingDependencies || null,
newCookies,
previousCookies,
)
) {
touches.value++
}
previousCookies = newCookies
}
cookies.addChangeListener(onChange)
tryOnScopeDispose(() => {
cookies.removeChangeListener(onChange)
})
return {
/**
* Reactive get cookie by name. If **autoUpdateDependencies = true** then it will update watching dependencies
*/
get: <T = any>(...args: Parameters<Cookie['get']>) => {
/**
* Auto update watching dependencies if needed
*/
if (autoUpdateDependencies && watchingDependencies && !watchingDependencies.includes(args[0]))
watchingDependencies.push(args[0])
// eslint-disable-next-line ts/no-unused-expressions
touches.value // adds reactivity to method
return cookies.get<T>(args[0], { doNotParse, ...args[1] })
},
/**
* Reactive get all cookies
*/
getAll: <T = any>(...args: Parameters<Cookie['getAll']>) => {
// eslint-disable-next-line ts/no-unused-expressions
touches.value // adds reactivity to method
return cookies.getAll<T>({ doNotParse, ...args[0] })
},
set: (...args: Parameters<Cookie['set']>) => cookies.set(...args),
remove: (...args: Parameters<Cookie['remove']>) => cookies.remove(...args),
addChangeListener: (...args: Parameters<Cookie['addChangeListener']>) => cookies.addChangeListener(...args),
removeChangeListener: (...args: Parameters<Cookie['removeChangeListener']>) => cookies.removeChangeListener(...args),
}
}
shouldUpdate(dependencies: string[] | null, newCookies: RawCookies, oldCookies: RawCookies): boolean¶
Parameters:
dependenciesstring[] | nullnewCookiesRawCookiesoldCookiesRawCookies
Returns: boolean
Code
Internal helpers¶
Declared inside another function in this file.
onChange(): void¶
Returns: void
Calls:
cookies.getAllshouldUpdate
Code
get(args: Parameters<Cookie['get']>): any¶
Parameters:
argsParameters<Cookie['get']>
Returns: any
Calls:
watchingDependencies.includeswatchingDependencies.pushcookies.get
Internal Comments:
/**
* Auto update watching dependencies if needed
*/
// eslint-disable-next-line ts/no-unused-expressions (x3)
Code
<T = any>(...args: Parameters<Cookie['get']>) => {
/**
* Auto update watching dependencies if needed
*/
if (autoUpdateDependencies && watchingDependencies && !watchingDependencies.includes(args[0]))
watchingDependencies.push(args[0])
// eslint-disable-next-line ts/no-unused-expressions
touches.value // adds reactivity to method
return cookies.get<T>(args[0], { doNotParse, ...args[1] })
}
getAll(args: Parameters<Cookie['getAll']>): any¶
Parameters:
argsParameters<Cookie['getAll']>
Returns: any
Calls:
cookies.getAll
Internal Comments:
Code
set(args: Parameters<Cookie['set']>): any¶
Parameters:
argsParameters<Cookie['set']>
Returns: any
Calls:
cookies.set
remove(args: Parameters<Cookie['remove']>): any¶
Parameters:
argsParameters<Cookie['remove']>
Returns: any
Calls:
cookies.remove
addChangeListener(args: Parameters<Cookie['addChangeListener']>): any¶
Parameters:
argsParameters<Cookie['addChangeListener']>
Returns: any
Calls:
cookies.addChangeListener
removeChangeListener(args: Parameters<Cookie['removeChangeListenerβ¦): any¶
Parameters:
argsParameters<Cookie['removeChangeListener']>
Returns: any
Calls:
cookies.removeChangeListener
Type Aliases¶
RawCookies¶
Generated by Syntax Scribe