@creamapi/cream
Version:
Concise REST API Maker - An extension library for express to create REST APIs faster
136 lines (135 loc) • 5.4 kB
JavaScript
"use strict";
/*
* Copyright 2025 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.TransactionManager = void 0;
const HeaderBuilder_1 = require("../HttpUtils/Headers/HeaderBuilder");
const HeadersManager_1 = require("../HttpUtils/Headers/HeadersManager");
const ResponseCookiesManager_1 = require("../HttpUtils/Cookies/ResponseCookiesManager");
/**
* This class is used to prepare the transaction before sending the data to the user.\
* It is accessible from any method that is decorated as {@link ExpressCall} or its derivatives
* like {@link Get}, {@link Put}, {@link Post}, {@link Delete}.\
* To access it in the method simply call ExpressModule.prepareTransaction().
*/
class TransactionManager {
method;
thisArg;
/**
* The transaction HTTP Return Code
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status | HTTP Status Codes}
*/
retcode;
/**
* This object will handle headers
*/
headers;
/**
* @param method The method that will handle the transaction
* @param thisArg The method owner (reserved for future usage)
*/
constructor(method, thisArg) {
this.method = method;
this.thisArg = thisArg;
this.headers = new HeadersManager_1.HeadersManager();
this.reset(thisArg);
}
/**
* This method is used by the ExpressCall wrapper to set the cookies in the transaction
* manager.
* @note This will overwrite any previously set headers.
* @param headers the headers manager cotaining new headers
* @returns a self reference to the transaction manager for chaining more operations
*/
setHeaders(headers) {
if (headers == undefined)
return this;
for (let key of headers.keys()) {
this.headers.set(key, headers.get(key));
}
return this;
}
/**
* This method allows to retrieve a header builder for the requested header
* @note this will create a new header builder if the one searched is undefined
* @param headerName the name of the header
* @returns the header builder associated with the headerName or undefined if none is found
*/
getHeaderBuilder(headerName) {
let builder = this.headers.getAs(headerName) ||
new HeaderBuilder_1.HeaderBuilder();
this.headers.set(headerName, builder);
return builder;
}
/**
* This method is used to set the content type of the transaction.
* @see {@link MessageType}
* @param contentType The content type of the data sent to the user. If contentType is undefined then nothing is changed from the last value
* @returns a self reference to the transaction manager for chaining more operations
*/
ContentType(contentType) {
if (contentType == undefined)
return this;
this.headers.getAs('Content-Type')[0] = contentType;
return this;
}
/**
* Returns the cookies manager that will set or unset cookies for responses to users
*/
getResponseCookiesManager() {
return this.headers.getAs('Set-Cookie');
}
/**
* This method is used to set the return code of the transaction.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status | HTTP Status Codes}
* @param retcode is the HTTP status code sent to the user. If retcode is undefined then nothing is changed from the last value
* @returns a self reference to the transaction manager for chaining more operations
*/
ReturnCode(retcode) {
if (retcode == undefined)
return this;
this.retcode = retcode;
return this;
}
/**
* @internal
* This method is used to reset to a default state the transaction manager. This method is called
* everytime before calling the associated method.
* @param thisArg the owner of the method
* @returns a self reference to the transaction manager
*/
reset(thisArg) {
this.headers.clear();
this.headers.set('Content-Type', new HeaderBuilder_1.HeaderBuilder());
this.headers.set('Set-Cookie', new ResponseCookiesManager_1.ResponseCookieManager());
this.ContentType('text/plain');
this.retcode = 200;
this.thisArg = thisArg;
return this;
}
/**
* This method will apply all saved information to the transaction
* @param res the transaction that will be modified by the content of this transaction manager
* @returns the modified transaction
*/
finalizeTransaction(res) {
for (let i of this.headers.keys()) {
res.set(i, this.headers.get(i).toConcreteHeader());
}
return res.status(this.retcode);
}
}
exports.TransactionManager = TransactionManager;