UNPKG

10.7 kBJavaScriptView Raw
1"use strict";
2/**
3 * -------------------------------------------------------------------------------------------
4 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
5 * See License in the project root for license information.
6 * -------------------------------------------------------------------------------------------
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.GraphResponseHandler = exports.DocumentType = void 0;
10var tslib_1 = require("tslib");
11var ResponseType_1 = require("./ResponseType");
12/**
13 * @enum
14 * Enum for document types
15 * @property {string} TEXT_HTML - The text/html content type
16 * @property {string} TEXT_XML - The text/xml content type
17 * @property {string} APPLICATION_XML - The application/xml content type
18 * @property {string} APPLICATION_XHTML - The application/xhml+xml content type
19 */
20var DocumentType;
21(function (DocumentType) {
22 DocumentType["TEXT_HTML"] = "text/html";
23 DocumentType["TEXT_XML"] = "text/xml";
24 DocumentType["APPLICATION_XML"] = "application/xml";
25 DocumentType["APPLICATION_XHTML"] = "application/xhtml+xml";
26})(DocumentType || (exports.DocumentType = DocumentType = {}));
27/**
28 * @enum
29 * Enum for Content types
30 * @property {string} TEXT_PLAIN - The text/plain content type
31 * @property {string} APPLICATION_JSON - The application/json content type
32 */
33var ContentType;
34(function (ContentType) {
35 ContentType["TEXT_PLAIN"] = "text/plain";
36 ContentType["APPLICATION_JSON"] = "application/json";
37})(ContentType || (ContentType = {}));
38/**
39 * @enum
40 * Enum for Content type regex
41 * @property {string} DOCUMENT - The regex to match document content types
42 * @property {string} IMAGE - The regex to match image content types
43 */
44var ContentTypeRegexStr;
45(function (ContentTypeRegexStr) {
46 ContentTypeRegexStr["DOCUMENT"] = "^(text\\/(html|xml))|(application\\/(xml|xhtml\\+xml))$";
47 ContentTypeRegexStr["IMAGE"] = "^image\\/.+";
48})(ContentTypeRegexStr || (ContentTypeRegexStr = {}));
49/**
50 * @class
51 * Class for GraphResponseHandler
52 */
53var GraphResponseHandler = /** @class */ (function () {
54 function GraphResponseHandler() {
55 }
56 /**
57 * @private
58 * @static
59 * To parse Document response
60 * @param {Response} rawResponse - The response object
61 * @param {DocumentType} type - The type to which the document needs to be parsed
62 * @returns A promise that resolves to a document content
63 */
64 GraphResponseHandler.parseDocumentResponse = function (rawResponse, type) {
65 if (typeof DOMParser !== "undefined") {
66 return new Promise(function (resolve, reject) {
67 rawResponse.text().then(function (xmlString) {
68 try {
69 var parser = new DOMParser();
70 var xmlDoc = parser.parseFromString(xmlString, type);
71 resolve(xmlDoc);
72 }
73 catch (error) {
74 reject(error);
75 }
76 });
77 });
78 }
79 else {
80 return Promise.resolve(rawResponse.body);
81 }
82 };
83 /**
84 * @private
85 * @static
86 * @async
87 * To convert the native Response to response content
88 * @param {Response} rawResponse - The response object
89 * @param {ResponseType} [responseType] - The response type value
90 * @returns A promise that resolves to the converted response content
91 */
92 GraphResponseHandler.convertResponse = function (rawResponse, responseType) {
93 return tslib_1.__awaiter(this, void 0, void 0, function () {
94 var responseValue, contentType, _a, mimeType;
95 return tslib_1.__generator(this, function (_b) {
96 switch (_b.label) {
97 case 0:
98 if (rawResponse.status === 204) {
99 // NO CONTENT
100 return [2 /*return*/, Promise.resolve()];
101 }
102 contentType = rawResponse.headers.get("Content-type");
103 _a = responseType;
104 switch (_a) {
105 case ResponseType_1.ResponseType.ARRAYBUFFER: return [3 /*break*/, 1];
106 case ResponseType_1.ResponseType.BLOB: return [3 /*break*/, 3];
107 case ResponseType_1.ResponseType.DOCUMENT: return [3 /*break*/, 5];
108 case ResponseType_1.ResponseType.JSON: return [3 /*break*/, 7];
109 case ResponseType_1.ResponseType.STREAM: return [3 /*break*/, 9];
110 case ResponseType_1.ResponseType.TEXT: return [3 /*break*/, 11];
111 }
112 return [3 /*break*/, 13];
113 case 1: return [4 /*yield*/, rawResponse.arrayBuffer()];
114 case 2:
115 responseValue = _b.sent();
116 return [3 /*break*/, 24];
117 case 3: return [4 /*yield*/, rawResponse.blob()];
118 case 4:
119 responseValue = _b.sent();
120 return [3 /*break*/, 24];
121 case 5: return [4 /*yield*/, GraphResponseHandler.parseDocumentResponse(rawResponse, DocumentType.TEXT_XML)];
122 case 6:
123 responseValue = _b.sent();
124 return [3 /*break*/, 24];
125 case 7: return [4 /*yield*/, rawResponse.json()];
126 case 8:
127 responseValue = _b.sent();
128 return [3 /*break*/, 24];
129 case 9: return [4 /*yield*/, Promise.resolve(rawResponse.body)];
130 case 10:
131 responseValue = _b.sent();
132 return [3 /*break*/, 24];
133 case 11: return [4 /*yield*/, rawResponse.text()];
134 case 12:
135 responseValue = _b.sent();
136 return [3 /*break*/, 24];
137 case 13:
138 if (!(contentType !== null)) return [3 /*break*/, 22];
139 mimeType = contentType.split(";")[0];
140 if (!new RegExp(ContentTypeRegexStr.DOCUMENT).test(mimeType)) return [3 /*break*/, 15];
141 return [4 /*yield*/, GraphResponseHandler.parseDocumentResponse(rawResponse, mimeType)];
142 case 14:
143 responseValue = _b.sent();
144 return [3 /*break*/, 21];
145 case 15:
146 if (!new RegExp(ContentTypeRegexStr.IMAGE).test(mimeType)) return [3 /*break*/, 16];
147 responseValue = rawResponse.blob();
148 return [3 /*break*/, 21];
149 case 16:
150 if (!(mimeType === ContentType.TEXT_PLAIN)) return [3 /*break*/, 18];
151 return [4 /*yield*/, rawResponse.text()];
152 case 17:
153 responseValue = _b.sent();
154 return [3 /*break*/, 21];
155 case 18:
156 if (!(mimeType === ContentType.APPLICATION_JSON)) return [3 /*break*/, 20];
157 return [4 /*yield*/, rawResponse.json()];
158 case 19:
159 responseValue = _b.sent();
160 return [3 /*break*/, 21];
161 case 20:
162 responseValue = Promise.resolve(rawResponse.body);
163 _b.label = 21;
164 case 21: return [3 /*break*/, 23];
165 case 22:
166 /**
167 * RFC specification {@link https://tools.ietf.org/html/rfc7231#section-3.1.1.5} says:
168 * A sender that generates a message containing a payload body SHOULD
169 * generate a Content-Type header field in that message unless the
170 * intended media type of the enclosed representation is unknown to the
171 * sender. If a Content-Type header field is not present, the recipient
172 * MAY either assume a media type of "application/octet-stream"
173 * ([RFC2046], Section 4.5.1) or examine the data to determine its type.
174 *
175 * So assuming it as a stream type so returning the body.
176 */
177 responseValue = Promise.resolve(rawResponse.body);
178 _b.label = 23;
179 case 23: return [3 /*break*/, 24];
180 case 24: return [2 /*return*/, responseValue];
181 }
182 });
183 });
184 };
185 /**
186 * @public
187 * @static
188 * @async
189 * To get the parsed response
190 * @param {Response} rawResponse - The response object
191 * @param {ResponseType} [responseType] - The response type value
192 * @param {GraphRequestCallback} [callback] - The graph request callback function
193 * @returns The parsed response
194 */
195 GraphResponseHandler.getResponse = function (rawResponse, responseType, callback) {
196 return tslib_1.__awaiter(this, void 0, void 0, function () {
197 var response;
198 return tslib_1.__generator(this, function (_a) {
199 switch (_a.label) {
200 case 0:
201 if (!(responseType === ResponseType_1.ResponseType.RAW)) return [3 /*break*/, 1];
202 return [2 /*return*/, Promise.resolve(rawResponse)];
203 case 1: return [4 /*yield*/, GraphResponseHandler.convertResponse(rawResponse, responseType)];
204 case 2:
205 response = _a.sent();
206 if (rawResponse.ok) {
207 // Status Code 2XX
208 if (typeof callback === "function") {
209 callback(null, response);
210 }
211 else {
212 return [2 /*return*/, response];
213 }
214 }
215 else {
216 // NOT OK Response
217 throw response;
218 }
219 _a.label = 3;
220 case 3: return [2 /*return*/];
221 }
222 });
223 });
224 };
225 return GraphResponseHandler;
226}());
227exports.GraphResponseHandler = GraphResponseHandler;
228//# sourceMappingURL=GraphResponseHandler.js.map
\No newline at end of file