Skip to content

Commit

Permalink
fix: fix maxAge in express
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiaqi Liu committed Sep 13, 2024
1 parent cbf6da7 commit b90228a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
10 changes: 9 additions & 1 deletion dist/server.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,17 @@ function setCookie(name, val, options) {
} = asyncLocalStorage.getStore() || {};
if (res) {
if (typeof res.cookie === 'function') {
/**
* The unit of maxAge in the specification is seconds,
* but in Express' cookie() method this is milliseconds
*/
const fixMaxAge = typeof options?.maxAge === 'number' ? {
maxAge: options.maxAge * 1000
} : undefined;
res.cookie(name, val, {
...defaultOptions,
...options
...options,
...fixMaxAge
});
if (req?.cookies) {
req.cookies[name] = val;
Expand Down
10 changes: 9 additions & 1 deletion dist/server.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,17 @@ function setCookie(name, val, options) {
} = asyncLocalStorage.getStore() || {};
if (res) {
if (typeof res.cookie === 'function') {
/**
* The unit of maxAge in the specification is seconds,
* but in Express' cookie() method this is milliseconds
*/
const fixMaxAge = typeof options?.maxAge === 'number' ? {
maxAge: options.maxAge * 1000
} : undefined;
res.cookie(name, val, {
...defaultOptions,
...options
...options,
...fixMaxAge
});
if (req?.cookies) {
req.cookies[name] = val;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cookick",
"version": "1.0.1",
"version": "1.0.3",
"description": "Utilities for cookie APIs",
"author": "qiqiboy",
"main": "dist/server.cjs.js",
Expand Down
14 changes: 13 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ export function setCookie(name: string, val: string | number, options?: CookieOp

if (res) {
if (typeof res.cookie === 'function') {
/**
* The unit of maxAge in the specification is seconds,
* but in Express' cookie() method this is milliseconds
*/
const fixMaxAge =
typeof options?.maxAge === 'number'
? {
maxAge: options.maxAge * 1000
}
: undefined;

res.cookie(name, val, {
...defaultOptions,
...options
...options,
...fixMaxAge
});

if (req?.cookies) {
Expand Down

0 comments on commit b90228a

Please sign in to comment.