WiFi Pineapple TypeScript Documentation
Table of contents
Introduction
WiFi Pineapple Mark VII Module front-ends include a service named ApiService
that offers a variety of helper functions, such as wrappers for easier interfaction with the REST API or to communicate with Modules. It is instantiated in the module component as API
, an should always be named the same in custom components written by the module developer.
@Component({
selector: 'lib-example-module',
templateUrl: './example-module.component.html',
styleUrls: ['./example-module.component.css']
})
export class ExampleModuleComponent implments OnInit {
constructor(private API: ApiService) { }
ngOnInit() { }
}
Module Requests
Module requests are made with the this.API.request()
function. It takes two arguments:
payload: any
callback: any
The payload
must be a JSON structure that contains at least two properties:
module: string
(The name of the module you’re making a request to)action: string
(The action you are making a request for)
The callback
is a function that takes a response: any
as an argument. Extra data can be added to the request structure, and will be passwd to the module back-end where it may be processed.
// A simple Module request
this.API.request({
module: 'ExampleModule',
action: 'some_action'
}, (response) => {
console.log(response);
});
// A module request with extra supplied data and error handling
this.API.request({
module: 'ExampleModule',
action: 'another_action',
my_data: 'My Own Data String',
other_data: could_be_a_variable
}, (response) {
if (response.error) {
// Handle Error
console.log('An error happened');
} else {
// Handle Success
console.log('Success!');
}
});
REST API Requests
Regular API requests should be made with the following functions, depending on the type of request as specified in the REST API docs.
Example
Unauthenticate the current session.
this.API.unauth();
Example
Enables a spinner in the UI title bar to indicate a busy state.
this.API.setBusy();
Example
Disables the UI title bar busy spinner.
this.API.setNotBusy();
this.API.APIGet(path: string, callback: (any));
Make a GET Request to a GET endpoint described in the REST API documentation.
Example
this.API.APIGet('/api/status', (resp) => {
console.log(resp);
});
async this.API.APIGetAsync(path: string);
Asynchronously make a GET Request to a GET endpoint described in the REST API documentation.
Example
const resp: any = await this.API.APIGetAsync('/api/status');
console.log(resp);
this.API.APIPut(path: string, callback: (any));
Make a PUT Request to a PUT endpoint described in the REST API documentation.
Example
this.API.APIPut('/api/status', (resp) => {
console.log(resp);
});
async this.API.APIPutAsync(path: string);
Asynchronously make a PUT Request to a PUT endpoint described in the REST API documentation.
Example
const resp: any = await this.API.APIPutAsync('/api/status');
console.log(resp);
this.API.APIPost(path: string, body: any, callback: (any));
Make a POST Request to a POST endpoint described in the REST API documentation.
Example
this.API.APIPost('/api/status', { content: 'content'}, (resp) => {
console.log(resp);
});
async this.API.APIGetAsync(path: string, body: any);
Asynchronously make a POST Request to a POST endpoint described in the REST API documentation.
Example
const resp: any = await this.API.APIPostAsync('/api/status', { content: 'content'});
console.log(resp);
this.API.APIDelete(path: string, callback: (any));
Make a DELETE Request to a DELETE endpoint described in the REST API documentation.
Example
this.API.APIDelete('/api/status', (resp) => {
console.log(resp);
});
async this.API.APIDeleteAsync(path: string);
Asynchronously make a DELETE Request to a DELETE endpoint described in the REST API documentation.
Example
const resp: any = await this.API.APIDeleteAsync('/api/status');
console.log(resp);
this.API.APIDownload(fullpath: string, filename: string);
Example
this.API.APIDownload('/tmp/log.txt', 'log-output');