Skip to content

Commit

Permalink
rewrite in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
irinakk committed May 16, 2018
1 parent 881a968 commit 6bc4fec
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 137 deletions.
1 change: 0 additions & 1 deletion .babelrc

This file was deleted.

6 changes: 0 additions & 6 deletions .eslintrc.js

This file was deleted.

23 changes: 23 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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)
89 changes: 23 additions & 66 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
53 changes: 0 additions & 53 deletions src/index.js

This file was deleted.

12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"outDir": "./lib",
"allowJs": true,
"checkJs": false,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"moduleResolution": "node",
"lib": ["es2015", "dom"]
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 6bc4fec

Please sign in to comment.