mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: handler: prefer args as first argument
This commit is contained in:
1 file changed
+7
-3
@@ -41,12 +41,16 @@ function _descriptor(v: ParamOption<any>) {
|
|||||||
target.__param ||= {};
|
target.__param ||= {};
|
||||||
target.__param[target.constructor.name] ||= {};
|
target.__param[target.constructor.name] ||= {};
|
||||||
if (!target.__param[target.constructor.name][funcName]) {
|
if (!target.__param[target.constructor.name][funcName]) {
|
||||||
target.__param[target.constructor.name][funcName] = [{ name: 'domainId', type: 'string', source: 'route' }];
|
|
||||||
const originalMethod = obj.value;
|
const originalMethod = obj.value;
|
||||||
|
const val = originalMethod.toString();
|
||||||
|
const firstArg = val.split(')')[0]?.split(',')[0]?.split('(')[1]?.trim() || '';
|
||||||
|
const domainIdStyle = firstArg.toLowerCase().includes('domainid');
|
||||||
|
target.__param[target.constructor.name][funcName] = [];
|
||||||
obj.value = function validate(this: Handler, rawArgs: any, ...extra: any[]) {
|
obj.value = function validate(this: Handler, rawArgs: any, ...extra: any[]) {
|
||||||
if (typeof rawArgs !== 'object' || extra.length) return originalMethod.call(this, rawArgs, ...extra);
|
if (typeof rawArgs !== 'object' || extra.length) return originalMethod.call(this, rawArgs, ...extra);
|
||||||
const c = [];
|
const c = [];
|
||||||
const arglist: ParamOption<any>[] = target.__param[target.constructor.name][funcName];
|
const arglist: ParamOption<any>[] = target.__param[target.constructor.name][funcName];
|
||||||
|
if (typeof rawArgs.domainId !== 'string' || !rawArgs.domainId) throw new ValidationError('domainId');
|
||||||
for (const item of arglist) {
|
for (const item of arglist) {
|
||||||
const src = item.source === 'all'
|
const src = item.source === 'all'
|
||||||
? rawArgs
|
? rawArgs
|
||||||
@@ -65,10 +69,10 @@ function _descriptor(v: ParamOption<any>) {
|
|||||||
c.push(item.convert ? item.convert(value) : value);
|
c.push(item.convert ? item.convert(value) : value);
|
||||||
} else c.push(undefined);
|
} else c.push(undefined);
|
||||||
}
|
}
|
||||||
return originalMethod.call(this, ...c);
|
return domainIdStyle ? originalMethod.call(this, rawArgs.domainId, ...c) : originalMethod.call(this, rawArgs, ...c);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
target.__param[target.constructor.name][funcName].splice(1, 0, v);
|
target.__param[target.constructor.name][funcName].unshift(v);
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user