Skip to content

Commit

Permalink
feat: 新增 AppSDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Saviio committed May 29, 2018
1 parent 6bc4fec commit e7576db
Show file tree
Hide file tree
Showing 14 changed files with 645 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.log
*.log
lib
88 changes: 77 additions & 11 deletions README.md
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

26 changes: 0 additions & 26 deletions lib/index.js

This file was deleted.

15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "tb-apps-sdk",
"version": "0.0.5",
"description": "A sdk for communications between teambiton web and plugins",
"description": "A sdk for communications between teambiton web and other envs.",
"main": "lib/index.js",
"scripts": {
"build": "tsc ./index --outDir lib",
"prepublish": "npm run build"
"build": "tsc -p ./tsconfig.json",
"prepublish": "npm run build",
"lint": "tslint -c tslint.json src/*.ts --project ./tsconfig.json \"src/**/*.ts\" \"./test/**/*.ts\" -e \"./test/e2e/*.ts\""
},
"repository": "https://github.com/teambition/tb-apps-sdk.git",
"keywords": [
Expand All @@ -18,7 +19,13 @@
"url": "https://github.com/teambition/tb-apps-sdk/issues"
},
"homepage": "https://github.com/teambition/tb-apps-sdk",
"peerDependencies": {
"tslib": "^1.6.0"
},
"devDependencies": {
"typescript": "^2.8.3"
"typescript": "^2.8.3",
"tslib": "^1.6.0",
"tslint": "^5.9.1",
"tslint-eslint-rules": "^5.1.0"
}
}
38 changes: 38 additions & 0 deletions src/api/base.ts
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
36 changes: 36 additions & 0 deletions src/api/internal.ts
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)
}
2 changes: 1 addition & 1 deletion index.ts → src/call-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type IframeMessageType = {
origin: string
toOrigin?: string

onSuccess?(): void
onSuccess?(): void
onError?({ error: any }): void
}

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './call-service'
export * from './interface'
export * from './sdk'
27 changes: 27 additions & 0 deletions src/interface/index.ts
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]
}
Loading

0 comments on commit e7576db

Please sign in to comment.