@getanthill/datastore
Version:
Event-Sourced Datastore
94 lines • 4.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const body_parser_1 = __importDefault(require("body-parser"));
const middleware_1 = require("./middleware");
const spec_1 = require("./spec");
const models_1 = __importDefault(require("./models"));
const admin_1 = __importDefault(require("./admin"));
const stream_1 = __importDefault(require("./stream"));
const aggregate_1 = __importDefault(require("./aggregate"));
async function initOpenApi(services) {
const cacheEntry = await services.models.getFromCache('open_api_specification');
const oas = cacheEntry
? JSON.parse(cacheEntry.oas)
: (0, spec_1.build)(services.models);
if (!cacheEntry) {
services.models.setToCache('open_api_specification', {
oas: JSON.stringify(oas),
});
}
services.telemetry.logger.debug('[API] Building Open API 3.0 Specification...');
return new middleware_1.OpenAPIMiddleware({
secret: services.config.security.apiSecret,
specification: oas,
warnOnInvalidSpecificationOnly: services.config.features.api.openAPI.warnOnInvalidSpecificationOnly,
telemetry: services.telemetry,
services,
}, services.config.mode === 'development' ||
services.config.features.api.updateSpecOnModelsChange === true
? async () => {
const _oas = (0, spec_1.build)(services.models);
services.models.setToCache('open_api_specification', {
oas: JSON.stringify(_oas),
});
return _oas;
}
: null);
}
async function routes(services) {
const app = express_1.default.Router({ mergeParams: true });
let openApi;
app
.use(body_parser_1.default.urlencoded({ extended: false }))
.use(body_parser_1.default.json({ limit: services.config.features.api.json.limit }));
if (services.config.authz.isEnabled === true) {
app.use((0, middleware_1.authz)(services));
}
if (services.config.features.api.openAPI.isEnabled === true) {
openApi = await initOpenApi(services);
app.use(openApi.registerInputValidation());
}
app.use((0, middleware_1.obligations)(services));
if (services.config.features.api.aggregate === true) {
services.telemetry.logger.info('[Feature] Aggregation API enabled');
app.use('/aggregate', (req, res, next) => {
req.setTimeout(services.config.features.api.timeout.aggregate);
next();
}, (0, middleware_1.authenticate)((0, middleware_1.getTokensByRole)(services.config.security.tokens, 'read')), (0, aggregate_1.default)(services));
}
app
.use('/admin', (0, admin_1.default)(services, openApi))
.use('/stream', (0, middleware_1.authenticate)((0, middleware_1.getTokensByRole)(services.config.security.tokens, 'read')), (0, stream_1.default)(services))
.use('/:model', (req, res, next) => {
if (req.params.model.startsWith('internal_')) {
const err = new Error('Protected model');
err.status = 403;
return next(err);
}
req.setTimeout(services.config.features.api.timeout.models);
next();
}, (0, models_1.default)(services));
app.use((0, middleware_1.obligations)(services));
if (openApi && services.config.features.api.openAPI.isEnabled === true) {
app.use(openApi.registerOutputValidation());
}
/* istanbul ignore next */
const jsonMiddleware = (_req, res, next) => {
// @ts-ignore
if (res.body) {
// @ts-ignore
return res.json(res.body);
}
/* istanbul ignore next */
next();
};
app.use(jsonMiddleware);
app.use((0, middleware_1.errorHandler)(services));
return app;
}
exports.default = routes;
//# sourceMappingURL=index.js.map