Skip to content

Commit f0a6383

Browse files
authored
feat(cli): cli invoke function support parameters (#884)
1 parent 3b30aec commit f0a6383

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

cli/src/action/function/index.ts

+37-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,14 @@ export async function pushOne(funcName: string) {
192192
console.log(`${getEmoji('✅')} function ${funcName} pushed`)
193193
}
194194

195-
export async function exec(funcName: string, options: { log: string; requestId: boolean }) {
195+
export async function exec(funcName: string, options: {
196+
log: string;
197+
requestId: boolean,
198+
method: string,
199+
query: string,
200+
data: string,
201+
headers: any,
202+
}) {
196203
// compile code
197204
const codePath = path.join(process.cwd(), 'functions', funcName + '.ts')
198205
if (!exist(codePath)) {
@@ -207,7 +214,35 @@ export async function exec(funcName: string, options: { log: string; requestId:
207214
const func = await functionControllerCompile(appConfig.appid, funcName, compileDto)
208215
// invoke debug
209216
const secretConfig = readSecretConfig()
210-
const res = await invokeFunction(appConfig.invokeUrl, secretConfig?.functionSecretConfig?.debugToken, funcName, func)
217+
218+
// transform headers json string to object. -H '{"Content-Type": "application/json"}'
219+
if (options.headers) {
220+
try {
221+
options.headers = JSON.parse(options.headers)
222+
} catch (e) {
223+
options.headers = {}
224+
}
225+
}
226+
227+
// transform data json string to object. eg -d '{"key": "val"}' or -d 'key=val'
228+
if (options.data) {
229+
try {
230+
options.data = JSON.parse(options.data)
231+
} catch (e) {
232+
options.data = options.data
233+
}
234+
}
235+
236+
const res = await invokeFunction(
237+
appConfig.invokeUrl || '',
238+
secretConfig?.functionSecretConfig?.debugToken,
239+
funcName,
240+
func,
241+
options.method,
242+
options.query,
243+
options.data,
244+
options.headers,
245+
)
211246

212247
// print requestId
213248
if (options.requestId) {

cli/src/api/debug.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ export async function invokeFunction(
55
invokeUrl: string,
66
token: string,
77
funcName: string,
8-
data: any,
8+
funcData: any,
9+
method: string = 'GET',
10+
query: string = '',
11+
data: string = '',
12+
customerHeader: any = {},
913
): Promise<{ res: any; requestId: string }> {
1014
const header: AxiosRequestHeaders | any = {
1115
'x-laf-develop-token': token,
12-
'x-laf-func-data': urlencode(JSON.stringify(data)),
16+
'x-laf-func-data': urlencode(JSON.stringify(funcData)),
17+
'Content-Type': 'application/x-www-form-urlencoded', // default content type
18+
...customerHeader,
1319
}
14-
const res = await request({ url: invokeUrl + '/' + funcName, method: 'GET', headers: header })
20+
const res = await request({
21+
url: invokeUrl + '/' + funcName + (query ? '?' + query : ''),
22+
method: method,
23+
headers: header,
24+
data: data,
25+
})
1526
return {
1627
res: res.data,
1728
requestId: res.headers['request-id'],

cli/src/command/function/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export function command(): Command {
6161
.command('exec <funcName>')
6262
.description('exec function')
6363
.option('-l --log <count>', 'print log')
64+
.option('-X --method <method>', 'request method, eg -X HEAD/GET/POST/PUT/DELETE')
65+
.option('-H --headers <request headers>', 'request headers, eg -H \'{"Content-Type": "application/json"}\'')
66+
.option('-q --query <request query params>', 'request query params, eg -q "key1=val1&key2=val2"')
67+
.option('-d --data <request body data>', 'request body data, eg -d \'{"key1": "val1"}\'')
6468
.option('-r --requestId', 'print requestId', false)
6569
.hook('preAction', async () => {
6670
await checkFunctionDebugToken()

0 commit comments

Comments
 (0)