New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@ffsm/cookie

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ffsm/cookie

Cookie handler for Server side and client side

latest
Source
npmnpm
Version
0.0.7
Version published
Maintainers
0
Created
Source

Installation

npm i @ffsm/cookie

OR

yarn add @ffsm/cookie

Using

Get and set

import { Cookie } from '@ffsm/cookie';

const cookie = Cookie.from(initialize);

const staticGet = Cookie.get('access_token', {}, initialize);
const staticGetAll = Cookie.getAll({}, initialize);

const all = cookie.getAll({});
const accessToken = cookie.get('access_token', {});

cookie.set('name', 'value', {
  // options
});

initialize is used to detect cookies. By default, if initialize is falsy, it will be set to the document of the browser if on the client side context.

With server side loader

'use server';

import { cookies } from 'next/headers';
import { Cookie } from '@ffsm/cookie';

export async function loader() {
  const initialize = await cookies();
  const cookie = Cookie.from(initialize);

  const accessToken = cookie.get('access_token', {})?.value;

  // Others code
}

With client side

import { useEffect } from 'react';
import { Cookie } from '@ffsm/cookie';

export default function App() {
  useEffect(() => {
    const accessToken = Cookie.get('access_token', {})?.value;
    console.log(accessToken);
  }, []);

  return <div>App</div>;
}

With server side context

export default function HomePage() {
  return <div>Home page</div>;
}

HomePage.getInitialProps = (ctx) => {
  const cookie = Cookie.from(ctx);
  const accessToken = cookie.get('access_token', {})?.value;

  return {
    accessToken,
  };
};

Method

.isValidName(name: string): boolean

.isValidValue(value: string): boolean

.isValidDomain(domain: string): boolean

.isValidPath(path: string): boolean

.serialize(name: string): string

.get(name: string, options?: ParseOptions): CookieSerialized

.getAll(options?: ParseOptions): CookieSerialized[]

.set(name: string, value: string, options?: CookieOptions): void

.remove(name: string): void;

.getBaseDomain(): string

Cookie.get(name: string, options?: ParseOptions, initialize?: any): CookieSerialized

Cookie.getAll(options?: ParseOptions, initialize?: any): CookieSerialized[]

Types

ParseOptions

export interface ParseOptions {
  decode?: boolean | ((value: string) => string);
}

CookieOptions

export interface CookieOptions {
  expires?: string | number | Date;
  path?: string;
  domain?: string;
  secure?: boolean;
  httpOnly?: boolean;
  sameSite?: 'strict' | 'lax' | 'none';
  encode?: boolean | ((value: string) => string);
  priority?: 'low' | 'medium' | 'high';
  maxAge?: number;
  partitioned?: boolean;
  baseDomain?: boolean;
}

CookieSerialized

export interface CookieSerialized extends CookieOptions {
  name: string;
  value: string;
}

Default options value

class Cookie {
  static DEFAULT_OPTIONS: CookieOptions = {
    expires: 7,
    path: '/',
    secure: true,
    httpOnly: true,
    sameSite: 'strict',
  };
}

WIP

Finding case and update more 🤥

Keywords

cookie

FAQs

Package last updated on 13 Mar 2025

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