UNPKG

@pnp/odata

Version:

pnp - provides shared odata functionality and base classes

153 lines 5.41 kB
import { __awaiter, __extends, __generator } from "tslib"; import { isFunc, hOP } from "@pnp/common"; var ODataParser = /** @class */ (function () { function ODataParser() { } ODataParser.prototype.parse = function (r) { var _this = this; return new Promise(function (resolve, reject) { if (_this.handleError(r, reject)) { _this.parseImpl(r, resolve, reject); } }); }; ODataParser.prototype.parseImpl = function (r, resolve, reject) { var _this = this; if ((r.headers.has("Content-Length") && parseFloat(r.headers.get("Content-Length")) === 0) || r.status === 204) { resolve({}); } else { // patch to handle cases of 200 response with no or whitespace only bodies (#487 & #545) r.text() .then(function (txt) { return txt.replace(/\s/ig, "").length > 0 ? JSON.parse(txt) : {}; }) .then(function (json) { return resolve(_this.parseODataJSON(json)); }) .catch(function (e) { return reject(e); }); } }; /** * Handles a response with ok === false by parsing the body and creating a ProcessHttpClientResponseException * which is passed to the reject delegate. This method returns true if there is no error, otherwise false * * @param r Current response object * @param reject reject delegate for the surrounding promise */ ODataParser.prototype.handleError = function (r, reject) { if (!r.ok) { HttpRequestError.init(r).then(reject); } return r.ok; }; /** * Normalizes the json response by removing the various nested levels * * @param json json object to parse */ ODataParser.prototype.parseODataJSON = function (json) { var result = json; if (hOP(json, "d")) { if (hOP(json.d, "results")) { result = json.d.results; } else { result = json.d; } } else if (hOP(json, "value")) { result = json.value; } return result; }; return ODataParser; }()); export { ODataParser }; var TextParser = /** @class */ (function (_super) { __extends(TextParser, _super); function TextParser() { return _super !== null && _super.apply(this, arguments) || this; } TextParser.prototype.parseImpl = function (r, resolve) { r.text().then(resolve); }; return TextParser; }(ODataParser)); export { TextParser }; var BlobParser = /** @class */ (function (_super) { __extends(BlobParser, _super); function BlobParser() { return _super !== null && _super.apply(this, arguments) || this; } BlobParser.prototype.parseImpl = function (r, resolve) { r.blob().then(resolve); }; return BlobParser; }(ODataParser)); export { BlobParser }; var JSONParser = /** @class */ (function (_super) { __extends(JSONParser, _super); function JSONParser() { return _super !== null && _super.apply(this, arguments) || this; } JSONParser.prototype.parseImpl = function (r, resolve) { r.json().then(resolve); }; return JSONParser; }(ODataParser)); export { JSONParser }; var BufferParser = /** @class */ (function (_super) { __extends(BufferParser, _super); function BufferParser() { return _super !== null && _super.apply(this, arguments) || this; } BufferParser.prototype.parseImpl = function (r, resolve) { if (isFunc(r.arrayBuffer)) { r.arrayBuffer().then(resolve); } else { r.buffer().then(resolve); } }; return BufferParser; }(ODataParser)); export { BufferParser }; var LambdaParser = /** @class */ (function (_super) { __extends(LambdaParser, _super); function LambdaParser(parser) { var _this = _super.call(this) || this; _this.parser = parser; return _this; } LambdaParser.prototype.parseImpl = function (r, resolve) { this.parser(r).then(resolve); }; return LambdaParser; }(ODataParser)); export { LambdaParser }; var HttpRequestError = /** @class */ (function (_super) { __extends(HttpRequestError, _super); function HttpRequestError(message, response, status, statusText) { if (status === void 0) { status = response.status; } if (statusText === void 0) { statusText = response.statusText; } var _this = _super.call(this, message) || this; _this.response = response; _this.status = status; _this.statusText = statusText; _this.isHttpRequestError = true; return _this; } HttpRequestError.init = function (r) { return __awaiter(this, void 0, void 0, function () { var t; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, r.clone().text()]; case 1: t = _a.sent(); return [2 /*return*/, new HttpRequestError("Error making HttpClient request in queryable [" + r.status + "] " + r.statusText + " ::> " + t, r.clone())]; } }); }); }; return HttpRequestError; }(Error)); export { HttpRequestError }; //# sourceMappingURL=parsers.js.map