fsm-sdk
Version:
Node.JS sdk to interface with SAP Field Service Management APIs.
62 lines • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchResponse = void 0;
class BatchResponse {
constructor(body) {
this.body = body;
}
parseResponseContentType(response) {
const matches = response.match(new RegExp('(content-type: )(.*)', 'ig'));
return !matches || !matches[1] // the first content-type is the type of the request
? undefined
: matches[1].toLowerCase().replace('content-type: ', '').trim();
}
parseRequestURL(response) {
const matches = response.match(new RegExp('(http|https)://[a-z0-9\-_]+(\.[a-z0-9\-_]+)+([a-z0-9\-\.,@\?^=%&;:/~\+#]*[a-z0-9\-@\?^=%&;/~\+#])?', 'i'));
return !matches
? undefined
: matches[0];
}
parseResponseStatusCode(response) {
const matches = response.match(new RegExp('(HTTP\/1.1 )([0-9]+)'));
return !matches || !matches[0]
? undefined
: parseInt(matches[0].replace('HTTP/1.1 ', '').trim(), 10);
}
parseResponseBody(response) {
try {
const [jsonString] = response.split('\n')
.filter(line => line.trim().indexOf('{') === 0);
const result = typeof jsonString === 'undefined' || !jsonString || jsonString === 'undefined' ? '{}' : jsonString;
return JSON.parse(result);
}
catch (ex) {
throw new Error(`Error (${ex instanceof Error && ex.message ? ex.message : ex}) parsing body of ${response}`);
}
}
parseRequestContentId(response) {
const matches = response.match(new RegExp('(Content-ID: )(.*)'));
return !matches || !matches[0]
? undefined
: matches[0].replace('Content-ID: ', '').trim();
}
parseChildResponse(response) {
return {
body: this.parseResponseBody(response),
statusCode: this.parseResponseStatusCode(response),
contentType: this.parseResponseContentType(response),
requestOptions: {
url: this.parseRequestURL(response),
contentId: this.parseRequestContentId(response)
}
};
}
toJson() {
return this.body.split('Content-ID')
.map((it) => 'Content-ID' + it)
.filter((it, idx) => idx !== 0)
.map((it) => this.parseChildResponse(it));
}
}
exports.BatchResponse = BatchResponse;
//# sourceMappingURL=batch-response.js.map