Script will run in a sandbox with some build-in API, this APIs will help you to implement powerful function.
require
Require function allows you to import some build-in js lib listed below:
- uuid: generate guid
- xlsx : read excel file
- request: request data in script
- lodash: popular javaScript utility lib
- crypto-js: crypto lib
Of course, these build-in libs cann't meet all user's demand. Hitchhiker also support custom js lib, yo u can upload js lib which you want in Project with zip format (ref to Custom js lib, then you can require this js lib in script, that's to say, you can do whatever you want in script, such as read data from DB.
const request = hitchhiker.require('request'); // could use `hkr` instead of `hitchhiker`
function getData() {
return new Promise((resolve, reject) => {
const req = request("http://httpbin.org/get?a=request in request", (err, res, body) => {
resolve({ err: err, response: res, body: body });
});
});
}
const res = await getData(); // MUST use async/await
saveFile
Parameters:
file: string content: string replaceIfExist: boolean default is true
Save data as a file, and use it in another request's script.
hitchhiker.saveFile('test.txt', 'test file content');
readFile
Parameters:
file: string Return string value of file content.
Read a file that you upload to Project Data or saved in some other requests.
const value = hitchhiker.readFile('test.txt');
readFileByReader
Parameters:
file: string reader: function(file) Return string value of file content.
Read a file with custom reader like excel reader.
const xlsx = hitchhiker.require('xlsx');
const workbook = hitchhiker.readFileByReader('test.xlsx', xlsx.readFile);
removeFile
Parameters:
file: string
Remove a file from server.
hitchhiker.removeFile('test.txt');
setEnvVariable
Parameters:
key: string value: any
Set a runtime variable.
hitchhiker.setEnvVariable('value', 'test');
getEnvVariable
Parameters:
key: string
Get a runtime variable's value.
const value = hitchhiker.getEnvVariable('value');
removeEnvVariable
Parameters:
key: string
Remove a runtime variable.
hitchhiker.removeEnvVariable('value');
environment
Get selected environment.
const env = hitchhiker.environment;
request
Get current request include url, method, headers, body.
const req = hitchhiker.request;
const {url, headers, method, body} = req;
setRequest
Change current request, you can get request and then modify url/method/headers/body and then set it.
const crypto = hitchhiker.require('crypto-js');
const sign = crypto.HmacSHA1('test', 'asdf');
const req = hitchhiker.request;
url = `${url}?sign=${sign}`;
hitchhiker.setRequest({...hitchhiker.request, url});
console
support console
console.log('test');
console.info('test');
console.warn('test');
console.error('test');
export
Export data for comparison with different environment in Schedule.
Reflect.deleteProperty(responseObj, 'date');
hitchhiker.export(responseObj);