Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NextRequest and NextResponse #524

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/typescript/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
2 changes: 1 addition & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "next start"
},
"dependencies": {
"next": "10.0.5",
"next": "^12.0.8",
"nookies": "workspace:*",
"react": "17.0.1",
"react-dom": "17.0.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/nookies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"@types/cookie": "0.4.1",
"@types/express": "4.17.13",
"@types/next": "9.0.0",
"@types/next": "^9.0.0",
"@types/node": "14.18.5",
"@types/set-cookie-parser": "2.4.2",
"terser": "5.10.0"
Expand Down
16 changes: 14 additions & 2 deletions packages/nookies/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as cookie from 'cookie'
import * as express from 'express'
import * as next from 'next'
import * as nextServer from 'next/server'
import * as setCookieParser from 'set-cookie-parser'
import { Cookie } from 'set-cookie-parser'

Expand All @@ -9,18 +10,23 @@ import { areCookiesEqual, createCookie, isBrowser } from './utils'
/**
* Parses cookies.
*
* @param ctx NextJS page or API context, express context, null or undefined.
* @param ctx NextJS page, middleware or API context, express context, null or undefined.
* @param options Options that we pass down to `cookie` library.
*/
export function parseCookies(
ctx?:
| Pick<next.NextPageContext, 'req'>
| { req: next.NextApiRequest }
| { req: nextServer.NextRequest }
| { req: express.Request }
| null
| undefined,
options?: cookie.CookieParseOptions,
) {
if (ctx?.req instanceof nextServer.NextRequest) {
return ctx.req.cookies
}

if (ctx?.req?.headers?.cookie) {
return cookie.parse(ctx.req.headers.cookie as string, options)
}
Expand All @@ -35,7 +41,7 @@ export function parseCookies(
/**
* Sets a cookie.
*
* @param ctx NextJS page or API context, express context, null or undefined.
* @param ctx NextJS page, middleware or API context, express context, null or undefined.
* @param name The name of your cookie.
* @param value The value of your cookie.
* @param options Options that we pass down to `cookie` library.
Expand All @@ -44,13 +50,19 @@ export function setCookie(
ctx:
| Pick<next.NextPageContext, 'res'>
| { res: next.NextApiResponse }
| { res: nextServer.NextResponse }
| { res: express.Response }
| null
| undefined,
name: string,
value: string,
options: cookie.CookieSerializeOptions = {},
) {
if (ctx?.res instanceof nextServer.NextResponse) {
ctx.res.cookie(name, value, options)
return {}
}

// SSR
if (ctx?.res?.getHeader && ctx.res.setHeader) {
// Check if response has finished and warn about it.
Expand Down
Loading