insomnia/plugins/insomnia-plugin-request/index.js

218 lines
6.5 KiB
JavaScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
const {
buildQueryStringFromParams,
joinUrlAndQueryString,
smartEncodeUrl,
2018-06-25 17:42:50 +00:00
} = require('insomnia-url');
const { jarFromCookies } = require('insomnia-cookies');
module.exports.templateTags = [
{
name: 'request',
displayName: 'Request',
description: 'reference value from current request',
args: [
{
displayName: 'Attribute',
type: 'enum',
options: [
{
displayName: 'Name',
value: 'name',
description: 'name of request',
2018-06-25 17:42:50 +00:00
},
{
displayName: 'Folder',
value: 'folder',
description: 'name of parent folder (or workspace)',
2018-06-25 17:42:50 +00:00
},
{
displayName: 'URL',
value: 'url',
description: 'fully qualified URL',
2018-06-25 17:42:50 +00:00
},
{
displayName: 'Query Parameter',
2018-12-12 15:34:28 +00:00
value: 'parameter',
description: 'query parameter by name',
2018-06-25 17:42:50 +00:00
},
{
displayName: 'Header',
value: 'header',
description: 'header value by name',
2018-06-25 17:42:50 +00:00
},
2018-12-12 15:34:28 +00:00
{
displayName: 'Cookie',
value: 'cookie',
description: 'cookie value by name',
2018-12-12 15:34:28 +00:00
},
2018-06-25 17:42:50 +00:00
{
displayName: 'OAuth 2.0 Token',
value: 'oauth2',
description: 'access token',
},
],
2018-06-25 17:42:50 +00:00
},
{
type: 'string',
2018-10-17 16:42:33 +00:00
hide: args => ['url', 'oauth2', 'name', 'folder'].includes(args[0].value),
2018-06-25 17:42:50 +00:00
displayName: args => {
switch (args[0].value) {
case 'cookie':
return 'Cookie Name';
case 'parameter':
return 'Query Parameter Name';
2018-06-25 17:42:50 +00:00
case 'header':
return 'Header Name';
default:
return 'Name';
}
},
},
{
hide: args => args[0].value !== 'folder',
displayName: 'Parent Index',
2018-10-17 16:42:33 +00:00
help: 'Specify an index (Starting at 0) for how high up the folder tree to look',
type: 'number',
},
2018-06-25 17:42:50 +00:00
],
async run(context, attribute, name, folderIndex) {
2018-06-25 17:42:50 +00:00
const { meta } = context;
2018-06-25 17:42:50 +00:00
if (!meta.requestId || !meta.workspaceId) {
return null;
}
2018-06-25 17:42:50 +00:00
const request = await context.util.models.request.getById(meta.requestId);
2018-10-17 16:42:33 +00:00
const workspace = await context.util.models.workspace.getById(meta.workspaceId);
2018-06-25 17:42:50 +00:00
if (!request) {
throw new Error(`Request not found for ${meta.requestId}`);
}
2018-06-25 17:42:50 +00:00
if (!workspace) {
throw new Error(`Workspace not found for ${meta.workspaceId}`);
}
2018-06-25 17:42:50 +00:00
switch (attribute) {
case 'url':
return getRequestUrl(context, request);
case 'cookie':
if (!name) {
throw new Error('No cookie specified');
}
2018-10-17 16:42:33 +00:00
const cookieJar = await context.util.models.cookieJar.getOrCreateForWorkspace(workspace);
2018-06-25 17:42:50 +00:00
const url = await getRequestUrl(context, request);
return getCookieValue(cookieJar, url, name);
case 'parameter':
if (!name) {
throw new Error('No query parameter specified');
}
const parameterNames = [];
if (request.parameters.length === 0) {
throw new Error('No query parameters available');
}
for (const queryParameter of request.parameters) {
const queryParameterName = await context.util.render(queryParameter.name);
parameterNames.push(queryParameterName);
if (queryParameterName.toLowerCase() === name.toLowerCase()) {
return context.util.render(queryParameter.value);
}
}
const parameterNamesStr = parameterNames.map(n => `"${n}"`).join(',\n\t');
throw new Error(
`No query parameter with name "${name}".\nChoices are [\n\t${parameterNamesStr}\n]`,
);
2018-06-25 17:42:50 +00:00
case 'header':
if (!name) {
throw new Error('No header specified');
}
const headerNames = [];
2018-06-25 17:42:50 +00:00
if (request.headers.length === 0) {
throw new Error('No headers available');
2018-06-25 17:42:50 +00:00
}
2018-06-25 17:42:50 +00:00
for (const header of request.headers) {
const headerName = await context.util.render(header.name);
headerNames.push(headerName);
2018-06-25 17:42:50 +00:00
if (headerName.toLowerCase() === name.toLowerCase()) {
return context.util.render(header.value);
}
}
const headerNamesStr = headerNames.map(n => `"${n}"`).join(',\n\t');
throw new Error(`No header with name "${name}".\nChoices are [\n\t${headerNamesStr}\n]`);
2018-06-25 17:42:50 +00:00
case 'oauth2':
2018-10-17 16:42:33 +00:00
const token = await context.util.models.oAuth2Token.getByRequestId(request._id);
2018-06-25 17:42:50 +00:00
if (!token) {
throw new Error('No OAuth 2.0 tokens found for request');
}
return token.accessToken;
case 'name':
return request.name;
case 'folder':
2018-10-17 16:42:33 +00:00
const ancestors = await context.util.models.request.getAncestors(request);
const doc = ancestors[folderIndex || 0];
if (!doc) {
throw new Error(
2018-06-27 05:16:57 +00:00
`Could not get folder by index ${folderIndex}. Must be between 0-${ancestors.length -
1}`,
);
}
return doc ? doc.name : null;
2018-06-25 17:42:50 +00:00
}
2018-06-25 17:42:50 +00:00
return null;
},
},
2018-06-25 17:42:50 +00:00
];
2018-06-25 17:42:50 +00:00
async function getRequestUrl(context, request) {
const url = await context.util.render(request.url);
const parameters = [];
for (const p of request.parameters) {
parameters.push({
name: await context.util.render(p.name),
value: await context.util.render(p.value),
});
}
const qs = buildQueryStringFromParams(parameters);
const finalUrl = joinUrlAndQueryString(url, qs);
return smartEncodeUrl(finalUrl, request.settingEncodeUrl);
}
2018-06-25 17:42:50 +00:00
function getCookieValue(cookieJar, url, name) {
return new Promise((resolve, reject) => {
const jar = jarFromCookies(cookieJar.cookies);
jar.getCookies(url, {}, (err, cookies) => {
if (err) {
console.warn(`Failed to find cookie for ${url}`, err);
}
if (!cookies || cookies.length === 0) {
reject(new Error(`No cookies in store for url "${url}"`));
}
const cookie = cookies.find(cookie => cookie.key === name);
if (!cookie) {
const names = cookies.map(c => `"${c.key}"`).join(',\n\t');
2018-06-25 17:42:50 +00:00
throw new Error(
`No cookie with name "${name}".\nChoices are [\n\t${names}\n] for url "${url}"`,
2018-06-25 17:42:50 +00:00
);
} else {
resolve(cookie ? cookie.value : null);
}
});
});
}