fix: type errors in js sdk (#1115)

This commit is contained in:
Nafees Nazik
2023-10-17 15:49:12 +05:30
committed by GitHub
parent 75dc3c6d44
commit 113a5a5653
3 changed files with 27 additions and 16 deletions

View File

@@ -5,7 +5,7 @@ export class CommandQueue {
private queue: {
command: (args: any) => Promise<Result<void, any>> | Result<void, any> | Promise<void>;
checkInitialized: boolean;
commandArgs: any[];
commandArgs: any[any];
}[] = [];
private running: boolean = false;
private resolvePromise: (() => void) | null = null;
@@ -47,10 +47,7 @@ export class CommandQueue {
if (initResult && initResult.ok !== true) errorHandler.handle(initResult.error);
}
const result = (await currentItem.command.apply(null, currentItem.commandArgs as [any])) as Result<
void,
any
>;
const result = (await currentItem?.command.apply(null, currentItem?.commandArgs)) as Result<void, any>;
if (!result) continue;

View File

@@ -2,13 +2,17 @@ import { Logger } from "./logger";
export type { ErrorHandler as IErrorHandler } from "@formbricks/types/js";
export type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E };
export type ResultError<T> = { ok: false; error: T };
export type ResultOk<T> = { ok: true; value: T };
export type Result<T, E = Error> = ResultOk<T> | ResultError<E>;
export const ok = <T, E>(value: T): Result<T, E> => ({ ok: true, value });
export const okVoid = <E>(): Result<void, E> => ({ ok: true, value: undefined });
export const err = <E = Error>(error: E): Result<never, E> => ({
export const err = <E = Error>(error: E): ResultError<E> => ({
ok: false,
error,
});

View File

@@ -1,7 +1,16 @@
import type { TActionClass } from "@formbricks/types/v1/actionClasses";
import type { TActionClassPageUrlRule } from "@formbricks/types/v1/actionClasses";
import { Config } from "./config";
import { ErrorHandler, InvalidMatchTypeError, NetworkError, Result, err, match, ok, okVoid } from "./errors";
import {
ErrorHandler,
InvalidMatchTypeError,
NetworkError,
Result,
err,
match,
ok,
okVoid,
} from "./errors";
import { trackAction } from "./actions";
import { Logger } from "./logger";
@@ -26,15 +35,15 @@ export const checkPageUrl = async (): Promise<Result<void, InvalidMatchTypeError
}
for (const event of pageUrlEvents) {
const { noCodeConfig } = event;
const { pageUrl } = noCodeConfig ?? {};
if (!pageUrl) {
if (!event.noCodeConfig?.pageUrl) {
continue;
}
const match = checkUrlMatch(window.location.href, pageUrl.value, pageUrl.rule as TActionClassPageUrlRule);
const {
noCodeConfig: { pageUrl },
} = event;
const match = checkUrlMatch(window.location.href, pageUrl.value, pageUrl.rule);
if (match.ok !== true) return err(match.error);
@@ -111,6 +120,7 @@ export const checkClickMatch = (event: MouseEvent) => {
const innerHtml = action.noCodeConfig?.innerHtml?.value;
const cssSelectors = action.noCodeConfig?.cssSelector?.value;
const pageUrl = action.noCodeConfig?.pageUrl?.value;
const pageUrlRule = action.noCodeConfig?.pageUrl?.rule;
if (!innerHtml && !cssSelectors && !pageUrl) {
return;
@@ -129,8 +139,8 @@ export const checkClickMatch = (event: MouseEvent) => {
}
}
}
if (pageUrl) {
const urlMatch = checkUrlMatch(window.location.href, pageUrl, action.noCodeConfig?.pageUrl?.rule!);
if (pageUrl && pageUrlRule) {
const urlMatch = checkUrlMatch(window.location.href, pageUrl, pageUrlRule);
if (!urlMatch.ok || !urlMatch.value) {
return;
}