diff --git a/es5/client.js b/es5/client.js index 8abf44d..777c72a 100644 --- a/es5/client.js +++ b/es5/client.js @@ -127,7 +127,7 @@ var Client = function () { }, { key: 'request', value: function () { - var _ref = _asyncToGenerator(_regenerator2.default.mark(function _callee(method, path, query, body) { + var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee(method, path, query, body) { var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; var url, postBody, buff, digest, md5, stringToSign, signature, response, responseBody, contentType, code, requestid, err; return _regenerator2.default.wrap(function _callee$(_context) { @@ -428,8 +428,32 @@ var Client = function () { }, { key: 'createFunction', value: function createFunction(serviceName, options, headers) { + this.normalizeParams(options); return this.post(`/services/${serviceName}/functions`, options, headers); } + }, { + key: 'normalizeParams', + value: function normalizeParams(opts) { + if (opts.functionName) { + opts.functionName = String(opts.functionName); + } + + if (opts.runtime) { + opts.runtime = String(opts.runtime); + } + + if (opts.handler) { + opts.handler = String(opts.handler); + } + + if (opts.memorySize) { + opts.memorySize = parseInt(opts.memorySize); + } + + if (opts.timeout) { + opts.timeout = parseInt(opts.timeout); + } + } /** * 获取Function列表 @@ -494,6 +518,7 @@ var Client = function () { }, { key: 'updateFunction', value: function updateFunction(serviceName, functionName, options, headers) { + this.normalizeParams(options); var path = `/services/${serviceName}/functions/${functionName}`; return this.put(path, options, headers); } diff --git a/lib/client.js b/lib/client.js index 769e471..8f405f8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -322,9 +322,33 @@ class Client { * @return {Promise} 返回 Function 信息 */ createFunction(serviceName, options, headers) { + this.normalizeParams(options) return this.post(`/services/${serviceName}/functions`, options, headers); } + normalizeParams(opts) { + if (opts.functionName){ + opts.functionName = String(opts.functionName) + } + + if (opts.runtime){ + opts.runtime = String(opts.runtime) + } + + if (opts.handler){ + opts.handler = String(opts.handler) + } + + if (opts.memorySize){ + opts.memorySize = parseInt(opts.memorySize) + } + + if (opts.timeout){ + opts.timeout = parseInt(opts.timeout) + + } + } + /** * 获取Function列表 * @@ -373,6 +397,7 @@ class Client { * @return {Promise} 返回 Object(包含headers和data属性[Function信息]) */ updateFunction(serviceName, functionName, options, headers) { + this.normalizeParams(options) const path = `/services/${serviceName}/functions/${functionName}`; return this.put(path, options, headers); } diff --git a/test/client.test.js b/test/client.test.js index d026d49..0ef960b 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -304,6 +304,39 @@ describe('client test', function () { }).to.throwException(/"event" must be String or Buffer/); }); + it('createFunction with invalid runtime should fail', async function() { + try { + await client.createFunction(serviceName, { + functionName: "test_invalid_runtime_function", + description: 'function desc', + memorySize: 128, + handler: 'main.handler', + runtime: 10, + timeout: 10, + code: { + zipFile: fs.readFileSync(path.join(__dirname, 'figures/test.zip'), 'base64') + } + }); + } catch (ex) { + expect(ex.stack).to.contain('FCInvalidArgumentError'); + expect(ex.stack).to.contain('Runtime is set to an invalid value'); + } + + }); + + it('updateFunction with invalid runtime should fail', async function() { + try { + await client.updateFunction(serviceName, functionName, { + description: 'updated function desc', + runtime: 10 + }); + } catch (ex) { + expect(ex.stack).to.contain('FCInvalidArgumentError'); + expect(ex.stack).to.contain('Runtime is set to an invalid value'); + } + + }); + it('deleteFunction should ok', async function() { await client.deleteFunction(serviceName, functionName); // No exception, no failed