{"version":3,"file":"FetchClient.js","sources":["../../src/network/FetchClient.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Constants, INetworkModule, NetworkRequestOptions, NetworkResponse } from \"@azure/msal-common\";\nimport { BrowserAuthError } from \"../error/BrowserAuthError\";\nimport { HTTP_REQUEST_TYPE } from \"../utils/BrowserConstants\";\n\n/**\n * This class implements the Fetch API for GET and POST requests. See more here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API\n */\nexport class FetchClient implements INetworkModule {\n\n /**\n * Fetch Client for REST endpoints - Get request\n * @param url \n * @param headers \n * @param body \n */\n async sendGetRequestAsync(url: string, options?: NetworkRequestOptions): Promise> {\n let response;\n try {\n response = await fetch(url, {\n method: HTTP_REQUEST_TYPE.GET,\n headers: this.getFetchHeaders(options)\n });\n } catch (e) {\n if (window.navigator.onLine) {\n throw BrowserAuthError.createGetRequestFailedError(e, url);\n } else {\n throw BrowserAuthError.createNoNetworkConnectivityError();\n }\n }\n\n try {\n return {\n headers: this.getHeaderDict(response.headers),\n body: await response.json() as T,\n status: response.status\n };\n } catch (e) {\n throw BrowserAuthError.createFailedToParseNetworkResponseError(url);\n }\n }\n\n /**\n * Fetch Client for REST endpoints - Post request\n * @param url \n * @param headers \n * @param body \n */\n async sendPostRequestAsync(url: string, options?: NetworkRequestOptions): Promise> {\n const reqBody = (options && options.body) || Constants.EMPTY_STRING;\n\n let response;\n try {\n response = await fetch(url, {\n method: HTTP_REQUEST_TYPE.POST,\n headers: this.getFetchHeaders(options),\n body: reqBody\n });\n } catch (e) {\n if (window.navigator.onLine) {\n throw BrowserAuthError.createPostRequestFailedError(e, url);\n } else {\n throw BrowserAuthError.createNoNetworkConnectivityError();\n }\n }\n\n try {\n return {\n headers: this.getHeaderDict(response.headers),\n body: await response.json() as T,\n status: response.status\n };\n } catch (e) {\n throw BrowserAuthError.createFailedToParseNetworkResponseError(url);\n }\n }\n\n /**\n * Get Fetch API Headers object from string map\n * @param inputHeaders \n */\n private getFetchHeaders(options?: NetworkRequestOptions): Headers {\n const headers = new Headers();\n if (!(options && options.headers)) {\n return headers;\n }\n const optionsHeaders = options.headers;\n Object.keys(optionsHeaders).forEach((key) => {\n headers.append(key, optionsHeaders[key]);\n });\n return headers;\n }\n\n private getHeaderDict(headers: Headers): Record {\n const headerDict: Record = {};\n headers.forEach((value: string, key: string) => {\n headerDict[key] = value;\n });\n return headerDict;\n }\n}\n"],"names":[],"mappings":";;;;;;;AAAA;;;;AASA;;;;IAGA;KA4FC;;;;;;;IApFS,yCAAmB,GAAzB,UAA6B,GAAW,EAAE,OAA+B;;;;;;;wBAGtD,qBAAM,KAAK,CAAC,GAAG,EAAE;gCACxB,MAAM,EAAE,iBAAiB,CAAC,GAAG;gCAC7B,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC;6BACzC,CAAC,EAAA;;wBAHF,QAAQ,GAAG,SAGT,CAAC;;;;wBAEH,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE;4BACzB,MAAM,gBAAgB,CAAC,2BAA2B,CAAC,GAAC,EAAE,GAAG,CAAC,CAAC;yBAC9D;6BAAM;4BACH,MAAM,gBAAgB,CAAC,gCAAgC,EAAE,CAAC;yBAC7D;;;;4BAKG,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,OAAO,CAAC;;wBACvC,qBAAM,QAAQ,CAAC,IAAI,EAAE,EAAA;4BAF/B,uBAEI,OAAI,IAAE,SAA0B,CAAA;4BAChC,SAAM,GAAE,QAAQ,CAAC,MAAM;iCACzB;;;wBAEF,MAAM,gBAAgB,CAAC,uCAAuC,CAAC,GAAG,CAAC,CAAC;;;;;KAE3E;;;;;;;IAQK,0CAAoB,GAA1B,UAA8B,GAAW,EAAE,OAA+B;;;;;;wBAChE,OAAO,GAAG,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC,YAAY,CAAC;;;;wBA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