cookies-next
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -28,6 +28,6 @@ "use strict"; | ||
var isClientSide = function () { return typeof window !== 'undefined'; }; | ||
var isCookiesFromAppRouterMiddleware = function (cookieStore) { | ||
var isCookiesFromAppRouter = function (cookieStore) { | ||
if (!cookieStore) | ||
return false; | ||
return ('getAll' && | ||
return ('getAll' in cookieStore && | ||
'set' in cookieStore && | ||
@@ -37,7 +37,8 @@ typeof cookieStore.getAll === 'function' && | ||
}; | ||
var isContextFromAppRouterMiddleware = function (context) { | ||
return ((!!(context === null || context === void 0 ? void 0 : context.req) && 'cookies' in context.req && isCookiesFromAppRouterMiddleware(context === null || context === void 0 ? void 0 : context.req.cookies)) || | ||
(!!(context === null || context === void 0 ? void 0 : context.res) && 'cookies' in context.res && isCookiesFromAppRouterMiddleware(context === null || context === void 0 ? void 0 : context.res.cookies))); | ||
var isContextFromAppRouter = function (context) { | ||
return ((!!(context === null || context === void 0 ? void 0 : context.req) && 'cookies' in context.req && isCookiesFromAppRouter(context === null || context === void 0 ? void 0 : context.req.cookies)) || | ||
(!!(context === null || context === void 0 ? void 0 : context.res) && 'cookies' in context.res && isCookiesFromAppRouter(context === null || context === void 0 ? void 0 : context.res.cookies)) || | ||
(!!(context === null || context === void 0 ? void 0 : context.cookies) && isCookiesFromAppRouter(context.cookies()))); | ||
}; | ||
var transformAppRouterMiddlewareCookies = function (cookies) { | ||
var transformAppRouterCookies = function (cookies) { | ||
var _cookies = {}; | ||
@@ -66,6 +67,12 @@ cookies.getAll().forEach(function (_a) { | ||
var getCookies = function (options) { | ||
if (isContextFromAppRouterMiddleware(options) && (options === null || options === void 0 ? void 0 : options.req)) | ||
return transformAppRouterMiddlewareCookies(options.req.cookies); | ||
if (isContextFromAppRouter(options)) { | ||
if (options === null || options === void 0 ? void 0 : options.req) { | ||
return transformAppRouterCookies(options.req.cookies); | ||
} | ||
if (options === null || options === void 0 ? void 0 : options.cookies) { | ||
return transformAppRouterCookies(options.cookies()); | ||
} | ||
} | ||
var req; | ||
// DefaultOptions['req] can be casted here because is narrowed by using the fn: isContextFromAppRouterMiddleware | ||
// DefaultOptions['req] can be casted here because is narrowed by using the fn: isContextFromAppRouter | ||
if (options) | ||
@@ -102,4 +109,4 @@ req = options.req; | ||
var setCookie = function (key, data, options) { | ||
if (isContextFromAppRouterMiddleware(options)) { | ||
var req = options.req, res = options.res, restOptions = __rest(options, ["req", "res"]); | ||
if (isContextFromAppRouter(options)) { | ||
var req = options.req, res = options.res, cookiesFn = options.cookies, restOptions = __rest(options, ["req", "res", "cookies"]); | ||
var payload = __assign({ name: key, value: data }, restOptions); | ||
@@ -112,2 +119,5 @@ if (req) { | ||
} | ||
if (cookiesFn) { | ||
cookiesFn().set(payload); | ||
} | ||
return; | ||
@@ -119,3 +129,3 @@ } | ||
if (options) { | ||
// DefaultOptions can be casted here because the AppRouterMiddlewareOptions is narrowed using the fn: isContextFromAppRouterMiddleware | ||
// DefaultOptions can be casted here because the AppRouterMiddlewareOptions is narrowed using the fn: isContextFromAppRouter | ||
var _a = options, req = _a.req, res = _a.res, _options = __rest(_a, ["req", "res"]); | ||
@@ -122,0 +132,0 @@ _req = req; |
@@ -5,3 +5,4 @@ /// <reference types="node" /> | ||
import type { NextRequest, NextResponse } from 'next/server'; | ||
export type OptionsType = DefaultOptions | AppRouterMiddlewareOptions; | ||
import type { cookies } from 'next/headers'; | ||
export type OptionsType = DefaultOptions | AppRouterOptions; | ||
export interface DefaultOptions extends CookieSerializeOptions { | ||
@@ -12,8 +13,11 @@ res?: ServerResponse; | ||
}; | ||
cookies?: CookiesFn; | ||
} | ||
export type AppRouterMiddlewareOptions = { | ||
export type CookiesFn = typeof cookies; | ||
export type AppRouterOptions = { | ||
res?: Response | NextResponse; | ||
req?: Request | NextRequest; | ||
cookies?: CookiesFn; | ||
}; | ||
export type AppRouterMiddlewareCookies = NextResponse['cookies'] | NextRequest['cookies']; | ||
export type AppRouterCookies = NextResponse['cookies'] | NextRequest['cookies']; | ||
export type TmpCookiesObj = { | ||
@@ -20,0 +24,0 @@ [key: string]: string; |
{ | ||
"name": "cookies-next", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Getting, setting and removing cookies on both client and server with next.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
134
README.md
@@ -11,4 +11,3 @@ # cookies-next | ||
- can be used in API handlers | ||
- can be used in appDir middleware | ||
- can be used in appDir route handlers | ||
- can be used in Next.js 13+ | ||
@@ -89,4 +88,17 @@ ## Installation | ||
#### Client Example | ||
In Next.js 13+, you can [read(only)](https://nextjs.org/docs/app/api-reference/functions/cookies) cookies in `Server Components` and read/update them in `Server Actions`. This can be achieved by using the `cookies` function as an option, which is imported from `next/headers`, instead of using `req` and `res`. | ||
#### Client - App Router Example | ||
```ts | ||
'use client' | ||
import { getCookies, setCookie, deleteCookie, getCookie } from 'cookies-next'; | ||
getCookies(); | ||
getCookie('key'); | ||
setCookie('key', 'value'); | ||
deleteCookie('key'); | ||
``` | ||
#### Client - Pages Router Example | ||
```js | ||
@@ -102,4 +114,31 @@ import { getCookies, setCookie, deleteCookie, getCookie } from 'cookies-next'; | ||
#### SSR Example | ||
#### SSR - App Router Example | ||
`/app/page.tsx` | ||
```tsx | ||
import { setCookie, getCookie, getCookies, deleteCookie, hasCookie } from 'cookies-next'; | ||
import { cookies } from 'next/headers'; | ||
const Home = async () => { | ||
// It's not possible to update the cookie in RSC | ||
❌ setCookie("test", "value", { cookies }); 👉🏻// Won't work. | ||
❌ deleteCookie('test1', { cookies }); 👉🏻// Won't work. | ||
✔️ getCookie('test1', { cookies }); | ||
✔️ getCookies({ cookies }); | ||
✔️ hasCookie('test1', { cookies }); | ||
return ( | ||
<main> | ||
<h1>Hello cookies next</h1> | ||
</main> | ||
); | ||
}; | ||
export default Home; | ||
``` | ||
#### SSR - Pages Router Example | ||
`/page/index.js` | ||
@@ -126,5 +165,23 @@ | ||
``` | ||
#### SSR - Server Actions Example | ||
```ts | ||
'use server'; | ||
#### API Example | ||
import { cookies } from 'next/headers'; | ||
import { setCookie, deleteCookie, hasCookie, getCookie, getCookies } from 'cookies-next'; | ||
export async function testAction() { | ||
setCookie('test', 'value', { cookies }); | ||
getCookie('test', { cookies }); | ||
getCookies({ cookies }); | ||
hasCookie('test', { cookies }); | ||
deleteCookie('test', { cookies }); | ||
} | ||
``` | ||
#### API - Pages Router Example | ||
`/page/api/example.js` | ||
@@ -145,3 +202,53 @@ | ||
``` | ||
#### API - App Router Example | ||
`/app/api/hello/route.ts` | ||
```ts | ||
import { cookies } from 'next/headers'; | ||
import type { NextRequest, NextResponse } from 'next/server'; | ||
import { deleteCookie, getCookie, setCookie, hasCookie, getCookies } from 'cookies-next'; | ||
export async function GET(req: NextRequest) { | ||
const res = new NextResponse(); | ||
setCookie('test', 'value', { res, req }); | ||
getCookie('test', { res, req }); | ||
getCookies({ res, req }); | ||
deleteCookie('test', { res, req }); | ||
hasCookie('test', { req, res }); | ||
// provide cookies fn | ||
setCookie('test1', 'value', { cookies }); | ||
getCookie('test1', { cookies }); | ||
getCookies({ cookies }); | ||
deleteCookie('test1', { cookies }); | ||
hasCookie('test1', { cookies }); | ||
return res; | ||
} | ||
``` | ||
#### Middleware | ||
```ts | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
import { getCookie, setCookie, deleteCookie, hasCookie, getCookies } from 'cookies-next'; | ||
import { cookies } from 'next/headers'; | ||
export function middleware(req: NextRequest) { | ||
const res = NextResponse.next(); | ||
setCookie('test', 'value', { res, req }); | ||
hasCookie('test', { req, res }); | ||
deleteCookie('test', { res, req }); | ||
getCookie('test', { res, req }); | ||
getCookies({ res, req }); | ||
❌ setCookie('test', 'value', { cookies }); 👉🏻// Won't work. | ||
// It's not possible to use cookies function from next/headers in middleware. | ||
return res; | ||
} | ||
``` | ||
## API | ||
@@ -156,2 +263,3 @@ | ||
setCookie('key', 'value', { req, res }); // - server side | ||
setCookie({ cookies }); // - server side(route handlers, server actions) | ||
``` | ||
@@ -164,2 +272,3 @@ | ||
getCookies({ req, res }); // - server side | ||
getCookies({ cookies }); // - server side(route handlers, server actions, server components, middleware) | ||
``` | ||
@@ -170,4 +279,5 @@ | ||
```js | ||
getCookie('key'); - client side | ||
getCookie('key', { req, res }); - server side | ||
getCookie('key'); // - client side | ||
getCookie('key', { req, res }); // - server side | ||
getCookie('key', { cookies }); // - server side(route handlers, server actions, server components, middleware) | ||
``` | ||
@@ -180,2 +290,3 @@ | ||
hasCookie('key', { req, res }); // - server side | ||
hasCookie('key', { cookies }); // server side(route handlers, server actions, server components, middleware) | ||
``` | ||
@@ -188,2 +299,3 @@ | ||
deleteCookie('key', { req, res }); // - server side | ||
deleteCookie('key', { cookies }); // - server side(route handlers, server actions) | ||
``` | ||
@@ -211,8 +323,12 @@ | ||
required for server side cookies (API and getServerSideProps) | ||
required for server side cookies (route handlers, middleware, API and getServerSideProps) | ||
##### res | ||
required for server side cookies (API and getServerSideProps) | ||
required for server side cookies (route handlers, middleware, API and getServerSideProps) | ||
##### cookies | ||
required for server actions and can be used in route handlers | ||
##### domain | ||
@@ -219,0 +335,0 @@ |
import { serialize, parse } from 'cookie'; | ||
import type { OptionsType, TmpCookiesObj, CookieValueTypes, AppRouterMiddlewareCookies, DefaultOptions } from './types'; | ||
import type { | ||
OptionsType, | ||
TmpCookiesObj, | ||
CookieValueTypes, | ||
AppRouterCookies, | ||
DefaultOptions, | ||
CookiesFn, | ||
} from './types'; | ||
import type { NextRequest, NextResponse } from 'next/server'; | ||
@@ -8,8 +15,8 @@ export { CookieValueTypes } from './types'; | ||
const isCookiesFromAppRouterMiddleware = ( | ||
cookieStore: TmpCookiesObj | AppRouterMiddlewareCookies | undefined, | ||
): cookieStore is AppRouterMiddlewareCookies => { | ||
const isCookiesFromAppRouter = ( | ||
cookieStore: TmpCookiesObj | AppRouterCookies | undefined, | ||
): cookieStore is AppRouterCookies => { | ||
if (!cookieStore) return false; | ||
return ( | ||
'getAll' && | ||
'getAll' in cookieStore && | ||
'set' in cookieStore && | ||
@@ -21,12 +28,13 @@ typeof cookieStore.getAll === 'function' && | ||
const isContextFromAppRouterMiddleware = ( | ||
const isContextFromAppRouter = ( | ||
context?: OptionsType, | ||
): context is { res?: NextResponse; req?: NextRequest } => { | ||
): context is { res?: NextResponse; req?: NextRequest; cookies?: CookiesFn } => { | ||
return ( | ||
(!!context?.req && 'cookies' in context.req && isCookiesFromAppRouterMiddleware(context?.req.cookies)) || | ||
(!!context?.res && 'cookies' in context.res && isCookiesFromAppRouterMiddleware(context?.res.cookies)) | ||
(!!context?.req && 'cookies' in context.req && isCookiesFromAppRouter(context?.req.cookies)) || | ||
(!!context?.res && 'cookies' in context.res && isCookiesFromAppRouter(context?.res.cookies)) || | ||
(!!context?.cookies && isCookiesFromAppRouter(context.cookies())) | ||
); | ||
}; | ||
const transformAppRouterMiddlewareCookies = (cookies: AppRouterMiddlewareCookies): TmpCookiesObj => { | ||
const transformAppRouterCookies = (cookies: AppRouterCookies): TmpCookiesObj => { | ||
let _cookies: Partial<TmpCookiesObj> = {}; | ||
@@ -56,6 +64,13 @@ | ||
export const getCookies = (options?: OptionsType): TmpCookiesObj => { | ||
if (isContextFromAppRouterMiddleware(options) && options?.req) | ||
return transformAppRouterMiddlewareCookies(options.req.cookies); | ||
if (isContextFromAppRouter(options)) { | ||
if (options?.req) { | ||
return transformAppRouterCookies(options.req.cookies); | ||
} | ||
if (options?.cookies) { | ||
return transformAppRouterCookies(options.cookies()); | ||
} | ||
} | ||
let req; | ||
// DefaultOptions['req] can be casted here because is narrowed by using the fn: isContextFromAppRouterMiddleware | ||
// DefaultOptions['req] can be casted here because is narrowed by using the fn: isContextFromAppRouter | ||
if (options) req = options.req as DefaultOptions['req']; | ||
@@ -95,4 +110,4 @@ | ||
export const setCookie = (key: string, data: any, options?: OptionsType): void => { | ||
if (isContextFromAppRouterMiddleware(options)) { | ||
const { req, res, ...restOptions } = options; | ||
if (isContextFromAppRouter(options)) { | ||
const { req, res, cookies: cookiesFn, ...restOptions } = options; | ||
const payload = { name: key, value: data, ...restOptions }; | ||
@@ -105,2 +120,5 @@ if (req) { | ||
} | ||
if (cookiesFn) { | ||
cookiesFn().set(payload); | ||
} | ||
return; | ||
@@ -112,3 +130,3 @@ } | ||
if (options) { | ||
// DefaultOptions can be casted here because the AppRouterMiddlewareOptions is narrowed using the fn: isContextFromAppRouterMiddleware | ||
// DefaultOptions can be casted here because the AppRouterMiddlewareOptions is narrowed using the fn: isContextFromAppRouter | ||
const { req, res, ..._options } = options as DefaultOptions; | ||
@@ -115,0 +133,0 @@ _req = req; |
import { CookieSerializeOptions } from 'cookie'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import type { NextRequest, NextResponse } from 'next/server'; | ||
import type { cookies } from 'next/headers'; | ||
export type OptionsType = DefaultOptions | AppRouterMiddlewareOptions; | ||
export type OptionsType = DefaultOptions | AppRouterOptions; | ||
export interface DefaultOptions extends CookieSerializeOptions { | ||
@@ -11,6 +12,13 @@ res?: ServerResponse; | ||
}; | ||
cookies?: CookiesFn; | ||
} | ||
export type AppRouterMiddlewareOptions = { res?: Response | NextResponse; req?: Request | NextRequest }; | ||
export type AppRouterMiddlewareCookies = NextResponse['cookies'] | NextRequest['cookies']; | ||
export type CookiesFn = typeof cookies; | ||
export type AppRouterOptions = { | ||
res?: Response | NextResponse; | ||
req?: Request | NextRequest; | ||
cookies?: CookiesFn; | ||
}; | ||
export type AppRouterCookies = NextResponse['cookies'] | NextRequest['cookies']; | ||
export type TmpCookiesObj = { [key: string]: string } | Partial<{ [key: string]: string }>; | ||
export type CookieValueTypes = string | undefined; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28780
377
397