Skip to content

Commit

Permalink
release: SDK 3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abarisain committed Dec 7, 2022
1 parent 3fe3278 commit e8af964
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 114 deletions.
6 changes: 3 additions & 3 deletions Sources/documentation_public_readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This documentation describe Batch SDK's public API.

Once you've called `batchSDK("setup", {...})`, the public API can be accessed.

You can get the [PublicAPI](interfaces/batchsdk.publicapi.html) implementation
You can get the PublicAPI implementation
by calling the global `batchSDK` method:

```
Expand All @@ -18,7 +18,7 @@ window.batchSDK(function(api) {
```

Example: Fetching your Installation ID, using
[getInstallationID](interfaces/batchsdk.publicapi.html#getinstallationid):
getInstallationID:

```
window.batchSDK(api => api.getInstallationID().then(console.log))
Expand All @@ -30,7 +30,7 @@ argument to `batchSDK()` is the method name.
Subsequent arguments are forwarded to the public API method.

Example: Forcibly showing an UI component
([ui.show](interfaces/batchsdk.uiapi.html#show)) with both API call styles:
(ui.show) with both API call styles:

```
window.batchSDK(api => api.ui.show("alert", true));
Expand Down
67 changes: 67 additions & 0 deletions Sources/lib/dom/sdk-impl/__tests__/sdk-factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-env jest */
// @ts-nocheck

import { keysByProvider } from "com.batch.shared/parameters/keys";
import ParameterStore from "com.batch.shared/parameters/parameter-store";

import { createSDKFactory } from "../sdk-factory";
import SafariSDKFactory from "../sdk-safari";
import StandardSDKFactory from "../sdk-standard";
jest.mock("com.batch.shared/persistence/profile");

const DEFAULT_UA = window.navigator.userAgent;

const UA_SAFARI = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15";

// Allow to set a custom user-agent
function setUserAgent(userAgent: string): void {
Object.defineProperty(window.navigator, "userAgent", {
get: function () {
return userAgent;
},
configurable: true,
});
}

let store = null;

beforeAll(() => {
return ParameterStore.getInstance().then(s => {
store = s;
});
});

afterEach(() => {
setUserAgent(DEFAULT_UA);
delete self.PushManager;
});

test("default is standard sdk factory", async () => {
const factory = await createSDKFactory();
expect(factory).toBe(StandardSDKFactory);
});

test("is safari sdk factory on safari 15-", async () => {
setUserAgent(UA_SAFARI);
const factory = await createSDKFactory();
expect(factory).toBe(SafariSDKFactory);
});

test("is standard sdk factory on safari 16+", async () => {
setUserAgent(UA_SAFARI);
self.PushManager = () => {
/** Way to mock safari 16 supporting WPP. */
};
const factory = await createSDKFactory();
expect(factory).toBe(StandardSDKFactory);
});

test("is safari sdk factory on safari 16+ when user already has apns subscription", async () => {
setUserAgent(UA_SAFARI);
self.PushManager = () => {
/** Way to mock safari 16 supporting WPP. */
};
await store.setParameterValue(keysByProvider.profile.Subscription, "faketokenapns");
const factory = await createSDKFactory();
expect(factory).toBe(SafariSDKFactory);
});
47 changes: 47 additions & 0 deletions Sources/lib/dom/sdk-impl/sdk-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ISDK } from "com.batch.dom/sdk-impl/sdk";
import SafariSDKFactory from "com.batch.dom/sdk-impl/sdk-safari";
import StandardSDKFactory from "com.batch.dom/sdk-impl/sdk-standard";
import UserAgent, { Browser } from "com.batch.shared/helpers/user-agent";
import { keysByProvider } from "com.batch.shared/parameters/keys";
import ParameterStore from "com.batch.shared/parameters/parameter-store";
/**
* The factory used to create a uniq ISDK instance.
* The instance is wrapped with a promise to always ensure that everything is correctly initialized.
*/
export interface ISDKFactory {
/**
* Setup a new ISDK instance (if not already done)
* and returns the newly uniq created instance.
*/
setup(config: object): Promise<ISDK>;

/**
* Return the uniq instance created by the factory.
* Always call setup before or the promise will be rejected.
*/
getInstance(): Promise<ISDK>;
}

