-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Saviio
committed
May 31, 2018
1 parent
6bc4fec
commit 6e8d1c4
Showing
14 changed files
with
688 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
*.log | ||
*.log | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,110 @@ | ||
# tb-apps-sdk | ||
|
||
## 接口 | ||
Teambition Host Environment API Bridge | ||
|
||
### callService | ||
## How to build | ||
|
||
`method`: web 中方法名 | ||
```js | ||
npm run build | ||
``` | ||
|
||
`params`: 方法对应参数 | ||
## How to publish | ||
```js | ||
npm run publish | ||
``` | ||
|
||
`isCI`: 环境判断 | ||
## For Plugin | ||
|
||
`origin`: 插件的origin | ||
```ts | ||
// in plugin | ||
import { callService } from 'tb-apps-sdk' | ||
|
||
`toOrigin`: 目标地址的 URL | ||
callService({ isCI: true, method: 'essage', params: [/* ... */] }) | ||
``` | ||
|
||
`onSuccess` | ||
## For Other Environment | ||
|
||
`onError` | ||
### 3rd-part | ||
|
||
### notify | ||
```ts | ||
// in 3rd env (e.g. Dashboard) | ||
import { sdk } from 'sdk' | ||
import { AppSDK } from 'tb-apps-sdk' | ||
import { hostAPI } from 'tb-apps-sdk/api/internal' | ||
|
||
const hostEnv = AppSDK.fork(hostAPI) | ||
hostEnv.init() | ||
|
||
sdk.fetch.getTask().subscribe(task => { | ||
hostEnv.openDetail('task', task._id) | ||
}) | ||
``` | ||
|
||
### Platform | ||
|
||
```ts | ||
// in host env (e.g. web) | ||
import { RemoteSchema } from 'tb-apps-sdk' | ||
import { InternalAPI } from 'tb-apps-sdk/api/internal' | ||
|
||
class PlatformAPI implements RemoteSchema<InternalAPI> { | ||
|
||
openDetail() { | ||
// ... | ||
} | ||
|
||
} | ||
``` | ||
|
||
### How to mock | ||
|
||
```ts | ||
import { AppSDK } from 'tb-apps-sdk' | ||
import { InternalAPI } from 'tb-apps-sdk/api/internal' | ||
import { factory } from 'tb-apps-sdk/api/base' | ||
|
||
class MockAPI implements InternalAPI { | ||
|
||
openDetail(...params: any[]) { | ||
console.log('Method openDetail was called.', params) | ||
} | ||
|
||
} | ||
|
||
const mockAPI = (sdk: AppSDK) => { | ||
return factory(sdk, MockAPI) | ||
} | ||
|
||
const mockEnv = AppSDK.fork(mockAPI) | ||
mockEnv.openDetail() | ||
``` | ||
|
||
## Interface | ||
##### `Function: callService = (data: IframeMessageType) => void` | ||
|
||
##### `Interface: IframeMessageType` | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| - | - | - | - | | ||
| method | 指定调用的方法 | string | - | | ||
| params | 指定调用的方法的参数 | any | - | | ||
| isCI | 是否是 CI 环境 | boolean | / | | ||
| origin | UNKNOWN | string | - | | ||
| toOrigin | 反向通讯的地址 | string | / | | ||
| onSuccess | 执行成功后的回调 | () => void | / | | ||
| onError | 执行失败后的回调 | ({ error }) => void | / | | ||
|
||
##### `Class: AppSDK` | ||
|
||
- ```Static Method: AppSDK.fork(service, onPush, timeout)``` | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| - | - | - | - | | ||
| service | 指定装载的宿主 API 配置容器 | (sdk: AppSDK) => any | - | | ||
| onPush | 指定宿主环境主动推送时的回调 | (data: any) => void | - | | ||
| timeout | 指定远端调用最大超时时间 | number | 10000 | | ||
|
||
`callService` with `method` set to "essage" | ||
|
||
## License | ||
MIT | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { AppSDK } from '../sdk' | ||
|
||
export interface IAPIBase { | ||
init(): void | ||
destroy(): void | ||
} | ||
|
||
export interface ICtor<T> { | ||
new(sdk: AppSDK): T | ||
} | ||
|
||
export function factory<T>(sdk: AppSDK, ctor: ICtor<T>): T & IAPIBase { | ||
return new ctor(sdk) as T & IAPIBase | ||
} | ||
|
||
export class APIBase { | ||
|
||
constructor(private sdk: AppSDK) { } | ||
|
||
init() { | ||
this.sdk.init() | ||
} | ||
|
||
destroy() { | ||
this.sdk.destroy() | ||
} | ||
|
||
call(name: string, ...params: any[]): any { | ||
return this.sdk.send({ method: name, params: params || [] }, this.isolatedAPI().indexOf(name) > -1) | ||
} | ||
|
||
// 返回 "只有在同域时才允许调用的受限制方法" 方法列表 | ||
isolatedAPI() { | ||
return [] | ||
} | ||
|
||
} | ||
|
||
export class MockBase { | ||
|
||
init() { | ||
return void 0 | ||
} | ||
|
||
destroy() { | ||
return void 0 | ||
} | ||
|
||
call(...params: any[]): any { | ||
return Promise.resolve(params) | ||
} | ||
|
||
isolatedAPI() { | ||
return [] | ||
} | ||
|
||
} | ||
|
||
export type IFactory<T> = (sdk: AppSDK) => T & IAPIBase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { AppSDK } from '../sdk/AppSDK' | ||
import { APIBase, factory, IFactory } from './base' | ||
|
||
export interface InternalAPI { | ||
registerHostNode(): Promise<HTMLElement> | ||
essage(type: 'show' | 'error' | 'log' | 'success' | 'warning', ...params: any[]): Promise<void> | ||
openDetail(type: 'task' | 'date' | 'file' | 'post' | 'bookkeeping', ...params: any[]): Promise<void> | ||
} | ||
|
||
class HostAPI extends APIBase { | ||
|
||
isolatedAPI() { | ||
return ['registerHostNode', 'transferStyleNode'] | ||
} | ||
|
||
registerHostNode(...params: any[]) { | ||
return this.call('registerHostNode', ...params) | ||
} | ||
|
||
transferStyleNode(...params: any[]) { | ||
return this.call('transferStyleNode', ...params) | ||
} | ||
|
||
essage(...params: any[]) { | ||
return this.call('essage', ...params) | ||
} | ||
|
||
openDetail(...params: any[]) { | ||
return this.call('openDetail', ...params) | ||
} | ||
|
||
} | ||
|
||
export const hostAPI: IFactory<InternalAPI> = (sdk: AppSDK) => { | ||
return factory<InternalAPI>(sdk, HostAPI) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './call-service' | ||
export * from './interface' | ||
export * from './sdk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export interface ProcedureConnector { | ||
resolve: Function | undefined | ||
reject: Function | undefined | ||
} | ||
|
||
export enum ResponseType { | ||
SYN = 'SYN', | ||
MSG = 'MSG', | ||
PSH = 'PSH', | ||
ACK = 'ACK', | ||
} | ||
|
||
export interface Procedures { | ||
[key: number]: ProcedureConnector | ||
} | ||
|
||
export interface RemoteCall { | ||
method: string, | ||
params?: any[] | ||
} | ||
|
||
export type RemoteSchema<T> = { | ||
[K in keyof T]: T[K] extends (...params: any[]) => Promise<infer U> | ||
? U extends HTMLElement | ||
? (...params: any[]) => string | ||
: (...params: any[]) => U | ||
: (...params: any[]) => T[K] | ||
} |
Oops, something went wrong.