Source

externalCode/axiosApi.js

/**
 * @class
 * API Requests Hooks.
 * Instance name: axiosApi
  
 Axios is a library that the app uses to communicate with servers. You can modify the process by using AxiosApi.
 * @example
 * externalCodeSetup.axiosApi.METHOD_NAME
 */
export class AxiosApi {
	isClearCacheEnabled = false;
	achievementsParams = {};

	/**
	 * You can use this to enable clearing API cache for each request to ensure the server gives the latest data.
	 * @method
	 * @example
	 * externalCodeSetup.axiosApi.enableClearCache();
	 */
	enableClearCache = () => {
		this.isClearCacheEnabled = true;
	};

	/**
	 * Sets additional params used for fetching achievements that you can display in the achievements sections of your app.
	 * @method
	 * @param {Object} params
	 * @example <caption>Add an "orderby" parameter when fetching achievements API</caption>
	 *
	 * externalCodeSetup.axiosApi.setAchievementsParams({orderby: "date"});
	 */
	setAchievementsParams = params => (this.achievementsParams = params);

	customRequestDecorator = null;

	/**
	 * Add decorator function to add additional headers in api requests
	 * @method
	 * @param {Function} decorator function which returns modified requests headers
	 * @example <caption>Add axios decorator to modify or overide api requests headers</caption>
	 *
	 * externalCodeSetup.axiosApi.addCustomRequestDecorator(() => {
	 *  return original => {
	 *    return args => {
	 *      args.headers = {
	 *        ...args.headers,
	 *        "custom-header-key": "custom-header-value"
	 *      };
	 *      return original(args)
	 *        .catch(error => Promise.reject(error))
	 *        .then(response => response);
	 *      };
	 *  }
	 *});
	 */
	addCustomRequestDecorator = decorator => {
		this.customRequestDecorator = decorator;
	};
}