UNPKG

@selfcommunity/api-services

Version:
140 lines (139 loc) 4 kB
import axios from 'axios'; /** * Create an abstraction wrapping of axios * Create a sort of interface between axios and the rest of application * This makes it easy for instance, to swap axios out for another package, * should we choose to do so in the future, without it breaking our app. */ export class ApiClient { constructor(config) { /** * Set default header * @param name * @param value * @param methods */ this.setDefaultHeader = ({ name, value, methods }) => { const headers = this.client.defaults.headers; if (Array.isArray(methods)) { methods.forEach((method) => { if (headers[method]) { headers[method][name] = value; } }); } else { headers.common[name] = value; } }; /** * Delete default header * @param name * @param methods */ this.deleteDefaultHeader = ({ name, methods }) => { const headers = this.client.defaults.headers; if (Array.isArray(methods)) { methods.forEach((method) => { if (headers[method]) { delete headers[method][name]; } }); } else { delete headers.common[name]; } }; this.client = this.createClient(config); this.setDefaultHeader({ name: 'Content-Type', value: 'application/json', methods: ['post'] }); } createClient(config) { return axios.create(Object.assign(Object.assign(Object.assign({}, (config && config.baseURL && { baseURL: config.baseURL })), (config && config.responseType ? { responseType: config.responseType } : { responseType: 'json' })), { headers: Object.assign({}, (config && config.accessToken && { Authorization: `Token ${config.accessToken}` })), timeout: ApiClient.DEFAULT_TIMEOUT })); } /** * Get client instance */ getClientInstance() { return this.client; } /** * Change configuration * @param config */ config(config) { this.client(config); } /** * setSupportWithCredentials * Disable/enable withCredentials * Bypass cookie if disabled * @param enable */ setSupportWithCredentials(enable) { this.client.defaults.withCredentials = enable; } /** * setBasePortal * Set base path of all http requests * @param portal */ setBasePortal(baseUrl) { this.client.defaults.baseURL = baseUrl; } /** * setAuthorizeToken * Set authorization header for all http requests * @param token */ setAuthorizeToken(token) { if (token) { this.setDefaultHeader({ name: 'Authorization', value: `Bearer ${token}` }); } else { this.deleteDefaultHeader({ name: 'Authorization' }); } } /** * request wrapper * @param config */ request(config) { return this.client.request(config); } /** * get wrapper * @param path * @param config */ get(path, config) { return this.client.get(path, config); } /** * post wrapper * @param path * @param payload * @param config */ post(path, payload, config) { return config ? this.client.post(path, payload, config) : this.client.post(path, payload); } /** * patch wrapper * @param path * @param object */ patch(path, payload) { return this.client.patch(path, payload); } /** * put wrapper * @param path * @param payload */ put(path, payload) { return this.client.put(path, payload); } } ApiClient.DEFAULT_TIMEOUT = 10 * 1000; const client = new ApiClient(); export default client;