@spigotmc/microservice
Version:
The library for powering the Microservice API for your Minecraft server.
92 lines • 3.41 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
const express_1 = __importDefault(require("express"));
/**
* @class Microservice
* The Microservice class.
*/
class Microservice {
/**
* @constructor Microservice
* @param connection The MySQL connection.
* @param app The Express application.
*/
constructor(app) {
this.app = app;
}
/**
* @param port The specified port of the service.
* @param options The credentials for the database.
* @returns A new instance of the Microservice class.
*/
static host(port) {
/**
* Create app and point routes.
*/
const app = (0, express_1.default)();
app.use(express_1.default.json());
app.use(express_1.default.urlencoded({ extended: true }));
/**
* Listen server on specified port.
*/
app.listen(port, () => console.log(`Microservice running on port ${port}`));
/**
* Return a new Microservice object.
*/
return new Microservice(app);
}
/**
* @returns The Express application on which the
* microservice is hosted for perhaps adding
* or modifying certain middleware.
*/
getApplication() {
return this.app;
}
/**
* @param name The name of the route for e.g. `/api`
* @param router The router holding the endpoints.
*/
setDefiningRoute(name, router) {
this.app.use(name, router);
}
}
exports.default = Microservice;
class Router {
constructor() {
this.router = express_1.default.Router();
}
register() {
return this.router;
}
use(name, router) {
this.router.use(name, router.register());
}
get(name, fn) {
this.router.get(name, (req, res) => __awaiter(this, void 0, void 0, function* () { return res.send(yield fn(req.query)); }));
}
post(name, fn) {
this.router.post(name, (req, res) => __awaiter(this, void 0, void 0, function* () { return res.send(yield fn(req.body)); }));
}
put(name, fn) {
this.router.put(name, (req, res) => __awaiter(this, void 0, void 0, function* () { return res.send(yield fn(req.body)); }));
}
delete(name, fn) {
this.router.delete(name, (req, res) => __awaiter(this, void 0, void 0, function* () { return res.send(yield fn(req.query)); }));
}
}
exports.Router = Router;
//# sourceMappingURL=index.js.map