Skip to content

Commit 8116ddd

Browse files
benoitbzldanielsogl
authored andcommitted
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
1 parent a6bcc9a commit 8116ddd

File tree

1 file changed

+110
-0
lines changed
  • src/@ionic-native/plugins/web-server

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { Injectable } from '@angular/core';
2+
import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core';
3+
import { Observable } from 'rxjs/Observable';
4+
5+
export interface Response {
6+
status: number;
7+
body: string;
8+
headers: { [key: string]: string};
9+
}
10+
11+
export interface Request {
12+
requestId: string;
13+
body: string;
14+
headers: string;
15+
method: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE';
16+
path: string;
17+
query: string;
18+
}
19+
20+
/**
21+
* @name Web Server
22+
* @description
23+
* This plugin allows you to start a local dynamic content web server for android and iOS devices.
24+
*
25+
* @usage
26+
* ```typescript
27+
* import { WebServer } from '@ionic-native/web-server';
28+
*
29+
*
30+
* constructor(private webServer: WebServer) { }
31+
*
32+
* ...
33+
*
34+
* this.webServer.onRequest().subscribe(data => {
35+
* console.log(data);
36+
* const res: Response = {
37+
* status: 200,
38+
* body: '',
39+
* headers: {
40+
* 'Content-Type': 'text/html'
41+
* }
42+
* };
43+
*
44+
* this.webServer.sendResponse(data.requestId, res)
45+
* .catch((error: any) => console.error(error));
46+
* });
47+
*
48+
* this.webServer.start(80)
49+
* .catch((error: any) => console.error(error));
50+
*
51+
* ```
52+
*
53+
* @interfaces
54+
* Response
55+
* Request
56+
*/
57+
@Plugin({
58+
pluginName: 'WebServer',
59+
plugin: 'cordova-plugin-webserver',
60+
pluginRef: 'window.webserver',
61+
repo: 'https://github.com/bykof/cordova-plugin-webserver.git',
62+
platforms: ['Android', 'iOS']
63+
})
64+
@Injectable()
65+
export class WebServer extends IonicNativePlugin {
66+
67+
/**
68+
* This method will start your webserver.
69+
* @param port {number} Port number (default to 8080)
70+
*/
71+
@Cordova({
72+
callbackOrder: 'reverse',
73+
})
74+
start(port?: number): Promise<any> {
75+
return;
76+
}
77+
78+
/**
79+
* This method will stop your webserver.
80+
*/
81+
@Cordova()
82+
stop(): Promise<any> {
83+
return;
84+
}
85+
86+
/**
87+
* This method returns an observable that streams HTTP requests to an observer.
88+
* @return {Observable<Request>} Returns an observable to resolve as a Request object
89+
*/
90+
@Cordova({
91+
callbackOrder: 'reverse',
92+
observable: true,
93+
clearFunction: 'stop'
94+
})
95+
onRequest(): Observable<Request> {
96+
return;
97+
}
98+
99+
/**
100+
* This method sends a response to a request.
101+
* @param requestId {string} Request ID to respond to
102+
* @param responseObject {Response} Response object
103+
* @return {Promise<any>} Returns a promise that resolves when something happens
104+
*/
105+
@Cordova()
106+
sendResponse(requestId: string, responseObject: Response): Promise<any> {
107+
return;
108+
}
109+
110+
}

0 commit comments

Comments
 (0)