@minimajs/cookie
Type-safe cookie management for Minima.js applications.
Installation
npm install @minimajs/cookieBasic Usage
The cookies() function returns all cookies, and provides namespace methods for getting, setting, and removing individual cookies:
import { cookies } from "@minimajs/cookie";
import { createApp } from "@minimajs/server";
const app = createApp();
app.get("/example", () => {
// Get all cookies
const allCookies = cookies();
// { "session-token": "abc123", "theme": "dark", ... }
// Get a single cookie
const sessionToken = cookies.get("session-token");
// Set a cookie
cookies.set("theme", "dark");
// Remove a cookie
cookies.remove("old-token");
return { sessionToken };
});API Reference
cookies<T>()
Returns all cookies as a record. Accepts an optional type parameter for type-safe access.
Type Parameter:
T(optional): Type definition for the cookies object (defaults toRecord<string, string>)
Returns: T - All cookies as a typed record
Examples:
// Get all cookies (untyped)
const allCookies = cookies();
// Type: Record<string, string>
// Get all cookies with type parameter
interface MyCookies {
sessionToken?: string;
userId?: string;
theme?: string;
}
const typedCookies = cookies<MyCookies>();
// Type: MyCookies
// Access with autocomplete: typedCookies.sessionTokencookies.get(name)
Retrieves a single cookie by name.
Parameters:
name(string): The cookie name
Returns: string | undefined
Example:
// Get a cookie
const theme = cookies.get("theme");
// theme is string | undefinedcookies.set(name, value, options?)
Sets a cookie with optional configuration.
Parameters:
name(string): The cookie namevalue(string): The cookie valueoptions(optional): SerializeOptionsdomain(string): Cookie domainpath(string): Cookie path (default: "/")maxAge(number): Max age in secondsexpires(Date): Expiration datehttpOnly(boolean): HTTP only flagsecure(boolean): Secure flagsameSite("strict" | "lax" | "none"): SameSite policy
Example:
// Simple cookie
cookies.set("theme", "dark");
// Cookie with options
cookies.set("session-token", "abc123", {
httpOnly: true,
secure: true,
maxAge: 3600, // 1 hour
sameSite: "strict",
});
// Cookie with expiration date
cookies.set("temp-token", "xyz789", {
expires: new Date(Date.now() + 86400000), // 1 day
});cookies.remove(name, options?)
Removes a cookie by setting its expiration to the past.
Parameters:
name(string): The cookie nameoptions(optional): Same asset()options (useful for matching path/domain)
Example:
// Remove a cookie
cookies.remove("session-token");
// Remove with specific path/domain
cookies.remove("session-token", {
path: "/admin",
domain: "example.com",
});Advanced Usage
Type-Safe Cookies
Use TypeScript generics to get type-safe access to your cookies:
interface UserCookies {
sessionId?: string;
userId?: string;
theme?: "light" | "dark";
locale?: string;
}
app.get("/profile", () => {
const userCookies = cookies<UserCookies>();
// TypeScript autocomplete and type checking
const theme = userCookies.theme; // Type: "light" | "dark" | undefined
const sessionId = userCookies.sessionId; // Type: string | undefined
return { theme };
});Session Management Example
import { cookies } from "@minimajs/cookie";
// Login endpoint
app.post("/login", () => {
// Authenticate user...
const userId = "12345";
// Set session cookie
cookies.set("session", userId, {
httpOnly: true,
secure: true,
maxAge: 86400, // 24 hours
sameSite: "strict",
});
return { success: true };
});
// Protected endpoint
app.get("/profile", () => {
// Get session
const userId = cookies.get("session");
if (!userId) {
throw new Error("Not authenticated");
}
// Fetch user data...
return { userId };
});
// Logout endpoint
app.post("/logout", () => {
cookies.remove("session");
return { success: true };
});Best Practices
Use
httpOnlyfor session cookies to prevent XSS attacks:typescriptcookies.set("session", token, { httpOnly: true });Use
securein production to ensure cookies are only sent over HTTPS:typescriptcookies.set("session", token, { secure: process.env.NODE_ENV === "production", });Set appropriate
sameSitepolicies to prevent CSRF:typescriptcookies.set("session", token, { sameSite: "strict" });Use short
maxAgefor sensitive data:typescriptcookies.set("auth-token", token, { maxAge: 900 }); // 15 minutes
Links
License
MIT