iron-session 6.0.5 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -417,6 +417,22 @@ export default async function sendEmailRoute(req, res) {
417
417
  }
418
418
  ```
419
419
 
420
+ The default `ttl` for such seals is 14 days. To specify a `ttl`, provide it in seconds like so:
421
+
422
+ ```ts
423
+ const fifteenMinutesInSeconds = 15 * 60;
424
+
425
+ const seal = await sealData(
426
+ {
427
+ userId: user.id,
428
+ },
429
+ {
430
+ password: "complex_password_at_least_32_characters_long",
431
+ ttl: fifteenMinutesInSeconds,
432
+ },
433
+ );
434
+ ```
435
+
420
436
  **Login the user automatically and redirect:**
421
437
 
422
438
  ```ts
@@ -570,7 +586,7 @@ Only two options are required: `password` and `cookieName`. Everything else is a
570
586
  }
571
587
  ```
572
588
 
573
- ### Next.js: withIronSessionApiRoute(handler, ironOptions)
589
+ ### Next.js: withIronSessionApiRoute(handler, ironOptions | (req: NextApiRequest, res: NextApiResponse) => IronSessionOptions | Promise\<IronSessionOptions\>)
574
590
 
575
591
  Wraps a [Next.js API Route](https://nextjs.org/docs/api-routes/dynamic-api-routes) and adds a `session` object to the request.
576
592
 
@@ -590,9 +606,30 @@ export default withIronSessionApiRoute(
590
606
  },
591
607
  },
592
608
  );
609
+
610
+ // You can also pass an async or sync function which takes request and response object and return IronSessionOptions
611
+ export default withIronSessionApiRoute(
612
+ function userRoute(req, res) {
613
+ res.send({ user: req.session.user });
614
+ },
615
+ (req, res) => {
616
+ // Infer max cookie from request
617
+ const maxCookieAge = getMaxCookieAge(req);
618
+ return {
619
+ cookieName: "myapp_cookiename",
620
+ password: "complex_password_at_least_32_characters_long",
621
+ // secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
622
+ cookieOptions: {
623
+ // setMaxCookie age here.
624
+ maxCookieAge,
625
+ secure: process.env.NODE_ENV === "production",
626
+ },
627
+ };
628
+ },
629
+ );
593
630
  ```
594
631
 
595
- ### Next.js: withIronSessionSsr(handler, ironOptions)
632
+ ### Next.js: withIronSessionSsr(handler, ironOptions | (req: IncomingMessage, res: ServerResponse) => IronSessionOptions | Promise\<IronSessionOptions\>)
596
633
 
597
634
  Wraps a [Next.js getServerSideProps](https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering) and adds a `session` object to the request of the context.
598
635
 
@@ -616,6 +653,27 @@ export const getServerSideProps = withIronSessionSsr(
616
653
  },
617
654
  },
618
655
  );
