@memberstack/dom 1.0.0-beta.22 → 1.0.0-beta.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- export { Persistence } from "./auth/persistence";
2
1
  declare const _default: {
3
- init: any;
2
+ init: (props: import("./methods").DOMConfig) => any;
4
3
  };
5
4
  export default _default;
package/lib/index.js CHANGED
@@ -1,7 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Persistence = void 0;
4
- var persistence_1 = require("./auth/persistence");
5
- Object.defineProperty(exports, "Persistence", { enumerable: true, get: function () { return persistence_1.Persistence; } });
6
3
  var methods_1 = require("./methods");
7
4
  exports.default = { init: methods_1.init };
@@ -10,14 +10,17 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
13
16
  Object.defineProperty(exports, "__esModule", { value: true });
14
17
  exports.initDOM = void 0;
15
- var window_1 = require("../../constants/window");
16
18
  var endpoint_1 = require("../../constants/endpoint");
17
19
  var cookies_1 = require("../../utils/cookies");
18
20
  var persistence_1 = require("../../auth/persistence");
21
+ var ui_1 = __importDefault(require("@memberstack/ui"));
19
22
  var initDOM = function (props) {
20
- var methods = window[window_1.Window.MEMBERSTACK].init({ publicKey: props.publicKey });
23
+ var methods = ui_1.default.init({ publicKey: props.publicKey });
21
24
  return {
22
25
  openModal: function (openModalProps) { return openModal(methods, __assign(__assign({}, props), openModalProps)); },
23
26
  hideModal: methods.hide,
@@ -1,3 +1 @@
1
- export declare function appendScript(props: {
2
- publicKey: string;
3
- }): HTMLScriptElement;
1
+ export declare function appendScript(): Promise<any>;
@@ -3,38 +3,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.appendScript = void 0;
4
4
  var endpoint_1 = require("../../constants/endpoint");
5
5
  var window_1 = require("../../constants/window");
6
- var isBrowser = typeof window !== "undefined";
7
6
  var SCRIPT_URL = endpoint_1.endpoints.loaderScript;
8
7
  var SCRIPT_URL_REGEX = new RegExp(SCRIPT_URL);
9
- var getScript = function (_a) {
10
- var publicKey = _a.publicKey;
11
- var existingScript = document.querySelector("script[src^=\"" + SCRIPT_URL + "\"]");
12
- if (existingScript && SCRIPT_URL_REGEX.test(existingScript.src)) {
13
- return existingScript;
8
+ var injectScript = function () {
9
+ var existingScript = document.querySelector("script[src^=\"".concat(SCRIPT_URL, "\"]"));
10
+ if (existingScript && SCRIPT_URL_REGEX.test(existingScript.src) && window[window_1.Window.MEMBERSTACK]) {
11
+ return Promise.resolve(window[window_1.Window.MEMBERSTACK]);
14
12
  }
15
13
  var script = document.createElement("script");
14
+ script.type = "text/javascript";
16
15
  script.src = SCRIPT_URL;
17
16
  script.defer = true;
18
17
  script.crossOrigin = "true";
19
- if (!document.body) {
20
- throw new Error("Memberstack requires a <body> element.");
21
- }
22
- script.addEventListener("load", function () {
23
- if (window[window_1.Window.MEMBERSTACK]) {
24
- window[window_1.Window.MEMBERSTACK].init({ publicKey: publicKey });
25
- console.info("\x1b[36m%s\x1b[0m", "Memberstack has been initialized.");
26
- }
27
- else {
28
- console.warn("Could not initialize Memberstack.");
29
- }
30
- });
31
18
  document.body.appendChild(script);
32
- return script;
19
+ return new Promise(function (res, rej) {
20
+ script.addEventListener("load", function () {
21
+ if (window[window_1.Window.MEMBERSTACK]) {
22
+ return res(window[window_1.Window.MEMBERSTACK]);
23
+ }
24
+ else {
25
+ return rej("Could not initialize Memberstack.");
26
+ }
27
+ });
28
+ });
33
29
  };
34
- function appendScript(props) {
35
- if (!isBrowser) {
30
+ function appendScript() {
31
+ if (typeof window === "undefined" || !(document === null || document === void 0 ? void 0 : document.body)) {
36
32
  throw new Error("Memberstack requires a <body> element.");
37
33
  }
38
- return getScript({ publicKey: props.publicKey });
34
+ return injectScript();
39
35
  }
40
36
  exports.appendScript = appendScript;
@@ -0,0 +1,26 @@
1
+ import type { DOMConfig } from ".";
2
+ declare type ActionParams = {
3
+ onCompleted?: (x: any) => any;
4
+ onError?: (x: any) => any;
5
+ };
6
+ export declare type OpenModalParams = ActionParams & ({
7
+ type: "LOGIN";
8
+ planId?: string;
9
+ priceId?: string;
10
+ } | {
11
+ type: "SIGNUP";
12
+ planId: string;
13
+ priceId?: string;
14
+ } | {
15
+ type: "FORGOT_PASSWORD";
16
+ });
17
+ export declare const initDOM: (props: {
18
+ publicKey: string;
19
+ } & DOMConfig) => {
20
+ openModal: (openModalProps: OpenModalParams) => void;
21
+ hideModal: any;
22
+ onAuthChange: (cb: any) => {
23
+ unsubscribe: () => boolean;
24
+ };
25
+ };
26
+ export {};
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.initDOM = void 0;
18
+ var ui_1 = __importDefault(require("@memberstack/ui"));
19
+ var endpoint_1 = require("../constants/endpoint");
20
+ var cookies_1 = require("../utils/cookies");
21
+ var persistence_1 = require("../auth/persistence");
22
+ var initDOM = function (props) {
23
+ var methods = ui_1.default.init({ publicKey: props.publicKey });
24
+ return {
25
+ openModal: function (openModalProps) { return openModal(methods, __assign(__assign({}, props), openModalProps)); },
26
+ hideModal: methods.hide,
27
+ onAuthChange: persistence_1.onAuthChange
28
+ };
29
+ };
30
+ exports.initDOM = initDOM;
31
+ var openModal = function (methods, props) {
32
+ methods.show(props.type, __assign(__assign({}, ((props.type !== "FORGOT_PASSWORD" && props.planId) && { planId: props.planId })), ((props.type !== "FORGOT_PASSWORD" && props.priceId) && { priceId: props.priceId })));
33
+ window.addEventListener("message", function (e) {
34
+ var _a, _b, _c, _d, _e, _f, _g, _h;
35
+ if (e.origin === endpoint_1.endpoints.parentOrigin) {
36
+ if (e.data.type === "CLOSE") {
37
+ return methods.hide();
38
+ }
39
+ if ((_b = (_a = e.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.member) {
40
+ (0, persistence_1.setPersistedMember)((_c = e.data) === null || _c === void 0 ? void 0 : _c.data.member, props.persistence);
41
+ }
42
+ if (props.useCookie && ((_f = (_e = (_d = e.data) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.tokens) === null || _f === void 0 ? void 0 : _f.accessToken)) {
43
+ (0, cookies_1.setMemberCookie)(e.data.data.tokens.accessToken);
44
+ }
45
+ (_g = props.onCompleted) === null || _g === void 0 ? void 0 : _g.call(props, (_h = e.data) === null || _h === void 0 ? void 0 : _h.data);
46
+ }
47
+ });
48
+ };
@@ -9,4 +9,4 @@ export declare type DOMConfig = ClientConfig & {
9
9
  persistence?: Persistence;
10
10
  useCookie?: boolean;
11
11
  };
12
- export declare const init: any;
12
+ export declare const init: (props: DOMConfig) => any;
@@ -15,21 +15,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.init = void 0;
18
- var persistence_1 = require("../auth/persistence");
19
18
  var proxy_1 = __importDefault(require("../utils/proxy"));
20
- var methods_1 = require("./dom/methods");
21
- var script_1 = require("./dom/script");
19
+ var dom_1 = require("./dom");
22
20
  var client_1 = require("./client");
23
21
  var interceptor_1 = require("../utils/interceptor");
24
- var withScript = function (fn) {
25
- return function (props) {
26
- var config = __assign({ persistence: persistence_1.Persistence.LOCAL, useCookie: true }, props);
27
- (0, script_1.appendScript)({ publicKey: props.publicKey });
28
- return fn(config);
29
- };
30
- };
31
- exports.init = withScript(function (props) {
22
+ var init = function (props) {
32
23
  var client = (0, proxy_1.default)((0, client_1.initClient)(__assign(__assign({}, props), { onMemberToken: function (token) { return (0, interceptor_1.handleMemberToken)({ persistence: props.persistence, token: token }); } })), props);
33
- var methods = (0, methods_1.initDOM)({ publicKey: props.publicKey });
34
- return Object.assign(client, methods);
35
- });
24
+ var ui = (0, dom_1.initDOM)({ publicKey: props.publicKey });
25
+ return Object.assign(client, ui);
26
+ };
27
+ exports.init = init;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberstack/dom",
3
- "version": "1.0.0-beta.22",
3
+ "version": "1.0.0-beta.23",
4
4
  "main": "./lib/index.js",
5
5
  "files": [
6
6
  "lib/**/*"
@@ -9,10 +9,14 @@
9
9
  "license": "MIT",
10
10
  "dependencies": {
11
11
  "@memberstack/client": "^1.0.1-beta.7",
12
+ "@memberstack/ui": "^0.0.2-2",
12
13
  "axios": "^0.24.0",
13
14
  "js-cookie": "^3.0.1"
14
15
  },
15
16
  "scripts": {
16
17
  "prepublishOnly": "tsc"
18
+ },
19
+ "devDependencies": {
20
+ "typescript": "^4.5.2"
17
21
  }
18
22
  }