@apillon/sdk
Version:
▶◀ Apillon SDK for NodeJS ▶◀
61 lines • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApillonApi = void 0;
const axios_1 = __importDefault(require("axios"));
const apillon_1 = require("../types/apillon");
const apillon_logger_1 = require("./apillon-logger");
const common_1 = require("./common");
class ApillonApi {
static initialize(apiConfig) {
const defaultOptions = {
key: process.env.APILLON_API_KEY,
secret: process.env.APILLON_API_SECRET,
apiUrl: process.env.APILLON_API_URL || 'https://api.apillon.io',
logLevel: apillon_1.LogLevel.NONE,
};
const config = Object.assign(Object.assign({}, defaultOptions), apiConfig);
const auth = config.key && config.secret
? {
username: config.key,
password: config.secret,
}
: undefined;
this.instance = axios_1.default.create({
baseURL: config.apiUrl,
headers: { 'content-type': 'application/json' },
auth,
});
this.instance.interceptors.request.use((request) => request, (error) => {
apillon_logger_1.ApillonLogger.log(error.message, apillon_1.LogLevel.ERROR);
throw new common_1.ApillonRequestError(error.request);
});
this.instance.interceptors.response.use((response) => response, (error) => {
var _a;
// ApillonLogger.log(error, LogLevel.ERROR);
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) {
throw new common_1.ApillonApiError(JSON.stringify(error.response.data, null, 2));
}
else {
throw new common_1.ApillonNetworkError(error.message);
}
});
return config;
}
static async get(url, config) {
const { data } = await this.instance.get(url, config);
return data.data;
}
static async post(url, body, config) {
const { data } = await this.instance.post(url, body, config);
return data.data;
}
static async delete(url, config) {
const { data } = await this.instance.delete(url, config);
return data.data;
}
}
exports.ApillonApi = ApillonApi;
//# sourceMappingURL=apillon-api.js.map