Closed Luxcium closed 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
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;
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${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();
});
}
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.