ioBroker.javascript
ioBroker.javascript copied to clipboard
Replace request with axios for Version 7.x
Just some brainstorming how to replace the deprecated request library with axios in blockly without breaking existing scripts.
Current situation
Example script (generated with blockly):
try {
require("request")('https://dummyjson.com/products/1', async (error, response, result) => {
}).on("error", (e) => { console.error(e); });
} catch (e) { console.error(e); }
The callback argument gets 3 arguments:
- An
errorwhen applicable (usually fromhttp.ClientRequestobject) - An
http.IncomingMessageobject (Response object) - The third is the response body (String or Buffer, or JSON object if the json option is supplied)
response
- https://nodejs.org/docs/latest-v18.x/api/http.html#class-httpincomingmessage
{
"statusCode": 200,
"body": "...",
"headers": {
"access-control-allow-origin": "*",
"x-dns-prefetch-control": "off",
"x-frame-options": "SAMEORIGIN",
"strict-transport-security": "max-age=15552000; includeSubDomains",
"x-download-options": "noopen",
"x-content-type-options": "nosniff",
"x-xss-protection": "1; mode=block",
"x-ratelimit-limit": "120",
"x-ratelimit-remaining": "119",
"date": "Thu, 13 Jul 2023 18:11:29 GMT",
"x-ratelimit-reset": "1689271903",
"content-type": "application/json; charset=utf-8",
"content-length": "519",
"etag": "W/\"207-QowjjkZS3dPvv4L6zPF2KPB7cKk\"",
"vary": "Accept-Encoding",
"server": "railway",
"connection": "close"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "dummyjson.com",
"port": 443,
"hostname": "dummyjson.com",
"hash": null,
"search": null,
"query": null,
"pathname": "/products/1",
"path": "/products/1",
"href": "https://dummyjson.com/products/1"
},
"method": "GET",
"headers": {}
}
}
error
- https://nodejs.org/docs/latest-v18.x/api/http.html#class-httpclientrequest
{
"errno": -3008,
"code": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "dumm"
}
Axios
- add as requirement to sandbox
- https://github.com/axios/axios#response-schema
// ???
Why not use got? https://github.com/sindresorhus/got/blob/main/documentation/migration-guides/request.md
Axios is already used in a lot of adapters. Even in this adapter:
https://github.com/ioBroker/ioBroker.javascript/blob/9e5ac60f61bfeb036ccb475959a01f6e90976820/package.json#L36
- https://github.com/ioBroker/ioBroker.javascript/issues/152
- https://github.com/ioBroker/ioBroker.javascript/issues/883