I've got tired of using all those cookie packages with no or wrong type declarations. Say hello to typescript-cookies.
import cookies from 'typescript-cookies'
cookies.set('cookie-name', 'cookie-value')
const cookie = cookies.get('cookie-name')
console.log(cookie)
# => cookie-value
cookies.remove('cookie-name')
For better understanding of options read MDM Docs.
Specify either number
or Date
.
For number
package will set up cookie expiration by number of specified dates.
For Date
- exact time of expiration.
cookies.set('cookie-name', 'cookie-value', { expires: 1 })
cookies.set('cookie-name', 'cookie-value', { expires: new Date() })
Set up max-age parameter. Read MDM docs for more.
cookies.set('cookie-name', 'cookie-value', { maxAge: 1000 })
This parameter defaults to '/'. So no need to set it up if that is what you need.
cookies.set('cookie-name', 'cookie-value', { path: '/some/path' })
cookies.set('cookie-name', 'cookie-value', { domain: 'www.example.com' })
cookies.set('cookie-name', 'cookie-value', { secure: true })
Accepts 'lax', 'strict' or 'none'
cookies.set('cookie-name', 'cookie-value', { samesite: 'lax' })
cookies.set('cookie-name', 'cookie-value', {
expires: 7,
path: '/',
domain: 'www.google.com',
secure: true,
samesite: 'none',
})
You can globally set options for set function if manually initialize Cookies
class
import {Cookies} from 'typescript-cookies'
const cookies = new Cookies({
path: '/',
expires: 7,
domain: 'www.google.com',
secure: true,
samesite: 'none',
})