Convert all attributes and userIds to string in formbricks-js (#688)

* convert any attribute input to string in formbricks-js

* add changeset, increase patch version of formbricks-js
This commit is contained in:
Matti Nannt
2023-08-14 12:26:40 +02:00
committed by GitHub
parent 534dd5050d
commit 01523393db
4 changed files with 30 additions and 14 deletions

View File

@@ -0,0 +1,5 @@
---
"@formbricks/js": patch
---
Convert all attributes and userIds to string in formbricks-js

View File

@@ -6,7 +6,7 @@ import { trackAction } from "./lib/actions";
import { initialize } from "./lib/init";
import { Logger } from "./lib/logger";
import { checkPageUrl } from "./lib/noCodeEvents";
import { resetPerson, setPersonAttribute, setPersonUserId, getPerson } from "./lib/person";
import { resetPerson, setPersonAttribute, setPersonUserId, getPerson, logoutPerson } from "./lib/person";
export type { EnvironmentId, KeyValueData, PersonId, ResponseId, SurveyId } from "@formbricks/api";
@@ -21,7 +21,7 @@ const init = async (initConfig: InitConfig) => {
await queue.wait();
};
const setUserId = async (userId: string): Promise<void> => {
const setUserId = async (userId: string | number): Promise<void> => {
queue.add(true, setPersonUserId, userId);
await queue.wait();
};
@@ -31,12 +31,17 @@ const setEmail = async (email: string): Promise<void> => {
await queue.wait();
};
const setAttribute = async (key: string, value: string): Promise<void> => {
const setAttribute = async (key: string, value: any): Promise<void> => {
queue.add(true, setPersonAttribute, key, value);
await queue.wait();
};
const logout = async (): Promise<void> => {
queue.add(true, logoutPerson);
await queue.wait();
};
const reset = async (): Promise<void> => {
queue.add(true, resetPerson);
await queue.wait();
};
@@ -58,6 +63,7 @@ const formbricks = {
setAttribute,
track,
logout,
reset,
registerRouteChange,
getApi,
getPerson,

View File

@@ -3,7 +3,7 @@ import { checkInitialized } from "./init";
export class CommandQueue {
private queue: {
command: (args: any) => Promise<Result<void, any>> | Result<void, any>;
command: (args: any) => Promise<Result<void, any>> | Result<void, any> | Promise<void>;
checkInitialized: boolean;
commandArgs: any[];
}[] = [];
@@ -13,7 +13,7 @@ export class CommandQueue {
public add<A>(
checkInitialized: boolean = true,
command: (...args: A[]) => Promise<Result<void, any>> | Result<void, any>,
command: (...args: A[]) => Promise<Result<void, any>> | Result<void, any> | Promise<void>,
...args: A[]
) {
this.queue.push({ command, checkInitialized, commandArgs: args });

View File

@@ -118,11 +118,11 @@ export const hasAttributeKey = (key: string): boolean => {
};
export const setPersonUserId = async (
userId: string
userId: string | number
): Promise<Result<void, NetworkError | MissingPersonError | AttributeAlreadyExistsError>> => {
logger.debug("setting userId: " + userId);
// check if attribute already exists with this value
if (hasAttributeValue("userId", userId)) {
if (hasAttributeValue("userId", userId.toString())) {
logger.debug("userId already set to this value. Skipping update.");
return okVoid();
}
@@ -132,7 +132,7 @@ export const setPersonUserId = async (
message: "userId cannot be changed after it has been set. You need to reset first",
});
}
const result = await updatePersonUserId(userId);
const result = await updatePersonUserId(userId.toString());
if (result.ok !== true) return err(result.error);
@@ -145,16 +145,16 @@ export const setPersonUserId = async (
export const setPersonAttribute = async (
key: string,
value: string
value: any
): Promise<Result<void, NetworkError | MissingPersonError>> => {
logger.debug("setting attribute: " + key + " to value: " + value);
logger.debug("Setting attribute: " + key + " to value: " + value);
// check if attribute already exists with this value
if (hasAttributeValue(key, value)) {
logger.debug("attribute already set to this value. Skipping update.");
if (hasAttributeValue(key, value.toString())) {
logger.debug("Attribute already set to this value. Skipping update.");
return okVoid();
}
const result = await updatePersonAttribute(key, value);
const result = await updatePersonAttribute(key, value.toString());
let error: NetworkError | MissingPersonError;
@@ -176,9 +176,14 @@ export const setPersonAttribute = async (
return okVoid();
};
export const logoutPerson = async (): Promise<void> => {
logger.debug("Resetting state");
config.update({ state: undefined });
};
export const resetPerson = async (): Promise<Result<void, NetworkError>> => {
logger.debug("Resetting state & getting new state from backend");
config.update({ state: undefined });
await logoutPerson();
try {
await sync();
return okVoid();