Skip to content

Commit

Permalink
Add init call on creation of SDK (#59)
Browse files Browse the repository at this point in the history
* Add init on creation of SDK

* Make edge calls wait on init

* Switch default promise to Promise.resolve
  • Loading branch information
Nikola-Milekic authored Oct 11, 2023
1 parent 981ff05 commit de5c08f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/addons/try-identify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("tryIdentifyFromParams", () => {

beforeEach(() => {
delete window.location;
SDK = new OptableSDK({ host: "localhost", site: "test" });
SDK = new OptableSDK({ host: "localhost", site: "test" }, false);
SDK.identify = jest.fn();
});

Expand Down
14 changes: 14 additions & 0 deletions lib/edge/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { OptableConfig } from "../config";
import { fetch } from "../core/network";

function Init(config: OptableConfig): Promise<void> {
return fetch("/init", config, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
}

export { Init };
export default Init;
20 changes: 14 additions & 6 deletions lib/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { WitnessProperties } from "./edge/witness";
import type { ProfileTraits } from "./edge/profile";
import { Identify } from "./edge/identify";
import { Uid2Token } from "./edge/uid2_token";
import { Init } from "./edge/init";
import { Site, SiteResponse } from "./edge/site";
import {
TargetingKeyValues,
Expand All @@ -19,23 +20,28 @@ import { sha256 } from "js-sha256";

class OptableSDK {
public sandbox: OptableConfig; // legacy
private init: any

constructor(public dcn: OptableConfig) {
constructor(public dcn: OptableConfig, writePassport: boolean = true) {
this.sandbox = dcn; // legacy
this.init = writePassport ? Init(dcn).catch(()=>{}) : Promise.resolve();
}

identify(...ids: string[]) {
async identify(...ids: string[]) {
await this.init;
return Identify(
this.dcn,
ids.filter((id) => id)
);
}

uid2Token(id: string) {
async uid2Token(id: string) {
await this.init;
return Uid2Token(this.dcn, id);
}

targeting(): Promise<TargetingResponse> {
async targeting(): Promise<TargetingResponse> {
await this.init;
return Targeting(this.dcn);
}

Expand Down Expand Up @@ -73,11 +79,13 @@ class OptableSDK {
return TargetingKeyValues(tdata);
}

witness(event: string, properties: WitnessProperties = {}): Promise<void> {
async witness(event: string, properties: WitnessProperties = {}): Promise<void> {
await this.init;
return Witness(this.dcn, event, properties);
}

profile(traits: ProfileTraits): Promise<void> {
async profile(traits: ProfileTraits): Promise<void> {
await this.init;
return Profile(this.dcn, traits);
}

Expand Down

0 comments on commit de5c08f

Please sign in to comment.