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 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
|
// handler for sending http request
|
||||||
ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => {
|
ipcMain.handle('send-http-request', async (event, item, collection, environment, collectionVariables) => {
|
||||||
const collectionUid = collection.uid;
|
const collectionUid = collection.uid;
|
||||||
@ -195,15 +333,6 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const cancelTokenUid = uuid();
|
const cancelTokenUid = uuid();
|
||||||
const requestUid = 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', {
|
mainWindow.webContents.send('main:run-request-event', {
|
||||||
type: 'request-queued',
|
type: 'request-queued',
|
||||||
requestUid,
|
requestUid,
|
||||||
@ -221,75 +350,21 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
||||||
|
|
||||||
try {
|
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();
|
const cancelToken = axios.CancelToken.source();
|
||||||
request.cancelToken = cancelToken.token;
|
request.cancelToken = cancelToken.token;
|
||||||
saveCancelToken(cancelTokenUid, cancelToken);
|
saveCancelToken(cancelTokenUid, cancelToken);
|
||||||
|
|
||||||
// run pre-request vars
|
await runPreRequest(
|
||||||
const preRequestVars = get(request, 'vars.req', []);
|
request,
|
||||||
if (preRequestVars?.length) {
|
requestUid,
|
||||||
const varsRuntime = new VarsRuntime();
|
envVars,
|
||||||
const result = varsRuntime.runPreRequestVars(
|
collectionPath,
|
||||||
preRequestVars,
|
collectionRoot,
|
||||||
request,
|
collectionUid,
|
||||||
envVars,
|
collectionVariables,
|
||||||
collectionVariables,
|
processEnvVars,
|
||||||
collectionPath,
|
scriptingConfig
|
||||||
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
// todo:
|
||||||
// i have no clue why electron can't send the request object
|
// i have no clue why electron can't send the request object
|
||||||
@ -352,55 +427,18 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const { data, dataBuffer } = parseDataFromResponse(response);
|
const { data, dataBuffer } = parseDataFromResponse(response);
|
||||||
response.data = data;
|
response.data = data;
|
||||||
|
|
||||||
// run post-response vars
|
await runPostResponse(
|
||||||
const postResponseVars = get(request, 'vars.res', []);
|
request,
|
||||||
if (postResponseVars?.length) {
|
response,
|
||||||
const varsRuntime = new VarsRuntime();
|
requestUid,
|
||||||
const result = varsRuntime.runPostResponseVars(
|
envVars,
|
||||||
postResponseVars,
|
collectionPath,
|
||||||
request,
|
collectionRoot,
|
||||||
response,
|
collectionUid,
|
||||||
envVars,
|
collectionVariables,
|
||||||
collectionVariables,
|
processEnvVars,
|
||||||
collectionPath,
|
scriptingConfig
|
||||||
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// run assertions
|
// run assertions
|
||||||
const assertions = get(request, 'assertions');
|
const assertions = get(request, 'assertions');
|
||||||
@ -501,8 +539,25 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const processEnvVars = getProcessEnvVars(collection.uid);
|
const requestUid = uuid();
|
||||||
interpolateVars(preparedRequest, envVars, collection.collectionVariables, processEnvVars);
|
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(
|
const axiosInstance = await configureRequest(
|
||||||
collection.uid,
|
collection.uid,
|
||||||
@ -513,6 +568,19 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
);
|
);
|
||||||
const response = await axiosInstance(preparedRequest);
|
const response = await axiosInstance(preparedRequest);
|
||||||
|
|
||||||
|
await runPostResponse(
|
||||||
|
request,
|
||||||
|
response,
|
||||||
|
requestUid,
|
||||||
|
envVars,
|
||||||
|
collectionPath,
|
||||||
|
collectionRoot,
|
||||||
|
collectionUid,
|
||||||
|
collectionVariables,
|
||||||
|
processEnvVars,
|
||||||
|
scriptingConfig
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
@ -543,15 +611,6 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
const scriptingConfig = get(brunoConfig, 'scripts', {});
|
||||||
const collectionRoot = get(collection, 'root', {});
|
const collectionRoot = get(collection, 'root', {});
|
||||||
|
|
||||||
const onConsoleLog = (type, args) => {
|
|
||||||
console[type](...args);
|
|
||||||
|
|
||||||
mainWindow.webContents.send('main:console-log', {
|
|
||||||
type,
|
|
||||||
args
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!folder) {
|
if (!folder) {
|
||||||
folder = collection;
|
folder = collection;
|
||||||
}
|
}
|
||||||
@ -601,67 +660,21 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
|
|
||||||
const _request = item.draft ? item.draft.request : item.request;
|
const _request = item.draft ? item.draft.request : item.request;
|
||||||
const request = prepareRequest(_request, collectionRoot);
|
const request = prepareRequest(_request, collectionRoot);
|
||||||
|
const requestUid = uuid();
|
||||||
const processEnvVars = getProcessEnvVars(collectionUid);
|
const processEnvVars = getProcessEnvVars(collectionUid);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// make axios work in node using form data
|
await runPreRequest(
|
||||||
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
request,
|
||||||
if (request.headers && request.headers['content-type'] === 'multipart/form-data') {
|
requestUid,
|
||||||
const form = new FormData();
|
envVars,
|
||||||
forOwn(request.data, (value, key) => {
|
collectionPath,
|
||||||
form.append(key, value);
|
collectionRoot,
|
||||||
});
|
collectionUid,
|
||||||
extend(request.headers, form.getHeaders());
|
collectionVariables,
|
||||||
request.data = form;
|
processEnvVars,
|
||||||
}
|
scriptingConfig
|
||||||
|
|
||||||
// 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
|
|
||||||
);
|
);
|
||||||
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:
|
// todo:
|
||||||
// i have no clue why electron can't send the request object
|
// i have no clue why electron can't send the request object
|
||||||
@ -737,54 +750,18 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// run post-response vars
|
await runPostResponse(
|
||||||
const postResponseVars = get(request, 'vars.res', []);
|
request,
|
||||||
if (postResponseVars?.length) {
|
response,
|
||||||
const varsRuntime = new VarsRuntime();
|
requestUid,
|
||||||
const result = varsRuntime.runPostResponseVars(
|
envVars,
|
||||||
postResponseVars,
|
collectionPath,
|
||||||
request,
|
collectionRoot,
|
||||||
response,
|
collectionUid,
|
||||||
envVars,
|
collectionVariables,
|
||||||
collectionVariables,
|
processEnvVars,
|
||||||
collectionPath,
|
scriptingConfig
|
||||||
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// run assertions
|
// run assertions
|
||||||
const assertions = get(item, 'request.assertions');
|
const assertions = get(item, 'request.assertions');
|
||||||
|
@ -137,6 +137,15 @@ const prepareRequest = (request, collectionRoot) => {
|
|||||||
each(enabledParams, (p) => (params[p.name] = p.value));
|
each(enabledParams, (p) => (params[p.name] = p.value));
|
||||||
axiosRequest.headers['content-type'] = 'multipart/form-data';
|
axiosRequest.headers['content-type'] = 'multipart/form-data';
|
||||||
axiosRequest.data = params;
|
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') {
|
if (request.body.mode === 'graphql') {
|
||||||
|
Loading…
Reference in New Issue
Block a user