API
The ByteMini Program development framework provides a rich set of native APIs that make it easy to access Byte's capabilities, such as getting user information, local storage, and payment functions.
Typically, in the ByteMini Program API, there are several types of APIs:
Event Listener APIs
We conventionally use APIs that start with on
to listen for whether a particular event has occurred. Examples include dlt.onKeyboardHeightChange
and dlt.onForegroundAudioCanPlay
.
These APIs accept a callback function as a parameter. When the event is triggered, this callback function is called, and relevant data is passed as parameters.
Code Example
dlt.onKeyboardHeightChange({
success: function (result) {
console.log(result.height);
},
});
Asynchronous APIs
Most APIs are asynchronous, such as dlt.request
and dlt.login
. These APIs typically accept an Object
type parameter, which supports specifying the following fields to receive the API call result as needed:
Object Parameter Explanation
Parameter | Type | Required | Description |
---|---|---|---|
success | function | No | Callback function for a successful API call |
fail | function | No | Callback function for a failed API call |
complete | function | No | Callback function for the end of the API call (executed for both success and failure) |
Others | Any | - | Other parameters defined by the API |
Callback Function Parameters
The success
, fail
, and complete
functions are passed an Object
type parameter that includes the following fields:
Property | Type | Description |
---|---|---|
errMsg | string | Error information, returns ${apiName}:ok if the call is successful |
errCode | number | Error code, supported by only some APIs; for specific meanings, refer to the documentation for the corresponding API; it is 0 for success |
Others | Any | Other data returned by the API |
The results of asynchronous APIs need to be obtained through the corresponding callback functions passed in the Object
type parameter. Some asynchronous APIs also have return values that can be used to implement more advanced functionality, such as dlt.request
.
Code Example
dlt.login({
success(res) {
console.log(res.code);
},
});
Note: Due to inconsistencies in API callback methods resulting from early development standards, there are differences in callback methods compared to the standards discussed above. For example:
// Callback Function Form
dlt.getSystemInfo((res) => {
console.log(res);
});
// Promise Form
dlt.getClipboard().then((res) => {
console.log(res);
});
// Object - success
dlt.chooseMedia({
success: (res) => {
console.log(res);
},
});
// Object - callback
dlt.downloadFile({
url: "https://xxx.png",
saveName: "xxx.png",
header: "",
callbackId: xxxCallbackId,
callback: function callback(res) {
console.log(res);
},
});