From 60f6a31a95cb55a2bbabff79c6de2d7396036bc4 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 26 Jun 2018 17:26:44 -0400 Subject: [PATCH] Add folder index to request tag (Closes #1011) --- .../app/templating/base-extension.js | 13 ++++++++-- .../ui/components/templating/tag-editor.js | 6 ++++- plugins/insomnia-plugin-request/index.js | 25 +++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/packages/insomnia-app/app/templating/base-extension.js b/packages/insomnia-app/app/templating/base-extension.js index f579c89a11..a5763693b8 100644 --- a/packages/insomnia-app/app/templating/base-extension.js +++ b/packages/insomnia-app/app/templating/base-extension.js @@ -1,6 +1,7 @@ import * as models from '../models/index'; import * as templating from './index'; import * as pluginContexts from '../plugins/context'; +import * as db from '../common/database'; const EMPTY_ARG = '__EMPTY_NUNJUCKS_ARG__'; @@ -81,8 +82,16 @@ export default class BaseExtension { util: { render: str => templating.render(str, { context: renderContext }), models: { - request: { getById: models.request.getById }, - requestGroup: { getById: models.requestGroup.getById }, + request: { + getById: models.request.getById, + getAncestors: async request => { + const ancestors = await db.withAncestors(request, [ + models.requestGroup.type, + models.workspace.type + ]); + return ancestors.filter(doc => doc._id !== request._id); + } + }, workspace: { getById: models.workspace.getById }, oAuth2Token: { getByRequestId: models.oAuth2Token.getByParentId }, cookieJar: { diff --git a/packages/insomnia-app/app/ui/components/templating/tag-editor.js b/packages/insomnia-app/app/ui/components/templating/tag-editor.js index 94f78528d9..0b08bb9fdd 100644 --- a/packages/insomnia-app/app/ui/components/templating/tag-editor.js +++ b/packages/insomnia-app/app/ui/components/templating/tag-editor.js @@ -552,7 +552,11 @@ class TagEditor extends React.PureComponent { } const help = - typeof argDefinition.help === 'string' ? argDefinition.help : ''; + typeof argDefinition.help === 'string' || + typeof argDefinition.help === 'function' + ? fnOrString(argDefinition.help, argDatas) + : ''; + const displayName = typeof argDefinition.displayName === 'string' || typeof argDefinition.displayName === 'function' diff --git a/plugins/insomnia-plugin-request/index.js b/plugins/insomnia-plugin-request/index.js index a215aee30f..9437818c4d 100644 --- a/plugins/insomnia-plugin-request/index.js +++ b/plugins/insomnia-plugin-request/index.js @@ -23,7 +23,7 @@ module.exports.templateTags = [ { displayName: 'Folder', value: 'folder', - description: 'name of immediate parent folder (or workspace)' + description: 'name of parent folder (or workspace)' }, { displayName: 'URL', @@ -61,10 +61,17 @@ module.exports.templateTags = [ return 'Name'; } } + }, + { + hide: args => args[0].value !== 'folder', + displayName: 'Parent Index', + help: + 'Specify an index (Starting at 0) for how high up the folder tree to look', + type: 'number' } ], - async run(context, attribute, name) { + async run(context, attribute, name, folderIndex) { const { meta } = context; if (!meta.requestId || !meta.workspaceId) { @@ -131,10 +138,18 @@ module.exports.templateTags = [ case 'name': return request.name; case 'folder': - const requestGroup = await context.util.models.requestGroup.getById( - request.parentId + const ancestors = await context.util.models.request.getAncestors( + request ); - return requestGroup ? requestGroup.name : workspace.name; + const doc = ancestors[folderIndex || 0]; + if (!doc) { + throw new Error( + `Could not get folder by index ${folderIndex}. Must be between 0-${ + ancestors.length + }` + ); + } + return doc ? doc.name : null; } return null;