/**
* Create the right SDK factory according to the browser
* @returns ISDKFactory
*/
export async function createSDKFactory(): Promise<ISDKFactory> {
// Instantiate the right SDK factory according to the browser
let sdkFactory: ISDKFactory = StandardSDKFactory;
const userAgent = new UserAgent(window.navigator.userAgent);
if (userAgent.browser === Browser.Safari) {
if ("PushManager" in self) {
const parameterStore: ParameterStore = await ParameterStore.getInstance();
const subscription = await parameterStore.getParameterValue(keysByProvider.profile.Subscription);
if (subscription && typeof subscription === "string") {
// User already has an APNS subscription, we keep using APNS.
sdkFactory = SafariSDKFactory;
}
} else {
// Safari does NOT supports WPP.
sdkFactory = SafariSDKFactory;
}
}
return sdkFactory;
}
3 changes: 2 additions & 1 deletion Sources/lib/dom/sdk-impl/sdk-safari.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { SAFARI_WS_URL } from "com.batch.shared/../../config";
import { Log } from "com.batch.shared/logger";
import { IPrivateBatchSDKConfiguration } from "com.batch.shared/sdk-config";

import { ISDK, ISDKFactory, Permission } from "./sdk";
import { ISDK, Permission } from "./sdk";
import BaseSDK from "./sdk-base";
import { ISDKFactory } from "./sdk-factory";

const logModuleName = "sdk-safari";
import { Timeout } from "com.batch.shared/helpers/timed-promise";
Expand Down
3 changes: 2 additions & 1 deletion Sources/lib/dom/sdk-impl/sdk-standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Timeout } from "com.batch.shared/helpers/timed-promise";
import { Log } from "com.batch.shared/logger";
import { IPrivateBatchSDKConfiguration } from "com.batch.shared/sdk-config";

import { ISDK, ISDKFactory } from "./sdk";
import { ISDK } from "./sdk";
import BaseSDK from "./sdk-base";
import { ISDKFactory } from "./sdk-factory";

const logModuleName = "sdk-standard";
const defaultTimeout = 10; // default service worker timeout in seconds
Expand Down
18 changes: 0 additions & 18 deletions Sources/lib/dom/sdk-impl/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,3 @@ export interface ISDK {

getUserTagCollections(): Promise<{ [key: string]: string[] }>;
}

/**
* The factory used to create a uniq ISDK instance.
* The instance is wrapped with a promise to always ensure that everything is correctly initialized.
*/
export interface ISDKFactory {
/**
* Setup a new ISDK instance (if not already done)
* and returns the newly uniq created instance.
*/
setup(config: object): Promise<ISDK>;

/**
* Return the uniq instance created by the factory.
* Always call setup before or the promise will be rejected.
*/
getInstance(): Promise<ISDK>;
}
9 changes: 9 additions & 0 deletions Sources/lib/dom/ui/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ export class DOMElement {
return this;
}

public href(content: string): DOMElement {
if (content !== null) {
this.filterHTML()
.filter(e => e instanceof HTMLAnchorElement)
.forEach(e => ((e as HTMLAnchorElement).href = content));
}
return this;
}

