UNPKG

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