@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
87 lines • 2.71 kB
JavaScript
export var MockAjaxCall;
(function (MockAjaxCall) {
/**
* Mocks a successful response to an Ajax call.
*
* @param mockData mocked data to be returned in the successful response's body.
*/
MockAjaxCall.mockResponse = function (mockData) {
var parsedResponse;
try {
parsedResponse = JSON.parse(mockData);
}
catch (e) {
// If parsing fails, use the raw string
parsedResponse = mockData;
}
return {
status: 200,
responseText: mockData,
response: parsedResponse
};
};
/**
* Mocks a unsuccessful 401 Not Authorized response to an Ajax call.
*
* @param mockData mocked data to be returned in the successful response's body.
*/
MockAjaxCall.mockNotAuthorizedResponse = function (mockData) {
if (mockData === void 0) { mockData = JSON.stringify({}); }
var parsedResponse;
try {
parsedResponse = JSON.parse(mockData);
}
catch (e) {
// If parsing fails, use the raw string
parsedResponse = mockData;
}
return {
status: 401,
responseText: mockData,
response: parsedResponse
};
};
/**
* Mocks a unsuccessful 404 Not Found response to an Ajax call.
*
* @param mockData mocked data to be returned in the successful response's body.
*/
MockAjaxCall.mockNotFoundResponse = function (mockData) {
if (mockData === void 0) { mockData = JSON.stringify({}); }
var parsedResponse;
try {
parsedResponse = JSON.parse(mockData);
}
catch (e) {
// If parsing fails, use the raw string
parsedResponse = mockData;
}
return {
status: 404,
responseText: mockData,
response: parsedResponse
};
};
/**
* Mocks a unsuccessful 400 Bad Request response to an Ajax call.
*
* @param mockData mocked data to be returned in the successful response's body.
*/
MockAjaxCall.mockBadRequestResponse = function (mockData) {
if (mockData === void 0) { mockData = JSON.stringify({}); }
var parsedResponse;
try {
parsedResponse = JSON.parse(mockData);
}
catch (e) {
// If parsing fails, use the raw string
parsedResponse = mockData;
}
return {
status: 400,
responseText: mockData,
response: parsedResponse
};
};
})(MockAjaxCall || (MockAjaxCall = {}));
//# sourceMappingURL=mockajaxcall.js.map