UNPKG

6.08 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.isCustomHost = exports.isGraphURL = exports.serializeContent = exports.urlJoin = exports.oDataQueryNames = void 0;
10/**
11 * @module GraphRequestUtil
12 */
13var Constants_1 = require("./Constants");
14var GraphClientError_1 = require("./GraphClientError");
15/**
16 * To hold list of OData query params
17 */
18exports.oDataQueryNames = ["$select", "$expand", "$orderby", "$filter", "$top", "$skip", "$skipToken", "$count"];
19/**
20 * To construct the URL by appending the segments with "/"
21 * @param {string[]} urlSegments - The array of strings
22 * @returns The constructed URL string
23 */
24var urlJoin = function (urlSegments) {
25 var removePostSlash = function (s) { return s.replace(/\/+$/, ""); };
26 var removePreSlash = function (s) { return s.replace(/^\/+/, ""); };
27 var joiner = function (pre, cur) { return [removePostSlash(pre), removePreSlash(cur)].join("/"); };
28 var parts = Array.prototype.slice.call(urlSegments);
29 return parts.reduce(joiner);
30};
31exports.urlJoin = urlJoin;
32/**
33 * Serializes the content
34 * @param {any} content - The content value that needs to be serialized
35 * @returns The serialized content
36 *
37 * Note:
38 * This conversion is required due to the following reasons:
39 * Body parameter of Request method of isomorphic-fetch only accepts Blob, ArrayBuffer, FormData, TypedArrays string.
40 * Node.js platform does not support Blob, FormData. Javascript File object inherits from Blob so it is also not supported in node. Therefore content of type Blob, File, FormData will only come from browsers.
41 * Parallel to ArrayBuffer in javascript, node provides Buffer interface. Node's Buffer is able to send the arbitrary binary data to the server successfully for both Browser and Node platform. Whereas sending binary data via ArrayBuffer or TypedArrays was only possible using Browser. To support both Node and Browser, `serializeContent` converts TypedArrays or ArrayBuffer to `Node Buffer`.
42 * If the data received is in JSON format, `serializeContent` converts the JSON to string.
43 */
44var serializeContent = function (content) {
45 var className = content && content.constructor && content.constructor.name;
46 if (className === "Buffer" || className === "Blob" || className === "File" || className === "FormData" || typeof content === "string") {
47 return content;
48 }
49 if (className === "ArrayBuffer") {
50 content = Buffer.from(content);
51 }
52 else if (className === "Int8Array" || className === "Int16Array" || className === "Int32Array" || className === "Uint8Array" || className === "Uint16Array" || className === "Uint32Array" || className === "Uint8ClampedArray" || className === "Float32Array" || className === "Float64Array" || className === "DataView") {
53 content = Buffer.from(content.buffer);
54 }
55 else {
56 try {
57 content = JSON.stringify(content);
58 }
59 catch (error) {
60 throw new Error("Unable to stringify the content");
61 }
62 }
63 return content;
64};
65exports.serializeContent = serializeContent;
66/**
67 * Checks if the url is one of the service root endpoints for Microsoft Graph and Graph Explorer.
68 * @param {string} url - The url to be verified
69 * @returns {boolean} - Returns true if the url is a Graph URL
70 */
71var isGraphURL = function (url) {
72 return isValidEndpoint(url);
73};
74exports.isGraphURL = isGraphURL;
75/**
76 * Checks if the url is for one of the custom hosts provided during client initialization
77 * @param {string} url - The url to be verified
78 * @param {Set} customHosts - The url to be verified
79 * @returns {boolean} - Returns true if the url is a for a custom host
80 */
81var isCustomHost = function (url, customHosts) {
82 customHosts.forEach(function (host) { return isCustomHostValid(host); });
83 return isValidEndpoint(url, customHosts);
84};
85exports.isCustomHost = isCustomHost;
86/**
87 * Checks if the url is for one of the provided hosts.
88 * @param {string} url - The url to be verified
89 * @param {Set<string>} allowedHosts - A set of hosts.
90 * @returns {boolean} - Returns true is for one of the provided endpoints.
91 */
92var isValidEndpoint = function (url, allowedHosts) {
93 if (allowedHosts === void 0) { allowedHosts = Constants_1.GRAPH_URLS; }
94 // Valid Graph URL pattern - https://graph.microsoft.com/{version}/{resource}?{query-parameters}
95 // Valid Graph URL example - https://graph.microsoft.com/v1.0/
96 url = url.toLowerCase();
97 if (url.indexOf("https://") !== -1) {
98 url = url.replace("https://", "");
99 // Find where the host ends
100 var startofPortNoPos = url.indexOf(":");
101 var endOfHostStrPos = url.indexOf("/");
102 var hostName = "";
103 if (endOfHostStrPos !== -1) {
104 if (startofPortNoPos !== -1 && startofPortNoPos < endOfHostStrPos) {
105 hostName = url.substring(0, startofPortNoPos);
106 return allowedHosts.has(hostName);
107 }
108 // Parse out the host
109 hostName = url.substring(0, endOfHostStrPos);
110 return allowedHosts.has(hostName);
111 }
112 }
113 return false;
114};
115/**
116 * Throws error if the string is not a valid host/hostname and contains other url parts.
117 * @param {string} url - The host to be verified
118 */
119var isCustomHostValid = function (host) {
120 if (host.indexOf("/") !== -1) {
121 throw new GraphClientError_1.GraphClientError("Please add only hosts or hostnames to the CustomHosts config. If the url is `http://example.com:3000/`, host is `example:3000`");
122 }
123};
124//# sourceMappingURL=GraphRequestUtil.js.map
\No newline at end of file