https://github.com/sec-ant/set-cookie-parser
Yet another "Set-Cookie" parser
https://github.com/sec-ant/set-cookie-parser
set-cookie set-cookie-parser
Last synced: 3 months ago
JSON representation
Yet another "Set-Cookie" parser
- Host: GitHub
- URL: https://github.com/sec-ant/set-cookie-parser
- Owner: Sec-ant
- License: mit
- Created: 2023-08-21T13:51:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-25T09:05:57.000Z (11 months ago)
- Last Synced: 2025-01-12T18:54:43.320Z (4 months ago)
- Topics: set-cookie, set-cookie-parser
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@sec-ant/set-cookie-parser/v/latest
- Size: 421 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @sec-ant/set-cookie-parser
Yet another `Set-Cookie` parser.
## Install
```bash
npm i @sec-ant/set-cookie-parser
```## Usage
This package provides three main functions: `parse`, `parseString`, and `splitCookiesString`.
### `parse`
The `parse` function takes a string or an array of strings representing cookies and an options object of type `ParseOptions`. It returns an array of objects representing the parsed cookies if the `map` property of the options object is `false` or not provided. If the `map` property is `true`, it returns an object with the cookie names as keys and the parsed cookies as values.
```ts
import { parse, ParseOptions } from "@sec-ant/set-cookie-parser";const cookies = "name=value; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure";
const options: ParseOptions = {
decodeValues: true,
map: true,
};
const parsedCookies = parse(cookies, options);
console.log(parsedCookies);
```### `parseString`
The `parseString` function takes a string representing a single cookie and an optional configuration object of type `ParseStringOptions`. It returns an object representing the parsed cookie.
```ts
import { parseString, ParseStringOptions } from "@sec-ant/set-cookie-parser";const cookie = "name=value; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure";
const options: ParseStringOptions = {
decodeValues: true,
};
const parsedCookie = parseString(cookie, options);
console.log(parsedCookie);
```### `splitCookiesString`
The `splitCookiesString` function takes a string representing multiple cookies separated by commas. It returns an array of strings representing the individual cookies.
```ts
import { splitCookiesString } from "@sec-ant/set-cookie-parser";const cookies =
"name1=value1; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure, name2=value2; expires=Wed, 21 Oct 2021 07:28:00 GMT; secure";
const splitCookies = splitCookiesString(cookies);
console.log(splitCookies);
```## Configuration
The `parse` and `parseString` functions accept an optional configuration object as the second parameter. The configuration object can have the following properties:
- `decodeValues`: A boolean indicating whether to decode the cookie values using `decodeURIComponent`. Defaults to `true`.
- `map`: A boolean indicating whether to return an object with the cookie names as keys and the parsed cookies as values. Only applicable to the `parse` function. Defaults to `false`.## License
MIT