Skip to content

Commit

Permalink
feat(cli): cli invoke function support parameters (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
genie88 committed Mar 10, 2023
1 parent 3b30aec commit f0a6383
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
39 changes: 37 additions & 2 deletions cli/src/action/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,14 @@ export async function pushOne(funcName: string) {
console.log(`${getEmoji('✅')} function ${funcName} pushed`)
}

export async function exec(funcName: string, options: { log: string; requestId: boolean }) {
export async function exec(funcName: string, options: {
log: string;
requestId: boolean,
method: string,
query: string,
data: string,
headers: any,
}) {
// compile code
const codePath = path.join(process.cwd(), 'functions', funcName + '.ts')
if (!exist(codePath)) {
Expand All @@ -207,7 +214,35 @@ export async function exec(funcName: string, options: { log: string; requestId:
const func = await functionControllerCompile(appConfig.appid, funcName, compileDto)
// invoke debug
const secretConfig = readSecretConfig()
const res = await invokeFunction(appConfig.invokeUrl, secretConfig?.functionSecretConfig?.debugToken, funcName, func)

// transform headers json string to object. -H '{"Content-Type": "application/json"}'
if (options.headers) {
try {
options.headers = JSON.parse(options.headers)
} catch (e) {
options.headers = {}
}
}

// transform data json string to object. eg -d '{"key": "val"}' or -d 'key=val'
if (options.data) {
try {
options.data = JSON.parse(options.data)
} catch (e) {
options.data = options.data
}
}

const res = await invokeFunction(
appConfig.invokeUrl || '',
secretConfig?.functionSecretConfig?.debugToken,
funcName,
func,
options.method,
options.query,
options.data,
options.headers,
)

// print requestId
if (options.requestId) {
Expand Down
17 changes: 14 additions & 3 deletions cli/src/api/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ export async function invokeFunction(
invokeUrl: string,
token: string,
funcName: string,
data: any,
funcData: any,
method: string = 'GET',
query: string = '',
data: string = '',
customerHeader: any = {},
): Promise<{ res: any; requestId: string }> {
const header: AxiosRequestHeaders | any = {
'x-laf-develop-token': token,
'x-laf-func-data': urlencode(JSON.stringify(data)),
'x-laf-func-data': urlencode(JSON.stringify(funcData)),
'Content-Type': 'application/x-www-form-urlencoded', // default content type
...customerHeader,
}
const res = await request({ url: invokeUrl + '/' + funcName, method: 'GET', headers: header })
const res = await request({
url: invokeUrl + '/' + funcName + (query ? '?' + query : ''),
method: method,
headers: header,
data: data,
})
return {
res: res.data,
requestId: res.headers['request-id'],
Expand Down
4 changes: 4 additions & 0 deletions cli/src/command/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export function command(): Command {
.command('exec <funcName>')
.description('exec function')
.option('-l --log <count>', 'print log')
.option('-X --method <method>', 'request method, eg -X HEAD/GET/POST/PUT/DELETE')
.option('-H --headers <request headers>', 'request headers, eg -H \'{"Content-Type": "application/json"}\'')
.option('-q --query <request query params>', 'request query params, eg -q "key1=val1&key2=val2"')
.option('-d --data <request body data>', 'request body data, eg -d \'{"key1": "val1"}\'')
.option('-r --requestId', 'print requestId', false)
.hook('preAction', async () => {
await checkFunctionDebugToken()
Expand Down

0 comments on commit f0a6383

Please sign in to comment.