Skip to main content

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

FieldTypeRequiredDescription
callFunctionOptionsICallFunctionOptions<ParaT>YesCloud function call request parameters
optsICustomReqOptsNoCustom configurations, currently supports setting the SDK request timeout, {timeout: number}

ICallFunctionOptions<ParaT>

Cloud Function Parameters:

FieldTypeRequiredDescription
namestringYesCloud function name
dataParaTNoCloud function parameters
qualifierstringNoCloud 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.

FieldTypeRequiredDescription
typecloudrunNoWhether to invoke the function-based cloud hosting service
methodstringNoHTTP request method
pathstringNoHTTP request path
headerRecord<string, string>NoHTTP request headers
dataobjectNoHTTP request body

3. Response

Promise<CallFunctionResult<ResultT>>

FieldTypeRequiredDescription
resultResultTNoCloud function execution result
requestIdstringNoRequest 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)
}