New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple-cookie-client

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-cookie-client

Simple and isomorphic cookie api, with support for hybrid client-side and server-side rendering applications.

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

ci_on_commit deploy_on_tag

Simple and isomorphic cookie api, with support for hybrid client-side and server-side rendering applications.

usage

install

npm install simple-cookie-client
import { getCookie } from 'simple-cookie-client';

const cookie = getCookie({ name: 'authorization' }); // returns `null` or `Cookie`

Supports both browser and server side env.

import { setCookie } from 'simple-cookie-client';

setCookie({ name: 'authorization', '821' });

Supports both browser and server side env.

import { deleteCookie } from 'simple-cookie-client';

deleteCookie({ name: 'authorization' });

Supports both browser and server side env.

import { getCookiesFromString } from 'simple-cookie-client';

const cookies = getCookiesFromString('authorization=abc; _ga=123') // => [Cookie({ name: 'authorization', value: 'abc'}), Cookie({ name: '_ga', value: '123' })]
expect(cookies.length).toEqual(2);
const string = castCookiesToString([
  new Cookie({ name: '_ga', value: '123' }),
  new Cookie({ name: 'authorization', value: 'opensaysame' }),
]);
expect(string).toEqual(
  '_ga=123; authorization=opensaysame;',
);

server side rendering support

In serverside rendering, you may need a cookie that is accessible to your clientside application in the document but not in your serverside application context. Typically, the same cookie that is accessible in the browser in the document object - is accessible on the server in the request object sent to your server.

Therefore, this library supports exposing cookies from the request in a way that is isomorphic (i.e., looks the same) to the clientside code you're writing.

For example, in a Next.JS application, you are able to access the req object with getServerSideProps. Here is how you can expose the cookie in that environment:

import { exposeCookieFromReq } from 'simple-cookie-client';

export const getStaticProps = async ({ req }) =>
  exposeCookieFromReq({
    name, // the name of the cookie you want to expose
    req, // the request object next.js was given
  });

And now, any code in your stack can access that cookie without needing to think about whether it gets it from the browser directly or whether it was exposed like above:

const cookie = getCookie({ name }); // this will work both in SSR (if cookie was exposed from req) as well as browser (where cookie is in `document` api)

Keywords

FAQs

Package last updated on 05 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc