Skip to content

fr33z3/typescript-cookies

Repository files navigation

typescript-cookies

I've got tired of using all those cookie packages with no or wrong type declarations. Say hello to typescript-cookies.

Basic usage

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')

Setter options

For better understanding of options read MDM Docs.

Expiration

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() })

Max Age

Set up max-age parameter. Read MDM docs for more.

cookies.set('cookie-name', 'cookie-value', { maxAge: 1000 })

Path

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' })

Domain

cookies.set('cookie-name', 'cookie-value', { domain: 'www.example.com' })

Secure

cookies.set('cookie-name', 'cookie-value', { secure: true })

Samesite

Accepts 'lax', 'strict' or 'none'

cookies.set('cookie-name', 'cookie-value', { samesite: 'lax' })

Example

cookies.set('cookie-name', 'cookie-value', {
  expires: 7,
  path: '/',
  domain: 'www.google.com',
  secure: true,
  samesite: 'none',
})

Global options

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',
  })

About

Typescript cookies module

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published