node-web-mvc
Version:
node spring mvc
45 lines (44 loc) • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureArray = void 0;
/**
* @module RouteMapping
* @description 路由映射
*/
const ensureArray = (value) => value instanceof Array ? value : [value].filter(Boolean);
exports.ensureArray = ensureArray;
class RequestMappingInfo {
/**
* 标准化传入的信息
*/
static create(value, method) {
if (value instanceof RequestMappingInfo) {
return value;
}
const data = (typeof value === 'string' ? { value: value } : value || {});
return new RequestMappingInfo(data.value, method, data.produces, data.params, data.headers, data.consumes);
}
/**
* 构造一个路由映射实例
* @param value 当前路由匹配路径,可以为 字符串或者字符串数组
* @param {String/Array} method 可以接受的请求方式
* @param {String} produces 允许的返回类型 'application/json'
* @param {Array} params 当前必要的参数 [ "userId","userName" ]
* @param {Array} header 当前必须要带的请求头 [ 'content-type=application/json' ]
* @param {Array} consumes 当前请求能处理的请求类型 例如: ['application/json'] ['application/octstream']
* @param {String}
*/
constructor(value, method, produces, params, headers, consumes) {
this.method = {};
this.value = (0, exports.ensureArray)(value);
this.consumes = typeof consumes === 'string' ? [consumes] : consumes;
this.produces = produces;
this.params = params;
this.headers = headers;
const methods = (0, exports.ensureArray)(method || ['GET', 'POST', 'DELETE', 'PUT', 'PATCH', 'OPTIONS']);
methods.forEach((k) => {
this.method[k.toUpperCase()] = true;
});
}
}
exports.default = RequestMappingInfo;