fix(organization): On update, organization metadata shouldn't be stringified unless it's already an object.

This commit is contained in:
Bereket Engida
2024-12-02 15:43:50 +03:00
parent bf9a2cf54b
commit 4a885bcc04
3 changed files with 11 additions and 7 deletions

View File

@@ -4,7 +4,7 @@ import { type WritableAtom } from "nanostores";
import type { AtomListener, ClientOptions } from "./types";
import { addCurrentURL, redirectPlugin } from "./fetch-plugins";
import { getSessionAtom } from "./session-atom";
import { betterJSON } from "./parser";
import { parseJSON } from "./parser";
export const getClientConfig = <O extends ClientOptions>(options?: O) => {
/* check if the credentials property is supported. Useful for cf workers */
@@ -19,7 +19,7 @@ export const getClientConfig = <O extends ClientOptions>(options?: O) => {
...(isCredentialsSupported ? { credentials: "include" } : {}),
method: "GET",
jsonParser(text) {
return betterJSON(text, {
return parseJSON(text, {
strict: false,
});
},

View File

@@ -78,7 +78,7 @@ function parseISODate(value: string): Date | null {
return isValidDate(date) ? date : null;
}
export function betterJSONParse<T = unknown>(
function betterJSONParse<T = unknown>(
value: unknown,
options: ParseOptions = {},
): T {
@@ -169,11 +169,11 @@ export function betterJSONParse<T = unknown>(
}
}
export function betterJSON<T = unknown>(
export function parseJSON<T = unknown>(
value: unknown,
options: ParseOptions = { strict: true },
): T {
return betterJSONParse<T>(value, options);
}
export default betterJSON;
export default parseJSON;

View File

@@ -11,6 +11,7 @@ import type {
} from "./schema";
import { BetterAuthError } from "../../error";
import type { AuthContext } from "../../types";
import parseJSON from "../../client/parser";
export const getOrgAdapter = (
context: AuthContext,
@@ -239,7 +240,10 @@ export const getOrgAdapter = (
],
update: {
...data,
metadata: data.metadata ? JSON.stringify(data.metadata) : undefined,
metadata:
typeof data.metadata === "object"
? JSON.stringify(data.metadata)
: data.metadata,
},
});
if (!organization) {
@@ -248,7 +252,7 @@ export const getOrgAdapter = (
return {
...organization,
metadata: organization.metadata
? JSON.parse(organization.metadata)
? parseJSON(organization.metadata)
: undefined,
};
},