MCPcopy Create free account
hub / github.com/Kong/insomnia / tryToExecuteAfterResponseScript

Function tryToExecuteAfterResponseScript

packages/insomnia/src/network/network.ts:788–850  ·  view source on GitHub ↗
(context: RequestAndContextAndResponse)

Source from the content-addressed store, hash-verified

786};
787
788export async function tryToExecuteAfterResponseScript(context: RequestAndContextAndResponse) {
789 const ancestorsWithScripts = context.ancestors.filter(
790 doc => isRequest(doc) || isRequestGroup(doc) || isWorkspace(doc),
791 );
792 // Reverse the order so that workspace script runs first, then folder scripts from furthest to nearest, and finally request scripts
793 const ancestorScripts = ancestorsWithScripts
794 .reverse()
795 .filter(group => group?.afterResponseScript)
796 .map(
797 (group, i) => `const fn${i} = async ()=>{
798 ${group.afterResponseScript}
799 }
800 await fn${i}();
801 `,
802 );
803 const originalRequestGroups = ancestorsWithScripts.filter(group => isRequestGroup(group));
804
805 if (ancestorScripts.length === 0) {
806 return {
807 ...context,
808 requestTestResults: new Array<RequestTestResult>(),
809 };
810 }
811 const joinedScript = [...ancestorScripts].join('\n');
812 const postMutatedContext = await tryToExecuteScript({ script: joinedScript, ...context, eventName: 'test' });
813 if (!postMutatedContext || 'error' in postMutatedContext) {
814 return {
815 error: `Execute after-response script failed: ${postMutatedContext?.error}`,
816 ...context,
817 };
818 }
819
820 // cookies from response should also be persisted
821 const respondedWithoutError = context.response && !('error' in context.response);
822 if (respondedWithoutError) {
823 const resp = context.response as sendCurlAndWriteTimelineResponse;
824 await savePatchesMadeByScript({
825 mutatedContext: postMutatedContext,
826 environment: context.environment,
827 baseEnvironment: context.baseEnvironment,
828 activeGlobalEnvironment: context.globals,
829 activeGlobalBaseEnvironment: context.baseGlobals,
830 originalRequestGroups: originalRequestGroups,
831 responseCookies: resp.cookies,
832 });
833 } else {
834 await savePatchesMadeByScript({
835 mutatedContext: postMutatedContext,
836 environment: context.environment,
837 baseEnvironment: context.baseEnvironment,
838 activeGlobalEnvironment: context.globals,
839 activeGlobalBaseEnvironment: context.baseGlobals,
840 originalRequestGroups: originalRequestGroups,
841 });
842 }
843
844 const postTestResults: RequestTestResult[] = (postMutatedContext?.requestTestResults || []).map(result => ({
845 ...result,

Callers 3

runAfterResponseFunction · 0.90
sendActionImplementationFunction · 0.90

Calls 7

isRequestFunction · 0.85
isRequestGroupFunction · 0.85
isWorkspaceFunction · 0.85
tryToExecuteScriptFunction · 0.85
savePatchesMadeByScriptFunction · 0.85
filterMethod · 0.80
mapMethod · 0.80

Tested by 1

runAfterResponseFunction · 0.72