@memberstack/dom 1.0.0-beta.18 → 1.0.0-beta.21

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.
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.endpoints = void 0;
4
4
  exports.endpoints = {
5
- parentOrigin: "https://ui.memberstack.com",
6
- loaderScript: "https://ui.memberstack.com/loader.cjs.production.min.js",
5
+ parentOrigin: "https://dev-ui.vercel.app",
6
+ loaderScript: "https://dev-ui.vercel.app/loader.cjs.production.min.js",
7
7
  };
@@ -1,4 +1,3 @@
1
1
  export declare const Window: {
2
- MSDOM: string;
3
- APP_ID: string;
2
+ MEMBERSTACK: string;
4
3
  };
@@ -2,6 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Window = void 0;
4
4
  exports.Window = {
5
- MSDOM: "__MSDOM",
6
- APP_ID: "__MS-APPID",
5
+ MEMBERSTACK: "memberstack",
7
6
  };
package/lib/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export { Persistence } from "./auth/persistence";
2
- import { Methods } from "./methods";
3
- export default Methods;
2
+ declare const _default: {
3
+ init: any;
4
+ };
5
+ export default _default;
package/lib/index.js CHANGED
@@ -4,4 +4,4 @@ exports.Persistence = void 0;
4
4
  var persistence_1 = require("./auth/persistence");
5
5
  Object.defineProperty(exports, "Persistence", { enumerable: true, get: function () { return persistence_1.Persistence; } });
6
6
  var methods_1 = require("./methods");
7
- exports.default = methods_1.Methods;
7
+ exports.default = { init: methods_1.init };
@@ -1,17 +1,31 @@
1
1
  import type { DOMConfig } from "..";
