Skip to content

Commit df81d79

Browse files
committed
Added cookieOptions to actionResult.
1 parent f5c3b25 commit df81d79

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Added
1616

17+
- Added `cookieOptions` to `actionResult`, for customizing the cookie when setting a [flash message](https://superforms.rocks/flash-messages).
1718
- [SuperDebug](https://superforms.rocks/super-debug) now has a `collapsible` prop, that will make the component collapsible on a per-route basis.
1819

1920
## [1.5.3] - 2023-08-16

src/lib/superValidate.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,17 @@ export function superValidateSync<
678678

679679
///////////////////////////////////////////////////////////////////////////////
680680

681+
/**
682+
* Cookie configuration options. The defaults are:
683+
* Path=/; Max-Age=120; SameSite=Strict;
684+
*/
685+
export interface CookieSerializeOptions {
686+
path?: string | undefined;
687+
maxAge?: number | undefined;
688+
sameSite?: 'Lax' | 'Strict' | 'None';
689+
secure?: boolean | undefined;
690+
}
691+
681692
export function actionResult<
682693
T extends Record<string, unknown> | App.Error | string,
683694
Type extends T extends string
@@ -691,8 +702,28 @@ export function actionResult<
691702
| {
692703
status?: number;
693704
message?: Type extends 'redirect' ? App.PageData['flash'] : never;
705+
cookieOptions?: CookieSerializeOptions;
694706
}
695707
) {
708+
function cookieData() {
709+
if (typeof options === 'number' || !options?.message) return '';
710+
711+
const extra = [
712+
`Path=${options?.cookieOptions?.path || '/'}`,
713+
`Max-Age=${options?.cookieOptions?.maxAge || 120}`,
714+
`SameSite=${options?.cookieOptions?.sameSite ?? 'Strict'}`
715+
];
716+
717+
if (options?.cookieOptions?.secure) {
718+
extra.push(`Secure`);
719+
}
720+
721+
return (
722+
`flash=${encodeURIComponent(JSON.stringify(options.message))}; ` +
723+
extra.join('; ')
724+
);
725+
}
726+
696727
const status =
697728
options && typeof options !== 'number' ? options.status : options;
698729

@@ -704,9 +735,7 @@ export function actionResult<
704735
headers:
705736
typeof options === 'object' && options.message
706737
? {
707-
'Set-Cookie': `flash=${encodeURIComponent(
708-
JSON.stringify(options.message)
709-
)}; Path=/; Max-Age=120`
738+
'Set-Cookie': cookieData()
710739
}
711740
: undefined
712741
}

0 commit comments

Comments
 (0)