@creamapi/cream
Version:
Concise REST API Maker - An extension library for express to create REST APIs faster
112 lines (111 loc) • 4.1 kB
JavaScript
;
/*
* Copyright 2024 Raul Radu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MiddlewareReturnData = exports.ExpressMiddleware = exports.AsyncExpressMiddleware = void 0;
const ExpressErrorHandler_1 = require("../ExpressErrorHandler/ExpressErrorHandler");
/**
* This abstract class implements the base middleware handle method for handling
* ASYNChronous middlewares.
* @remarks The API for middlewares is still in its early stage and will need some refactoring
* to make it simpler for users.
*/
class AsyncExpressMiddleware {
app;
/**
* @internal
* this implementation of handle is going to set the collection in the
* collection map with the new content.
* If behavior throws it will check if the error extends RestError
* and then uses the rest error statusCode to send information to the user.
* @remarks This might not be necessary since the error handler already does this
*/
async handle(req, res, next) {
try {
let data = await this.behaviour(req, res);
let collections = req.middlewareDataCollections || new Map();
if (data.content) {
collections.set(data.collectionName, data.content);
}
req.middlewareDataCollections = collections;
next();
}
catch (e) {
if (e instanceof ExpressErrorHandler_1.RestError) {
res.status(e.statusCode);
}
else {
res.status(500);
}
next(e);
}
}
}
exports.AsyncExpressMiddleware = AsyncExpressMiddleware;
/**
* This abstract class implements the base middleware handle method for handling
* SYNChronous middlewares.
* @remarks The API for middlewares is still in its early stage and will need some refactoring
* to make it simpler for users.
*/
class ExpressMiddleware {
app;
/**
* @internal
* this implementation of handle is going to set the collection in the
* collection map with the new content.
* If behavior throws it will check if the error extends RestError
* and then uses the rest error statusCode to send information to the user.
* @remarks This might not be necessary since the error handler already does this
*/
handle(req, res, next) {
try {
this.app = req.expressApp;
let data = this.behaviour(req, res);
let collections = req.middlewareDataCollections || new Map();
collections.set(data.collectionName, data.content);
req.middlewareDataCollections = collections;
next();
}
catch (e) {
if (e instanceof ExpressErrorHandler_1.RestError) {
res.status(e.statusCode);
}
else {
res.status(500);
}
next(e);
}
}
}
exports.ExpressMiddleware = ExpressMiddleware;
/**
* This method is used to define a collection in the collection mapping
* To access this data in a method use {@link MiddlewareData}
*/
class MiddlewareReturnData {
collectionName;
content;
/**
* @param collectionName the collection identifier that should be used when saving the data in the map
* @param content the content of the collection
*/
constructor(collectionName = 'default', content) {
this.collectionName = collectionName;
this.content = content;
}
}
exports.MiddlewareReturnData = MiddlewareReturnData;