2
- export declare type OpenModalParams = {
3
- type: "LOGIN" | "SIGNUP" | "FORGOT_PASSWORD";
2
+ declare type ActionParams = {
3
+ onCompleted?: (x: any) => any;
4
+ onError?: (x: any) => any;
5
+ };
6
+ export declare type OpenModalParams = ActionParams & ({
7
+ type: "LOGIN";
4
8
  params?: {
5
9
  planId?: string;
6
- planPaymentOptionId?: string;
10
+ priceId?: string;
7
11
  };
8
- options?: {
9
- onCompleted?: (x: any) => any;
10
- onError?: (x: any) => any;
12
+ } | {
13
+ type: "SIGNUP";
14
+ params: {
15
+ planId: string;
16
+ priceId?: string;
11
17
  };
12
- };
13
- export declare const load: (props: {
18
+ } | {
19
+ type: "FORGOT_PASSWORD";
20
+ params: {};
21
+ });
22
+ export declare const initDOM: (props: {
14
23
  publicKey: string;
15
- }) => void;
16
- export declare const openModal: (props: OpenModalParams & DOMConfig) => void;
17
- export declare const hideModal: () => void;
24
+ } & DOMConfig) => {
25
+ openModal: (openModalProps: OpenModalParams) => void;
26
+ hideModal: any;
27
+ onAuthChange: (cb: any) => {
28
+ unsubscribe: () => boolean;
29
+ };
30
+ };
31
+ export {};
@@ -1,64 +1,45 @@
1
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
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hideModal = exports.openModal = exports.load = void 0;
14
+ exports.initDOM = void 0;
4
15
  var window_1 = require("../../constants/window");
5
16
  var endpoint_1 = require("../../constants/endpoint");
6
17
  var cookies_1 = require("../../utils/cookies");
7
18
  var persistence_1 = require("../../auth/persistence");
8
- var isBrowser = typeof window !== "undefined";
9
- var isInitialized = function () {
10
- if (!isBrowser)
11
- return;
12
- if (!(window === null || window === void 0 ? void 0 : window[window_1.Window.MSDOM])) {
13
- console.warn("Memberstack has not been initialized yet.");
14
- return false;
15
- }
16
- return true;
17
- };
18
- var hasIFrame = function () {
19
- var initialized = isInitialized();
20
- if (!initialized)
21
- return false;
22
- var iframe = document.querySelector("iframe[id=\"_ms-iframe\"]");
23
- if (!iframe) {
24
- console.warn("iFrame has not been loaded yet.");
25
- return false;
26
- }
27
- return true;
28
- };
29
- var load = function (props) {
30
- var _a;
31
- if (isInitialized()) {
32
- (_a = window[window_1.Window.MSDOM]) === null || _a === void 0 ? void 0 : _a.load({ publicKey: props.publicKey });
33
- }
19
+ var initDOM = function (props) {
20
+ var methods = window[window_1.Window.MEMBERSTACK].init({ publicKey: props.publicKey });
21
+ return {
22
+ openModal: function (openModalProps) { return openModal(methods, __assign(__assign({}, props), openModalProps)); },
23
+ hideModal: methods.hide,
24
+ onAuthChange: persistence_1.onAuthChange
25
+ };
34
26
  };
35
- exports.load = load;
36
- var openModal = function (props) {
37
- var _a;
38
- if (hasIFrame()) {
39
- (_a = window[window_1.Window.MSDOM]) === null || _a === void 0 ? void 0 : _a.show(props.type, props.params);
40
- window.addEventListener("message", function (e) {
41
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
42
- if (e.origin === endpoint_1.endpoints.parentOrigin) {
43
- if (e.data.type === "CLOSE") {
44
- return (0, exports.hideModal)();
45
- }
46
- if ((_b = (_a = e.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.member) {
47
- (0, persistence_1.setPersistedMember)((_c = e.data) === null || _c === void 0 ? void 0 : _c.data.member, props.persistence);
48
- }
49
- 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)) {
50
- (0, cookies_1.setMemberCookie)(e.data.data.tokens.accessToken);
51
- }
52
- (_h = (_g = props.options) === null || _g === void 0 ? void 0 : _g.onCompleted) === null || _h === void 0 ? void 0 : _h.call(_g, (_j = e.data) === null || _j === void 0 ? void 0 : _j.data);
27
+ exports.initDOM = initDOM;
28
+ var openModal = function (methods, props) {
29
+ methods.show(props.type, props.params);
30
+ window.addEventListener("message", function (e) {
31
+ var _a, _b, _c, _d, _e, _f, _g, _h;
32
+ if (e.origin === endpoint_1.endpoints.parentOrigin) {
33
+ if (e.data.type === "CLOSE") {
34
+ return methods.hide();
53
35
  }
54
- });
55
- }
56
- };
57
- exports.openModal = openModal;
58
- var hideModal = function () {
59
- var _a;
60
- if (hasIFrame()) {
61
- (_a = window[window_1.Window.MSDOM]) === null || _a === void 0 ? void 0 : _a.hide();
62
- }
36
+ if ((_b = (_a = e.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.member) {
37
+ (0, persistence_1.setPersistedMember)((_c = e.data) === null || _c === void 0 ? void 0 : _c.data.member, props.persistence);
38
+ }
39
+ 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)) {
40
+ (0, cookies_1.setMemberCookie)(e.data.data.tokens.accessToken);
41
+ }
42
+ (_g = props.onCompleted) === null || _g === void 0 ? void 0 : _g.call(props, (_h = e.data) === null || _h === void 0 ? void 0 : _h.data);
43
+ }
44
+ });
63
45
  };
64
- exports.hideModal = hideModal;
@@ -1,3 +1,3 @@
1
- export declare function initialize(props: {
1
+ export declare function appendScript(props: {
2
2
  publicKey: string;
3
- }): Promise<any>;
3
+ }): HTMLScriptElement;
@@ -1,48 +1,13 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.initialize = void 0;
3
+ exports.appendScript = void 0;
40
4
  var endpoint_1 = require("../../constants/endpoint");
41
5
  var window_1 = require("../../constants/window");
42
6
  var isBrowser = typeof window !== "undefined";
43
7
  var SCRIPT_URL = endpoint_1.endpoints.loaderScript;
44
8
  var SCRIPT_URL_REGEX = new RegExp(SCRIPT_URL);
45
- var getScript = function () {
9
+ var getScript = function (_a) {
10
+ var publicKey = _a.publicKey;
46
11
  var existingScript = document.querySelector("script[src^=\"" + SCRIPT_URL + "\"]");
47
12
  if (existingScript && SCRIPT_URL_REGEX.test(existingScript.src)) {
48
13
  return existingScript;
@@ -54,27 +19,22 @@ var getScript = function () {
54
19
  if (!document.body) {
55
20
  throw new Error("Memberstack requires a <body> element.");
56
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
+ });
57
31
  document.body.appendChild(script);
58
32
  return script;
59
33
  };
60
- function initialize(props) {
61
- return __awaiter(this, void 0, void 0, function () {
62
- var script;
63
- return __generator(this, function (_a) {
64
- if (!isBrowser)
65
- return [2 /*return*/, null];
66
- script = getScript();
67
- script.addEventListener("load", function () {
68
- if (window[window_1.Window.MSDOM]) {
69
- window[window_1.Window.MSDOM].load({ publicKey: props.publicKey });
70
- console.info("\x1b[36m%s\x1b[0m", "Memberstack has been initialized.");
71
- }
72
- else {
73
- console.warn("Could not initialize Memberstack.");
74
- }
75
- });
76
- return [2 /*return*/];
77
- });
78
- });
34
+ function appendScript(props) {
35
+ if (!isBrowser) {
36
+ throw new Error("Memberstack requires a <body> element.");
37
+ }
38
+ return getScript({ publicKey: props.publicKey });
79
39
  }
80
- exports.initialize = initialize;
40
+ exports.appendScript = appendScript;
@@ -1,26 +1,12 @@
1
1
  import { Persistence } from "../auth/persistence";
2
- import * as DOM from "./dom/methods";
3
- import { initClient, ClientConfig } from "./client";
2
+ import { ClientConfig } from "./client";
4
3
  declare global {
5
4
  interface Window {
6
- __MSDOM: any;
7
- APP_ID: string;
5
+ memberstack: any;
8
6
  }
9
7
  }
10
8
  export declare type DOMConfig = ClientConfig & {
11
9
  persistence?: Persistence;
12
10
  useCookie?: boolean;
13
11
  };
14
- declare const DOMMethods: ({ useCookie, persistence, ...props }: DOMConfig) => {
15
- load: () => void;
16
- hideModal: () => void;
17
- onAuthChange: (cb: any) => {
18
- unsubscribe: () => boolean;
19
- };
20
- openModal: (type: DOM.OpenModalParams["type"], params?: DOM.OpenModalParams["params"], options?: DOM.OpenModalParams["options"]) => void;
21
- };
22
- export declare const init: (props: DOMConfig) => ReturnType<typeof initClient> & ReturnType<typeof DOMMethods>;
23
- export declare const Methods: {
24
- init: (props: DOMConfig) => ReturnType<typeof initClient> & ReturnType<typeof DOMMethods>;
25
- };
26
- export {};
12
+ export declare const init: any;
@@ -10,66 +10,26 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16
- }) : (function(o, m, k, k2) {
17
- if (k2 === undefined) k2 = k;
18
- o[k2] = m[k];
19
- }));
20
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
- Object.defineProperty(o, "default", { enumerable: true, value: v });
22
- }) : function(o, v) {
23
- o["default"] = v;
24
- });
25
- var __importStar = (this && this.__importStar) || function (mod) {
26
- if (mod && mod.__esModule) return mod;
27
- var result = {};
28
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29
- __setModuleDefault(result, mod);
30
- return result;
31
- };
32
- var __rest = (this && this.__rest) || function (s, e) {
33
- var t = {};
34
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
35
- t[p] = s[p];
36
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
37
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
38
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
39
- t[p[i]] = s[p[i]];
40
- }
41
- return t;
42
- };
43
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
44
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
45
15
  };
