{"version":3,"sources":["ApiV2.js"],"names":["let","ROOT_BASE_URL","Config","api","scheme","host","port","API_BASE_URL","ApiV2Error","constructor","message","code","_isApiError","ApiV2Client","static","clientForUser","user","accessToken","idToken","options","getAsync","async","methodName","args","extraOptions","_requestAsync","httpMethod","queryParameters","json","postAsync","data","body","putAsync","extraRequestOptions","url","reqOptions","method","headers","params","paramsSerializer","QueryString","stringify","_","merge","response","result","await","axios","request","e","maybeErrorData","idx","errors","length","error","Error","responseBody","logger","responseError","serverStack","stack","details"],"mappings":";;;;;;;;;AAIA;AAAA;AAAA;;;;AACA;AAAA;AAAA;;AACA;;;;AACA;AAAA;AAAA;;;;AACA;AAAA;AAAA;;;;AAEA;AAAA;AAAA;;;;AAEA;AAAA;AAAA;;;;;;AAIAA,IAAIC,gBAAiB,GAAEC,oCAAOC,GAAPD,CAAWE,MAAO,MAAKF,oCAAOC,GAAPD,CAAWG,IAAK,EAA9DL;AACA,IAAIE,oCAAOC,GAAPD,CAAWI,IAAf,EAAqB;AACnBL,mBAAiB,MAAMC,oCAAOC,GAAPD,CAAWI,IAAlCL;AACF;AACA,MAAMM,eAAeN,gBAAgB,YAArC;;AAEO,MAAMO,UAAN,iDAAyC;;AAK9CC,cAAYC,OAAZD,EAA6BE,OAAe,SAA5CF,EAAuD;AACrD,UAAMC,OAAN;AACA,SAAKC,IAAL,GAAYA,IAAZ;AACA,SAAKC,WAAL,GAAmB,IAAnB;AACF;AAT8C;;QAAnCJ,U,GAAAA,U;AA8BE,MAAMK,WAAN,CAAkB;;AAI/BC,SAAOC,aAAPD,CAAqBE,IAArBF,EAA+C;AAC7C,QAAIE,IAAJ,EAAU;AACR,aAAO,IAAIH,WAAJ,CAAgB;AACrBI,qBAAaD,KAAKC,WADG;AAErBC,iBAASF,KAAKE;AAFO,OAAhB,CAAP;AAIF;;AAEA,WAAO,IAAIL,WAAJ,EAAP;AACF;;AAEAJ,cAAYU,UAA8B,EAA1CV,EAA8C;AAAA,SAd9CS,OAc8C,GA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+CAAX,CAAnCvD;AACAsD,cAAME,YAANF,GAAqBT,MAArBS;AACA,cAAMA,KAAN;AACF;;AAEA,UAAIT,OAAOO,MAAPP,IAAiBA,OAAOO,MAAPP,CAAcQ,MAAnC,EAA2C;AACzCrD,YAAI0D,gBAAgBb,OAAOO,MAAPP,CAAc,CAAdA,CAApB7C;AACAA,YAAIsD,QAAQ,IAAI9C,UAAJ,CAAekD,cAAchD,OAA7B,EAAsCgD,cAAc/C,IAApD,CAAZX;AACAsD,cAAMK,WAANL,GAAoBI,cAAcE,KAAlCN;AACAA,cAAMO,OAANP,GAAgBI,cAAcG,OAA9BP;AACA,cAAMA,KAAN;AACF;;AAEA,aAAOT,OAAOf,IAAd;AAjEY;AAkEd;AArI+B;kBAAZjB,W","file":"../ApiV2.js","sourcesContent":["/**\n * @flow\n */\n\nimport _ from 'lodash';\nimport ExtendableError from 'es6-error';\nimport QueryString from 'querystring';\nimport axios from 'axios';\nimport idx from 'idx';\n\nimport Config from './Config';\n\nimport logger from './Logger';\n\nimport type { User } from './User';\n\nlet ROOT_BASE_URL = `${Config.api.scheme}://${Config.api.host}`;\nif (Config.api.port) {\n ROOT_BASE_URL += ':' + Config.api.port;\n}\nconst API_BASE_URL = ROOT_BASE_URL + '/--/api/v2';\n\nexport class ApiV2Error extends ExtendableError {\n code: string;\n details: Object;\n serverStack: ?string;\n\n constructor(message: string, code: string = 'UNKNOWN') {\n super(message);\n this.code = code;\n this._isApiError = true;\n }\n}\n\ntype RequestOptions = {\n httpMethod: 'get' | 'post' | 'put',\n queryParameters?: ?QueryParameters,\n body?: ?Object,\n json?: boolean,\n};\n\ntype QueryParameters = { [key: string]: ?(string | number | boolean) };\n\ntype ErrorWithResponseBody = Error & {\n responseBody?: any,\n};\n\ntype APIV2ClientOptions = {\n idToken?: string,\n accessToken?: string,\n};\n\nexport default class ApiV2Client {\n idToken: ?string = null;\n accessToken: ?string = null;\n\n static clientForUser(user: ?User): ApiV2Client {\n if (user) {\n return new ApiV2Client({\n accessToken: user.accessToken,\n idToken: user.idToken,\n });\n }\n\n return new ApiV2Client();\n }\n\n constructor(options: APIV2ClientOptions = {}) {\n if (options.idToken) {\n this.idToken = options.idToken;\n }\n\n if (options.accessToken) {\n this.accessToken = options.accessToken;\n }\n }\n\n async getAsync(\n methodName: string,\n args: QueryParameters = {},\n extraOptions: Object = {}\n ): Promise<*> {\n return this._requestAsync(\n methodName,\n {\n httpMethod: 'get',\n queryParameters: args,\n json: true,\n },\n extraOptions\n );\n }\n\n async postAsync(methodName: string, data: Object = {}, extraOptions: Object = {}): Promise<*> {\n return this._requestAsync(\n methodName,\n {\n httpMethod: 'post',\n body: data,\n },\n extraOptions\n );\n }\n\n async putAsync(methodName: string, data: Object = {}, extraOptions: Object = {}): Promise<*> {\n return this._requestAsync(\n methodName,\n {\n httpMethod: 'put',\n body: data,\n },\n extraOptions\n );\n }\n\n async _requestAsync(\n methodName: string,\n options: RequestOptions,\n extraRequestOptions: Object\n ): Promise<*> {\n const url = `${API_BASE_URL}/${methodName}`;\n let reqOptions: Object = {\n url,\n method: options.httpMethod,\n headers: {\n 'Exponent-Client': 'xdl',\n },\n json: typeof options.json !== 'undefined' ? options.json : false,\n };\n\n if (this.idToken) {\n reqOptions.headers['Authorization'] = `Bearer ${this.idToken}`;\n }\n\n if (this.accessToken) {\n reqOptions.headers['Exp-Access-Token'] = this.accessToken;\n }\n\n // Handle qs\n if (options.queryParameters) {\n reqOptions.params = options.queryParameters;\n reqOptions.paramsSerializer = QueryString.stringify;\n }\n\n // Handle body\n if (options.body) {\n reqOptions.data = options.body;\n }\n\n reqOptions = _.merge({}, reqOptions, extraRequestOptions);\n let response;\n let result;\n try {\n response = await axios.request(reqOptions);\n result = response.data;\n } catch (e) {\n // axios errors on 400's, pass this case to better error handling downstream\n const maybeErrorData = idx(e, _ => _.response.data.errors.length);\n if (maybeErrorData) {\n result = e.response.data;\n } else {\n const error: ErrorWithResponseBody = new Error(\n `There was a problem understanding the server. Please try again.`\n );\n error.responseBody = result;\n logger.error(error);\n throw error;\n }\n }\n\n if (!result || typeof result !== 'object') {\n let error: ErrorWithResponseBody = new Error(`There was a problem understanding the server.`);\n error.responseBody = result;\n throw error;\n }\n\n if (result.errors && result.errors.length) {\n let responseError = result.errors[0];\n let error = new ApiV2Error(responseError.message, responseError.code);\n error.serverStack = responseError.stack;\n error.details = responseError.details;\n throw error;\n }\n\n return result.data;\n }\n}\n"],"sourceRoot":"/xdl/src"}