mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
Ability to resend Response Tag's dependent request (#1470)
This commit is contained in:
parent
d67bd50806
commit
a7c2fd01fc
@ -193,7 +193,7 @@ export async function getRenderContext(
|
|||||||
request: Request,
|
request: Request,
|
||||||
environmentId: string,
|
environmentId: string,
|
||||||
ancestors: Array<BaseModel> | null = null,
|
ancestors: Array<BaseModel> | null = null,
|
||||||
purpose: string | null = null,
|
purpose: RenderPurpose | null = null,
|
||||||
): Promise<Object> {
|
): Promise<Object> {
|
||||||
if (!ancestors) {
|
if (!ancestors) {
|
||||||
ancestors = await db.withAncestors(request, [
|
ancestors = await db.withAncestors(request, [
|
||||||
@ -218,7 +218,7 @@ export async function getRenderContext(
|
|||||||
keySource[key] = 'root';
|
keySource[key] = 'root';
|
||||||
}
|
}
|
||||||
if (subEnvironment) {
|
if (subEnvironment) {
|
||||||
for (let key in subEnvironment.data || {}) {
|
for (const key of Object.keys(subEnvironment.data || {})) {
|
||||||
if (subEnvironment.name) {
|
if (subEnvironment.name) {
|
||||||
keySource[key] = subEnvironment.name;
|
keySource[key] = subEnvironment.name;
|
||||||
}
|
}
|
||||||
@ -232,7 +232,7 @@ export async function getRenderContext(
|
|||||||
ancestor.hasOwnProperty('environment') &&
|
ancestor.hasOwnProperty('environment') &&
|
||||||
ancestor.hasOwnProperty('name')
|
ancestor.hasOwnProperty('name')
|
||||||
) {
|
) {
|
||||||
for (let key in ancestor.environment || {}) {
|
for (const key of Object.keys(ancestor.environment || {})) {
|
||||||
keySource[key] = ancestor.name || '';
|
keySource[key] = ancestor.name || '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -252,6 +252,8 @@ export async function getRenderContext(
|
|||||||
|
|
||||||
baseContext.getPurpose = () => purpose;
|
baseContext.getPurpose = () => purpose;
|
||||||
|
|
||||||
|
baseContext.getEnvironmentId = () => environmentId;
|
||||||
|
|
||||||
// Generate the context we need to render
|
// Generate the context we need to render
|
||||||
return buildRenderContext(ancestors, rootEnvironment, subEnvironment, baseContext);
|
return buildRenderContext(ancestors, rootEnvironment, subEnvironment, baseContext);
|
||||||
}
|
}
|
||||||
@ -259,7 +261,7 @@ export async function getRenderContext(
|
|||||||
export async function getRenderedRequestAndContext(
|
export async function getRenderedRequestAndContext(
|
||||||
request: Request,
|
request: Request,
|
||||||
environmentId: string,
|
environmentId: string,
|
||||||
purpose?: string,
|
purpose?: RenderPurpose,
|
||||||
): Promise<{ request: RenderedRequest, context: Object }> {
|
): Promise<{ request: RenderedRequest, context: Object }> {
|
||||||
const ancestors = await db.withAncestors(request, [
|
const ancestors = await db.withAncestors(request, [
|
||||||
models.request.type,
|
models.request.type,
|
||||||
@ -315,7 +317,6 @@ export async function getRenderedRequestAndContext(
|
|||||||
context: renderContext,
|
context: renderContext,
|
||||||
request: {
|
request: {
|
||||||
// Add the yummy cookies
|
// Add the yummy cookies
|
||||||
// TODO: Eventually get rid of RenderedRequest type and put these elsewhere
|
|
||||||
cookieJar: renderedCookieJar,
|
cookieJar: renderedCookieJar,
|
||||||
cookies: [],
|
cookies: [],
|
||||||
isPrivate: false,
|
isPrivate: false,
|
||||||
@ -349,7 +350,7 @@ export async function getRenderedRequestAndContext(
|
|||||||
export async function getRenderedRequest(
|
export async function getRenderedRequest(
|
||||||
request: Request,
|
request: Request,
|
||||||
environmentId: string,
|
environmentId: string,
|
||||||
purpose?: string,
|
purpose?: RenderPurpose,
|
||||||
): Promise<RenderedRequest> {
|
): Promise<RenderedRequest> {
|
||||||
const result = await getRenderedRequestAndContext(request, environmentId, purpose);
|
const result = await getRenderedRequestAndContext(request, environmentId, purpose);
|
||||||
return result.request;
|
return result.request;
|
||||||
|
@ -3,8 +3,10 @@ import * as _app from './app';
|
|||||||
import * as _store from './store';
|
import * as _store from './store';
|
||||||
import * as _request from './request';
|
import * as _request from './request';
|
||||||
import * as _response from './response';
|
import * as _response from './response';
|
||||||
|
import * as _network from './network';
|
||||||
|
|
||||||
export const app = _app;
|
export const app = _app;
|
||||||
export const store = _store;
|
export const store = _store;
|
||||||
export const request = _request;
|
export const request = _request;
|
||||||
export const response = _response;
|
export const response = _response;
|
||||||
|
export const network = _network;
|
||||||
|
17
packages/insomnia-app/app/plugins/context/network.js
Normal file
17
packages/insomnia-app/app/plugins/context/network.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import { send } from '../../network/network';
|
||||||
|
import type { Request } from '../../models/request';
|
||||||
|
import * as models from '../../models';
|
||||||
|
|
||||||
|
export function init(activeEnvironmentId: string): { network: Object } {
|
||||||
|
const network = {
|
||||||
|
async sendRequest(request: Request): Promise<Response> {
|
||||||
|
const responsePatch = await send(request._id, activeEnvironmentId);
|
||||||
|
const settings = await models.settings.getOrCreate();
|
||||||
|
return models.response.create(responsePatch, settings.maxHistoryResponses);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return { network };
|
||||||
|
}
|
@ -70,6 +70,9 @@ export default class BaseExtension {
|
|||||||
// Pull out the purpose
|
// Pull out the purpose
|
||||||
const renderPurpose = renderContext.getPurpose ? renderContext.getPurpose() : null;
|
const renderPurpose = renderContext.getPurpose ? renderContext.getPurpose() : null;
|
||||||
|
|
||||||
|
// Pull out the environment ID
|
||||||
|
const environmentId = renderContext.getEnvironmentId ? renderContext.getEnvironmentId() : 'n/a';
|
||||||
|
|
||||||
// Extract the rest of the args
|
// Extract the rest of the args
|
||||||
const args = runArgs.slice(0, runArgs.length - 1).filter(a => a !== EMPTY_ARG);
|
const args = runArgs.slice(0, runArgs.length - 1).filter(a => a !== EMPTY_ARG);
|
||||||
|
|
||||||
@ -77,8 +80,10 @@ export default class BaseExtension {
|
|||||||
const helperContext = {
|
const helperContext = {
|
||||||
...pluginContexts.app.init(renderPurpose),
|
...pluginContexts.app.init(renderPurpose),
|
||||||
...pluginContexts.store.init(this._plugin),
|
...pluginContexts.store.init(this._plugin),
|
||||||
|
...pluginContexts.network.init(environmentId),
|
||||||
context: renderContext,
|
context: renderContext,
|
||||||
meta: renderMeta,
|
meta: renderMeta,
|
||||||
|
renderPurpose,
|
||||||
util: {
|
util: {
|
||||||
render: str => templating.render(str, { context: renderContext }),
|
render: str => templating.render(str, { context: renderContext }),
|
||||||
models: {
|
models: {
|
||||||
|
@ -48,9 +48,31 @@ module.exports.templateTags = [
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Trigger Behavior',
|
||||||
|
help: 'Configure when to resend the dependent request',
|
||||||
|
type: 'enum',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
displayName: 'Always',
|
||||||
|
description: 'resend request when needed',
|
||||||
|
value: 'ALWAYS',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Sometimes',
|
||||||
|
description: 'resend when no response exists yet',
|
||||||
|
value: 'NO_HISTORY',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Never',
|
||||||
|
description: 'never resend request',
|
||||||
|
value: 'NEVER',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
async run(context, field, id, filter) {
|
async run(context, field, id, filter, resendBehavior) {
|
||||||
filter = filter || '';
|
filter = filter || '';
|
||||||
|
|
||||||
if (!['body', 'header', 'raw'].includes(field)) {
|
if (!['body', 'header', 'raw'].includes(field)) {
|
||||||
@ -66,7 +88,22 @@ module.exports.templateTags = [
|
|||||||
throw new Error(`Could not find request ${id}`);
|
throw new Error(`Could not find request ${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await context.util.models.response.getLatestForRequestId(id);
|
let response = await context.util.models.response.getLatestForRequestId(id);
|
||||||
|
|
||||||
|
let shouldResend = false;
|
||||||
|
if (resendBehavior === 'ALWAYS') {
|
||||||
|
shouldResend = true;
|
||||||
|
} else if (resendBehavior === 'NO_HISTORY' && !response) {
|
||||||
|
shouldResend = true;
|
||||||
|
} else if (resendBehavior === 'NEVER') {
|
||||||
|
shouldResend = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resend dependent request if needed but only if we're rendering for a send
|
||||||
|
if (shouldResend && (!response || context.renderPurpose === 'send')) {
|
||||||
|
console.log('[response tag] Resending dependency');
|
||||||
|
response = await context.network.sendRequest(request);
|
||||||
|
}
|
||||||
|
|
||||||
if (!response) {
|
if (!response) {
|
||||||
throw new Error('No responses for request');
|
throw new Error('No responses for request');
|
||||||
|
Loading…
Reference in New Issue
Block a user