ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

Replace request with axios for Version 7.x

Open klein0r opened this issue 2 years ago • 3 comments

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:

  1. An error when applicable (usually from http.ClientRequest object)
  2. An http.IncomingMessage object (Response object)
  3. 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
// ???

klein0r avatar Jul 13 '23 18:07 klein0r

Why not use got? https://github.com/sindresorhus/got/blob/main/documentation/migration-guides/request.md

Eistee82 avatar Aug 23 '23 16:08 Eistee82

Axios is already used in a lot of adapters. Even in this adapter:

https://github.com/ioBroker/ioBroker.javascript/blob/9e5ac60f61bfeb036ccb475959a01f6e90976820/package.json#L36

klein0r avatar Aug 25 '23 12:08 klein0r

  • https://github.com/ioBroker/ioBroker.javascript/issues/152
  • https://github.com/ioBroker/ioBroker.javascript/issues/883

klein0r avatar Jan 30 '24 13:01 klein0r