{"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","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,oBAGCC,WAAWC,GACjB,OAAO,IAAIC,QAAiB,KAAM,GAS/BC,UAAUC,EAAsBC,GAAwB,EAAMC,GACjE,IAAMC,EAAOZ,KAAKa,eAAe,MAAO,KAAMF,GAC9C,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,GASzCK,WAAWN,EAAsBO,EAAgBN,GAAwB,EAAMC,GAClF,IAAMC,EAAOZ,KAAKa,eAAe,OAAQG,EAAUL,GACnD,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,GASzCO,UAAUR,EAAsBO,EAAgBN,GAAwB,EAAMC,GACjF,IAAMC,EAAOZ,KAAKa,eAAe,MAAOG,EAAUL,GAClD,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,GASzCQ,aAAaT,EAAsBO,EAAgBN,GAAwB,EAAMC,GACpF,IAAMC,EAAOZ,KAAKa,eAAe,SAAUG,EAAUL,GACrD,OAAaX,KAAKc,MAAML,EAAcG,EAAMF,GAOzCS,kBAAkBR,GACjBX,KAAKG,iBAAiBQ,KACtBX,KAAKG,iBAAiBQ,GAAUS,eACzBpB,KAAKG,iBAAiBQ,IAO9BG,YAAYL,EAAsBY,EAA0BX,GAC/DY,IAAIhB,EAEJ,IACIA,QAAiBQ,MAAM,GAAGd,KAAKD,SAAWU,EAAgBY,GAE9D,MAAOE,GACH,GAAmB,eAAfA,EAAMC,KACN,MAAMD,EAGV,OAAO,KAGX,aAAWvB,KAAKK,WAAWC,GAIvBI,EAEaJ,EAASmB,OAGnBnB,EARI,KAWPO,eAAea,EAAgBV,EAAgBL,GACnD,IAAMgB,EAAOX,EAAWY,KAAKC,UAAUb,GAAY,KAM7Cc,GAJFnB,GAAUoB,SAAW/B,KAAKG,iBAAiBQ,KAC3CX,KAAKG,iBAAiBQ,GAAY,IAAIT,iBAGlBS,GAAUoB,OAAS/B,KAAKG,iBAAiBQ,GAAYX,KAAKC,wBAElF,MAAO,CACHyB,OAAQA,EACRM,OAAQF,EAAgBE,OACxBC,KAAM,OACNC,MAAO,WACPC,YAAa,cACbC,QAAS,CACLC,OAAU,mBACVC,eAAgB,oBAEpBX,KAAMA,GAINvB,oBACJmC,OAAOC,iBAAiB,eAAgB,KACpCC,OAAOC,KAAK1C,KAAKG,kBAAkBwC,QAAQC,IACvC5C,KAAKG,iBAAiByC,GAAKxB,UAE/BpB,KAAKC,uBAAuBmB,kBA5HlBvB","file":"api-base.js","sourcesContent":["export abstract class ApiBase {\n //basePath = \"/startup/api/v1\"; - portal\n //basePath = \"/api\"; - app\n\n constructor(private basePath: string) {\n this.initAbortOnUnload();\n }\n\n protected checkError(response: Response): Promise{\n return new Promise(() => true);\n };\n\n /**\n * Make a GET request with supplied path and query to portal API.\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\n * @param bodyData - An optional object or an array with data to include in body as Json\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\n */\n public async get(pathAndQuery: string, jsonResponse: boolean = true, abortKey?: string) {\n const init = this.getRequestInit(\"GET\", null, abortKey);\n return await this.fetch(pathAndQuery, init, jsonResponse);\n }\n\n /**\n * Make a POST request with supplied path and query to portal API.\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\n * @param bodyData - An optional object or an array with data to include in body as Json\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\n */\n public async post(pathAndQuery: string, bodyData?: any, jsonResponse: boolean = true, abortKey?: string) {\n const init = this.getRequestInit(\"POST\", bodyData, abortKey);\n return await this.fetch(pathAndQuery, init, jsonResponse);\n }\n\n /**\n * Make a PUT request with supplied path and query to portal API.\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\n * @param bodyData - An optional object or an array with data to include in body as Json\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\n */\n public async put(pathAndQuery: string, bodyData?: any, jsonResponse: boolean = true, abortKey?: string) {\n const init = this.getRequestInit(\"PUT\", bodyData, abortKey);\n return await this.fetch(pathAndQuery, init, jsonResponse);\n }\n \n /**\n * Make a DELETE request with supplied path and query to portal API.\n * @param pathAndQuery - should only contain the path after v1. ex. /user/search\n * @param bodyData - An optional object or an array with data to include in body as Json\n * @param jsonResponse - when true, returns the result of parsing the response body text as JSON; otherwise the Response object is returned.\n */\n public async delete(pathAndQuery: string, bodyData?: any, jsonResponse: boolean = true, abortKey?: string) {\n const init = this.getRequestInit(\"DELETE\", bodyData, abortKey);\n return await this.fetch(pathAndQuery, init, jsonResponse);\n }\n\n /**\n * Aborts a request by the key used to identify it\n * @param abortKey - The key used to identify the request to abort\n */\n public abortRequestByKey(abortKey: string) {\n if (this.abortControllers[abortKey]) {\n this.abortControllers[abortKey].abort();\n delete this.abortControllers[abortKey];\n }\n }\n\n private defaultAbortController = new AbortController();\n private abortControllers: { [key: string]: AbortController } = {};\n\n public async fetch(pathAndQuery: string, requestInit: RequestInit, jsonResponse: boolean) : Promise {\n let response: Response;\n\n try {\n response = await fetch(`${this.basePath}${pathAndQuery}`, requestInit);\n }\n catch (error: any) {\n if (error.name !== 'AbortError') {\n throw error;\n }\n\n return null;\n }\n\n if (!await this.checkError(response)) {\n return null;\n }\n\n if (jsonResponse)\n {\n return await response.json();\n }\n\n return response;\n }\n\n private getRequestInit(method: string, bodyData?: any, abortKey?: string) {\n const body = bodyData ? JSON.stringify(bodyData) : null;\n \n if (abortKey?.length && !this.abortControllers[abortKey]) {\n this.abortControllers[abortKey] = new AbortController();\n }\n\n const abortController = abortKey?.length ? this.abortControllers[abortKey] : this.defaultAbortController;\n\n return {\n method: method,\n signal: abortController.signal,\n mode: \"cors\",\n cache: \"no-cache\",\n credentials: \"same-origin\",\n headers: {\n \"Accept\": \"application/json\",\n \"Content-Type\": \"application/json\"\n },\n body: body\n } as RequestInit;\n }\n\n private initAbortOnUnload() {\n window.addEventListener(\"beforeunload\", () => {\n Object.keys(this.abortControllers).forEach(key => {\n this.abortControllers[key].abort();\n });\n this.defaultAbortController.abort();\n });\n }\n}"]}