forked from extern/bruno
Merge pull request #705 from cwilper/feature/gql-schema-prerequest-postresponse
feat: run prerequest/postresponse actions when fetching gql schema
This commit is contained in:
commit
64b46caacd
@ -188,6 +188,144 @@ const parseDataFromResponse = (response) => {
|
||||
};
|
||||
|
||||
const registerNetworkIpc = (mainWindow) => {
|
||||
const onConsoleLog = (type, args) => {
|
||||
console[type](...args);
|
||||
|
||||
mainWindow.webContents.send('main:console-log', {
|
||||
type,
|
||||
args
|
||||
});
|
||||
};
|
||||
|
||||
const runPreRequest = async (
|
||||
request,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
) => {
|
||||
// run pre-request vars
|
||||
const preRequestVars = get(request, 'vars.req', []);
|
||||
if (preRequestVars?.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPreRequestVars(
|
||||
preRequestVars,
|
||||
request,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
processEnvVars
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run pre-request script
|
||||
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(os.EOL);
|
||||
if (requestScript?.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const result = await scriptRuntime.runRequestScript(
|
||||
decomment(requestScript),
|
||||
request,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
|
||||
// interpolate variables inside request
|
||||
interpolateVars(request, envVars, collectionVariables, processEnvVars);
|
||||
|
||||
// stringify the request url encoded params
|
||||
if (request.headers['content-type'] === 'application/x-www-form-urlencoded') {
|
||||
request.data = qs.stringify(request.data);
|
||||
}
|
||||
};
|
||||
|
||||
const runPostResponse = async (
|
||||
request,
|
||||
response,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
) => {
|
||||
// run post-response vars
|
||||
const postResponseVars = get(request, 'vars.res', []);
|
||||
if (postResponseVars?.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPostResponseVars(
|
||||
postResponseVars,
|
||||
request,
|
||||
response,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
processEnvVars
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run post-response script
|
||||
const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join(
|
||||
os.EOL
|
||||
);
|
||||
if (responseScript?.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const result = await scriptRuntime.runResponseScript(
|
||||
decomment(responseScript),
|
||||
request,
|
||||
response,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// handler for sending http request
|
||||
ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => {
|
||||
const collectionUid = collection.uid;
|
||||
@ -195,15 +333,6 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
const cancelTokenUid = uuid();
|
||||
const requestUid = uuid();
|
||||
|
||||
const onConsoleLog = (type, args) => {
|
||||
console[type](...args);
|
||||
|
||||
mainWindow.webContents.send('main:console-log', {
|
||||
type,
|
||||
args
|
||||
});
|
||||
};
|
||||
|
||||
mainWindow.webContents.send('main:run-request-event', {
|
||||
type: 'request-queued',
|
||||
requestUid,
|
||||
@ -221,75 +350,21 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
||||
|
||||
try {
|
||||
// make axios work in node using form data
|
||||
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
||||
if (request.headers && request.headers['content-type'] === 'multipart/form-data') {
|
||||
const form = new FormData();
|
||||
forOwn(request.data, (value, key) => {
|
||||
form.append(key, value);
|
||||
});
|
||||
extend(request.headers, form.getHeaders());
|
||||
request.data = form;
|
||||
}
|
||||
|
||||
const cancelToken = axios.CancelToken.source();
|
||||
request.cancelToken = cancelToken.token;
|
||||
saveCancelToken(cancelTokenUid, cancelToken);
|
||||
|
||||
// run pre-request vars
|
||||
const preRequestVars = get(request, 'vars.req', []);
|
||||
if (preRequestVars?.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPreRequestVars(
|
||||
preRequestVars,
|
||||
request,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
processEnvVars
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run pre-request script
|
||||
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(
|
||||
os.EOL
|
||||
await runPreRequest(
|
||||
request,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
if (requestScript?.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const result = await scriptRuntime.runRequestScript(
|
||||
decomment(requestScript),
|
||||
request,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
|
||||
interpolateVars(request, envVars, collectionVariables, processEnvVars);
|
||||
|
||||
// stringify the request url encoded params
|
||||
if (request.headers['content-type'] === 'application/x-www-form-urlencoded') {
|
||||
request.data = qs.stringify(request.data);
|
||||
}
|
||||
|
||||
// todo:
|
||||
// i have no clue why electron can't send the request object
|
||||
@ -352,55 +427,18 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
const { data, dataBuffer } = parseDataFromResponse(response);
|
||||
response.data = data;
|
||||
|
||||
// run post-response vars
|
||||
const postResponseVars = get(request, 'vars.res', []);
|
||||
if (postResponseVars?.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPostResponseVars(
|
||||
postResponseVars,
|
||||
request,
|
||||
response,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
processEnvVars
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run post-response script
|
||||
const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join(
|
||||
os.EOL
|
||||
await runPostResponse(
|
||||
request,
|
||||
response,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
if (responseScript?.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const result = await scriptRuntime.runResponseScript(
|
||||
decomment(responseScript),
|
||||
request,
|
||||
response,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
|
||||
// run assertions
|
||||
const assertions = get(request, 'assertions');
|
||||
@ -501,8 +539,25 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
});
|
||||
}
|
||||
|
||||
const processEnvVars = getProcessEnvVars(collection.uid);
|
||||
interpolateVars(preparedRequest, envVars, collection.collectionVariables, processEnvVars);
|
||||
const requestUid = uuid();
|
||||
const collectionPath = collection.pathname;
|
||||
const collectionUid = collection.uid;
|
||||
const collectionVariables = collection.collectionVariables;
|
||||
const processEnvVars = getProcessEnvVars(collectionUid);
|
||||
const brunoConfig = getBrunoConfig(collection.uid);
|
||||
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
||||
|
||||
await runPreRequest(
|
||||
request,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
const axiosInstance = await configureRequest(
|
||||
collection.uid,
|
||||
@ -513,6 +568,19 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
);
|
||||
const response = await axiosInstance(preparedRequest);
|
||||
|
||||
await runPostResponse(
|
||||
request,
|
||||
response,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
@ -543,15 +611,6 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
||||
const collectionRoot = get(collection, 'root', {});
|
||||
|
||||
const onConsoleLog = (type, args) => {
|
||||
console[type](...args);
|
||||
|
||||
mainWindow.webContents.send('main:console-log', {
|
||||
type,
|
||||
args
|
||||
});
|
||||
};
|
||||
|
||||
if (!folder) {
|
||||
folder = collection;
|
||||
}
|
||||
@ -601,67 +660,21 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
|
||||
const _request = item.draft ? item.draft.request : item.request;
|
||||
const request = prepareRequest(_request, collectionRoot);
|
||||
const requestUid = uuid();
|
||||
const processEnvVars = getProcessEnvVars(collectionUid);
|
||||
|
||||
try {
|
||||
// make axios work in node using form data
|
||||
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
||||
if (request.headers && request.headers['content-type'] === 'multipart/form-data') {
|
||||
const form = new FormData();
|
||||
forOwn(request.data, (value, key) => {
|
||||
form.append(key, value);
|
||||
});
|
||||
extend(request.headers, form.getHeaders());
|
||||
request.data = form;
|
||||
}
|
||||
|
||||
// run pre-request vars
|
||||
const preRequestVars = get(request, 'vars.req', []);
|
||||
if (preRequestVars && preRequestVars.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPreRequestVars(
|
||||
preRequestVars,
|
||||
request,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run pre-request script
|
||||
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(
|
||||
os.EOL
|
||||
await runPreRequest(
|
||||
request,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
if (requestScript?.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const result = await scriptRuntime.runRequestScript(
|
||||
decomment(requestScript),
|
||||
request,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
|
||||
// interpolate variables inside request
|
||||
interpolateVars(request, envVars, collectionVariables, processEnvVars);
|
||||
|
||||
// todo:
|
||||
// i have no clue why electron can't send the request object
|
||||
@ -737,54 +750,18 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
}
|
||||
}
|
||||
|
||||
// run post-response vars
|
||||
const postResponseVars = get(request, 'vars.res', []);
|
||||
if (postResponseVars?.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPostResponseVars(
|
||||
postResponseVars,
|
||||
request,
|
||||
response,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
processEnvVars
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run response script
|
||||
const responseScript = compact([
|
||||
get(collectionRoot, 'request.script.res'),
|
||||
get(request, 'script.res')
|
||||
]).join(os.EOL);
|
||||
if (responseScript && responseScript.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const result = await scriptRuntime.runResponseScript(
|
||||
decomment(responseScript),
|
||||
request,
|
||||
response,
|
||||
envVars,
|
||||
collectionVariables,
|
||||
collectionPath,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
await runPostResponse(
|
||||
request,
|
||||
response,
|
||||
requestUid,
|
||||
envVars,
|
||||
collectionPath,
|
||||
collectionRoot,
|
||||
collectionUid,
|
||||
collectionVariables,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
||||
// run assertions
|
||||
const assertions = get(item, 'request.assertions');
|
||||
|
@ -137,6 +137,15 @@ const prepareRequest = (request, collectionRoot) => {
|
||||
each(enabledParams, (p) => (params[p.name] = p.value));
|
||||
axiosRequest.headers['content-type'] = 'multipart/form-data';
|
||||
axiosRequest.data = params;
|
||||
|
||||
// make axios work in node using form data
|
||||
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
||||
const form = new FormData();
|
||||
forOwn(axiosRequest.data, (value, key) => {
|
||||
form.append(key, value);
|
||||
});
|
||||
extend(axiosRequest.headers, form.getHeaders());
|
||||
axiosRequest.data = form;
|
||||
}
|
||||
|
||||
if (request.body.mode === 'graphql') {
|
||||
|
Loading…
Reference in New Issue
Block a user