Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use RequestPromise in TypeScript?

I have installed request-promise library and trying to use it in my TypeScript app but without much luck.

If I use it like this:

import {RequestPromise} from'request-promise';

RequestPromise('http://www.google.com')
        .then(function (htmlString) {
            // Process html...
        })
        .catch(function (err) {
            // Crawling failed...
        });

I see this on TS compile output:

error TS2304: Cannot find name 'RequestPromise'.

If I use it this way:

import * as rp from'request-promise';

rp('http://www.google.com')
    .then(function (htmlString) {
        // Process html...
    })
    .catch(function (err) {
        // Crawling failed...
    });

I see an error that says that there is no '.then()' method on object rp.

How do I properly use it with TypeScript?

like image 387
Sergei Basharov Avatar asked Nov 01 '16 08:11

Sergei Basharov


People also ask

What does request promise do?

Request-Promise adds a Bluebird-powered . then(...) method to Request call objects. By default, http response codes other than 2xx will cause the promise to be rejected.

Is request promise deprecated?

This package is also deprecated because it depends on request . Fyi, here is the reasoning of request 's deprecation and a list of alternative libraries.

What can I use instead of NPM request?

I'd strongly suggest using node-fetch. It is based on the fetch API in modern browsers.


1 Answers

You must import all (*) not just RequestPromise:

import * as request from "request-promise";
request.get(...);

This answer elaborates on the differences between import/from and require.

like image 188
Dave Clark Avatar answered Sep 30 '22 20:09

Dave Clark