tspace-spear
Version:
tspace-spear is a lightweight API framework for Node.js that is fast and highly focused on providing the best developer experience. It utilizes the native HTTP server
69 lines • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cookies = exports.Query = exports.Params = exports.Files = exports.Body = void 0;
const Body = (...bodyParms) => {
return function (target, key, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (ctx, next) {
const q = ctx?.body ?? {};
const body = bodyParms.reduce((acc, key) => (q[key] != null ? { ...acc, [key]: q[key] } : acc), {});
ctx.body = Object.keys(body).length ? body : {};
return await originalMethod.call(this, ctx, next);
};
return descriptor;
};
};
exports.Body = Body;
const Files = (...filesParms) => {
return function (target, key, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (ctx, next) {
const q = ctx?.files ?? {};
const files = filesParms.reduce((acc, key) => (q[key] != null ? { ...acc, [key]: q[key] } : acc), {});
ctx.files = Object.keys(files).length ? files : {};
return await originalMethod.call(this, ctx, next);
};
return descriptor;
};
};
exports.Files = Files;
const Params = (...paramsData) => {
return function (target, key, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (ctx, next) {
const q = ctx?.params ?? {};
const params = paramsData.reduce((acc, key) => (q[key] != null ? { ...acc, [key]: q[key] } : acc), {});
ctx.params = Object.keys(params).length ? params : {};
return await originalMethod.call(this, ctx, next);
};
return descriptor;
};
};
exports.Params = Params;
const Query = (...queryParms) => {
return function (target, key, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (ctx, next) {
const q = ctx?.query ?? {};
const query = queryParms.reduce((acc, key) => (q[key] != null ? { ...acc, [key]: q[key] } : acc), {});
ctx.query = Object.keys(query).length ? query : {};
return await originalMethod.call(this, ctx, next);
};
return descriptor;
};
};
exports.Query = Query;
const Cookies = (...cookiesParms) => {
return function (target, key, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (ctx, next) {
const q = ctx?.cookies ?? {};
const cookies = cookiesParms.reduce((acc, key) => (q[key] != null ? { ...acc, [key]: q[key] } : acc), {});
ctx.cookies = Object.keys(cookies).length ? cookies : {};
return await originalMethod.call(this, ctx, next);
};
return descriptor;
};
};
exports.Cookies = Cookies;
//# sourceMappingURL=context.js.map