{"version":3,"sources":["api/base/api-base.ts"],"names":["ApiBase","constructor","basePath","this","defaultAbortController","AbortController","abortControllers","initAbortOnUnload","checkError","response","Promise","get","pathAndQuery","jsonResponse","abortKey","init","getRequestInit","fetch","post","bodyData","put","delete","abortRequestByKey","abort","requestInit","let","await","error","name","json","method","body","JSON","stringify","abortController","length","signal","mode","cache","credentials","headers","Accept","Content-Type","window","addEventListener","Object","keys","forEach","key"],"mappings":"MAAsBA,QAIlBC,YAAoBC,GAAAC,KAAAD,SAAAA,EA+DZC,KAAAC,uBAAyB,IAAIC,gBAC7BF,KAAAG,iBAAuD,GA/D3DH,KAAKI,kBAAiB,CAC1B,CAEUC,WAAWC,GACjB,OAAO,IAAIC,QAAiB,IAAM,CAAA,CAAI,CAC1C,CAQOC,UAAUC,EAAsBC,EAAwB,CAAA,EAAMC,GACjE,IAAMC,EAAOZ,KAAKa,eAAe,MAAO,KAAMF,CAAQ,EACtD,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,CAAY,CAC5D,CAQOK,WAAWN,EAAsBO,EAAgBN,EAAwB,CAAA,EAAMC,GAClF,IAAMC,EAAOZ,KAAKa,eAAe,OAAQG,EAAUL,CAAQ,EAC3D,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,CAAY,CAC5D,CAQOO,UAAUR,EAAsBO,EAAgBN,EAAwB,CAAA,EAAMC,GACjF,IAAMC,EAAOZ,KAAKa,eAAe,MAAOG,EAAUL,CAAQ,EAC1D,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,CAAY,CAC5D,CAQOQ,aAAaT,EAAsBO,EAAgBN,EAAwB,CAAA,EAAMC,GACpF,IAAMC,EAAOZ,KAAKa,eAAe,SAAUG,EAAUL,CAAQ,EAC7D,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,CAAY,CAC5D,CAMOS,kBAAkBR,GACjBX,KAAKG,iBAAiBQ,KACtBX,KAAKG,iBAAiBQ,GAAUS,MAAK,EACrC,OAAOpB,KAAKG,iBAAiBQ,GAErC,CAKOG,YAAYL,EAAsBY,EAA0BX,GAC/DY,IAAIhB,EAEJ,IACIA,EAAWiB,MAAMT,MAAM,GAAGd,KAAKD,SAAWU,EAAgBY,CAAW,C,CAEzE,MAAOG,GACH,GAAmB,eAAfA,EAAMC,KACN,MAAMD,EAGV,OAAO,I,CAGX,OAAKD,MAAMvB,KAAKK,WAAWC,CAAQ,EAI/BI,EAEaJ,EAASoB,KAAI,EAGvBpB,EARI,IASf,CAEQO,eAAec,EAAgBX,EAAgBL,GACnD,IAAMiB,EAAOZ,EAAWa,KAAKC,UAAUd,CAAQ,EAAI,KAM7Ce,GAJFpB,GAAUqB,QAAU,CAAChC,KAAKG,iBAAiBQ,KAC3CX,KAAKG,iBAAiBQ,GAAY,IAAIT,iBAGlBS,GAAUqB,OAAShC,KAAKG,iBAAiBQ,GAAYX,KAAKC,wBAElF,MAAO,CACH0B,OAAQA,EACRM,OAAQF,EAAgBE,OACxBC,KAAM,OACNC,MAAO,WACPC,YAAa,cACbC,QAAS,CACLC,OAAU,mBACVC,eAAgB,kB,EAEpBX,KAAMA,C,CAEd,CAEQxB,oBACJoC,OAAOC,iBAAiB,eAAgB,KACpCC,OAAOC,KAAK3C,KAAKG,gBAAgB,EAAEyC,QAAQC,IACvC7C,KAAKG,iBAAiB0C,GAAKzB,MAAK,CACpC,CAAC,EACDpB,KAAKC,uBAAuBmB,MAAK,CACrC,CAAC,CACL,C,QA9HkBvB,O","file":"api-base.js","sourcesContent":["export abstract class ApiBase {\r\n //basePath = \"/startup/api/v1\"; - portal\r\n //basePath = \"/api\"; - app\r\n\r\n constructor(private basePath: string) {\r\n this.initAbortOnUnload();\r\n }\r\n\r\n protected checkError(response: Response): Promise{\r\n return new Promise(() => true);\r\n };\r\n\r\n /**\r\n * Make a GET request with supplied path and query to portal API.\r\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\r\n * @param bodyData - An optional object or an array with data to include in body as Json\r\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\r\n */\r\n public async get(pathAndQuery: string, jsonResponse: boolean = true, abortKey?: string) {\r\n const init = this.getRequestInit(\"GET\", null, abortKey);\r\n return await this.fetch(pathAndQuery, init, jsonResponse);\r\n }\r\n\r\n /**\r\n * Make a POST request with supplied path and query to portal API.\r\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\r\n * @param bodyData - An optional object or an array with data to include in body as Json\r\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\r\n */\r\n public async post(pathAndQuery: string, bodyData?: any, jsonResponse: boolean = true, abortKey?: string) {\r\n const init = this.getRequestInit(\"POST\", bodyData, abortKey);\r\n return await this.fetch(pathAndQuery, init, jsonResponse);\r\n }\r\n\r\n /**\r\n * Make a PUT request with supplied path and query to portal API.\r\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\r\n * @param bodyData - An optional object or an array with data to include in body as Json\r\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\r\n */\r\n public async put(pathAndQuery: string, bodyData?: any, jsonResponse: boolean = true, abortKey?: string) {\r\n const init = this.getRequestInit(\"PUT\", bodyData, abortKey);\r\n return await this.fetch(pathAndQuery, init, jsonResponse);\r\n }\r\n \r\n /**\r\n * Make a DELETE request with supplied path and query to portal API.\r\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\r\n * @param bodyData - An optional object or an array with data to include in body as Json\r\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\r\n */\r\n public async delete(pathAndQuery: string, bodyData?: any, jsonResponse: boolean = true, abortKey?: string) {\r\n const init = this.getRequestInit(\"DELETE\", bodyData, abortKey);\r\n return await this.fetch(pathAndQuery, init, jsonResponse);\r\n }\r\n\r\n /**\r\n * Aborts a request by the key used to identify it\r\n * @param abortKey - The key used to identify the request to abort\r\n */\r\n public abortRequestByKey(abortKey: string) {\r\n if (this.abortControllers[abortKey]) {\r\n this.abortControllers[abortKey].abort();\r\n delete this.abortControllers[abortKey];\r\n }\r\n }\r\n\r\n private defaultAbortController = new AbortController();\r\n private abortControllers: { [key: string]: AbortController } = {};\r\n\r\n public async fetch(pathAndQuery: string, requestInit: RequestInit, jsonResponse: boolean) : Promise {\r\n let response: Response;\r\n\r\n try {\r\n response = await fetch(`${this.basePath}${pathAndQuery}`, requestInit);\r\n }\r\n catch (error: any) {\r\n if (error.name !== 'AbortError') {\r\n throw error;\r\n }\r\n\r\n return null;\r\n }\r\n\r\n if (!await this.checkError(response)) {\r\n return null;\r\n }\r\n\r\n if (jsonResponse)\r\n {\r\n return await response.json();\r\n }\r\n\r\n return response;\r\n }\r\n\r\n private getRequestInit(method: string, bodyData?: any, abortKey?: string) {\r\n const body = bodyData ? JSON.stringify(bodyData) : null;\r\n \r\n if (abortKey?.length && !this.abortControllers[abortKey]) {\r\n this.abortControllers[abortKey] = new AbortController();\r\n }\r\n\r\n const abortController = abortKey?.length ? this.abortControllers[abortKey] : this.defaultAbortController;\r\n\r\n return {\r\n method: method,\r\n signal: abortController.signal,\r\n mode: \"cors\",\r\n cache: \"no-cache\",\r\n credentials: \"same-origin\",\r\n headers: {\r\n \"Accept\": \"application/json\",\r\n \"Content-Type\": \"application/json\"\r\n },\r\n body: body\r\n } as RequestInit;\r\n }\r\n\r\n private initAbortOnUnload() {\r\n window.addEventListener(\"beforeunload\", () => {\r\n Object.keys(this.abortControllers).forEach(key => {\r\n this.abortControllers[key].abort();\r\n });\r\n this.defaultAbortController.abort();\r\n });\r\n }\r\n}"]}