Skip to content

Commit

Permalink
feat: expose version string from functional interface (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab authored Mar 7, 2023
1 parent ab78587 commit 8a3f8f9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/__tests__/_createInsightsClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createInsightsClient } from "../_createInsightsClient";
import { version } from "../../package.json";

describe("createInsightsClient", () => {
beforeEach(() => {
Expand All @@ -9,6 +10,12 @@ describe("createInsightsClient", () => {
expect(typeof createInsightsClient(() => {})).toBe("function");
});

it("returns its version number", () => {
const aa = createInsightsClient(() => {});

expect(aa.version).toEqual(version);
});

it("registers itself to window", () => {
const aa = createInsightsClient(() => {});

Expand Down
3 changes: 3 additions & 0 deletions lib/_createInsightsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AlgoliaAnalytics from './insights';
import { getFunctionalInterface } from './_getFunctionalInterface';
import { RequestFnType } from './utils/request';
import { createUUID } from './utils/uuid';
import { version } from '../package.json';

export function createInsightsClient(requestFn: RequestFnType) {
const aa = getFunctionalInterface(new AlgoliaAnalytics({ requestFn }));
Expand All @@ -17,5 +18,7 @@ export function createInsightsClient(requestFn: RequestFnType) {
}
}

aa.version = version;

return aa;
}
5 changes: 4 additions & 1 deletion lib/entry-umd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import AlgoliaAnalytics from "./insights";
import { getRequesterForBrowser } from "./utils/getRequesterForBrowser";
import { processQueue } from "./_processQueue";
import { RequestFnType } from "./utils/request";
import { version } from "../package.json";

export function createInsightsClient(requestFn: RequestFnType) {
const instance = new AlgoliaAnalytics({ requestFn });
if (typeof window === 'object') {
if (typeof window === "object") {
// Process queue upon script execution
processQueue.call(instance, window);
}

instance.version = version;
return instance;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export type ViewedFilters = MethodType<"viewedFilters">;

export type SendEvents = MethodType<"sendEvents">;

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

export type InsightsEventType = "click" | "conversion" | "view";

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"bundlesize": [
{
"path": "./dist/search-insights.min.js",
"maxSize": "2.75 kB"
"maxSize": "2.80 kB"
}
]
}

0 comments on commit 8a3f8f9

Please sign in to comment.