Skip to content

Commit

Permalink
Merge pull request #69 from greg2012201/consistent-value-type
Browse files Browse the repository at this point in the history
Consistent value type
  • Loading branch information
andreizanik authored May 21, 2024
2 parents bc4b46b + ddcf6e9 commit e679167
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -106,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);
}
Expand Down

0 comments on commit e679167

Please sign in to comment.