Download next.js/examples/api-routes-middleware/utils/cookies.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 583 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/api-routes-middleware/utils/cookies.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/examples/api-routes-middleware/utils/cookies.ts
-
curl -L -o cookies.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/api-routes-middleware/utils/cookies.ts
583 Bytes
| import type { NextApiResponse } from "next"; | |
| import { serialize, CookieSerializeOptions } from "cookie"; | |
| /** | |
| * This sets `cookie` using the `res` object | |
| */ | |
| export const setCookie = ( | |
| res: NextApiResponse, | |
| name: string, | |
| value: unknown, | |
| options: CookieSerializeOptions = {}, | |
| ) => { | |
| const stringValue = | |
| typeof value === "object" ? "j:" + JSON.stringify(value) : String(value); | |
| if (typeof options.maxAge === "number") { | |
| options.expires = new Date(Date.now() + options.maxAge * 1000); | |
| } | |
| res.setHeader("Set-Cookie", serialize(name, stringValue, options)); | |
| }; | |