@homebridge/plugin-ui-utils
Version:
A tool to help plugins provide custom UI screens in the Homebridge UI.
96 lines • 2.78 kB
TypeScript
/**
* Homebridge Custom Plugin UI Base Class
* This provides the api to facilitate two-way communication between a plugin
* custom UI HTML code and the server.
*
* This is a base class and is intended to be extended.
*
* @example
* ```ts
* class MyPluginUiServer extends HomebridgePluginUiServer {
* constructor() {
* // this MUST be called first
* super();
*
* // this MUST be called to let the UI know your script is ready for requests
* this.ready();
*
* // example handling requests
* this.onRequest('/hello', (payload) => {
* return { hello: 'world' };
* });
* }
* }
*
* // start the class
* (() => {
* return new MyPluginUiServer();
* })();
* ```
*/
export declare class HomebridgePluginUiServer {
private handlers;
constructor();
get homebridgeStoragePath(): string | undefined;
get homebridgeConfigPath(): string | undefined;
get homebridgeUiVersion(): string | undefined;
private sendResponse;
private processRequest;
/**
* Let the server and UI know you are ready to receive requests.
* This method must be called when you are ready to process requests!
* @example
* ```ts
* this.ready();
* ```
*/
ready(): void;
/**
* Register a new request handler for a given route.
* @param path the request route name
* @param fn the function to handle the request and provide a response
*
* @example
* ```ts
* this.onRequest('/hello', async (payload) => {
* return {hello: 'user'};
* });
* ```
*
* You can then make requests to this endpoint from the client / ui using `homebridge.request`:
* @example
* ```ts
* homebridge.request('/hello', {some: 'payload data'});
* ```
*
*/
onRequest(path: string, fn: RequestHandler): void;
/**
* Push an event or stream data to the UI.
* @param event the event name, the plugin UI can listen for this event
* @param data the data to send
*
* @example
* ```ts
* this.pushEvent('my-event', {some: 'data'});
* ```
*
* In the client / ui, you would then listen to this event using `homebridge.addEventListener`:
*
* @example
* ```ts
* homebridge.addEventListener('my-event', (event) => {
* // do something with the event
* });
* ```
*/
pushEvent(event: string, data: any): void;
}
export declare class RequestError extends Error {
requestError: any;
constructor(message: string, requestError: any);
}
type RequestResponse = string | number | Record<any, any> | Array<any>;
type RequestHandler = (arg: any) => Promise<RequestResponse> | RequestResponse;
export {};
//# sourceMappingURL=server.d.ts.map