656
+
657
+ // You can also pass an async or sync function which takes request and response object and return IronSessionOptions
658
+ export const getServerSideProps = withIronSessionSsr(
659
+ async function getServerSideProps({ req }) {
660
+ return {
661
+ props: {
662
+ user: req.session.user,
663
+ },
664
+ };
665
+ },
666
+ (req, res) => {
667
+ return {
668
+ cookieName: "myapp_cookiename",
669
+ password: "complex_password_at_least_32_characters_long",
670
+ // secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
671
+ cookieOptions: {
672
+ secure: process.env.NODE_ENV === "production",
673
+ },
674
+ };
675
+ },
676
+ );
619
677
  ```
620
678
 
621
679
  ### Express: ironSession(ironOptions)
@@ -1,11 +1,14 @@
1
- import { NextApiHandler, GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
1
+ import { NextApiHandler, GetServerSidePropsContext, GetServerSidePropsResult, NextApiRequest, NextApiResponse } from 'next';
2
2
  import { IronSessionOptions } from 'iron-session';
3
+ import { IncomingMessage, ServerResponse } from 'http';
3
4
 
4
- declare function withIronSessionApiRoute(handler: NextApiHandler, options: IronSessionOptions): NextApiHandler;
5
+ declare type GetIronSessionApiOptions = (request: NextApiRequest, response: NextApiResponse) => Promise<IronSessionOptions> | IronSessionOptions;
6
+ declare function withIronSessionApiRoute(handler: NextApiHandler, options: IronSessionOptions | GetIronSessionApiOptions): NextApiHandler;
7
+ declare type GetIronSessionSSROptions = (request: IncomingMessage, response: ServerResponse) => Promise<IronSessionOptions> | IronSessionOptions;
5
8
  declare function withIronSessionSsr<P extends {
6
9
  [key: string]: unknown;
7
10
  } = {
8
11
  [key: string]: unknown;
9
- }>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, options: IronSessionOptions): (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
12
+ }>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, options: IronSessionOptions | GetIronSessionSSROptions): (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
10
13
 
11
14
  export { withIronSessionApiRoute, withIronSessionSsr };
@@ -54,6 +54,9 @@ function getPropertyDescriptorForReqSession(session) {
54
54
  // next/index.ts
55
55
  function withIronSessionApiRoute(handler, options) {
56
56
  return async function nextApiHandlerWrappedWithIronSession(req, res) {
57
+ if (options instanceof Function) {
58
+ options = await options(req, res);
59
+ }
57
60
  const session = await (0, import_iron_session.getIronSession)(req, res, options);
58
61
  Object.defineProperty(req, "session", getPropertyDescriptorForReqSession(session));
59
62
  return handler(req, res);
@@ -61,6 +64,9 @@ function withIronSessionApiRoute(handler, options) {
61
64
  }
62
65
  function withIronSessionSsr(handler, options) {
63
66
  return async function nextGetServerSidePropsHandlerWrappedWithIronSession(context) {
67
+ if (options instanceof Function) {
68
+ options = await options(context.req, context.res);
69
+ }
64
70
  const session = await (0, import_iron_session.getIronSession)(context.req, context.res, options);
65
71
  Object.defineProperty(context.req, "session", getPropertyDescriptorForReqSession(session));
66
72
  return handler(context);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../index.ts", "../../src/getPropertyDescriptorForReqSession.ts"],
4
- "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAMA,0BAA+B;;;ACJhB,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADdtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AACnE,UAAM,UAAU,MAAM,wCAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAIjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AACA,UAAM,UAAU,MAAM,wCAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
4
+ "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n NextApiRequest,\n NextApiResponse,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\nimport { IncomingMessage, ServerResponse } from \"http\";\n\n// Argument types based on getIronSession function\ntype GetIronSessionApiOptions = (\n request: NextApiRequest,\n response: NextApiResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions | GetIronSessionApiOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(req, res);\n }\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\n// Argument type based on the SSR context\ntype GetIronSessionSSROptions = (\n request: IncomingMessage,\n response: ServerResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions | GetIronSessionSSROptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(context.req, context.res);\n }\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAQA,0BAA+B;;;ACNhB,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADLtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AAEnE,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,KAAK;AAAA;AAE/B,UAAM,UAAU,MAAM,wCAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAUjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AAEA,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,QAAQ,KAAK,QAAQ;AAAA;AAE/C,UAAM,UAAU,MAAM,wCAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
6
6
  "names": []
7
7
  }
@@ -26,6 +26,9 @@ function getPropertyDescriptorForReqSession(session) {
26
26
  // next/index.ts
27
27
  function withIronSessionApiRoute(handler, options) {
28
28
  return async function nextApiHandlerWrappedWithIronSession(req, res) {
29
+ if (options instanceof Function) {
30
+ options = await options(req, res);
31
+ }
29
32
  const session = await getIronSession(req, res, options);
30
33
  Object.defineProperty(req, "session", getPropertyDescriptorForReqSession(session));
31
34
  return handler(req, res);
@@ -33,6 +36,9 @@ function withIronSessionApiRoute(handler, options) {
33
36
  }
34
37
  function withIronSessionSsr(handler, options) {
35
38
  return async function nextGetServerSidePropsHandlerWrappedWithIronSession(context) {
39
+ if (options instanceof Function) {
40
+ options = await options(context.req, context.res);
41
+ }
36
42
  const session = await getIronSession(context.req, context.res, options);
37
43
  Object.defineProperty(context.req, "session", getPropertyDescriptorForReqSession(session));
38
44
  return handler(context);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../index.ts", "../../src/getPropertyDescriptorForReqSession.ts"],
4
- "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
- "mappings": ";AAMA;;;ACJe,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADdtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AACnE,UAAM,UAAU,MAAM,eAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAIjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AACA,UAAM,UAAU,MAAM,eAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
4
+ "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n NextApiRequest,\n NextApiResponse,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\nimport { IncomingMessage, ServerResponse } from \"http\";\n\n// Argument types based on getIronSession function\ntype GetIronSessionApiOptions = (\n request: NextApiRequest,\n response: NextApiResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions | GetIronSessionApiOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(req, res);\n }\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\n// Argument type based on the SSR context\ntype GetIronSessionSSROptions = (\n request: IncomingMessage,\n response: ServerResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions | GetIronSessionSSROptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(context.req, context.res);\n }\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
+ "mappings": ";AAQA;;;ACNe,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADLtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AAEnE,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,KAAK;AAAA;AAE/B,UAAM,UAAU,MAAM,eAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAUjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AAEA,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,QAAQ,KAAK,QAAQ;AAAA;AAE/C,UAAM,UAAU,MAAM,eAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
6
6
  "names": []
7
7
  }
package/next/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
- import { NextApiHandler, GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
1
+ import { NextApiHandler, GetServerSidePropsContext, GetServerSidePropsResult, NextApiRequest, NextApiResponse } from 'next';
2
2
  import { IronSessionOptions } from 'iron-session';
3
+ import { IncomingMessage, ServerResponse } from 'http';
3
4
 
4
- declare function withIronSessionApiRoute(handler: NextApiHandler, options: IronSessionOptions): NextApiHandler;
5
+ declare type GetIronSessionApiOptions = (request: NextApiRequest, response: NextApiResponse) => Promise<IronSessionOptions> | IronSessionOptions;
6
+ declare function withIronSessionApiRoute(handler: NextApiHandler, options: IronSessionOptions | GetIronSessionApiOptions): NextApiHandler;
7
+ declare type GetIronSessionSSROptions = (request: IncomingMessage, response: ServerResponse) => Promise<IronSessionOptions> | IronSessionOptions;
5
8
  declare function withIronSessionSsr<P extends {
6
9
  [key: string]: unknown;
7
10
  } = {
8
11
  [key: string]: unknown;
9
- }>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, options: IronSessionOptions): (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
12
+ }>(handler: (context: GetServerSidePropsContext) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>, options: IronSessionOptions | GetIronSessionSSROptions): (context: GetServerSidePropsContext) => Promise<GetServerSidePropsResult<P>>;
10
13
 
11
14
  export { withIronSessionApiRoute, withIronSessionSsr };
package/next/index.js CHANGED
@@ -54,6 +54,9 @@ function getPropertyDescriptorForReqSession(session) {
54
54
  // next/index.ts
55
55
  function withIronSessionApiRoute(handler, options) {
56
56
  return async function nextApiHandlerWrappedWithIronSession(req, res) {
57
+ if (options instanceof Function) {
58
+ options = await options(req, res);
59
+ }
57
60
  const session = await (0, import_iron_session.getIronSession)(req, res, options);
58
61
  Object.defineProperty(req, "session", getPropertyDescriptorForReqSession(session));
59
62
  return handler(req, res);
@@ -61,6 +64,9 @@ function withIronSessionApiRoute(handler, options) {
61
64
  }
62
65
  function withIronSessionSsr(handler, options) {
63
66
  return async function nextGetServerSidePropsHandlerWrappedWithIronSession(context) {
67
+ if (options instanceof Function) {
68
+ options = await options(context.req, context.res);
69
+ }
64
70
  const session = await (0, import_iron_session.getIronSession)(context.req, context.res, options);
65
71
  Object.defineProperty(context.req, "session", getPropertyDescriptorForReqSession(session));
66
72
  return handler(context);
package/next/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../index.ts", "../../src/getPropertyDescriptorForReqSession.ts"],
4
- "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAMA,0BAA+B;;;ACJhB,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADdtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AACnE,UAAM,UAAU,MAAM,wCAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAIjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AACA,UAAM,UAAU,MAAM,wCAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
4
+ "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n NextApiRequest,\n NextApiResponse,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\nimport { IncomingMessage, ServerResponse } from \"http\";\n\n// Argument types based on getIronSession function\ntype GetIronSessionApiOptions = (\n request: NextApiRequest,\n response: NextApiResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions | GetIronSessionApiOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(req, res);\n }\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\n// Argument type based on the SSR context\ntype GetIronSessionSSROptions = (\n request: IncomingMessage,\n response: ServerResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions | GetIronSessionSSROptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(context.req, context.res);\n }\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAQA,0BAA+B;;;ACNhB,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADLtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AAEnE,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,KAAK;AAAA;AAE/B,UAAM,UAAU,MAAM,wCAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAUjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AAEA,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,QAAQ,KAAK,QAAQ;AAAA;AAE/C,UAAM,UAAU,MAAM,wCAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
6
6
  "names": []
7
7
  }
package/next/index.mjs CHANGED
@@ -26,6 +26,9 @@ function getPropertyDescriptorForReqSession(session) {
26
26
  // next/index.ts
27
27
  function withIronSessionApiRoute(handler, options) {
28
28
  return async function nextApiHandlerWrappedWithIronSession(req, res) {
29
+ if (options instanceof Function) {
30
+ options = await options(req, res);
31
+ }
29
32
  const session = await getIronSession(req, res, options);
30
33
  Object.defineProperty(req, "session", getPropertyDescriptorForReqSession(session));
31
34
  return handler(req, res);
@@ -33,6 +36,9 @@ function withIronSessionApiRoute(handler, options) {
33
36
  }
34
37
  function withIronSessionSsr(handler, options) {
35
38
  return async function nextGetServerSidePropsHandlerWrappedWithIronSession(context) {
39
+ if (options instanceof Function) {
40
+ options = await options(context.req, context.res);
41
+ }
36
42
  const session = await getIronSession(context.req, context.res, options);
37
43
  Object.defineProperty(context.req, "session", getPropertyDescriptorForReqSession(session));
38
44
  return handler(context);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../index.ts", "../../src/getPropertyDescriptorForReqSession.ts"],
4
- "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
- "mappings": ";AAMA;;;ACJe,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADdtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AACnE,UAAM,UAAU,MAAM,eAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAIjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AACA,UAAM,UAAU,MAAM,eAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
4
+ "sourcesContent": ["import type {\n NextApiHandler,\n GetServerSidePropsContext,\n GetServerSidePropsResult,\n NextApiRequest,\n NextApiResponse,\n} from \"next\";\nimport type { IronSessionOptions } from \"iron-session\";\nimport { getIronSession } from \"iron-session\";\nimport getPropertyDescriptorForReqSession from \"../src/getPropertyDescriptorForReqSession\";\nimport { IncomingMessage, ServerResponse } from \"http\";\n\n// Argument types based on getIronSession function\ntype GetIronSessionApiOptions = (\n request: NextApiRequest,\n response: NextApiResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionApiRoute(\n handler: NextApiHandler,\n options: IronSessionOptions | GetIronSessionApiOptions,\n): NextApiHandler {\n return async function nextApiHandlerWrappedWithIronSession(req, res) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(req, res);\n }\n const session = await getIronSession(req, res, options);\n\n // we define req.session as being enumerable (so console.log(req) shows it)\n // and we also want to allow people to do:\n // req.session = { admin: true }; or req.session = {...req.session, admin: true};\n // req.session.save();\n Object.defineProperty(\n req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(req, res);\n };\n}\n\n// Argument type based on the SSR context\ntype GetIronSessionSSROptions = (\n request: IncomingMessage,\n response: ServerResponse,\n) => Promise<IronSessionOptions> | IronSessionOptions;\n\nexport function withIronSessionSsr<\n P extends { [key: string]: unknown } = { [key: string]: unknown },\n>(\n handler: (\n context: GetServerSidePropsContext,\n ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,\n options: IronSessionOptions | GetIronSessionSSROptions,\n) {\n return async function nextGetServerSidePropsHandlerWrappedWithIronSession(\n context: GetServerSidePropsContext,\n ) {\n // If options is a function, call it and assign the results back.\n if (options instanceof Function) {\n options = await options(context.req, context.res);\n }\n const session = await getIronSession(context.req, context.res, options);\n Object.defineProperty(\n context.req,\n \"session\",\n getPropertyDescriptorForReqSession(session),\n );\n return handler(context);\n };\n}\n", "import type { IronSession } from \".\";\n\nexport default function getPropertyDescriptorForReqSession(\n session: IronSession,\n): PropertyDescriptor {\n return {\n enumerable: true,\n get() {\n return session;\n },\n set(value) {\n const keys = Object.keys(value);\n const currentKeys = Object.keys(session);\n\n currentKeys.forEach((key) => {\n if (!keys.includes(key)) {\n // @ts-ignore See comment in IronSessionData interface\n delete session[key];\n }\n });\n\n keys.forEach((key) => {\n // @ts-ignore See comment in IronSessionData interface\n session[key] = value[key];\n });\n },\n };\n}\n"],
5
+ "mappings": ";AAQA;;;ACNe,4CACb,SACoB;AACpB,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,MAAM;AACJ,aAAO;AAAA;AAAA,IAET,IAAI,OAAO;AACT,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,cAAc,OAAO,KAAK;AAEhC,kBAAY,QAAQ,CAAC,QAAQ;AAC3B,YAAI,CAAC,KAAK,SAAS,MAAM;AAEvB,iBAAO,QAAQ;AAAA;AAAA;AAInB,WAAK,QAAQ,CAAC,QAAQ;AAEpB,gBAAQ,OAAO,MAAM;AAAA;AAAA;AAAA;AAAA;;;ADLtB,iCACL,SACA,SACgB;AAChB,SAAO,oDAAoD,KAAK,KAAK;AAEnE,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,KAAK;AAAA;AAE/B,UAAM,UAAU,MAAM,eAAe,KAAK,KAAK;AAM/C,WAAO,eACL,KACA,WACA,mCAAmC;AAErC,WAAO,QAAQ,KAAK;AAAA;AAAA;AAUjB,4BAGL,SAGA,SACA;AACA,SAAO,mEACL,SACA;AAEA,QAAI,mBAAmB,UAAU;AAC/B,gBAAU,MAAM,QAAQ,QAAQ,KAAK,QAAQ;AAAA;AAE/C,UAAM,UAAU,MAAM,eAAe,QAAQ,KAAK,QAAQ,KAAK;AAC/D,WAAO,eACL,QAAQ,KACR,WACA,mCAAmC;AAErC,WAAO,QAAQ;AAAA;AAAA;",
6
6
  "names": []
7
7
  }
@@ -60,6 +60,31 @@ test("withIronSessionApiRoute: req.session.save creates a cookie", async () => {
60
60
  await wrappedHandler(getDefaultReq(), getDefaultRes());
61
61
  });
62
62
 
63
+ test("withIconSessionApiRoute: IronSessionOptions passed as a function works correctly", async () => {
64
+ const wrappedHandler = withIronSessionApiRoute(
65
+ async function handler(req, res) {
66
+ req.session.user = { id: 200 };
67
+ await req.session.save();
68
+ const headerValue = (res.setHeader as jest.Mock).mock.calls[0][1];
69
+ const cookie = headerValue[0];
70
+ const cookieParams = cookie.split(";").slice(1).join(";");
71
+ const cookieName = cookie.split("=")[0];
72
+
73
+ expect(cookieParams).toMatchInlineSnapshot(
74
+ `" Max-Age=60; Path=/; HttpOnly; Secure; SameSite=Lax"`,
75
+ );
76
+ expect(cookieName).toBe("dynamic-cookie-name");
77
+ },
78
+ () => ({
79
+ cookieName: "dynamic-cookie-name",
80
+ password,
81
+ ttl: 120,
82
+ }),
83
+ );
84
+
85
+ await wrappedHandler(getDefaultReq(), getDefaultRes());
86
+ });
87
+
63
88
  test("withIronSessionApiRoute: req.session.destroy removes the cookie", async () => {
64
89
  const wrappedHandler = withIronSessionApiRoute(
65
90
  async function handler(req, res) {
package/next/index.ts CHANGED
@@ -2,16 +2,29 @@ import type {
2
2
  NextApiHandler,
3
3
  GetServerSidePropsContext,
4
4
  GetServerSidePropsResult,
5
+ NextApiRequest,
6
+ NextApiResponse,
5
7
  } from "next";
6
8
  import type { IronSessionOptions } from "iron-session";
7
9
  import { getIronSession } from "iron-session";
8
10
  import getPropertyDescriptorForReqSession from "../src/getPropertyDescriptorForReqSession";
11
+ import { IncomingMessage, ServerResponse } from "http";
12
+
13
+ // Argument types based on getIronSession function
14
+ type GetIronSessionApiOptions = (
15
+ request: NextApiRequest,
16
+ response: NextApiResponse,
17
+ ) => Promise<IronSessionOptions> | IronSessionOptions;
9
18
 
10
19
  export function withIronSessionApiRoute(
11
20
  handler: NextApiHandler,
12
- options: IronSessionOptions,
21
+ options: IronSessionOptions | GetIronSessionApiOptions,
13
22
  ): NextApiHandler {
14
23
  return async function nextApiHandlerWrappedWithIronSession(req, res) {
24
+ // If options is a function, call it and assign the results back.
25
+ if (options instanceof Function) {
26
+ options = await options(req, res);
27
+ }
15
28
  const session = await getIronSession(req, res, options);
16
29
 
17
30
  // we define req.session as being enumerable (so console.log(req) shows it)
@@ -27,17 +40,27 @@ export function withIronSessionApiRoute(
27
40
  };
28
41
  }
29
42
 
43
+ // Argument type based on the SSR context
44
+ type GetIronSessionSSROptions = (
45
+ request: IncomingMessage,
46
+ response: ServerResponse,
47
+ ) => Promise<IronSessionOptions> | IronSessionOptions;
48
+
30
49
  export function withIronSessionSsr<
31
50
  P extends { [key: string]: unknown } = { [key: string]: unknown },
32
51
  >(
33
52
  handler: (
34
53
  context: GetServerSidePropsContext,
35
54
  ) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>>,
36
- options: IronSessionOptions,
55
+ options: IronSessionOptions | GetIronSessionSSROptions,
37
56
  ) {
38
57
  return async function nextGetServerSidePropsHandlerWrappedWithIronSession(
39
58
  context: GetServerSidePropsContext,
40
59
  ) {
60
+ // If options is a function, call it and assign the results back.
61
+ if (options instanceof Function) {
62
+ options = await options(context.req, context.res);
63
+ }
41
64
  const session = await getIronSession(context.req, context.res, options);
42
65
  Object.defineProperty(
43
66
  context.req,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iron-session",
3
- "version": "6.0.5",
3
+ "version": "6.1.0",
4
4
  "description": "Node.js stateless session utility using signed and encrypted cookies to store data. Works with Next.js, Express, NestJs, Fastify, and any Node.js HTTP framework.",
5
5
  "keywords": [
6
6
  "Next.js",
@@ -125,23 +125,23 @@
125
125
  "@types/cookie": "^0.4.0",
126
126
  "@types/express": "^4.17.13",
127
127
  "@types/node": "^16.11.7",
128
- "cookie": "^0.4.1"
128
+ "cookie": "^0.4.2"
129
129
  },
130
130
  "devDependencies": {
131
- "@swc/core": "^1.2.107",
132
- "@swc/jest": "0.2.5",
131
+ "@swc/core": "^1.2.156",
132
+ "@swc/jest": "^0.2.20",
133
133
  "@tsconfig/node12": "1.0.9",
134
- "@types/jest": "27.0.2",
135
- "@typescript-eslint/eslint-plugin": "5.3.0",
136
- "@typescript-eslint/parser": "^5.3.1",
137
- "concurrently": "6.3.0",
138
- "eslint": "^8.2.0",
139
- "jest": "27.3.1",
140
- "prettier": "2.4.1",
141
- "prettier-plugin-packagejson": "2.2.13",
134
+ "@types/jest": "^27.4.1",
135
+ "@typescript-eslint/eslint-plugin": "^5.15.0",
136
+ "@typescript-eslint/parser": "^5.15.0",
137
+ "concurrently": "^7.0.0",
138
+ "eslint": "^8.11.0",
139
+ "jest": "^27.5.1",
140
+ "prettier": "^2.6.0",
141
+ "prettier-plugin-packagejson": "^2.2.16",
142
142
  "rimraf": "3.0.2",
143
143
  "tsup": "5.6.0",
144
- "typescript": "4.4.4"
144
+ "typescript": "^4.6.2"
145
145
  },
146
146
  "peerDependencies": {
147
147
  "express": ">=4",