0.0.7 • Published 7 months ago

@ffsm/cookie v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Cookie handler for server side and client side

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.from(initialize?: any): Cookie

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 🤥

0.0.7

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

8 months ago

0.0.1

8 months ago