Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): cli invoke function support parameters (#842) #884

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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