Answer the question
In order to leave comments, you need to log in
Result parsing in Angular 5+, how to?
Good day everyone.
Data comes from the server but wrapped in its own response format:
{
success: boolean; // статус выполнения запроса
data: any | null; //здесь может вернуться любой объект/массив объектов/null/пустые объект или массив
msg: string | null; // текст ошибки либо null в случае успеха
}
export interface IResponse<T> {
success: boolean;
data: T | null;
msg: string | null;
}
getDataResponse<T>(response: IResponse<T>) {
if (response && response.data) {
return response.data;
} else {
return null;
}
}
/**
* Получение информации об авторизованном пользователе
* @param {number} id
* @returns {Observable<IUser>}
*/
public getUserInfo(id: number): Observable<IUser> {
const url = `${env.url}/${id}/info`;
return this.httpClient
.get(url)
.pipe(
map((response: IResponse<IUser>) => this.getData(response))
);
}
Answer the question
In order to leave comments, you need to log in
Describe an Http Interceptor that will intercept the response, perform manipulations, and return it in the format you need. Examples can be found by googling: " angular 5 http interceptor response "
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question