From 3ce214b8379431507d5f546f38f943204cb5b257 Mon Sep 17 00:00:00 2001 From: Grzegorz Dubiel Date: Mon, 13 May 2024 23:46:33 +0200 Subject: [PATCH 1/2] Stringify any data type. --- src/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index fc8c032..bf5bea6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,10 +43,13 @@ const transformAppRouterCookies = (cookies: AppRouterCookies): TmpCookiesObj => return _cookies; }; -const stringify = (value: string = '') => { +const stringify = (value: any) => { try { - const result = JSON.stringify(value); - return /^[\{\[]/.test(result) ? result : value; + if (typeof value === 'string') { + return value; + } + const stringifiedValue = JSON.stringify(value); + return stringifiedValue; } catch (e) { return value; } From ddcf6e9809f62358882c36e90c06a639154e6065 Mon Sep 17 00:00:00 2001 From: Grzegorz Dubiel Date: Mon, 13 May 2024 23:59:34 +0200 Subject: [PATCH 2/2] Stringify data when app router context has been detected. --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index bf5bea6..1f3ca08 100644 --- a/src/index.ts +++ b/src/index.ts @@ -109,7 +109,7 @@ export const getCookie = (key: string, options?: OptionsType): CookieValueTypes export const setCookie = (key: string, data: any, options?: OptionsType): void => { if (isContextFromAppRouter(options)) { const { req, res, cookies: cookiesFn, ...restOptions } = options; - const payload = { name: key, value: data, ...restOptions }; + const payload = { name: key, value: stringify(data), ...restOptions }; if (req) { req.cookies.set(payload); }