ts-mailcow-api
Version:
TypeScript wrapper for the mailcow API.
107 lines • 4.01 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapPromiseToArray = wrapPromiseToArray;
const axios_1 = require("axios");
const types_1 = require("./types");
/**
* Ensures output is an array.
* @param item
* @internal
*/
function wrapToArray(item) {
return Array.isArray(item) ? item : [item];
}
/**
* Function that wraps T | T[] to T[]
* @internal
* @param promise - The promise of which the output to wrap.
*/
function wrapPromiseToArray(promise) {
return new Promise((resolve, reject) => {
promise
.then((res) => resolve(wrapToArray(res)))
.catch((err) => reject(err instanceof Error ? err : new Error(String(err))));
});
}
function isErrorType(type) {
return type === 'danger' || type === 'error';
}
/**
* Checks if a Mailcow API response indicates an error.
* Throws a MailcowException if a definite error is detected.
*/
function checkMailcowResponse(res) {
// Accepts either a single object or an array
const arr = Array.isArray(res) ? res : [res];
for (const item of arr) {
if (isErrorType(item.type)) {
throw new types_1.MailcowException(Array.isArray(item.msg) ? item.msg.join(', ') : item.msg);
}
}
}
/**
* Factory method patterns for creating Axios Requests.
* @internal
*/
class RequestFactory {
constructor(ctx) {
this.ctx = ctx;
}
/**
* POST Request Factory
* @param route - The route to which to send the request.
* @param payload - The payload to send with the request.
*/
post(route, payload) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const res = yield axios_1.default.post(this.ctx.BASE_URL + route, payload, this.ctx.AXIOS_CONFIG);
// Only throws if response has danger or error type
checkMailcowResponse(res.data);
return res.data;
}
catch (e) {
if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data)) {
// Check if the response *itself* is an error type
checkMailcowResponse(e.response.data);
// If no definite error, throw generic
throw new types_1.MailcowException('Unknown Mailcow error');
}
throw e;
}
});
}
/**
* GET Request Factory
* @param route - The route to which to send the request.
*/
get(route) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const res = yield axios_1.default.get(this.ctx.BASE_URL + route, this.ctx.AXIOS_CONFIG);
checkMailcowResponse(res.data);
return res.data;
}
catch (e) {
if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data)) {
checkMailcowResponse(e.response.data);
throw new types_1.MailcowException('Unknown Mailcow error');
}
throw e;
}
});
}
}
exports.default = RequestFactory;
//# sourceMappingURL=request-factory.js.map