tinyrpc
v1.0.1
Published
A type safe rpc for es6 Proxy
Downloads
2
Readme
Tinyrpc
A type safe rpc for es6 Proxy.
Example: Chrome Extension
iface.ts
export interface IBase {
ping(): Promise<string>
getType(): Promise<string>
}
export interface IBackground extends IBase {
getStatus(): Promise<object>
updateStatus(status: object): Promise<object>
updateContextMenu(opt: { selection?: string, selected: boolean });
showNotify(opt: string | { title?, message } | NotificationOptions);
requestCall({phoneNumber});
}
export interface IContent extends IBase {
}
export interface IPage extends IBase {
piwik: IPiwik
}
// https://developer.piwik.org/guides/tracking-javascript
export interface IPiwik {
trackEvent(category, action, name?, value?)
trackPageView(customTitle?)
trackSiteSearch(keyword, category?, resultsCount?)
trackGoal(idGoal, customRevenue?)
trackLink(url, linkType)
}
popup.ts
import {ProxyByRuntime, Scripts} from "tinyrpc";
let background = window[Scripts.Background] = ProxyByRuntime({type: Scripts.Background}) as IBackground;
// Call background method
background.getStatus().then(v=>console.log(v))
content_script.ts
// Providing rpc
class Content implements IContent {
ping() {
console.log('Content recv ping');
return Promise.resolve('PONG');
}
getType() {
return Promise.resolve('Scripts.Content')
}
}
let content = window[Scripts.Content] = BindByRuntime(new Content(), {type: Scripts.Content});
// Consuming rpc
let page = window[Scripts.Page] = ProxyByDom({type: Scripts.Page}) as IPage;
let background = window[Scripts.Background] = ProxyByRuntime({type: Scripts.Background}) as IBackground;
// Allow called from page
BindByDom(background, {type: Scripts.Background});
BindByDom(content, {type: Scripts.Content});