
Security News
pnpm 10.12 Introduces Global Virtual Store and Expanded Version Catalogs
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.
@types/cookie-parser
Advanced tools
TypeScript definitions for cookie-parser
The @types/cookie-parser package provides TypeScript type definitions for the cookie-parser middleware, which is used in Express applications for parsing Cookie header and populating req.cookies with an object keyed by the cookie names. Essentially, it enables strong typing for TypeScript projects using cookie-parser, ensuring that developers can work with cookies in a type-safe manner.
Type Definitions for Cookie Parsing Middleware
This code demonstrates how to use the cookie-parser middleware in an Express application with TypeScript. The @types/cookie-parser package provides the necessary type definitions for TypeScript to understand and correctly type the `req.cookies` object, allowing for a more robust and error-free development experience.
import express from 'express';
import cookieParser from 'cookie-parser';
const app = express();
app.use(cookieParser());
app.get('/', (req, res) => {
console.log(req.cookies);
res.send('Cookies: ' + JSON.stringify(req.cookies));
});
app.listen(3000, () => console.log('Server running on port 3000'));
cookie-session is a middleware for Express that enables session support through cookies. Unlike @types/cookie-parser, which is focused on parsing cookies and populating req.cookies, cookie-session is used for managing session state in web applications, storing session data on the client-side within cookies. It provides a different layer of functionality compared to simply parsing cookies.
express-session is another session management middleware for Express, but unlike cookie-session, it supports storing session data on the server-side, with the session ID being stored in a cookie on the client-side. This approach offers more control and security over session data compared to storing all session data within cookies. While @types/cookie-parser is focused on parsing cookies, express-session deals with sessions more comprehensively.
npm install --save @types/cookie-parser
This package contains type definitions for cookie-parser (https://github.com/expressjs/cookie-parser).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cookie-parser.
import * as express from "express";
declare module "express" {
// Inject additional properties on express.Request
interface Request {
/**
* This request's secret.
* Optionally set by cookie-parser if secret(s) are provided. Can be used by other middleware.
* [Declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) can be used to add your own properties.
*/
secret?: string | undefined;
/** Parsed cookies that have not been signed */
cookies: Record<string, any>;
/** Parsed cookies that have been signed */
signedCookies: Record<string, any>;
}
}
declare function cookieParser(
secret?: string | string[],
options?: cookieParser.CookieParseOptions,
): express.RequestHandler;
declare namespace cookieParser {
interface CookieParseOptions {
decode?(val: string): string;
}
function JSONCookie(jsonCookie: string): object | undefined;
function JSONCookies<T extends { [key: string]: string }>(jsonCookies: T): { [P in keyof T]: object | undefined };
function signedCookie(cookie: string, secret: string | string[]): string | false;
function signedCookies<T extends { [key: string]: string }>(
cookies: T,
secret: string | string[],
): { [P in keyof T]?: string | false };
}
export = cookieParser;
These definitions were written by Santi Albo, BendingBender, and Sebastian Beltran.
FAQs
Did you know?
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.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.
Security News
Amaro 1.0 lays the groundwork for stable TypeScript support in Node.js, bringing official .ts loading closer to reality.
Research
A deceptive PyPI package posing as an Instagram growth tool collects user credentials and sends them to third-party bot services.