@nasriya/hypercloud
Version:
Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.
40 lines (39 loc) • 962 B
JavaScript
class RequestBody {
#_data = {};
/**
* Securely set key:value pair
* @param name
* @param value
*/
set(name, value) {
if (typeof name !== 'string') {
throw new TypeError(`The property name can only be a string, instead got ${typeof name}`);
}
if (!(name === '__proto__' || name === 'constructor' || name === 'prototype')) {
this.#_data[name] = decodeURIComponent(value);
}
}
/**
* Get the value
* @param name
*/
get(name) {
return this.#_data[name];
}
_toJSON() {
return this.#_data;
}
/**
* Safely copy an object
* @param value
*/
from(value) {
if (typeof value === 'object' && Object.keys(value).length > 0) {
for (const prop in value) {
this.set(prop, value[prop]);
}
}
return this._toJSON();
}
}
export default RequestBody;