1.0.2 • Published 8 months ago

@yurishvetsov/cookie-wrapper v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

Cookie Wrapper

A simple JavaScript API for handling cookies

  • Extensive browser support
  • Tested
  • No dependency
  • Typescript
  • Supports ES modules
  • Supports CommonJS

Installation

npm i @yurishvetsov/cookie-wrapper

Basic Usage

Import the library

import cookieWrapper from '@yurishvetsov/cookie-wrapper'
// or
const cookieWrapper = require('@yurishvetsov/cookie-wrapper')

Reading

Read all available cookies:

cookieWrapper.get() // => [{ name: 'name', value: 'value' }]

Read cookie:

cookieWrapper.get('name') // => 'value'
cookieWrapper.get('nothing') // => undefined

Read cookies by regular expression:

cookieWrapper.get(new RegExp('^name')) // => [{ name: 'name_123', value: 'value' }]

Creating

Create a cookie, valid across the entire site:

cookieWrapper.set('name', 'value')

Create a cookie that expires on December 17, 2024, valid across the entire site:

cookieWrapper.set('name', 'value', { expires: new Date('2024-12-17') })

Create a cookie that expires 1 hour from now, valid across the entire site:

cookieWrapper.set('name', 'value', { 'max-age': 3600 })

Create cookie, valid to the path of the current page:

cookieWrapper.set('name', 'value', { path: '' })

Create cookie with domain:

cookieWrapper.set('name', 'value', { domain: '.site.com' })

Deleting

Delete cookie:

cookieWrapper.remove('name')

Delete a cookie valid to the path of the current page:

cookieWrapper.remove('name', { path: '' })

Delete all cookies by regular expression and domain:

cookieWrapper.remove(new RegExp('^name_'), { domain: '.site.com' })

Cookie Attributes

AttributeTypeDescription
expiresDateDefine when the cookie will be removed.
max-agenumberDetermine when the cookie will be deleted, the value in seconds.
pathstringA String indicating the path where the cookie is visible.
domainstringA String indicating a valid domain where the cookie should be visible. The cookie will also be visible to all subdomains.

Authors

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

0.0.1-beta.1

8 months ago

0.0.1-beta.0

8 months ago