46
16
  Object.defineProperty(exports, "__esModule", { value: true });
47
- exports.Methods = exports.init = void 0;
17
+ exports.init = void 0;
48
18
  var persistence_1 = require("../auth/persistence");
49
19
  var proxy_1 = __importDefault(require("../utils/proxy"));
50
- var DOM = __importStar(require("./dom/methods"));
20
+ var methods_1 = require("./dom/methods");
51
21
  var script_1 = require("./dom/script");
52
22
  var client_1 = require("./client");
53
23
  var interceptor_1 = require("../utils/interceptor");
54
- var DOMMethods = function (_a) {
55
- var _b = _a.useCookie, useCookie = _b === void 0 ? true : _b, _c = _a.persistence, persistence = _c === void 0 ? persistence_1.Persistence.LOCAL : _c, props = __rest(_a, ["useCookie", "persistence"]);
56
- return ({
57
- load: function () { return DOM.load({ publicKey: props.publicKey }); },
58
- hideModal: DOM.hideModal,
59
- onAuthChange: persistence_1.onAuthChange,
60
- openModal: function (type, params, options) { return DOM.openModal(__assign({ type: type, params: params, options: options, useCookie: useCookie, persistence: persistence }, props)); },
61
- });
62
- };
63
24
  var withScript = function (fn) {
64
25
  return function (props) {
65
26
  var config = __assign({ persistence: persistence_1.Persistence.LOCAL, useCookie: true }, props);
66
- (0, script_1.initialize)({ publicKey: props.publicKey });
27
+ (0, script_1.appendScript)({ publicKey: props.publicKey });
67
28
  return fn(config);
68
29
  };
69
30
  };
70
31
  exports.init = withScript(function (props) {
71
32
  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);
72
- var methods = DOMMethods(props);
33
+ var methods = (0, methods_1.initDOM)({ publicKey: props.publicKey });
73
34
  return Object.assign(client, methods);
74
35
  });
75
- exports.Methods = { init: exports.init };
@@ -1,40 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __read = (this && this.__read) || function (o, n) {
39
3
  var m = typeof Symbol === "function" && o[Symbol.iterator];
40
4
  if (!m) return o;
@@ -86,13 +50,10 @@ var handleResponse = function (fn, props) {
86
50
  };
87
51
  var handler = function (props) { return ({
88
52
  get: function (target, prop, receiver) {
89
- var _this = this;
90
53
  if (prop === "getCurrentMember") {
91
54
  var persistedMember_1 = (0, persistence_1.getPersistedMember)(props.persistence);
92
55
  if (persistedMember_1) {
93
- return function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
94
- return [2 /*return*/, ({ data: persistedMember_1 })];
95
- }); }); };
56
+ return function () { return ({ data: persistedMember_1 }); };
96
57
  }
97
58
  }
98
59
  return handleResponse(Reflect.get(target, prop, receiver), props);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberstack/dom",
3
- "version": "1.0.0-beta.18",
3
+ "version": "1.0.0-beta.21",
4
4
  "main": "./lib/index.js",
5
5
  "files": [
6
6
  "lib/**/*"