{"version":3,"file":"XhrClient.js","sources":["../../src/network/XhrClient.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { INetworkModule, NetworkRequestOptions, NetworkResponse } from \"@azure/msal-common\";\nimport { BrowserAuthError } from \"../error/BrowserAuthError\";\nimport { HTTP_REQUEST_TYPE } from \"../utils/BrowserConstants\";\n\n/**\n * This client implements the XMLHttpRequest class to send GET and POST requests.\n */\nexport class XhrClient implements INetworkModule {\n\n /**\n * XhrClient for REST endpoints - Get request\n * @param url \n * @param headers \n * @param body \n */\n async sendGetRequestAsync(url: string, options?: NetworkRequestOptions): Promise> {\n return this.sendRequestAsync(url, HTTP_REQUEST_TYPE.GET, options);\n }\n\n /**\n * XhrClient for REST endpoints - Post request\n * @param url \n * @param headers \n * @param body \n */\n async sendPostRequestAsync(url: string, options?: NetworkRequestOptions): Promise> {\n return this.sendRequestAsync(url, HTTP_REQUEST_TYPE.POST, options);\n }\n\n /**\n * Helper for XhrClient requests.\n * @param url \n * @param method \n * @param options \n */\n private sendRequestAsync(url: string, method: HTTP_REQUEST_TYPE, options?: NetworkRequestOptions): Promise> {\n return new Promise>((resolve, reject) => {\n const xhr = new XMLHttpRequest();\n xhr.open(method, url, /* async: */ true);\n this.setXhrHeaders(xhr, options);\n xhr.onload = (): void => {\n if (xhr.status < 200 || xhr.status >= 300) {\n if (method === HTTP_REQUEST_TYPE.POST) {\n reject(BrowserAuthError.createPostRequestFailedError(`Failed with status ${xhr.status}`, url));\n } else {\n reject(BrowserAuthError.createGetRequestFailedError(`Failed with status ${xhr.status}`, url));\n }\n }\n try {\n const jsonResponse = JSON.parse(xhr.responseText) as T;\n const networkResponse: NetworkResponse = {\n headers: this.getHeaderDict(xhr),\n body: jsonResponse,\n status: xhr.status\n };\n resolve(networkResponse);\n } catch (e) {\n reject(BrowserAuthError.createFailedToParseNetworkResponseError(url));\n }\n };\n\n xhr.onerror = (): void => {\n if (window.navigator.onLine) {\n if (method === HTTP_REQUEST_TYPE.POST) {\n reject(BrowserAuthError.createPostRequestFailedError(`Failed with status ${xhr.status}`, url));\n } else {\n reject(BrowserAuthError.createGetRequestFailedError(`Failed with status ${xhr.status}`, url));\n }\n } else {\n reject(BrowserAuthError.createNoNetworkConnectivityError());\n }\n };\n\n if (method === HTTP_REQUEST_TYPE.POST && options && options.body) {\n xhr.send(options.body);\n } else if (method === HTTP_REQUEST_TYPE.GET) {\n xhr.send();\n } else {\n throw BrowserAuthError.createHttpMethodNotImplementedError(method);\n }\n });\n }\n\n /**\n * Helper to set XHR headers for request.\n * @param xhr \n * @param options \n */\n private setXhrHeaders(xhr: XMLHttpRequest, options?: NetworkRequestOptions): void {\n if (options && options.headers) {\n const headers = options.headers;\n Object.keys(headers).forEach((key: string) => {\n xhr.setRequestHeader(key, headers[key]);\n });\n }\n }\n\n /**\n * Gets a string map of the headers received in the response.\n * \n * Algorithm comes from https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders\n * @param xhr \n */\n private getHeaderDict(xhr: XMLHttpRequest): Record {\n const headerString = xhr.getAllResponseHeaders();\n const headerArr = headerString.trim().split(/[\\r\\n]+/);\n const headerDict: Record = {};\n headerArr.forEach((value: string) => {\n const parts = value.split(\": \");\n const headerName = parts.shift();\n const headerVal = parts.join(\": \");\n if (headerName && headerVal) {\n headerDict[headerName] = headerVal;\n }\n });\n\n return headerDict;\n }\n}\n"],"names":[],"mappings":";;;;;;AAAA;;;;AASA;;;;IAGA;KA+GC;;;;;;;IAvGS,uCAAmB,GAAzB,UAA6B,GAAW,EAAE,OAA+B;;;gBACrE,sBAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,iBAAiB,CAAC,GAAG,EAAE,OAAO,CAAC,EAAC;;;KACrE;;;;;;;IAQK,wCAAoB,GAA1B,UAA8B,GAAW,EAAE,OAA+B;;;gBACtE,sBAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,iBAAiB,C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