"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// next/index.ts
var next_exports = {};
__export(next_exports, {
withIronSessionApiRoute: () => withIronSessionApiRoute,
withIronSessionSsr: () => withIronSessionSsr
});
module.exports = __toCommonJS(next_exports);
var import_iron_session = require("iron-session");
// src/getPropertyDescriptorForReqSession.ts
function getPropertyDescriptorForReqSession(session) {
return {
enumerable: true,
get() {
return session;
},
set(value) {
const keys = Object.keys(value);
const currentKeys = Object.keys(session);
currentKeys.forEach((key) => {
if (!keys.includes(key)) {
delete session[key];
}
});
keys.forEach((key) => {
session[key] = value[key];
});
}
};
}
// next/index.ts
function withIronSessionApiRoute(handler, options) {
return async function nextApiHandlerWrappedWithIronSession(req, res) {
let sessionOptions;
if (options instanceof Function) {
sessionOptions = await options(req, res);
} else {
sessionOptions = options;
}
const session = await (0, import_iron_session.getIronSession)(req, res, sessionOptions);
Object.defineProperty(
req,
"session",
getPropertyDescriptorForReqSession(session)
);
return handler(req, res);
};
}
function withIronSessionSsr(handler, options) {
return async function nextGetServerSidePropsHandlerWrappedWithIronSession(context) {
let sessionOptions;
if (options instanceof Function) {
sessionOptions = await options(context.req, context.res);
} else {
sessionOptions = options;
}
const session = await (0, import_iron_session.getIronSession)(
context.req,
context.res,
sessionOptions
);
Object.defineProperty(
context.req,
"session",
getPropertyDescriptorForReqSession(session)
);
return handler(context);
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
withIronSessionApiRoute,
withIronSessionSsr
});
//# sourceMappingURL=index.js.map