rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
141 lines (140 loc) • 5.03 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Route_1 = __importDefault(require("../Route"));
const RateLimit_1 = __importDefault(require("./RateLimit"));
const utils_1 = require("@rjweb/utils");
class Http {
/**
* Create a new HTTP Route Builder
* @since 7.0.0
*/ constructor(method, path, ratelimit) {
this.route = new Route_1.default('http', method, path, {});
if (ratelimit)
this.route.ratelimit = ratelimit;
}
/**
* Add OpenAPI Documentation to all HTTP Endpoints in this Path (and all children)
* @since 9.0.0
*/ document(item) {
this.route.openApi = utils_1.object.deepMerge(this.route.openApi, item);
return this;
}
/**
* Add a Ratelimit to this Endpoint
*
* When a User requests this Endpoint, that will count against their hit count, if
* the hits exceeds the `<maxHits>` in `<timeWindow>ms`, they wont be able to access
* the route for `<penalty>ms`.
* @example
* ```
* import { time } from "@rjweb/utils"
* const server = new Server(...)
*
* server.path('/', (path) => path
* .http('GET', '/hello', (ws) => ws
* .ratelimit((limit) => limit
* .hits(5)
* .timeWindow(time(20).s())
* .penalty(0)
* ) // This will allow 5 requests every 20 seconds
* .onRequest(async(ctr) => {
* ctr.print('Hello bro!')
* })
* )
* )
*
* server.rateLimit('httpRequest', (ctr) => {
* ctr.print(`Please wait ${ctr.getRateLimit()?.resetIn}ms!!!!!`)
* })
* ```
* @since 8.6.0
*/ ratelimit(callback) {
const limit = new RateLimit_1.default();
if (this.route.ratelimit)
limit['data'] = Object.assign({}, this.route.ratelimit);
callback(limit);
this.route.ratelimit = limit['data'];
return (0, utils_1.as)(this);
}
/**
* Add additional Context to this Endpoint
*
* This will add additional context to this Endpoint, which will be available in the
* `ctr` object when the Endpoint is executed.
* @since 7.0.0
*/ context() {
return (0, utils_1.as)(this);
}
/**
* Use a Validator on this Endpoint
*
* This will attach a Validator to this Endpoint, which will be run before the
* Endpoint is executed. If the Validator fails, the Endpoint will not be executed.
* @since 9.0.0
*/ validate(validator) {
this.route.validators.push(validator);
this.route.openApi = utils_1.object.deepMerge(this.route.openApi, validator.openApi);
return (0, utils_1.as)(this);
}
/**
* Attach a Callback for when someone makes an HTTP request
*
* This will attach a callback for when the server recieves a http request and
* finishes parsing it for the user. This Handler should always be set unless you
* are reserving a path for later or something.
* @example
* ```
* const server = new Server(...)
*
* server.path('/', (path) => path
* .http('GET', '/hello', (http) => http
* .onRequest((ctr) => {
* ctr.print('Hello')
* })
* )
* )
* ```
* @since 6.0.0
*/ onRequest(callback) {
this.route.data.onRequest = callback;
return (0, utils_1.as)(this);
}
/**
* Attach a Callback for when the server recieves a HTTP body chunk
*
* This will attach a callback for when the server receives an http POST body chunk, the
* request can always be ended by calling the 2nd function argument. Attaching this will
* cause `ctr.body`, `ctr.rawBody` and `ctr.rawBodyBytes` to be empty unless you manually
* assign them by doing `ctr.context.body.chunks.push(chunk)`.
* @warning when using this, `ctr.body`, `ctr.rawBody` and `ctr.rawBodyBytes` will always be empty
* @example
* ```
* const server = new Server(...)
*
* server.path('/', (path) => path
* .http('POST', '/hello', (http) => http
* .context<{
* chunkCount: number
* }>()
* .onRawBody((ctr, end, chunk, isLast) => {
* ctr["@"].chunkCount = (ctr["@"].chunkCount || 0) + 1
*
* console.log(`Recieved Chunk, isLast: ${isLast}`, chunk)
* if (ctr["@"].chunkCount > 10) end(ctr.status(ctr.$status.BAD_REUQEST).print('too many chunks')) // This stops recieving chunks and will end the http request
* })
* .onRequest((ctr) => {
* return ctr.print(`I received ${ctr["@"].chunkCount} chunks!`)
* })
* )
* )
* ```
* @since 6.0.0
*/ onRawBody(callback) {
this.route.data.onRawBody = callback;
return (0, utils_1.as)(this);
}
}
exports.default = Http;