2 Star 0 Fork 0

mirrors_tencentyun / tencentcloud-serverless-nodejs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Tencentcloud-Serverless-Nodejs

GitHub license NPM Version NODE Version

腾讯云云函数SDK,集成云函数业务流接口

配置需求

  • 打开函数外网访问权限
  • 如果没有手动传入secretId和secretKey等参数,函数需绑定有SCF Invoke 权限(或者包含SCF Invoke,比如SCF FullAccess)的角色

Install

npm install tencentcloud-serverless-nodejs

Example

const { SDK, LogType }  = require('tencentcloud-serverless-nodejs')

exports.main_handler = async (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false
  const sdk = new SDK({
    region:'ap-guangzhou'
  }) //如果在云函数中运行并且绑定了有SCF调用资格的运行角色,会默认取环境变量中的鉴权信息
  const res = await sdk.invoke({
    functionName: 'test_function',
    logType: LogType.Tail,
    data: {
      name: 'juli',
      role: 'big boss'
    }
  })
  console.log(res)
  // {
  //  "RequestId":"195434cd-ecf1-43d1-b707-90a293f5a282",
  //  "Result":{
  //     "BillDuration":3,
  //     "Duration":3,
  //     "ErrMsg":"",
  //     "FunctionRequestId":"195434cd-ecf1-43d1-b707-90a293f5a282",
  //     "InvokeResult":0,
  //     "Log":"START RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282\nEvent RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282\n2020-06-15T12:48:44.194Z\t195434cd-ecf1-43d1-b707-90a293f5a282\tHello World 111\n2020-06-15T12:48:44.194Z\t195434cd-ecf1-43d1-b707-90a293f5a282\t{ name: 'juli', role: 'big boss' }\n2020-06-15T12:48:44.196Z\t195434cd-ecf1-43d1-b707-90a293f5a282\tundefined\n2020-06-15T12:48:44.196Z\t195434cd-ecf1-43d1-b707-90a293f5a282\t{\n  callbackWaitsForEmptyEventLoop: [Getter/Setter],\n  getRemainingTimeInMillis: [Function: getRemainingTimeInMillis],\n  memory_limit_in_mb: 128,\n  time_limit_in_ms: 3000,\n  request_id: '195434cd-ecf1-43d1-b707-90a293f5a282',\n  environment: '{\"SCF_NAMESPACE\":\"default\"}',\n  environ: 'SCF_NAMESPACE=default;SCF_NAMESPACE=default',\n  function_version: '$LATEST',\n  function_name: 'test_function',\n  namespace: 'default',\n  tencentcloud_region: '',\n  tencentcloud_appid: 'xxxxxxxx',\n  tencentcloud_uin: 'xxxxxxxxxx'\n}\n\nEND RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282\nReport RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282 Duration:3ms Memory:128MB MemUsage:10.5977MB",
  //     "MemUsage":11112448,
  //     "RetMsg":"{\"name\":\"juli\",\"role\":\"big boss\"}"
  //  }
}
};

API

Init

new SDK(Params)

使用SDK前,需要先初始化SDK,参数中undefined的值会被忽略。

Params:

参数名 是否必填 类型 描述
region string 地域,默认广州(ap-guangzhou)
secretId string 默认会取process.env.TENCENTCLOUD_SECRETID
secretKey string 默认会取process.env.TENCENTCLOUD_SECRETKEY
token string 默认会取process.env.TENCENTCLOUD_SESSIONTOKEN

Invoke

invoke(Params)

调用函数。支持同步和异步调用

Params:

参数名 是否必填 类型 描述
functionName string 函数名称
qualifier string 函数版本,默认为$LATEST
data string | object 函数运行入参
namespace string 命名空间,默认为default
invocationType 'RequestResponse' | 'Event' 同步/异步调用
routingKey string | object 函数灰度流量控制调用,以json格式传入,例如{"k":"v"},注意kv都需要是字符串类型,最大支持的参数长度是1024字节
logType 'None'| 'Tail' 日志类型,默认None,如传入Tail会返回调用过程中打印的日志

Return:

正常调用返回一个带有返回值结果和RequestId的object

 {
   "RequestId":"195434cd-ecf1-43d1-b707-90a293f5a282",
   "Result":{
      "BillDuration":3,
      "Duration":3,
      "ErrMsg":"",
      "FunctionRequestId":"195434cd-ecf1-43d1-b707-90a293f5a282",
      "InvokeResult":0,
      "Log":"START RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282\nEvent RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282\n2020-06-15T12:48:44.194Z\t195434cd-ecf1-43d1-b707-90a293f5a282\tHello World 111\n2020-06-15T12:48:44.194Z\t195434cd-ecf1-43d1-b707-90a293f5a282\t{ name: 'juli', role: 'big boss' }\n2020-06-15T12:48:44.196Z\t195434cd-ecf1-43d1-b707-90a293f5a282\tundefined\n2020-06-15T12:48:44.196Z\t195434cd-ecf1-43d1-b707-90a293f5a282\t{\n  callbackWaitsForEmptyEventLoop: [Getter/Setter],\n  getRemainingTimeInMillis: [Function: getRemainingTimeInMillis],\n  memory_limit_in_mb: 128,\n  time_limit_in_ms: 3000,\n  request_id: '195434cd-ecf1-43d1-b707-90a293f5a282',\n  environment: '{\"SCF_NAMESPACE\":\"default\"}',\n  environ: 'SCF_NAMESPACE=default;SCF_NAMESPACE=default',\n  function_version: '$LATEST',\n  function_name: 'test_function',\n  namespace: 'default',\n  tencentcloud_region: '',\n  tencentcloud_appid: 'xxxxxxxx',\n  tencentcloud_uin: 'xxxxxxxxxx'\n}\n\nEND RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282\nReport RequestId: 195434cd-ecf1-43d1-b707-90a293f5a282 Duration:3ms Memory:128MB MemUsage:10.5977MB",
      "MemUsage":11112448,
      "RetMsg":"{\"name\":\"juli\",\"role\":\"big boss\"}"
   }

错误调用返回一个带有错误信息,错误码,以及RequestId的object,【错误码详情】

{
   "Error":{
      "Code":"ResourceNotFound.FunctionName",
      "Message":"Function not found: [n11112]"
   },
   "RequestId":"bb994f2f-526e-4142-bf9b-b8cf482c14ea"
}

Licence

MIT

MIT License Copyright (c) 2018 Jack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
JavaScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_tencentyun/tencentcloud-serverless-nodejs.git
git@gitee.com:mirrors_tencentyun/tencentcloud-serverless-nodejs.git
mirrors_tencentyun
tencentcloud-serverless-nodejs
tencentcloud-serverless-nodejs
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891