1.0.19 • Published 6 years ago

@robotkittens/cookie-universal-nuxt v1.0.19

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

cookie-universal-nuxt

npm (scoped with tag) npm js-standard-style

Universal cookie plugin for Nuxt, perfect for SSR

You can use cookie-universal-nuxt to set, get and remove cookies in both client and server side nuxt apps. cookie-universal-nuxt parse cookies with the popular cookie node module.

Install

  • yarn: yarn add cookie-universal-nuxt
  • npm: npm i --save cookie-universal-nuxt

Add cookie-universal-nuxt to nuxt.config.js:

{
  modules: [
    // Simple usage
    'cookie-universal-nuxt',

    // With options
    ['cookie-universal-nuxt', { alias: 'cookiz' }],
 ]
}

Api

  • name (string): Cookie name to set.
  • value (string|object): Cookie value.
  • opts (object): Same as the cookie node module.
    • path (string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
    • expires (date): Specifies the Date object to be the value for the Expires Set-Cookie attribute.
    • maxAge (number): Specifies the number (in milliseconds) to be the value for the Max-Age Set-Cookie attribute.
    • httpOnly (boolean): Specifies the boolean value for the HttpOnly Set-Cookie attribute.
    • domain (string): specifies the value for the Domain Set-Cookie attribute.
    • encode (function): Specifies a function that will be used to encode a cookie's value.
    • sameSite (boolean|string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
    • secure (boolean): Specifies the boolean value for the Secure Set-Cookie attribute.
const cookieValObject = { param1: 'value1', param2: 'value2' }

// server
app.$cookies.set('cookie-name', 'cookie-value', { 
  path: '/',
  maxAge: 60 * 60 * 24 * 7
})
app.$cookies.set('cookie-name', cookieValObject, { 
  path: '/',
  maxAge: 60 * 60 * 24 * 7
})

// client
this.$cookies.set('cookie-name', 'cookie-value', { 
  path: '/',
  maxAge: 60 * 60 * 24 * 7
})
this.$cookies.set('cookie-name', cookieValObject, { 
  path: '/',
  maxAge: 60 * 60 * 24 * 7
})

  • cookieArray (array)
    • name (string): Cookie name to set.
    • value (string|object): Cookie value.
    • opts (object): Same as the cookie node module
      • path (string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
      • expires (date): Specifies the Date object to be the value for the Expires Set-Cookie attribute.
      • maxAge (number): Specifies the number (in milliseconds) to be the value for the Max-Age Set-Cookie attribute.
      • httpOnly (boolean): Specifies the boolean value for the HttpOnly Set-Cookie attribute.
      • domain (string): specifies the value for the Domain Set-Cookie attribute.
      • encode (function): Specifies a function that will be used to encode a cookie's value.
      • sameSite (boolean|string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
      • secure (boolean): Specifies the boolean value for the Secure Set-Cookie attribute.
const options = {
  path: '/',
  maxAge: 60 * 60 * 24 * 7
}
const cookieList = [
  { name: 'cookie-name1', value: 'value1', opts: options },
  { name: 'cookie-name2', value: 'value2', opts: options },
  { name: 'cookie-name3', value: 'value3', opts: options },
  { name: 'cookie-name4', value: 'value4', opts: options }
]

// server
app.$cookies.setAll(cookieList)

// client
this.$cookies.setAll(cookieList)

  • name (string): Cookie name to get.
  • fromRes (boolean): Get cookies from res instead of req.

// server
const cookieRes = app.$cookies.get('cookie-name') 
const cookieRes = app.$cookies.get('cookie-name', true) // get from res instead of req 
// returns the cookie value or undefined

// client
const cookieRes = this.$cookies.get('cookie-name') 
// returns the cookie value or undefined

  • fromRes (boolean): Get cookies from res instead of req.
// server
const cookiesRes = app.$cookies.getAll() 
const cookiesRes = app.$cookies.getAll(true) // get from res instead of req 
// returns all cookies or []
[
  {
    "name": "cookie-1",
    "value": "value1"
  },
  {
    "name": "cookie-2",
    "value": "value2"
  }
]

// client
const cookiesRes = this.$cookies.getAll() 
// returns all cookies or []
[
  {
    "name": "cookie-1",
    "value": "value1"
  },
  {
    "name": "cookie-2",
    "value": "value2"
  }
]

  • name (string): Cookie name to remove.
  • opts (object): The only option available is path. Use it to remove the cookie from a specific location.

// server
app.$cookies.remove('cookie-name') 
app.$cookies.remove('cookie-name', {
  // this will allow you to remove a cookie
  // from a different path
  path: '/my-path' 
})

// client
this.$cookies.remove('cookie-name') 

// note that removeAll does not currently allow you 
// to remove cookies that have a 
// path different from '/'

// server
app.$cookies.removeAll() 

// client
this.$cookies.removeAll() 

  • alias (string): Specifies the plugin alias to use
{
  modules: [
    ['cookie-universal-nuxt', { alias: 'cookiz' }],
 ]
}


// usage
this.$cookiz.set('cookie-name', 'cookie-value')

License

MIT License

Copyright (c) Salvatore Tedde microcipcip@gmail.com