bilalq / iex-api

Unofficial SDK for using the IEX API
MIT License
42 stars 13 forks source link

unable to use in react with typescript #10

Closed Luxcium closed 5 years ago

Luxcium commented 5 years ago

TS2345: Argument of type 'typeof fetch' is not assignable to parameter of type '{ (input: RequestInfo, init?: RequestInit | undefined): Promise; (input: RequestInfo, init?: RequestInit | undefined): Promise; }'. Types of parameters 'url' and 'input' are incompatible. Type 'RequestInfo' is not assignable to type 'string | Request'. Type 'Request' is not assignable to type 'string | Request'. Type 'Request' is missing the following properties from type 'Request': context, compress, counter, follow, and 6 more.

Luxcium commented 5 years ago

I have changed the code to be typeof fetch | any instead of typeof fetch :

[...]
export default class IEXClient {
  private fetchFunction: typeof fetch | any ;
  private httpsEndpoint: string;
[...]
public constructor(fetchFunction: typeof fetch | any, httpsEndpoint = 'https://api.iextrading.com/1.0') {
[...]
}
[...]

and I recive this error mesage now : Failed to execute 'fetch' on 'Window': Illegal invocation

Luxcium commented 5 years ago

Unhandled Rejection (TypeError): Failed to execute 'fetch' on 'Window': Illegal invocation IEXClient.request src/iexApi/client.ts:46 43 | @param path The path to hit the IEX API endpoint at. 44 | / 45 | public request(path: string): Promise {

46 | return this.fetchFunction(${this.httpsEndpoint}/${path}).then((res: any) => { | ^ 47 | const contentType = res.headers.get('content-type'); 48 | if (contentType === null) { 49 | return null;

Luxcium commented 5 years ago

If I change the code this way it is working (below) but if I set the "this.fetchFunction = window.fetch;" it does fail I am not sure if I have to bind something ...

public request(path: string): Promise { return window.fetch(${this.httpsEndpoint}/${path}).then((res: any) => { const contentType = res.headers.get('content-type'); if (contentType === null) { return null; } if (contentType.includes('application/json')) { return res.json(); } return res.text(); }); }