UNPKG

@dasch-swiss/dsp-js

Version:
53 lines 1.9 kB
import { ApiResponse } from './api-response'; import { ApiResponseError } from './api-response-error'; import { DataError } from './data-error'; /** * @category Response */ export class ApiResponseData extends ApiResponse { // </editor-fold> /// ////////////// // CONSTRUCTOR // /// ////////////// // <editor-fold desc=""> /** * Constructor. */ constructor() { super(); } // </editor-fold> /// ////////// // METHODS // /// ////////// // <editor-fold desc=""> /** * Create an instance from an AjaxResponse. * If the return type in the body shall be checked, this method requires the arguments dataType and jsonConvert. * @throws DataError */ static fromAjaxResponse(ajaxResponse, dataType, jsonConvert) { const responseData = new ApiResponseData(); responseData.response = ajaxResponse; if (ajaxResponse.request && ajaxResponse.request.method) responseData.method = ajaxResponse.request.method; if (ajaxResponse.request && ajaxResponse.request.url) responseData.url = ajaxResponse.request.url; if (ajaxResponse.xhr) responseData.status = ajaxResponse.xhr.status; // Apply json2typescript if required responseData.body = ajaxResponse.response; if (dataType && jsonConvert) { try { responseData.body = jsonConvert.deserializeObject(ajaxResponse.response, dataType); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); const responseError = ApiResponseError.fromErrorString(errorMessage, responseData); throw new DataError(errorMessage, responseError); } } return responseData; } } //# sourceMappingURL=api-response-data.js.map