UNPKG

noticebord-client

Version:

The official Typescript client library for the Noticebord API.

47 lines (46 loc) 1.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Service = void 0; const axios_1 = __importDefault(require("axios")); const humps_1 = require("humps"); class Service { constructor(token, baseUrl) { this.token = token; this.baseUrl = baseUrl; this.api = axios_1.default.create({ baseURL: baseUrl, withCredentials: true, headers: { Accept: "application/json", 'Bearer-Token': `Bearer ${token}`, "Content-Type": "application/json", "X-Requested-With": "XMLHttpRequest", } }); // Axios middleware to convert all api responses to camelCase this.api.interceptors.response.use((response) => { if (response.data && response.headers['content-type'] === 'application/json') { response.data = (0, humps_1.camelizeKeys)(response.data); } return response; }); // Axios middleware to convert all api requests to snake_case this.api.interceptors.request.use((config) => { const newConfig = Object.assign({}, config); newConfig.url = `api/${config.url}`; if (newConfig.headers['Content-Type'] === 'multipart/form-data') return newConfig; if (config.params) { newConfig.params = (0, humps_1.decamelizeKeys)(config.params); } if (config.data) { newConfig.data = (0, humps_1.decamelizeKeys)(config.data); } return newConfig; }); } } exports.Service = Service;