1.0.1 • Published 2 years ago

@hw-agconnect/function-server v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

CP_WiseFunction_FunctionSDK_NodeJs

集成SDK

集成云函数SDK如果需要在工程中调用云函数,则必须集成云函数的Server SDK。 请参考Server使用入门集成AGC SDK。

 1.在package.json中添加AGC的common依赖与function-server依赖。
    "@agconnect/common-server": "1.1.0",
    "@hw-agconnect/function-server": "1.0.1"

2.导入AGC模块。
    const {AGCClient} = require("@agconnect/common-server");
    const {CredentialParser} = require("@agconnect/common-server");
    const {AGCFunction} = require("@hw-agconnect/function-server");

使用SDK

1.创建一个AGCClient对象。先手动加载认证凭据agc-apiclient.json,然后初始化AGCClient实例。

// 加载凭证文件
let client_name = "./agc-apiclient.json";
let client_path = path.join(__dirname, client_name);
let credential = CredentialParser.toCredential(client_path);
AGCClient.initialize(credential);

2.构造AGCFunction实例。

// 构造云函数实例
let agcFunction = new AGCFunction();

3.调用AGCFunction.wrap设置函数,在方法中传入函数名和版本(或别名)。

let callable = agcFunction.wrap("function-test", "$latest");

4.根据函数入参情况,调用相关方法来调用函数。

4.1 无参调用。
try {
    let res = await callable.call();
} catch (error) {
    console.error("-------abnormal-------");
    callback(bad_res);
}

4.2 有参调用
try {
    let obj = {"aa": "bb"};
    let res = await callable.call(obj);
} catch (error) {
    console.error("-------abnormal-------");
    callback(bad_res);
}

4.如果您需要关注函数的返回值,可使用FunctionResult.getValue来获取被调函数执行的结果。

let result = res.getValue();