app.net
Networking utility to communicate with server. This utility simplify usage of XMLHTTPRequest and provide better future support.
Methods#
| Name | Return Type | Description |
|---|---|---|
| get | Promise<string> | Get something from server asynchronously, as plain string. |
| getJSON | Promise<Object> | Get JSON data from server then decode as an Object. |
| getImage | Promise<ImageBitmap> | Get image from anywhere at internet, asynchronously. |
| post | Promise<string> | Send something to server asynchronously. |
| setHeader | void | Set or add new to request header. |
| getHeader | string | Get request header value corresponds to key name. |
| removeHeader | void | Removes request header item identified by key name. |
| download | void | Download file from anywhere at internet or your own server. |
| upload | Promise<boolean> | Upload a single or multiple files to server, asynchronously. |
| setCredential | void | Set request-level credential. |
| removeCredential | void | Remove request-level credential. |
get#
Get something from server asynchronously, as plain string.
Parameters:
- url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
Return: Promise<string>. Use .then(response => { your code here }) to get the response.
Throw: NetworkException. Use .catch(error => { your code here }) to get the error code and message.
getJSON#
Get JSON data from server then decode as an Object.
Parameters:
- url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
Return: Promise<Object>. Use .then(response => { your code here }) to get the parsed response.
Throw: NetworkException. Use .catch(error => { your code here }) to get the error code and message.
getImage#
Get image from anywhere at internet, asynchronously. Keep in mind that targeted image must allowed by server's CORS Origin Policy.
Parameters:
- url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
Return: Promise<ImageBitmap>. Use .then(bitmap => { your code here }) to get the image bitmap.
Throw: NetworkException. Use .catch(error => { your code here }) to get the error code and message.
post#
Send something to server asynchronously. Make sure the intended URL allows POST Method.
Parameters:
- url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
- thing: (string or Object type) Something to be sent. If object, it will encoded as JSON.
Return: Promise<string>. Use .then(response => { your code here }) to get the response.
Throw: NetworkException. Use .catch(error => { your code here }) to get the error code and message.
setHeader#
Set or add new to request header. Every added HTTP request header will stay persist until app exit. For more explanation of about HTTP request header, see this documentation.
Parameters:
- key: (string type) Header key name.
- value: (string type) Value for intended header key.
Return: void
getHeader#
Get request header value corresponds to key name. For more explanation of about HTTP request header, see this documentation.
Parameters:
- key: (string type) Header key name.
Return: string or undefined. Value of intended header key. If the key isn't exist, returns undefined.
removeHeader#
Removes request header item identified by key name. For more explanation of about HTTP request header, see this documentation.
Parameters:
- key: (string type) Header key name.
Return: void
download#
Download file from anywhere at internet or your own server. Progress and response status are handled by browser. Targeted file must allowed by server's CORS Origin Policy.
Parameters:
- url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
- filename: (string type) File name for client device.
Return: void
upload#
Upload a single or multiple files to server, asynchronously. Make sure the intended URL allows POST Method.
Parameters:
- url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
- callback: (object type) an object that contains one of or multiple of these functions:
onProgress(percentage, loadedBytes, totalBytes, fileCount)onCancel()onSuccess()onFailed(error)
- filetype: (string type, optional) MIME type or file extension. This filters which file formats allowed to be uploaded.
- dataKey: (string type, optional) Key name to be shown on server side, default is "data".
Return: Promise<boolean>. Use .then(isSuccess => { your code here }) to run something when done (if isSuccess == true) or fail (if isSuccess == false).
Throw: NetworkException. Use .catch(error => { your code here }) to get the error code and message.
setCredential#
Set request-level credential. Typically used on corporate network which requires credential to get proper response from server. To know if intended server requires request-level credential, try manually enter URL toward that server on browser and we expecting a dialog box asking for username and password.
Parameters:
- username: (string type) Typically an email or Unix-like account username.
- password: (string type) a Password with charset matches with server's requirement.
Return: void
removeCredential#
Remove request-level credential.
Return: void