Files
formbricks/packages/js/src/index.ts
T
Anshuman Pandey 53ef8771f3 feat: Make formbricks-js ready for public websites (#1470)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Co-authored-by: Johannes <johannes@formbricks.com>
2023-11-12 09:12:58 +00:00

72 lines
1.8 KiB
TypeScript

import { TJsConfigInput } from "@formbricks/types/js";
import { getApi } from "./lib/api";
import { CommandQueue } from "./lib/commandQueue";
import { ErrorHandler } from "./lib/errors";
import { trackAction } from "./lib/actions";
import { initialize } from "./lib/initialize";
import { Logger } from "./lib/logger";
import { checkPageUrl } from "./lib/noCodeActions";
import { resetPerson, setPersonAttribute, setPersonUserId, getPerson, logoutPerson } from "./lib/person";
const logger = Logger.getInstance();
logger.debug("Create command queue");
const queue = new CommandQueue();
const init = async (initConfig: TJsConfigInput) => {
ErrorHandler.init(initConfig.errorHandler);
queue.add(false, initialize, initConfig);
await queue.wait();
};
const setUserId = async (): Promise<void> => {
queue.add(true, setPersonUserId);
await queue.wait();
};
const setEmail = async (email: string): Promise<void> => {
setAttribute("email", email);
await queue.wait();
};
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();
};
const track = async (name: string, properties: any = {}): Promise<void> => {
queue.add<any>(true, trackAction, name, properties);
await queue.wait();
};
const registerRouteChange = async (): Promise<void> => {
queue.add(true, checkPageUrl);
await queue.wait();
};
const formbricks = {
init,
setUserId,
setEmail,
setAttribute,
track,
logout,
reset,
registerRouteChange,
getApi,
getPerson,
};
export type FormbricksType = typeof formbricks;
export default formbricks as FormbricksType;