npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cjkihl/cookies

v1.0.7

Published

A minimal, type-safe cookies library for Bun/Node.js with Standard Schema validation support

Downloads

223

Readme

@cjkihl/cookies

A minimal, type-safe cookies library for Bun/Node.js with Standard Schema validation support.

Features

  • 🔒 Type-safe: Full TypeScript support with generics
  • 🎯 Standard Schema compatible: Works with any Standard Schema library (Zod, Valibot, ArkType, etc.)
  • 🌐 Universal: Works in both browser and server environments
  • Minimal: Lightweight with minimal dependencies
  • 🔧 Flexible: Support for all cookie attributes and options

Installation

bun add @cjkihl/cookies

The library uses the official @standard-schema/spec package for Standard Schema types, which is included as a dependency.

Quick Start

Root Import (All Functions)

You can import all functions from the root package:

import { 
  // Client functions
  getClientCookies, setClientCookie, getClientCookie, deleteClientCookie,
  // Server functions  
  getServerCookies, setServerCookie, getServerCookie, deleteServerCookie,
  // Types and utilities
  type Cookie, type CookieOptions
} from "@cjkihl/cookies";

Client-side (Browser)

import { getCookies, setCookie, getCookie, deleteCookie } from "@cjkihl/cookies/client";

// Get all cookies
const cookies = getCookies();

// Get a specific cookie
const userCookie = getCookie("user");

// Set a simple cookie
setCookie("theme", { value: "dark" });

// Set a cookie with options
setCookie("session", {
  value: "abc123",
  expires: new Date("2024-12-31"),
  path: "/",
  secure: true,
  httpOnly: false,
  sameSite: "Strict"
});

// Delete a cookie
deleteCookie("old-cookie");

Server-side

import { getCookies, setCookie, getCookie, deleteCookie } from "@cjkihl/cookies/server";

// Get cookies from request
const cookies = getCookies(request);

// Get a specific cookie
const userCookie = getCookie(request, "user");

// Set cookie on response
setCookie(response, "session", {
  value: "abc123",
  expires: new Date("2024-12-31"),
  path: "/",
  secure: true,
  httpOnly: true,
  sameSite: "Strict"
});

// Delete a cookie
deleteCookie(response, "old-cookie");

Standard Schema Validation

The library supports validation using any Standard Schema compatible library.

With Zod

import { z } from "zod";
import { getCookies, setCookie } from "@cjkihl/cookies/client";

// Create a schema
const userSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.string().email()
});

// Get cookies with validation
const cookies = getCookies(userSchema);
// cookies.user.value will be typed as { id: number; name: string; email: string }

// Set cookie with validation
setCookie("user", {
  value: { id: 1, name: "John", email: "[email protected]" },
  schema: userSchema
});

With Valibot

import { object, number, string, email } from "valibot";
import { getCookies, setCookie } from "@cjkihl/cookies/client";

// Create a schema
const userSchema = object({
  id: number(),
  name: string(),
  email: string([email()])
});

// Get cookies with validation
const cookies = getCookies(userSchema);

// Set cookie with validation
setCookie("user", {
  value: { id: 1, name: "John", email: "[email protected]" },
  schema: userSchema
});

With ArkType

import { type } from "arktype";
import { getCookies, setCookie } from "@cjkihl/cookies/client";

// Create a schema
const userSchema = type({
  id: "number",
  name: "string",
  email: "string"
});

// Get cookies with validation
const cookies = getCookies(userSchema);

// Set cookie with validation
setCookie("user", {
  value: { id: 1, name: "John", email: "[email protected]" },
  schema: userSchema
});

API Reference

Client Functions

getCookies<T = string>(schema?: StandardSchemaV1<T>): CookieRecord<T>

Get all cookies from document.cookie.

getCookie<T = string>(key: string, schema?: StandardSchemaV1<T>): Cookie<T> | undefined

Get a specific cookie by key.

setCookie<T = string>(key: string, options: CookieOptions<T>): void

Set a cookie with full options.

setCookieSimple<T = string>(key: string, value: T, options?: Omit<CookieOptions<T>, "value">): void

Set a cookie with a simple value and optional options.

deleteCookie(key: string, options?: { path?: string; domain?: string }): void

Delete a cookie by setting an expired date.

Server Functions

getCookies<T = string>(req: Request | Headers, schema?: StandardSchemaV1<T>): CookieRecord<T>

Get all cookies from request headers.

getCookie<T = string>(req: Request | Headers, key: string, schema?: StandardSchemaV1<T>): Cookie<T> | undefined

Get a specific cookie from request headers.

setCookie<T = string>(res: Response | Headers, key: string, options: CookieOptions<T>): void

Set a cookie on response headers.

setCookieSimple<T = string>(res: Response | Headers, key: string, value: T, options?: Omit<CookieOptions<T>, "value">): void

Set a cookie with a simple value on response headers.

deleteCookie(res: Response | Headers, key: string, options?: { path?: string; domain?: string }): void

Delete a cookie on response headers.

Types

Cookie<T = string>

interface Cookie<T = string> {
  value: T;
  expires?: Date;
  maxAge?: number;
  domain?: string;
  path?: string;
  secure: boolean;
  httpOnly: boolean;
  sameSite?: "Strict" | "Lax" | "None";
}

CookieOptions<T = string>

interface CookieOptions<T = string> {
  value: T;
  expires?: Date;
  maxAge?: number;
  domain?: string;
  path?: string;
  secure?: boolean;
  httpOnly?: boolean;
  sameSite?: "Strict" | "Lax" | "None";
  schema?: StandardSchemaV1<T>;
}

Examples

Basic Cookie Management

import { getCookies, setCookie, deleteCookie } from "@cjkihl/cookies/client";

// Set a simple cookie
setCookie("theme", { value: "dark" });

// Set a cookie with expiration
setCookie("session", {
  value: "abc123",
  expires: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
  path: "/",
  secure: true
});

// Get all cookies
const cookies = getCookies();
console.log(cookies.theme?.value); // "dark"

// Delete a cookie
deleteCookie("old-session", { path: "/" });

Validation with Zod

import { z } from "zod";
import { getCookie, setCookie } from "@cjkihl/cookies/client";

// Define schemas
const themeSchema = z.enum(["light", "dark"]);
const userSchema = z.object({
  id: z.number(),
  name: z.string().min(1),
  preferences: z.object({
    theme: themeSchema,
    notifications: z.boolean()
  })
});

// Set validated cookies
setCookie("theme", { value: "dark", schema: themeSchema });
setCookie("user", {
  value: {
    id: 1,
    name: "John Doe",
    preferences: { theme: "dark", notifications: true }
  },
  schema: userSchema
});

// Get validated cookies
const theme = getCookie("theme", themeSchema);
const user = getCookie("user", userSchema);

// TypeScript knows the exact types
console.log(theme?.value); // "light" | "dark"
console.log(user?.value.preferences.theme); // "light" | "dark"

Server-side Usage

import { getCookies, setCookie } from "@cjkihl/cookies/server";

// Handle incoming request
async function handleRequest(request: Request) {
  // Get cookies from request
  const cookies = getCookies(request);
  
  // Process request...
  const response = new Response("Hello World");
  
  // Set cookies on response
  setCookie(response, "session", {
    value: "new-session-id",
    httpOnly: true,
    secure: true,
    sameSite: "Strict"
  });
  
  return response;
}

Testing

Run the test suite:

bun test

Requirements

  • Node.js >= 18.0.0 or Bun
  • TypeScript >= 5.0.0 (for type definitions)

License

MIT