From 8116ddd1399444d4eb023ee2416310f22f7f1f9c Mon Sep 17 00:00:00 2001 From: Benoit Perrin Date: Tue, 25 Sep 2018 16:42:07 +0200 Subject: [PATCH] feat(plugins): add web-server plugin (#2726) * feat(plugins): add web-server plugin Add support of the cordova-plugin-webserver plugin, to start a a dynamic content web server on iOS and android devices. * Update index.ts --- src/@ionic-native/plugins/web-server/index.ts | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/@ionic-native/plugins/web-server/index.ts diff --git a/src/@ionic-native/plugins/web-server/index.ts b/src/@ionic-native/plugins/web-server/index.ts new file mode 100644 index 0000000000..199bbc7f84 --- /dev/null +++ b/src/@ionic-native/plugins/web-server/index.ts @@ -0,0 +1,110 @@ +import { Injectable } from '@angular/core'; +import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core'; +import { Observable } from 'rxjs/Observable'; + +export interface Response { + status: number; + body: string; + headers: { [key: string]: string}; +} + +export interface Request { + requestId: string; + body: string; + headers: string; + method: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE'; + path: string; + query: string; +} + +/** + * @name Web Server + * @description + * This plugin allows you to start a local dynamic content web server for android and iOS devices. + * + * @usage + * ```typescript + * import { WebServer } from '@ionic-native/web-server'; + * + * + * constructor(private webServer: WebServer) { } + * + * ... + * + * this.webServer.onRequest().subscribe(data => { + * console.log(data); + * const res: Response = { + * status: 200, + * body: '', + * headers: { + * 'Content-Type': 'text/html' + * } + * }; + * + * this.webServer.sendResponse(data.requestId, res) + * .catch((error: any) => console.error(error)); + * }); + * + * this.webServer.start(80) + * .catch((error: any) => console.error(error)); + * + * ``` + * + * @interfaces + * Response + * Request + */ +@Plugin({ + pluginName: 'WebServer', + plugin: 'cordova-plugin-webserver', + pluginRef: 'window.webserver', + repo: 'https://github.com/bykof/cordova-plugin-webserver.git', + platforms: ['Android', 'iOS'] +}) +@Injectable() +export class WebServer extends IonicNativePlugin { + + /** + * This method will start your webserver. + * @param port {number} Port number (default to 8080) + */ + @Cordova({ + callbackOrder: 'reverse', + }) + start(port?: number): Promise { + return; + } + + /** + * This method will stop your webserver. + */ + @Cordova() + stop(): Promise { + return; + } + + /** + * This method returns an observable that streams HTTP requests to an observer. + * @return {Observable} Returns an observable to resolve as a Request object + */ + @Cordova({ + callbackOrder: 'reverse', + observable: true, + clearFunction: 'stop' + }) + onRequest(): Observable { + return; + } + + /** + * This method sends a response to a request. + * @param requestId {string} Request ID to respond to + * @param responseObject {Response} Response object + * @return {Promise} Returns a promise that resolves when something happens + */ + @Cordova() + sendResponse(requestId: string, responseObject: Response): Promise { + return; + } + +}