insomnia/packages/insomnia-app/app/models/request.ts

488 lines
12 KiB
TypeScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
import type { BaseModel } from './index';
import {
AUTH_ASAP,
AUTH_AWS_IAM,
AUTH_BASIC,
AUTH_DIGEST,
AUTH_HAWK,
AUTH_NETRC,
AUTH_NONE,
AUTH_NTLM,
AUTH_OAUTH_1,
AUTH_OAUTH_2,
CONTENT_TYPE_FILE,
CONTENT_TYPE_FORM_DATA,
CONTENT_TYPE_FORM_URLENCODED,
CONTENT_TYPE_GRAPHQL,
CONTENT_TYPE_JSON,
CONTENT_TYPE_OTHER,
getContentTypeFromHeaders,
HAWK_ALGORITHM_SHA256,
METHOD_GET,
METHOD_POST,
2018-06-25 17:42:50 +00:00
} from '../common/constants';
import { database as db } from '../common/database';
2018-06-25 17:42:50 +00:00
import { getContentTypeHeader } from '../common/misc';
2018-11-06 02:48:00 +00:00
import { deconstructQueryStringToParams } from 'insomnia-url';
2018-06-25 17:42:50 +00:00
import { GRANT_TYPE_AUTHORIZATION_CODE } from '../network/o-auth-2/constants';
import { SIGNATURE_METHOD_HMAC_SHA1 } from '../network/o-auth-1/constants';
export const name = 'Request';
export const type = 'Request';
export const prefix = 'req';
export const canDuplicate = true;
Version Control (beta) (#1439) * VCS proof of concept underway! * Stuff * Some things * Replace deprecated Electron makeSingleInstance * Rename `window` variables so not to be confused with window object * Don't unnecessarily update request when URL does not change * Regenerate package-lock * Fix tests + ESLint * Publish - insomnia-app@1.0.49 - insomnia-cookies@0.0.12 - insomnia-httpsnippet@1.16.18 - insomnia-importers@2.0.13 - insomnia-libcurl@0.0.23 - insomnia-prettify@0.1.7 - insomnia-url@0.1.6 - insomnia-xpath@1.0.9 - insomnia-plugin-base64@1.0.6 - insomnia-plugin-cookie-jar@1.0.8 - insomnia-plugin-core-themes@1.0.5 - insomnia-plugin-default-headers@1.1.9 - insomnia-plugin-file@1.0.7 - insomnia-plugin-hash@1.0.7 - insomnia-plugin-jsonpath@1.0.12 - insomnia-plugin-now@1.0.11 - insomnia-plugin-os@1.0.13 - insomnia-plugin-prompt@1.1.9 - insomnia-plugin-request@1.0.18 - insomnia-plugin-response@1.0.16 - insomnia-plugin-uuid@1.0.10 * Broken but w/e * Some tweaks * Big refactor. Create local snapshots and push done * POC merging and a lot of improvements * Lots of work done on initial UI/UX * Fix old tests * Atomic writes and size-based batches * Update StageEntry definition once again to be better * Factor out GraphQL query logic * Merge algorithm, history modal, other minor things * Fix test * Merge, checkout, revert w/ user changes now work * Force UI to refresh when switching branches changes active request * Rough draft pull() and some cleanup * E2EE stuff and some refactoring * Add ability to share project with team and fixed tests * VCS now created in root component and better remote project handling * Remove unused definition * Publish - insomnia-account@0.0.2 - insomnia-app@1.1.1 - insomnia-cookies@0.0.14 - insomnia-httpsnippet@1.16.20 - insomnia-importers@2.0.15 - insomnia-libcurl@0.0.25 - insomnia-prettify@0.1.9 - insomnia-sync@0.0.2 - insomnia-url@0.1.8 - insomnia-xpath@1.0.11 - insomnia-plugin-base64@1.0.8 - insomnia-plugin-cookie-jar@1.0.10 - insomnia-plugin-core-themes@1.0.7 - insomnia-plugin-file@1.0.9 - insomnia-plugin-hash@1.0.9 - insomnia-plugin-jsonpath@1.0.14 - insomnia-plugin-now@1.0.13 - insomnia-plugin-os@1.0.15 - insomnia-plugin-prompt@1.1.11 - insomnia-plugin-request@1.0.20 - insomnia-plugin-response@1.0.18 - insomnia-plugin-uuid@1.0.12 * Move some deps around * Fix Flow errors * Update package.json * Fix eslint errors * Fix tests * Update deps * bootstrap insomnia-sync * TRy fixing appveyor * Try something else * Bump lerna * try powershell * Try again * Fix imports * Fixed errors * sync types refactor * Show remote projects in workspace dropdown * Improved pulling of non-local workspaces * Loading indicators and some tweaks * Clean up sync staging modal * Some sync improvements: - No longer store stage - Upgrade Electron - Sync UI/UX improvements * Fix snyc tests * Upgraded deps and hot loader tweaks (it's broken for some reason) * Fix tests * Branches dialog, network refactoring, some tweaks * Fixed merging when other branch is empty * A bunch of small fixes from real testing * Fixed pull merge logic * Fix tests * Some bug fixes * A few small tweaks * Conflict resolution and other improvements * Fix tests * Add revert changes * Deal with duplicate projects per workspace * Some tweaks and accessibility improvements * Tooltip accessibility * Fix API endpoint * Fix tests * Remove jest dep from insomnia-importers
2019-04-18 00:50:03 +00:00
export const canSync = true;
export type RequestAuthentication = Record<string, any>;
export interface RequestHeader {
name: string;
value: string;
description?: string;
disabled?: boolean;
}
export interface RequestParameter {
name: string;
value: string;
disabled?: boolean;
id?: string;
fileName?: string;
}
export interface RequestBodyParameter {
name: string;
value: string;
description?: string;
disabled?: boolean;
multiline?: string;
id?: string;
fileName?: string;
type?: string;
}
2017-07-18 22:10:57 +00:00
export interface RequestBody {
mimeType?: string | null;
text?: string;
fileName?: string;
params?: RequestBodyParameter[];
}
interface BaseRequest {
url: string;
name: string;
description: string;
method: string;
body: RequestBody;
parameters: RequestParameter[];
headers: RequestHeader[];
authentication: RequestAuthentication;
metaSortKey: number;
isPrivate: boolean;
2017-07-18 22:10:57 +00:00
// Settings
settingStoreCookies: boolean;
settingSendCookies: boolean;
settingDisableRenderRequestBody: boolean;
settingEncodeUrl: boolean;
settingRebuildPath: boolean;
settingFollowRedirects: string;
}
2017-07-18 22:10:57 +00:00
2017-07-19 01:55:47 +00:00
export type Request = BaseModel & BaseRequest;
2018-06-25 17:42:50 +00:00
export function init(): BaseRequest {
2016-11-10 01:15:27 +00:00
return {
url: '',
name: 'New Request',
description: '',
method: METHOD_GET,
body: {},
parameters: [],
headers: [],
authentication: {},
metaSortKey: -1 * Date.now(),
isPrivate: false,
// Settings
settingStoreCookies: true,
settingSendCookies: true,
settingDisableRenderRequestBody: false,
settingEncodeUrl: true,
settingRebuildPath: true,
settingFollowRedirects: 'global',
2016-11-10 01:15:27 +00:00
};
}
2018-10-17 16:42:33 +00:00
export function newAuth(type: string, oldAuth: RequestAuthentication = {}): RequestAuthentication {
switch (type) {
// No Auth
case AUTH_NONE:
return {};
// HTTP Basic Authentication
case AUTH_BASIC:
return {
type,
useISO88591: oldAuth.useISO88591 || false,
disabled: oldAuth.disabled || false,
username: oldAuth.username || '',
password: oldAuth.password || '',
};
case AUTH_DIGEST:
case AUTH_NTLM:
return {
type,
disabled: oldAuth.disabled || false,
username: oldAuth.username || '',
password: oldAuth.password || '',
};
2017-11-06 19:26:31 +00:00
case AUTH_OAUTH_1:
return {
type,
disabled: false,
signatureMethod: SIGNATURE_METHOD_HMAC_SHA1,
2017-11-06 21:43:00 +00:00
consumerKey: '',
consumerSecret: '',
tokenKey: '',
tokenSecret: '',
privateKey: '',
2017-11-06 21:43:00 +00:00
version: '1.0',
nonce: '',
timestamp: '',
callback: '',
2017-11-06 19:26:31 +00:00
};
// OAuth 2.0
case AUTH_OAUTH_2:
2017-11-07 18:19:16 +00:00
return {
type,
grantType: GRANT_TYPE_AUTHORIZATION_CODE,
2017-11-07 18:19:16 +00:00
};
// Aws IAM
case AUTH_AWS_IAM:
return {
type,
disabled: oldAuth.disabled || false,
accessKeyId: oldAuth.accessKeyId || '',
secretAccessKey: oldAuth.secretAccessKey || '',
sessionToken: oldAuth.sessionToken || '',
};
2017-11-07 18:19:16 +00:00
// Hawk
case AUTH_HAWK:
2017-11-07 18:19:16 +00:00
return {
type,
algorithm: HAWK_ALGORITHM_SHA256,
2017-11-07 18:19:16 +00:00
};
// Atlassian ASAP
case AUTH_ASAP:
2017-11-07 18:19:16 +00:00
return {
type,
issuer: '',
subject: '',
audience: '',
additionalClaims: '',
2017-11-07 18:19:16 +00:00
keyId: '',
privateKey: '',
2017-11-07 18:19:16 +00:00
};
// Types needing no defaults
2017-11-07 18:19:16 +00:00
case AUTH_NETRC:
default:
return {
type,
};
}
}
2018-06-25 17:42:50 +00:00
export function newBodyNone(): RequestBody {
return {};
}
2018-06-25 17:42:50 +00:00
export function newBodyRaw(rawBody: string, contentType?: string): RequestBody {
if (typeof contentType !== 'string') {
return {
text: rawBody,
};
}
const mimeType = contentType.split(';')[0];
return {
mimeType,
text: rawBody,
};
}
export function newBodyGraphQL(rawBody: string): RequestBody {
try {
// Only strip the newlines if rawBody is a parsable JSON
JSON.parse(rawBody);
return {
mimeType: CONTENT_TYPE_GRAPHQL,
text: rawBody.replace(/\\\\n/g, ''),
};
} catch (e) {
if (e instanceof SyntaxError) {
return {
mimeType: CONTENT_TYPE_GRAPHQL,
text: rawBody,
};
} else {
throw e;
}
}
}
export function newBodyFormUrlEncoded(parameters: RequestBodyParameter[] | null): RequestBody {
return {
mimeType: CONTENT_TYPE_FORM_URLENCODED,
params: parameters || [],
};
}
2018-06-25 17:42:50 +00:00
export function newBodyFile(path: string): RequestBody {
2016-11-22 22:26:52 +00:00
return {
mimeType: CONTENT_TYPE_FILE,
fileName: path,
};
2016-11-22 22:26:52 +00:00
}
export function newBodyForm(parameters: RequestBodyParameter[]): RequestBody {
return {
mimeType: CONTENT_TYPE_FORM_DATA,
params: parameters || [],
};
}
2018-06-25 17:42:50 +00:00
export function migrate(doc: Request): Request {
doc = migrateBody(doc);
doc = migrateWeirdUrls(doc);
doc = migrateAuthType(doc);
return doc;
}
export function create(patch: Partial<Request> = {}) {
2016-09-21 20:32:45 +00:00
if (!patch.parentId) {
2018-10-17 16:42:33 +00:00
throw new Error(`New Requests missing \`parentId\`: ${JSON.stringify(patch)}`);
2016-09-21 20:32:45 +00:00
}
return db.docCreate<Request>(type, patch);
}
2016-09-21 20:32:45 +00:00
2018-06-25 17:42:50 +00:00
export function getById(id: string): Promise<Request | null> {
return db.get(type, id);
}
2016-09-21 20:32:45 +00:00
export function findByParentId(parentId: string) {
return db.find<Request>(type, { parentId: parentId });
}
2016-09-21 20:32:45 +00:00
export function update(request: Request, patch: Partial<Request>) {
return db.docUpdate<Request>(request, patch);
}
2016-09-21 20:32:45 +00:00
2018-06-25 17:42:50 +00:00
export function updateMimeType(
2017-07-18 22:10:57 +00:00
request: Request,
mimeType: string,
doCreate = false,
savedBody: RequestBody = {},
) {
let headers = request.headers ? [...request.headers] : [];
2016-11-10 17:33:28 +00:00
const contentTypeHeader = getContentTypeHeader(headers);
2017-07-25 18:00:30 +00:00
// GraphQL uses JSON content-type
2018-10-17 16:42:33 +00:00
const contentTypeHeaderValue = mimeType === CONTENT_TYPE_GRAPHQL ? CONTENT_TYPE_JSON : mimeType;
2017-07-25 18:00:30 +00:00
// GraphQL must be POST
if (mimeType === CONTENT_TYPE_GRAPHQL) {
request.method = METHOD_POST;
}
// Check if we are converting to/from variants of XML or JSON
let leaveContentTypeAlone = false;
if (contentTypeHeader && mimeType) {
const current = contentTypeHeader.value;
if (current.includes('xml') && mimeType.includes('xml')) {
leaveContentTypeAlone = true;
} else if (current.includes('json') && mimeType.includes('json')) {
leaveContentTypeAlone = true;
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// 1. Update Content-Type header //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
const hasBody = typeof mimeType === 'string';
if (!hasBody) {
headers = headers.filter(h => h !== contentTypeHeader);
} else if (mimeType === CONTENT_TYPE_OTHER) {
// Leave headers alone
} else if (mimeType && contentTypeHeader && !leaveContentTypeAlone) {
2017-07-25 18:00:30 +00:00
contentTypeHeader.value = contentTypeHeaderValue;
} else if (mimeType && !contentTypeHeader) {
headers.push({
name: 'Content-Type',
value: contentTypeHeaderValue,
});
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// 2. Make a new request body //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
let body;
2018-10-17 16:42:33 +00:00
const oldBody = Object.keys(savedBody).length === 0 ? request.body : savedBody;
2017-07-25 18:00:30 +00:00
if (mimeType === CONTENT_TYPE_FORM_URLENCODED) {
// Urlencoded
body = oldBody.params
? newBodyFormUrlEncoded(oldBody.params)
// @ts-expect-error -- TSCONVERSION
: newBodyFormUrlEncoded(deconstructQueryStringToParams(oldBody.text));
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
// Form Data
body = oldBody.params
? newBodyForm(oldBody.params)
// @ts-expect-error -- TSCONVERSION
: newBodyForm(deconstructQueryStringToParams(oldBody.text));
2016-11-22 22:26:52 +00:00
} else if (mimeType === CONTENT_TYPE_FILE) {
// File
body = newBodyFile('');
} else if (mimeType === CONTENT_TYPE_GRAPHQL) {
if (contentTypeHeader) {
contentTypeHeader.value = CONTENT_TYPE_JSON;
}
body = newBodyGraphQL(oldBody.text || '');
} else if (typeof mimeType !== 'string') {
// No body
body = newBodyNone();
} else {
// Raw Content-Type (ex: application/json)
body = newBodyRaw(oldBody.text || '', mimeType);
2016-09-21 20:32:45 +00:00
}
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
// 2. create/update request //
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
if (doCreate) {
const newRequest: Request = Object.assign({}, request, {
headers,
body,
});
2017-07-18 22:10:57 +00:00
return create(newRequest);
} else {
return update(request, {
headers,
body,
});
}
}
2016-09-21 20:32:45 +00:00
export async function duplicate(request: Request, patch: Partial<Request> = {}) {
// Only set name and "(Copy)" if the patch does
// not define it and the request itself has a name.
// Otherwise leave it blank so the request URL can
// fill it in automatically.
if (!patch.name && request.name) {
patch.name = `${request.name} (Copy)`;
}
// Get sort key of next request
const q = {
metaSortKey: {
$gt: request.metaSortKey,
},
};
// @ts-expect-error -- TSCONVERSION appears to be a genuine error
const [nextRequest] = await db.find<Request>(type, q, {
metaSortKey: 1,
});
const nextSortKey = nextRequest ? nextRequest.metaSortKey : request.metaSortKey + 100;
// Calculate new sort key
const sortKeyIncrement = (nextSortKey - request.metaSortKey) / 2;
const metaSortKey = request.metaSortKey + sortKeyIncrement;
return db.duplicate<Request>(request, {
name,
metaSortKey,
...patch,
});
}
2016-09-21 20:32:45 +00:00
export function remove(request: Request) {
2016-09-21 20:32:45 +00:00
return db.remove(request);
}
export async function all() {
return db.all<Request>(type);
}
// ~~~~~~~~~~ //
// Migrations //
// ~~~~~~~~~~ //
/**
* Migrate old body (string) to new body (object)
* @param request
*/
function migrateBody(request: Request) {
2016-12-01 18:48:49 +00:00
if (request.body && typeof request.body === 'object') {
return request;
}
// Second, convert all existing urlencoded bodies to new format
const contentType = getContentTypeFromHeaders(request.headers) || '';
2018-10-17 16:42:33 +00:00
const wasFormUrlEncoded = !!contentType.match(/^application\/x-www-form-urlencoded/i);
if (wasFormUrlEncoded) {
// Convert old-style form-encoded request bodies to new style
2017-07-18 22:10:57 +00:00
const body = typeof request.body === 'string' ? request.body : '';
2018-10-17 16:42:33 +00:00
request.body = newBodyFormUrlEncoded(deconstructQueryStringToParams(body, false));
2016-12-01 18:48:49 +00:00
} else if (!request.body && !contentType) {
request.body = {};
} else {
2017-07-18 22:10:57 +00:00
const body: string = typeof request.body === 'string' ? request.body : '';
request.body = newBodyRaw(body, contentType);
}
return request;
}
/**
* Fix some weird URLs that were caused by an old bug
* @param request
*/
function migrateWeirdUrls(request: Request) {
// Some people seem to have requests with URLs that don't have the indexOf
// function. This should clear that up. This can be removed at a later date.
if (typeof request.url !== 'string') {
request.url = '';
}
return request;
}
/**
* Ensure the request.authentication.type property is added
* @param request
*/
function migrateAuthType(request: Request) {
const isAuthSet = request.authentication && request.authentication.username;
if (isAuthSet && !request.authentication.type) {
request.authentication.type = AUTH_BASIC;
}
return request;
}