Skip to content

Commit

Permalink
Merge pull request #11 from teambition/feat/plugin-api
Browse files Browse the repository at this point in the history
feat(api): 新增 PluginAPI
  • Loading branch information
irinakk committed May 22, 2019
2 parents b84b019 + 5cdf0cc commit 984fb42
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tb-apps-sdk",
"version": "0.6.9",
"version": "0.6.10-alpha.1",
"description": "A sdk for communications between teambiton web and other envs.",
"main": "./index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class APIBase {
this.sdk.destroy()
}

call(name: string, ...params: any[]): any {
return this.sdk.send(
call<T = void>(name: string, ...params: any[]): Promise<T> {
return this.sdk.send<T>(
{ method: name, params: params || [] /*, version: this.version */ },
this.isolatedAPI().indexOf(name) > -1
)
Expand Down
2 changes: 1 addition & 1 deletion src/api/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface DashboardAPI {
class HostAPI extends APIBase {

registerHostNode(...params: any[]) {
return this.call('registerHostNode', ...params)
return this.call<HTMLElement>('registerHostNode', ...params)
}

transferStyleNode(...params: any[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HostAPI extends APIBase {
}

registerHostNode(...params: any[]) {
return this.call('registerHostNode', ...params)
return this.call<HTMLElement>('registerHostNode', ...params)
}

transferStyleNode(...params: any[]) {
Expand Down
68 changes: 68 additions & 0 deletions src/api/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { AppSDK } from '../sdk/AppSDK'
import { APIBase, factory, IFactory } from './base'

const bridge: TeambitionMobileSDK | void = window.TeambitionMobileSDK

export interface PluginAPI {
/**
* 显示 Toast 消息
*/
essage(type: PluginAPIEssageType, config: PluginAPIEssageConfig): Promise<void>

/**
* 重新拉取插件数据
*/
refresh(): Promise<void>

/**
* 关闭 iframe 弹窗
*/
close(): Promise<void>
}

class HostAPI extends APIBase implements PluginAPI {
essage(type: PluginAPIEssageType, config: PluginAPIEssageConfig) {
bridge && bridge.call('showToast', { type, ...config })
return this.call('essage', config)
}

refresh() {
bridge && bridge.call('forwardAction', { action: 'refreshPlugin' })
return this.call('refresh')
}

close() {
bridge && bridge.call('exit')
return this.call('close')
}
}

export type PluginAPIEssageType = 'success' | 'error' | 'info' | 'warning'

export interface PluginAPIEssageConfig {
message: string
description?: string
}

export const hostAPI: IFactory<PluginAPI> = (sdk: AppSDK) => {
return factory<HostAPI>(sdk, HostAPI)
}

declare global {
interface TeambitionMobileSDK {
call(
method: 'showToast',
params: {
type: 'success' | 'error' | 'info' | 'warning'
message: string
description?: string
},
): void
call(method: 'exit'): void
call(method: 'forwardAction', params: { action: 'refreshPlugin' }): void
}

interface Window {
TeambitionMobileSDK?: TeambitionMobileSDK
}
}

0 comments on commit 984fb42

Please sign in to comment.