use active env id in response (#5669)

* use active env in response

* fix types
This commit is contained in:
Jack Kavanagh 2023-01-13 17:30:59 +01:00 committed by GitHub
parent 7168d01cf4
commit ca12ad1358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -84,7 +84,7 @@ export async function getSendRequestCallbackMemDb(environmentId: string, memDB:
caCert, caCert,
settings, settings,
); );
const res = await responseTransform(response, renderedRequest, renderResult.context); const res = await responseTransform(response, environmentId, renderedRequest, renderResult.context);
const { statusCode: status, statusMessage, headers: headerArray, elapsedTime: responseTime } = res; const { statusCode: status, statusMessage, headers: headerArray, elapsedTime: responseTime } = res;
const headers = headerArray?.reduce((acc, { name, value }) => ({ ...acc, [name.toLowerCase() || '']: value || '' }), []); const headers = headerArray?.reduce((acc, { name, value }) => ({ ...acc, [name.toLowerCase() || '']: value || '' }), []);
const bodyBuffer = await getBodyBuffer(res) as Buffer; const bodyBuffer = await getBodyBuffer(res) as Buffer;

View File

@ -54,7 +54,8 @@ export async function sendWithSettings(
environment, environment,
settings, settings,
clientCertificates, clientCertificates,
caCert } = await fetchRequestData(requestId); caCert,
activeEnvironmentId } = await fetchRequestData(requestId);
const newRequest: Request = await models.initModel(models.request.type, requestPatch, { const newRequest: Request = await models.initModel(models.request.type, requestPatch, {
_id: request._id + '.other', _id: request._id + '.other',
@ -70,7 +71,7 @@ export async function sendWithSettings(
caCert, caCert,
{ ...settings, validateSSL: settings.validateAuthSSL }, { ...settings, validateSSL: settings.validateAuthSSL },
); );
return responseTransform(response, renderedRequest, renderResult.context); return responseTransform(response, activeEnvironmentId, renderedRequest, renderResult.context);
} }
// used by test feature, inso, and plugin api // used by test feature, inso, and plugin api
@ -86,7 +87,8 @@ export async function send(
environment, environment,
settings, settings,
clientCertificates, clientCertificates,
caCert } = await fetchRequestData(requestId); caCert,
activeEnvironmentId } = await fetchRequestData(requestId);
const renderResult = await tryToInterpolateRequest(request, environment._id, RENDER_PURPOSE_SEND, extraInfo); const renderResult = await tryToInterpolateRequest(request, environment._id, RENDER_PURPOSE_SEND, extraInfo);
const renderedRequest = await tryToTransformRequestWithPlugins(renderResult); const renderedRequest = await tryToTransformRequestWithPlugins(renderResult);
@ -96,7 +98,7 @@ export async function send(
caCert, caCert,
settings, settings,
); );
return responseTransform(response, renderedRequest, renderResult.context); return responseTransform(response, activeEnvironmentId, renderedRequest, renderResult.context);
} }
const fetchRequestData = async (requestId: string) => { const fetchRequestData = async (requestId: string) => {
@ -114,8 +116,9 @@ const fetchRequestData = async (requestId: string) => {
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspace._id); const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspace._id);
// fallback to base environment // fallback to base environment
const environment = workspaceMeta.activeEnvironmentId ? const activeEnvironmentId = workspaceMeta.activeEnvironmentId;
await models.environment.getById(workspaceMeta.activeEnvironmentId) const environment = activeEnvironmentId ?
await models.environment.getById(activeEnvironmentId)
: await models.environment.getOrCreateForParentId(workspace._id); : await models.environment.getOrCreateForParentId(workspace._id);
invariant(environment, 'failed to find environment'); invariant(environment, 'failed to find environment');
@ -124,7 +127,7 @@ const fetchRequestData = async (requestId: string) => {
const clientCertificates = await models.clientCertificate.findByParentId(workspaceId); const clientCertificates = await models.clientCertificate.findByParentId(workspaceId);
const caCert = await models.caCertificate.findByParentId(workspaceId); const caCert = await models.caCertificate.findByParentId(workspaceId);
return { request, environment, settings, clientCertificates, caCert }; return { request, environment, settings, clientCertificates, caCert, activeEnvironmentId };
}; };
export const tryToInterpolateRequest = async (request: Request, environmentId: string, purpose?: RenderPurpose, extraInfo?: ExtraRenderInfo) => { export const tryToInterpolateRequest = async (request: Request, environmentId: string, purpose?: RenderPurpose, extraInfo?: ExtraRenderInfo) => {
@ -220,10 +223,11 @@ export async function sendCurlAndWriteTimeline(
...patch, ...patch,
}; };
} }
export const responseTransform = (patch: ResponsePatch, renderedRequest: RenderedRequest, context: Record<string, any>) => { export const responseTransform = (patch: ResponsePatch, environmentId: string | null, renderedRequest: RenderedRequest, context: Record<string, any>) => {
const response = { const response = {
...patch, ...patch,
environmentId: patch.environmentId, // important for filter by responses
environmentId,
bodyCompression: null, bodyCompression: null,
settingSendCookies: renderedRequest.settingSendCookies, settingSendCookies: renderedRequest.settingSendCookies,
settingStoreCookies: renderedRequest.settingStoreCookies, settingStoreCookies: renderedRequest.settingStoreCookies,