diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 9d8d5165..00000000 --- a/.babelrc +++ /dev/null @@ -1 +0,0 @@ -{ "presets": ["es2015"] } diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 614c3446..00000000 --- a/.eslintrc.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - "extends": "standard", - "plugins": [ - "standard" - ] -}; \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 00000000..e8f8bb52 --- /dev/null +++ b/index.ts @@ -0,0 +1,23 @@ +const ORIGIN_CI = 'http://project.ci' +const ORIGIN_PROD = 'https://www.teambition.com' + +export type IframeMessageType = { + method: string + params: any + + isCI?: boolean + origin: string + toOrigin?: string + + onSuccess?(): void + onError?({ error: any }): void +} + +export const notify = (args) => callService({ ...args, method: 'essage' }) + +export const callService = ({ + isCI, + toOrigin = isCI ? ORIGIN_CI : ORIGIN_PROD, + ...data +}: IframeMessageType) => + window.parent.postMessage(JSON.parse(JSON.stringify(data)), toOrigin) diff --git a/lib/index.js b/lib/index.js index 757f1647..3e9850a7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,69 +1,26 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -var ORIGIN_CI = 'http://project.ci'; -var ORIGIN_PROD = 'https://www.teambition.com'; - -var toggleEventListener = function toggleEventListener(callback, isRemove) { - var func = isRemove ? 'removeEventListener' : 'addEventListener'; - return window[func]('message', callback, false); -}; - -var consoleError = function consoleError(_ref) { - var error = _ref.error; - - var errorMessage = error || 'ERROR'; - return console.error(errorMessage); +"use strict"; +var __assign = (this && this.__assign) || Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; }; - -var notify = function notify(_ref2) { - var isCI = _ref2.isCI, - origin = _ref2.origin, - onError = _ref2.onError, - onSuccess = _ref2.onSuccess, - params = _ref2.params; - - callService({ - isCI: isCI, - origin: origin, - params: params, - onSuccess: onSuccess, - onError: onError, - method: 'essage' - }); +var __rest = (this && this.__rest) || function (s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) + t[p[i]] = s[p[i]]; + return t; }; - -var callService = function callService(_ref3) { - var isCI = _ref3.isCI, - method = _ref3.method, - params = _ref3.params, - onSuccess = _ref3.onSuccess, - onError = _ref3.onError, - fromOrigin = _ref3.origin, - toOrigin = _ref3.toOrigin; - - var postId = Date.now(); - var targetOrigin = toOrigin || (isCI ? ORIGIN_CI : ORIGIN_PROD); - var content = { method: method, params: params, postId: postId, origin: fromOrigin }; - window.parent.postMessage(content, targetOrigin); - var callback = function callback(_ref4) { - var data = _ref4.data; - - if (postId !== data.postId) return; - if (data.error) { - onError = onError || consoleError; - onError(data); - } else { - if (typeof onSuccess === "function") { - onSuccess(data); - } - } - toggleEventListener(callback, true); - }; - toggleEventListener(callback); +exports.__esModule = true; +var ORIGIN_CI = 'http://project.ci'; +var ORIGIN_PROD = 'https://www.teambition.com'; +exports.notify = function (args) { return exports.callService(__assign({}, args, { method: 'essage' })); }; +exports.callService = function (_a) { + var isCI = _a.isCI, _b = _a.toOrigin, toOrigin = _b === void 0 ? isCI ? ORIGIN_CI : ORIGIN_PROD : _b, data = __rest(_a, ["isCI", "toOrigin"]); + return window.parent.postMessage(JSON.parse(JSON.stringify(data)), toOrigin); }; - -exports.notify = notify; -exports.callService = callService; \ No newline at end of file diff --git a/package.json b/package.json index 7c2b560c..6d88b808 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,10 @@ "description": "A sdk for communications between teambiton web and plugins", "main": "lib/index.js", "scripts": { - "build": "babel src --out-dir lib", + "build": "tsc ./index --outDir lib", "prepublish": "npm run build" }, - "repository": { - "type": "git", - "url": "https://github.com/teambition/tb-apps-sdk.git" - }, + "repository": "https://github.com/teambition/tb-apps-sdk.git", "keywords": [ "teambition", "plugin" @@ -22,11 +19,6 @@ }, "homepage": "https://github.com/teambition/tb-apps-sdk", "devDependencies": { - "babel-cli": "^6.10.1", - "babel-preset-es2015": "^6.9.0", - "eslint": "^3.0.1", - "eslint-config-standard": "^6.0.0-beta.0", - "eslint-plugin-promise": "^2.0.0", - "eslint-plugin-standard": "^2.0.0" + "typescript": "^2.8.3" } } diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 5edcfd1a..00000000 --- a/src/index.js +++ /dev/null @@ -1,53 +0,0 @@ -const ORIGIN_CI = 'http://project.ci' -const ORIGIN_PROD = 'https://www.teambition.com' - -const toggleEventListener = (callback, isRemove) => { - const func = isRemove ? 'removeEventListener' : 'addEventListener'; - return window[func]('message', callback, false); -} - -const consoleError = ({error}) => { - const errorMessage = error || 'ERROR'; - return console.error(errorMessage); -}; - -const notify = ({isCI, origin, onError, onSuccess, params}) => { - callService({ - isCI, - origin, - params, - onSuccess, - onError, - method: 'essage' - }); -}; - -const callService = ({ - isCI, - method, - params, - onSuccess, - onError, - origin: fromOrigin, - toOrigin -}) => { - const postId = Date.now(); - const targetOrigin = toOrigin || (isCI ? ORIGIN_CI : ORIGIN_PROD); - const content = {method, params, postId, origin: fromOrigin}; - window.parent.postMessage(content, targetOrigin); - const callback = ({data}) => { - if (postId !== data.postId) return - if (data.error) { - onError = onError || consoleError; - onError(data); - } else { - if (typeof onSuccess === "function") { - onSuccess(data); - } - } - toggleEventListener(callback, true); - }; - toggleEventListener(callback); -} - -export { notify, callService }; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..c6dfcaf5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "outDir": "./lib", + "allowJs": true, + "checkJs": false, + "module": "commonjs", + "target": "es5", + "jsx": "react", + "moduleResolution": "node", + "lib": ["es2015", "dom"] + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 00000000..3529c2d2 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,7 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +typescript@^2.8.3: + version "2.8.3" + resolved "http://r.cnpmjs.org/typescript/download/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170"