-
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 29, 2018
1 parent
6bc4fec
commit e7576db
Showing
14 changed files
with
645 additions
and
45 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,92 @@ | ||
# tb-apps-sdk | ||
|
||
## 接口 | ||
Teambition Host Environment API Bridge | ||
|
||
### callService | ||
## How to build | ||
|
||
`method`: web 中方法名 | ||
```js | ||
npm run build | ||
``` | ||
|
||
`params`: 方法对应参数 | ||
## For Plugin | ||
|
||
`isCI`: 环境判断 | ||
```ts | ||
// in plugin | ||
import { callService } from 'tb-apps-sdk' | ||
|
||
`origin`: 插件的origin | ||
callService({ isCI: true, method: 'essage', params: [/* ... */] }) | ||
``` | ||
|
||
`toOrigin`: 目标地址的 URL | ||
## For Other Environment | ||
|
||
`onSuccess` | ||
### 3rd-part | ||
|
||
`onError` | ||
```ts | ||
import { AppSDK } from 'tb-apps-sdk' | ||
import { hostAPI } from 'tb-apps-sdk/api/internal' | ||
|
||
### notify | ||
const hostEnv = AppSDK.fork(hostAPI) | ||
hostEnv.init() | ||
``` | ||
|
||
### Platform | ||
|
||
```ts | ||
import { RemoteSchema } from 'tb-apps-sdk' | ||
import { InternalAPI } from 'tb-apps-sdk/api/internal' | ||
|
||
class PlatformAPI implements RemoteSchema<InternalAPI> { | ||
// ... | ||
} | ||
``` | ||
|
||
### How to mock | ||
|
||
```ts | ||
import { AppSDK } from 'tb-apps-sdk' | ||
import { hostAPI } from 'tb-apps-sdk/api/internal' | ||
import { factory } from 'tb-apps-sdk/api/base' | ||
|
||
class MockAPI { | ||
add() { | ||
console.log('Method add was called.') | ||
} | ||
} | ||
|
||
const mockAPI = (sdk: AppSDK) => { | ||
return factory(sdk, MockAPI) | ||
} | ||
|
||
const mockEnv = AppSDK.fork(mockAPI) | ||
mockEnv.add() | ||
``` | ||
|
||
## 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,38 @@ | ||
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 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,27 @@ | ||
export interface ProcedureConnector { | ||
resolve: Function | undefined | ||
reject: Function | undefined | ||
} | ||
|
||
export enum ResponseType { | ||
SYN = 'SYN', | ||
MSG = 'MSG', | ||
PSH = 'PSH' | ||
} | ||
|
||
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.