bridgets
Version:
<p align="center"> <a href="https://bridgets.co"> <img src="http://bridgets.co/assets/logo-short.svg" height="48" /> <h1 align="center">BridgeTS</h1> </a> </p>
70 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BridgeHandler = void 0;
const resolver_1 = require("./resolver");
const Validators_1 = require("./Validators");
const handler_1 = require("./handler");
class BridgeHandler extends handler_1.AbstractHandler {
p;
isBridgeHandler = true;
resolve;
handler;
description;
method;
filesConfig;
bodySchema;
querySchema;
headersSchema;
middlewares;
constructor(p) {
super();
this.p = p;
this.resolve = p.resolve;
this.description = p.description;
this.method = p.method;
this.filesConfig = p.filesConfig;
this.bodySchema = p.bodySchema;
this.querySchema = p.querySchema;
this.headersSchema = p.headersSchema;
this.middlewares = p.middlewares;
if (p.method === 'GET' && p.bodySchema)
throw new Error("You can't have a body with a GET endpoint.");
if (p.bodySchema && p.filesConfig)
throw new Error("You can't get a JSON body and files in the same endpoint.");
const firstHandler = new Validators_1.MethodValidator(p.method);
let handler = firstHandler;
if (p.bodySchema)
handler = handler.setNext(new Validators_1.DataValidator(p.bodySchema, 'body'));
if (p.querySchema)
handler = handler.setNext(new Validators_1.DataValidator(p.querySchema, 'query'));
if (p.headersSchema)
handler = handler.setNext(new Validators_1.DataValidator(p.headersSchema, 'headers'));
if (p.filesConfig)
handler = handler.setNext(new Validators_1.FilesValidator(p.filesConfig));
if (p.middlewares)
handler = handler.setNext(new handler_1.MiddelwaresHandler(p.middlewares));
// if (p.middlewares)
// p.middlewares.forEach((mid) => {
// handler = handler.setNext(mid);
// });
handler = handler.setNext(new resolver_1.Resolver(p.resolve));
this.handler = firstHandler;
}
/**
*
* If the middleware returns an error, we stop the chain and return it
* otherwise we add the result in the mid data of the next handler
* If there is no next handler, we return the last result
*/
handle = async (data) => {
const res = await this.handler.handle(data);
if (res && res.error)
return res;
data.mid = { ...res, ...data.mid };
if (this.nextHandler)
return this.nextHandler.handle(data);
return res;
};
}
exports.BridgeHandler = BridgeHandler;
//# sourceMappingURL=bridgeHandler.js.map