"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasCookie = exports.deleteCookie = exports.setCookie = exports.getCookie = exports.getCookies = void 0; var cookie_1 = require("cookie"); var utils_1 = require("../common/utils"); var ensureServerSide = function (context) { if ((0, utils_1.isClientSide)(context)) { throw new Error('You are trying to access cookies on the client side. Please, use the client-side import with `cookies-next/client` instead.'); } }; var isCookiesFromNext = function (cookieStore) { if (!cookieStore) return false; return ('getAll' in cookieStore && 'set' in cookieStore && typeof cookieStore.getAll === 'function' && typeof cookieStore.set === 'function'); }; var isContextFromNext = function (context) { return ((!!(context === null || context === void 0 ? void 0 : context.req) && 'cookies' in context.req && isCookiesFromNext(context.req.cookies)) || (!!(context === null || context === void 0 ? void 0 : context.res) && 'cookies' in context.res && isCookiesFromNext(context.res.cookies)) || (!!context && 'cookies' in context && typeof context.cookies === 'function')); }; var transformAppRouterCookies = function (cookies) { var _cookies = {}; cookies.getAll().forEach(function (_a) { var name = _a.name, value = _a.value; _cookies[name] = value; }); return _cookies; }; var getCookies = function (options) { return __awaiter(void 0, void 0, void 0, function () { var _a, httpRequest; return __generator(this, function (_b) { switch (_b.label) { case 0: ensureServerSide(options); if (!isContextFromNext(options)) return [3 /*break*/, 2]; if (options.req) { return [2 /*return*/, transformAppRouterCookies(options.req.cookies)]; } if (options.res) { return [2 /*return*/, transformAppRouterCookies(options.res.cookies)]; } if (!options.cookies) return [3 /*break*/, 2]; _a = transformAppRouterCookies; return [4 /*yield*/, options.cookies()]; case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; case 2: if (options === null || options === void 0 ? void 0 : options.req) { httpRequest = options.req; } if (httpRequest === null || httpRequest === void 0 ? void 0 : httpRequest.cookies) { return [2 /*return*/, httpRequest.cookies]; } if (httpRequest === null || httpRequest === void 0 ? void 0 : httpRequest.headers.cookie) { return [2 /*return*/, (0, cookie_1.parse)(httpRequest.headers.cookie)]; } return [2 /*return*/, {}]; } }); }); }; exports.getCookies = getCookies; var getCookie = function (key, options) { return __awaiter(void 0, void 0, void 0, function () { var cookies, value; return __generator(this, function (_a) { switch (_a.label) { case 0: ensureServerSide(options); return [4 /*yield*/, getCookies(options)]; case 1: cookies = _a.sent(); value = cookies[key]; if (value === undefined) return [2 /*return*/, undefined]; return [2 /*return*/, (0, utils_1.decode)(value)]; } }); }); }; exports.getCookie = getCookie; var setCookie = function (key, data, options) { return __awaiter(void 0, void 0, void 0, function () { var req_1, res_1, cookiesFn, restOptions, payload, cookieOptions, req, res, _a, _req, _res, rest, cookieStore, currentCookies, cookies, cookies; return __generator(this, function (_b) { switch (_b.label) { case 0: ensureServerSide(options); if (!isContextFromNext(options)) return [3 /*break*/, 3]; req_1 = options.req, res_1 = options.res, cookiesFn = options.cookies, restOptions = __rest(options, ["req", "res", "cookies"]); payload = __assign({ name: key, value: (0, utils_1.stringify)(data) }, restOptions); if (req_1) { req_1.cookies.set(payload); } if (res_1) { res_1.cookies.set(payload); } if (!cookiesFn) return [3 /*break*/, 2]; return [4 /*yield*/, cookiesFn()]; case 1: (_b.sent()).set(payload); _b.label = 2; case 2: return [2 /*return*/]; case 3: cookieOptions = {}; if (options) { _a = options, _req = _a.req, _res = _a.res, rest = __rest(_a, ["req", "res"]); req = _req; res = _res; cookieOptions = rest; } cookieStore = (0, cookie_1.serialize)(key, (0, utils_1.stringify)(data), __assign({ path: '/' }, cookieOptions)); if (res && req) { currentCookies = res.getHeader('Set-Cookie'); if (!Array.isArray(currentCookies)) { currentCookies = !currentCookies ? [] : [String(currentCookies)]; } res.setHeader('Set-Cookie', currentCookies.concat(cookieStore)); if (req && req.cookies) { cookies = req.cookies; data === '' ? delete cookies[key] : (cookies[key] = (0, utils_1.stringify)(data)); } if (req && req.headers && req.headers.cookie) { cookies = (0, cookie_1.parse)(req.headers.cookie); data === '' ? delete cookies[key] : (cookies[key] = (0, utils_1.stringify)(data)); req.headers.cookie = Object.entries(cookies).reduce(function (accum, item) { return accum.concat("".concat(item[0], "=").concat(item[1], ";")); }, ''); } } return [2 /*return*/]; } }); }); }; exports.setCookie = setCookie; var deleteCookie = function (key, options) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { ensureServerSide(options); return [2 /*return*/, setCookie(key, '', __assign(__assign({}, options), { maxAge: -1 }))]; }); }); }; exports.deleteCookie = deleteCookie; var hasCookie = function (key, options) { return __awaiter(void 0, void 0, void 0, function () { var cookie; return __generator(this, function (_a) { switch (_a.label) { case 0: ensureServerSide(options); if (!key) return [2 /*return*/, false]; return [4 /*yield*/, getCookies(options)]; case 1: cookie = _a.sent(); return [2 /*return*/, cookie.hasOwnProperty(key)]; } }); }); }; exports.hasCookie = hasCookie; __exportStar(require("../common/types"), exports);