Cloud Function
callFunction
1. Interface Description
Function: Execute cloud function
Interface declaration: callFunction<ParaT, ResultT>(callFunctionOptions: ICallFunctionOptions<ParaT>, opts?: ICustomReqOpts): Promise<CallFunctionResult<ResultT>>
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
callFunctionOptions | ICallFunctionOptions<ParaT> | Yes | Cloud function call request parameters |
opts | ICustomReqOpts | No | Custom configurations, currently supports setting the SDK request timeout, {timeout: number} |
ICallFunctionOptions<ParaT>
Cloud Function Parameters:
Field | Type | Required | Description |
---|---|---|---|
name | string | Yes | Cloud function name |
data | ParaT | No | Cloud function parameters |
qualifier | string | No | Cloud function version identifier: $LATEST (latest version) 1 2 3 . If omitted, traffic is allocated according to the platform-configured traffic ratio |
Function Cloud Hosting can additionally pass parameters. After passing the type:'cloudrun'
parameter, the Function Cloud Hosting service will be invoked.
Field | Type | Required | Description |
---|---|---|---|
type | cloudrun | No | Whether to invoke the function-based cloud hosting service |
method | string | No | HTTP request method |
path | string | No | HTTP request path |
header | Record<string, string> | No | HTTP request headers |
data | object | No | HTTP request body |
3. Response
Promise<CallFunctionResult<ResultT>>
Field | Type | Required | Description |
---|---|---|---|
result | ResultT | No | Cloud function execution result |
requestId | string | No | Request ID, used for error troubleshooting |
Function execution errors will throw exceptions
4. Sample Code
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "xxx",
});
exports.main = async (event, context) => {
const res = await app.callFunction({
name: "test",
data: { a: 1 },
});
console.log(res); // Print the function call result
const res1 = await app.callFunction(
{
name: "test",
data: { a: 1 },
},
{
timeout: 5000,
}
);
console.log(res1);
};
Function Cloud Hosting Sample Code:
import tcb from '@cloudbase/node-sdk'
exports.main = async (event, context) => {
const { httpContext } = context
const { url, httpMethod } = httpContext
return `[${httpMethod}][${url}] Hello world!`
const tcbapp = tcb.init({ context })
const result = await tcbapp.callFunction({
name: 'helloworld',
// Function Cloud Hosting parameters
type: 'cloudrun',
method: 'POST',
path: '/abc',
data: {
key1: 'test value 1',
key2: 'test value 2'
},
{
timeout: 5000
}
})
console.log(result)
}