Skip to content

Commit

Permalink
feat: update getFunctionalInterface to return the method being passed…
Browse files Browse the repository at this point in the history
… instead of just executing (#552)

Fixes #524
  • Loading branch information
JasonBerry authored Aug 23, 2024
1 parent 0324139 commit ebb41f8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 36 deletions.
64 changes: 63 additions & 1 deletion lib/__tests__/_getFunctionalInterface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import { getFunctionalInterface } from "../_getFunctionalInterface";
import AlgoliaAnalytics from "../insights";
import type { InsightsClient } from "../types";
import type { InsightsClient, InsightsMethodMap } from "../types";

jest.mock("../../package.json", () => ({
version: "1.0.1"
}));

describe("_getFunctionalInterface", () => {
let aa: InsightsClient;
Expand All @@ -23,4 +27,62 @@ describe("_getFunctionalInterface", () => {
"The method `unknown-function` doesn't exist."
);
});

it.each<keyof InsightsMethodMap>([
"addedToCartObjectIDs",
"addedToCartObjectIDsAfterSearch",
"clickedFilters",
"clickedObjectIDs",
"clickedObjectIDsAfterSearch",
"convertedFilters",
"convertedObjectIDs",
"convertedObjectIDsAfterSearch",
"purchasedObjectIDs",
"purchasedObjectIDsAfterSearch",
"viewedFilters",
"viewedObjectIDs"
])("returns a promise to be waited on for %s call", async (method) => {
aa("init", {
appId: "appId",
apiKey: "apiKey"
});
const result = await aa(method, {
eventName: "click",
objectIDs: ["1", "2"],
index: "index"
});
expect(result).toBe(true);
});

it("returns a promise to be waited on for sendEvents call", async () => {
aa("init", {
appId: "appId",
apiKey: "apiKey"
});
const result = await aa("sendEvents", [
{
eventName: "click",
eventType: "click",
objectIDs: ["1", "2"],
index: "index"
}
]);
expect(result).toBe(true);
});

it("returns values on sync calls", () => {
aa("init", {
appId: "appId",
apiKey: "apiKey",
userToken: "userToken",
authenticatedUserToken: "authenticatedUserToken"
});
expect(aa("getAuthenticatedUserToken")).toBe("authenticatedUserToken");
expect(aa("getUserToken")).toBe("userToken");
expect(aa("getVersion")).toBe("1.0.1");
expect(aa("setAuthenticatedUserToken", "newAuthenticatedUserToken")).toBe(
"newAuthenticatedUserToken"
);
expect(aa("setUserToken", "newUserToken")).toBe("newUserToken");
});
});
10 changes: 5 additions & 5 deletions lib/_getFunctionalInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export function getFunctionalInterface(
instance: AlgoliaAnalytics
): InsightsClient {
return (functionName, ...functionArguments) => {
if (functionName && isFunction((instance as any)[functionName])) {
if (functionName && isFunction(instance[functionName])) {
// @ts-expect-error
instance[functionName](...functionArguments);
} else {
// eslint-disable-next-line no-console
console.warn(`The method \`${functionName}\` doesn't exist.`);
return instance[functionName](...functionArguments);
}
// eslint-disable-next-line no-console
console.warn(`The method \`${functionName}\` doesn't exist.`);
return undefined;
};
}
5 changes: 3 additions & 2 deletions lib/_getVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { isFunction } from "./utils";

export function getVersion(
this: AlgoliaAnalytics,
callback: (version: string) => void
): void {
callback?: (version: string) => void
): string {
if (isFunction(callback)) {
callback(this.version);
}
return this.version;
}
4 changes: 2 additions & 2 deletions lib/_sendEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export function makeSendEvents(requestFn: RequestFnType) {
this: AlgoliaAnalytics,
eventData: InsightsEvent[],
additionalParams?: InsightsAdditionalEventParams
): Promise<boolean> | undefined {
): Promise<boolean> {
if (this._userHasOptedOut) {
return undefined;
return Promise.resolve(false);
}
const hasCredentials =
(!isUndefined(this._apiKey) && !isUndefined(this._appId)) ||
Expand Down
62 changes: 36 additions & 26 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,53 @@ import type {
import type { init } from "./init";
import type { viewedObjectIDs, viewedFilters } from "./view";

type ParamReturnTypeTuple<T extends (...args: any) => any> = [
Parameters<T>,
ReturnType<T>
];
export type InsightsMethodMap = {
init: Parameters<typeof init>;
getVersion: Parameters<typeof getVersion>;
addAlgoliaAgent: Parameters<typeof addAlgoliaAgent>;
setUserToken: Parameters<typeof setUserToken>;
getUserToken: Parameters<typeof getUserToken>;
onUserTokenChange: Parameters<typeof onUserTokenChange>;
setAuthenticatedUserToken: Parameters<typeof setAuthenticatedUserToken>;
getAuthenticatedUserToken: Parameters<typeof getAuthenticatedUserToken>;
onAuthenticatedUserTokenChange: Parameters<
init: ParamReturnTypeTuple<typeof init>;
getVersion: ParamReturnTypeTuple<typeof getVersion>;
addAlgoliaAgent: ParamReturnTypeTuple<typeof addAlgoliaAgent>;
setUserToken: ParamReturnTypeTuple<typeof setUserToken>;
getUserToken: ParamReturnTypeTuple<typeof getUserToken>;
onUserTokenChange: ParamReturnTypeTuple<typeof onUserTokenChange>;
setAuthenticatedUserToken: ParamReturnTypeTuple<
typeof setAuthenticatedUserToken
>;
getAuthenticatedUserToken: ParamReturnTypeTuple<
typeof getAuthenticatedUserToken
>;
onAuthenticatedUserTokenChange: ParamReturnTypeTuple<
typeof onAuthenticatedUserTokenChange
>;
clickedObjectIDsAfterSearch: Parameters<typeof clickedObjectIDsAfterSearch>;
clickedObjectIDs: Parameters<typeof clickedObjectIDs>;
clickedFilters: Parameters<typeof clickedFilters>;
convertedObjectIDsAfterSearch: Parameters<
clickedObjectIDsAfterSearch: ParamReturnTypeTuple<
typeof clickedObjectIDsAfterSearch
>;
clickedObjectIDs: ParamReturnTypeTuple<typeof clickedObjectIDs>;
clickedFilters: ParamReturnTypeTuple<typeof clickedFilters>;
convertedObjectIDsAfterSearch: ParamReturnTypeTuple<
typeof convertedObjectIDsAfterSearch
>;
convertedObjectIDs: Parameters<typeof convertedObjectIDs>;
convertedFilters: Parameters<typeof convertedFilters>;
viewedObjectIDs: Parameters<typeof viewedObjectIDs>;
viewedFilters: Parameters<typeof viewedFilters>;
purchasedObjectIDs: Parameters<typeof purchasedObjectIDs>;
purchasedObjectIDsAfterSearch: Parameters<
convertedObjectIDs: ParamReturnTypeTuple<typeof convertedObjectIDs>;
convertedFilters: ParamReturnTypeTuple<typeof convertedFilters>;
viewedObjectIDs: ParamReturnTypeTuple<typeof viewedObjectIDs>;
viewedFilters: ParamReturnTypeTuple<typeof viewedFilters>;
purchasedObjectIDs: ParamReturnTypeTuple<typeof purchasedObjectIDs>;
purchasedObjectIDsAfterSearch: ParamReturnTypeTuple<
typeof purchasedObjectIDsAfterSearch
>;
addedToCartObjectIDs: Parameters<typeof addedToCartObjectIDs>;
addedToCartObjectIDsAfterSearch: Parameters<
addedToCartObjectIDs: ParamReturnTypeTuple<typeof addedToCartObjectIDs>;
addedToCartObjectIDsAfterSearch: ParamReturnTypeTuple<
typeof addedToCartObjectIDsAfterSearch
>;
sendEvents: Parameters<ReturnType<typeof makeSendEvents>>;
sendEvents: ParamReturnTypeTuple<ReturnType<typeof makeSendEvents>>;
};

type MethodType<MethodName extends keyof InsightsMethodMap> = (
method: MethodName,
...args: InsightsMethodMap[MethodName]
) => void;
...args: InsightsMethodMap[MethodName][0]
) => InsightsMethodMap[MethodName][1];

export type Init = MethodType<"init">;

Expand Down Expand Up @@ -98,8 +108,8 @@ export type SendEvents = MethodType<"sendEvents">;

export type InsightsClient = (<MethodName extends keyof InsightsMethodMap>(
method: MethodName,
...args: InsightsMethodMap[MethodName]
) => void) & { version?: string };
...args: InsightsMethodMap[MethodName][0]
) => InsightsMethodMap[MethodName][1]) & { version?: string };

export type InsightsEventType = "click" | "conversion" | "view";
export type InsightsEventConversionSubType = "addToCart" | "purchase";
Expand Down

0 comments on commit ebb41f8

Please sign in to comment.