From ea1bea11c388c97bca7b73f61b463c0f0e98a711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B6derlund?= Date: Fri, 15 Dec 2023 01:00:29 +0100 Subject: [PATCH] The redirect function now accept the cookies object. --- CHANGELOG.md | 8 ++++-- README.md | 18 ++++++------- src/lib/server.ts | 32 ++++++++++++++--------- src/routes/(app)/+page.server.ts | 6 ++--- src/routes/(app)/issue-11/+page.server.ts | 3 ++- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0eb017..fc7971f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,15 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [2.2.3] - 2023-12-15 +## [2.3.0] - 2023-12-15 + +### Added + +- The `redirect` function can now take the [cookies](https://kit.svelte.dev/docs/load#cookies) object as well as the whole RequestEvent. ### Fixed -- Added peer dependency support for SvelteKit 2. ([#32](https://github.com/ciscoheat/sveltekit-flash-message/issues/32)) +- Peer dependency support for SvelteKit 2. ([#32](https://github.com/ciscoheat/sveltekit-flash-message/issues/32)) ## [2.2.2] - 2023-11-13 diff --git a/README.md b/README.md index 063db8e..ab11b13 100644 --- a/README.md +++ b/README.md @@ -88,20 +88,20 @@ throw redirect( status: number, location: string, message: App.PageData['flash'], - event: RequestEvent + event: RequestEvent | Cookies ) // Makes a 303 redirect throw redirect( location: string, message: App.PageData['flash'], - event: RequestEvent + event: RequestEvent | Cookies ) // Makes a 303 redirect to the current URL throw redirect( message: App.PageData['flash'], - event: RequestEvent + event: RequestEvent | Cookies ) // For compatibility, the sveltekit signature can also be used, @@ -120,15 +120,15 @@ throw redirect( import { redirect } from 'sveltekit-flash-message/server'; export const actions = { - default: async (event) => { - const form = await event.request.formData(); + default: async ({ request, locals, cookies }) => { + const form = await request.formData(); - await api('POST', `/todos/${event.locals.userid}`, { + await api('POST', `/todos/${locals.userid}`, { text: form.get('text') }); const message = { type: 'success', message: "That's the entrepreneur spirit!" } as const; - throw redirect(message, event); + throw redirect(message, cookies); } }; ``` @@ -141,9 +141,9 @@ export const actions = { import type { RequestEvent } from '@sveltejs/kit'; import { redirect } from 'sveltekit-flash-message/server'; -export const POST = async (event) => { +export const POST = async ({ cookies }) => { const message = { type: 'success', message: 'Endpoint POST successful!' } as const; - throw redirect(303, '/', message, event); + throw redirect(303, '/', message, cookies); }; ``` diff --git a/src/lib/server.ts b/src/lib/server.ts index 794fbc4..57cef0c 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -7,7 +7,7 @@ import type { CookieSerializeOptions } from './cookie-es-main/index.js'; // Cannot change. const cookieName = 'flash'; -export const flashCookieOptions: CookieSerializeOptions & { path: string } = { +export const flashCookieOptions: CookieSerializeOptions = { path: '/', maxAge: 120, httpOnly: false, @@ -62,7 +62,7 @@ export function _loadFlash( //d('Possible fetch request, keeping cookie for client.'); } else { //d('Flash cookie found, clearing'); - event.cookies.delete(cookieName, { path: flashCookieOptions.path }); + event.cookies.delete(cookieName, { path: flashCookieOptions.path ?? '/' }); } try { @@ -98,11 +98,11 @@ export function redirect(status: RedirectStatus, location: string | URL): Return * If thrown during request handling, SvelteKit will return a redirect response. * Make sure you're not catching the thrown redirect, which would prevent SvelteKit from handling it. * @param {App.PageData['flash']} message The flash message. - * @param {RequestEvent} event The event for the load function or form action. + * @param {RequestEvent | Cookies} event The event for the load function or form action. */ export function redirect( message: App.PageData['flash'], - event: RequestEvent + event: RequestEvent | Cookies ): ReturnType; /** @@ -111,12 +111,12 @@ export function redirect( * Make sure you're not catching the thrown redirect, which would prevent SvelteKit from handling it. * @param {string | URL} location The redirect URL/location. * @param {App.PageData['flash']} message The flash message. - * @param {RequestEvent} event The event for the load function or form action. + * @param {RequestEvent | Cookies} event The event for the load function or form action. */ export function redirect( location: string | URL, message: App.PageData['flash'], - event: RequestEvent + event: RequestEvent | Cookies ): ReturnType; /** @@ -126,20 +126,20 @@ export function redirect( * @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308} status The [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages). Must be in the range 300-308. * @param {string | URL} location The redirect URL/location. * @param {App.PageData['flash']} message The flash message. - * @param {RequestEvent} event The event for the load function or form action. + * @param {RequestEvent | Cookies} event The event for the load function or form action. */ export function redirect( status: RedirectStatus, location: string | URL, message: App.PageData['flash'], - event: RequestEvent + event: RequestEvent | Cookies ): ReturnType; export function redirect( status: unknown, location: unknown, message?: unknown, - event?: RequestEvent + event?: RequestEvent | Cookies ): ReturnType { switch (arguments.length) { case 2: @@ -184,12 +184,17 @@ function realRedirect( status: RedirectStatus, location: string | URL, message?: App.PageData['flash'], - event?: RequestEvent + event?: RequestEvent | Cookies ) { if (!message) return redir(status, location.toString()); if (!event) throw new Error('RequestEvent is required for redirecting with flash message'); - event.cookies.set(cookieName, JSON.stringify(message), flashCookieOptions); + const cookies = 'cookies' in event ? event.cookies : event; + + cookies.set(cookieName, JSON.stringify(message), { + ...flashCookieOptions, + path: flashCookieOptions.path ?? '/' + }); return redir(status, location.toString()); } @@ -202,5 +207,8 @@ function realRedirect( */ export function setFlash(message: App.PageData['flash'], event: RequestEvent | Cookies) { const cookies = 'cookies' in event ? event.cookies : event; - cookies.set(cookieName, JSON.stringify(message), flashCookieOptions); + cookies.set(cookieName, JSON.stringify(message), { + ...flashCookieOptions, + path: flashCookieOptions.path ?? '/' + }); } diff --git a/src/routes/(app)/+page.server.ts b/src/routes/(app)/+page.server.ts index 5294256..2c2f9aa 100644 --- a/src/routes/(app)/+page.server.ts +++ b/src/routes/(app)/+page.server.ts @@ -23,7 +23,7 @@ export const actions = { ); }, - enhanced: async (event) => { + enhanced: async ({ cookies }) => { throw redirect( '/posted', [ @@ -32,7 +32,7 @@ export const actions = { text: '+page.server.ts POST to /posted ' + count.next() } ], - event + cookies ); }, @@ -45,7 +45,7 @@ export const actions = { }, async toast(event) { - throw redirect( + redirect( [ { status: 'ok', diff --git a/src/routes/(app)/issue-11/+page.server.ts b/src/routes/(app)/issue-11/+page.server.ts index ae53fb5..7c13f3b 100644 --- a/src/routes/(app)/issue-11/+page.server.ts +++ b/src/routes/(app)/issue-11/+page.server.ts @@ -7,7 +7,8 @@ export const load = async (event) => { text: 'Redirecting back to start.' } as const; console.log('🚀 ~ /issue-11 ~ message:', message); - throw redirect(303, '/', [message], event); + // SvelteKit 2 check (no throw) + redirect(303, '/', [message], event); }; /*