У меня есть этот вызов извлечения:
api<T>(url: string, headers: Request): Promise<T> {
return fetch(url, headers)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json().then(data => data as T);
})
.catch((error: Error) => {
throw error;
});
}
componentDidMount(){
this.api<Array<Response>>(url, requestData)
.then(data => {
this.setState({
jobs: data
});
})
.catch(error => {
console.error(error);
});
}
Но ответ, который я получаю, это поток + json, поэтому я получаю недопустимый json в .json ().
Я видел, что тамбиблиотека, которая может мне помочь: http://oboejs.com/examples
Но у меня проблемы с использованием гобоя и машинописи (для начинающих) (с использованием https://www.npmjs.com/package/@types/oboe).
Я пытался:
api<T>(headers: Request): Oboe<T> {
return oboe(headers)
.done(function(response) {
return response;
})
.fail(function(error: Error) {
throw error;
});
}
componentDidMount(){
this.api<Array<Response>>(requestData)
.done(data => {
this.setState({
jobs: data
});
})
.fail(error => {
console.error(error);
});
}
Но есть очевидные ошибки, так как я не знаю, какой тип гобой должен возвращаться, поэтому я получаю ошибку Oboe is not generic
.