From 32a22304482594ddac23160f666cfc3f099687de Mon Sep 17 00:00:00 2001 From: yuekailu Date: Fri, 5 May 2023 15:37:00 +0800 Subject: [PATCH] add addInvokeShortcut --- README.md | 28 +++++++++ index.js | 171 +++++++++++++++++++++++++++++---------------------- test/test.js | 33 ++++++++++ 3 files changed, 158 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 57f59a2..9cf5a88 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,34 @@ apis.addProcessorCreator( ); ``` +#### addInvokeShortcut + +`说明` + +新增调用场景简写,新增的场景同内置场景使用方式一样(但不可以冲掉内置的场景 ) + +`参数` + +- `{string}` name 注册的场景名称 +- `{Function}` processorCreator 需要注册场景的invokes,是一个由invoke字符串组成的数组 + +`返回` + +`{APIContainer}` this + +`示例` + +```js +apis.addInvokeShortcut( + 'myMethod.json', + [ + "ArgCheck", + "CallMethod", + "ReturnDecode:JSON" + ] +); +``` + #### config `说明` diff --git a/index.js b/index.js index 6e97d5b..a11b1e5 100644 --- a/index.js +++ b/index.js @@ -542,82 +542,17 @@ return target; } - /** - * 对调用描述对象进行标准化处理 - * - * @inner - * @param {Object} description 调用描述对象 - * @param {Function?} propMerger 属性合并方法,默认实现为for...in,APIContainer可用于提升性能 - * @return {Object} - */ - function normalizeDescription(description, propMerger) { - var args = []; - if (description.args instanceof Array) { - for (var i = 0; i < description.args.length; i++) { - var arg = description.args[i]; - - args.push({ - name: arg.name || arg.n, - value: arg.value || arg.v - }); - } - } - - propMerger = propMerger || descriptionPropMerger; - return propMerger( - { - name: description.name, - args: args, - invoke: normalizeInvoke(description.invoke), - method: description.method, - scheme: description.scheme || description.schema, - authority: description.authority, - path: description.path, - handler: description.handler - }, - description - ); - } - - /** - * 对 description 中的 invoke 属性进行标准化处理 - * - * @inner - * @param {Array|Object|string} invoke description的invoke属性 - * @return {Array?} - */ - function normalizeInvoke(invoke) { - if (invoke instanceof Array) { - return invoke; - } - - switch (typeof invoke) { - case 'string': - return INVOKE_SHORTCUT[invoke]; - - case 'object': - var result = []; - - if (invoke.check) { - result.push('ArgCheck'); - } - - if (invoke.before) { - result = result.concat(INVOKE_BEFORE_MAP[invoke.before]); - } - - result.push(INVOKE_CALL_MAP[invoke.call]); - - if (invoke.after === 'JSON') { - result.push('ReturnDecode:JSON'); - } - - return result; - - } - } + function APIContainer(options) { + + /** + * invokeShortcuts 快捷命令集合 + * + * @inner + * @type {Object} + */ + var invokeShortcuts = Object.assign({}, INVOKE_SHORTCUT); /** * processor 创建方法集合 * @@ -994,6 +929,20 @@ processorCreators[name] = processorCreator; return this; }, + /** + * 开发者补充shortcut的自定义集(TIPS:不能刷掉内置的shortcut) + * + * @param {string} name 注册的shortcut名称 + * @param {Array} 需要注册的shortcut invokes,是一个由invoke字符串组成的数组 + * @return {APIContainer} + */ + addInvokeShortcut: function (name, invokes) { + if (invokeShortcuts[name]) { + throw new Error('[' + this.options.errorTitle + '] invokeShortcuts exists: ' + name); + } + invokeShortcuts[name] = invokes; + return this + }, /** * 设置 description 额外的属性列表 @@ -1093,6 +1042,80 @@ return process(Array.prototype.slice.call(arguments, 0, description.args.length)); }; } + /** + * 对调用描述对象进行标准化处理 + * + * @inner + * @param {Object} description 调用描述对象 + * @param {Function?} propMerger 属性合并方法,默认实现为for...in,APIContainer可用于提升性能 + * @return {Object} + */ + function normalizeDescription(description, propMerger) { + var args = []; + if (description.args instanceof Array) { + for (var i = 0; i < description.args.length; i++) { + var arg = description.args[i]; + + args.push({ + name: arg.name || arg.n, + value: arg.value || arg.v + }); + } + } + + propMerger = propMerger || descriptionPropMerger; + return propMerger( + { + name: description.name, + args: args, + invoke: normalizeInvoke(description.invoke), + method: description.method, + scheme: description.scheme || description.schema, + authority: description.authority, + path: description.path, + handler: description.handler + }, + description + ); + } + + /** + * 对 description 中的 invoke 属性进行标准化处理 + * + * @inner + * @param {Array|Object|string} invoke description的invoke属性 + * @return {Array?} + */ + function normalizeInvoke(invoke) { + if (invoke instanceof Array) { + return invoke; + } + + switch (typeof invoke) { + case 'string': + return invokeShortcuts[invoke]; + + case 'object': + var result = []; + + if (invoke.check) { + result.push('ArgCheck'); + } + + if (invoke.before) { + result = result.concat(INVOKE_BEFORE_MAP[invoke.before]); + } + + result.push(INVOKE_CALL_MAP[invoke.call]); + + if (invoke.after === 'JSON') { + result.push('ReturnDecode:JSON'); + } + + return result; + + } + } } // export object =========== diff --git a/test/test.js b/test/test.js index 6426ad3..acfd468 100644 --- a/test/test.js +++ b/test/test.js @@ -479,6 +479,39 @@ describe('APIContainer', () => { expect(apis.invoke('api15', [2, 1])).to.be.equal('san'); }); + it('addInvokeShortcut', () => { + tAPI.api16 = (a, b) => { + return a + b; + }; + apis.addProcessorCreator( + 'increaseNumber', + () => args => { + return Array.isArray(args) ? args.map((i) => typeof i === 'number' ? ++i : i) : args + } + ); + apis.addInvokeShortcut('increaseNumberAndCallMethod', [ + 'increaseNumber', + 'CallMethod' + ]); + apis.add({ + invoke: 'increaseNumberAndCallMethod', + name: 'api16', + method: "tAPI.api16", + args: [ + {name: 'one', value: 'number'}, + {name: 'two', value: 'number'} + ], + }); + expect(apis.invoke('api16', [2, 1])).to.be.equal(5); + }); + it('addInvokeShortcut error', () => { + expect(() => { + apis.addInvokeShortcut('method', [ + "ArgCheck", + "CallMethod" + ]); + }).to.throw('invokeShortcuts exists'); + }); }); describe('Processor CallMethod', () => {