app.net

Networking utility to communicate with server. This utility simplify usage of XMLHTTPRequest and provide better future support.

Methods#

NameReturn TypeDescription
getPromise<string>Get something from server asynchronously, as plain string.
getJSONPromise<Object>Get JSON data from server then decode as an Object.
getImagePromise<ImageBitmap>Get image from anywhere at internet, asynchronously.
postPromise<string>Send something to server asynchronously.
setHeadervoidSet or add new to request header.
getHeaderstringGet request header value corresponds to key name.
removeHeadervoidRemoves request header item identified by key name.
downloadvoidDownload file from anywhere at internet or your own server.
uploadPromise<boolean>Upload a single or multiple files to server, asynchronously.
setCredentialvoidSet request-level credential.
removeCredentialvoidRemove request-level credential.

get#

app.net.get(url);

Get something from server asynchronously, as plain string.

Parameters:

  1. 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#

app.net.getJSON(url);

Get JSON data from server then decode as an Object.

Parameters:

  1. 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#

app.net.getImage(url);

Get image from anywhere at internet, asynchronously. Keep in mind that targeted image must allowed by server's CORS Origin Policy.

Parameters:

  1. 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#

app.net.post(url, thing);

Send something to server asynchronously. Make sure the intended URL allows POST Method.

Parameters:

  1. url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
  2. 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#

app.net.setHeader(key, value);

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:

  1. key: (string type) Header key name.
  2. value: (string type) Value for intended header key.

Return: void


getHeader#

app.net.getHeader(key);

Get request header value corresponds to key name. For more explanation of about HTTP request header, see this documentation.

Parameters:

  1. key: (string type) Header key name.

Return: string or undefined. Value of intended header key. If the key isn't exist, returns undefined.


removeHeader#

app.net.removeHeader(key);

Removes request header item identified by key name. For more explanation of about HTTP request header, see this documentation.

Parameters:

  1. key: (string type) Header key name.

Return: void


download#

app.net.download(url, filename);

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:

  1. url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
  2. filename: (string type) File name for client device.

Return: void


upload#

app.net.upload(url, callback, filetype?, dataKey?);

Upload a single or multiple files to server, asynchronously. Make sure the intended URL allows POST Method.

Parameters:

  1. url: (string type) Targeted HTTP-based URL. Use prefix "/" to reference your own web server.
  2. callback: (object type) an object that contains one of or multiple of these functions:
    • onProgress(percentage, loadedBytes, totalBytes, fileCount)
    • onCancel()
    • onSuccess()
    • onFailed(error)
  3. filetype: (string type, optional) MIME type or file extension. This filters which file formats allowed to be uploaded.
  4. 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#

app.net.setCredential(username, password);

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:

  1. username: (string type) Typically an email or Unix-like account username.
  2. password: (string type) a Password with charset matches with server's requirement.

Return: void


removeCredential#

app.net.removeCredential();

Remove request-level credential.

Return: void