public text(content: string): DOMElement {
if (content !== null) {
this.filterHTML().forEach(e => {
Expand Down
8 changes: 2 additions & 6 deletions Sources/lib/worker/notification-receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export default class NotificationReceiver {

private static isConfigValid(config: object | undefined | null): config is IPrivateBatchSDKConfiguration {
if (config) {
const { apiKey, subdomain, authKey } = config as IPrivateBatchSDKConfiguration;
if (typeof apiKey !== "string" || typeof subdomain !== "string" || typeof authKey !== "string") {
const { apiKey, authKey } = config as IPrivateBatchSDKConfiguration;
if (typeof apiKey !== "string" || typeof authKey !== "string") {
return false;
}
return true;
Expand All @@ -58,10 +58,6 @@ export default class NotificationReceiver {
}

// Public methods
public shouldDisplayNotifications(): boolean {
return this.isSubscribed;
}

public static isBatchPushPayload(payload: unknown): boolean {
if (typeof payload === "object") {
return Payload.hasEssentialKeys(payload);
Expand Down
40 changes: 20 additions & 20 deletions Sources/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
{
"license" : "Proprietary",
"majorVersion" : "3",
"name" : "batch-webpush-sdk",
"private" : true,
"version" : "3.3.1",
"main" : "index.js",
"scripts" : {
"build" : "yarn type-check && yarn build-webpack",
"doc" : "cd src\/public\/types\/ && typedoc --out ..\/..\/..\/web-api-reference --name \"Batch SDK - Web\" --readme ..\/..\/..\/documentation_public_readme.md --excludeExternals --excludePrivate .\/*.d.ts",
"test" : "jest",
"lint" : "eslint --no-fix \"src\/**\"",
"type-check" : "tsc --build",
"\/\/" : "All scripts here are overwritten for the open source release. Edit package.overlay.json if you need to edit scripts part of the OSS release.",
"lint:report" : "eslint --no-fix --output-file eslint_report.json --format json \"src\/**\""
},
"packageManager" : "[email protected]",
"repository" : {

},
"dependencies" : {
"babel-eslint" : "^10.1.0",
"@babel\/plugin-proposal-private-methods" : "^7.14.5",
Expand Down Expand Up @@ -67,11 +49,29 @@
"@babel\/preset-env" : "^7.15.0",
"eslint-config-prettier" : "^6.15.0",
"eslint-plugin-simple-import-sort" : "^5.0.3",
"puppeteer-core" : "^13.5.2",
"puppeteer-core" : "^19.2.0",
"pnp-webpack-plugin" : "^1.7.0",
"webpack" : "^5.51.2",
"web-push" : "^3.4.5",
"cross-env" : "^7.0.3",
"typescript" : "4.2"
}
},
"version" : "3.4.0",
"repository" : {

},
"packageManager" : "[email protected]",
"majorVersion" : "3",
"license" : "Proprietary",
"scripts" : {
"build" : "yarn type-check && yarn build-webpack",
"doc" : "cd src\/public\/types\/ && typedoc --out ..\/..\/..\/web-api-reference --name \"Batch SDK - Web\" --readme ..\/..\/..\/documentation_public_readme.md --excludeExternals --excludePrivate .\/*.d.ts",
"test" : "jest",
"lint" : "eslint --no-fix \"src\/**\"",
"type-check" : "tsc --build",
"\/\/" : "All scripts here are overwritten for the open source release. Edit package.overlay.json if you need to edit scripts part of the OSS release.",
"lint:report" : "eslint --no-fix --output-file eslint_report.json --format json \"src\/**\""
},
"name" : "batch-webpush-sdk",
"main" : "index.js"
}
8 changes: 6 additions & 2 deletions Sources/public/browser/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ const setupBatchSDK = (): void => {
}

const userAgent = new UserAgent(window.navigator.userAgent);
if (userAgent.platform === Platform.IOS && userAgent.browser === Browser.Safari) {
return;
if (localStorage.getItem("__batchSDK__.staging_enableMobileSafari") === "1") {
console.log("[Batch] Staging mode: enabling mobile safari");
} else {
if (userAgent.platform === Platform.IOS && userAgent.browser === Browser.Safari) {
return;
}
}

if (Promise.toString().indexOf("[native code]") === -1) {
Expand Down
15 changes: 5 additions & 10 deletions Sources/public/browser/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ISDK, ISubscriptionState, Permission } from "com.batch.dom/sdk-impl/sdk";
import SafariSDKFactory from "com.batch.dom/sdk-impl/sdk-safari";
import StandardSDKFactory from "com.batch.dom/sdk-impl/sdk-standard";
import { createSDKFactory } from "com.batch.dom/sdk-impl/sdk-factory";
import getTranslator from "com.batch.dom/ui/translator";
import { UIComponentHandler, UIComponentState } from "com.batch.dom/ui/uicomponent-handler";
import deepClone from "com.batch.shared/helpers/object-deep-clone";
import { asBoolean } from "com.batch.shared/helpers/primitive";
import UserAgent, { Browser } from "com.batch.shared/helpers/user-agent";
import { Evt, LocalEventBus } from "com.batch.shared/local-event-bus";
import LocalSDKEvent, { IUIComponentReadyEventArgs } from "com.batch.shared/local-sdk-events";
import { Log } from "com.batch.shared/logger";
Expand Down Expand Up @@ -163,13 +161,10 @@ export default function newPublicAPI(): BatchSDK.IPublicAPI {
);
}

// update the instance
const typeSDKFactory =
new UserAgent(window.navigator.userAgent).browser === Browser.Safari && new UserAgent(window.navigator.userAgent).isDesktop
? SafariSDKFactory
: StandardSDKFactory;

instance = ready.then(() => typeSDKFactory.setup(sdkConfig));
instance = ready.then(async () => {
const factory = await createSDKFactory();
return factory.setup(sdkConfig);
});

// Handle window hash change
// This should be done in the SDK itself but we have three unrelated implementations of it...
Expand Down
Loading

0 comments on commit e8af964

Please sign in to comment.