2021-07-22 23:04:56 +00:00
|
|
|
import { deconstructQueryStringToParams } from 'insomnia-url';
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
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,
|
2018-12-12 17:36:11 +00:00
|
|
|
METHOD_POST,
|
2018-06-25 17:42:50 +00:00
|
|
|
} from '../common/constants';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { getContentTypeHeader } from '../common/misc';
|
|
|
|
import { SIGNATURE_METHOD_HMAC_SHA1 } from '../network/o-auth-1/constants';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { GRANT_TYPE_AUTHORIZATION_CODE } from '../network/o-auth-2/constants';
|
|
|
|
import type { BaseModel } from './index';
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Request';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Request';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const prefix = 'req';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = true;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = true;
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
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
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface RequestBody {
|
|
|
|
mimeType?: string | null;
|
|
|
|
text?: string;
|
|
|
|
fileName?: string;
|
2021-05-18 20:32:18 +00:00
|
|
|
params?: RequestBodyParameter[];
|
2021-05-12 06:35:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 04:31:08 +00:00
|
|
|
export interface BaseRequest {
|
2021-05-12 06:35:00 +00:00
|
|
|
url: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
method: string;
|
|
|
|
body: RequestBody;
|
2021-05-18 20:32:18 +00:00
|
|
|
parameters: RequestParameter[];
|
|
|
|
headers: RequestHeader[];
|
2021-05-12 06:35:00 +00:00
|
|
|
authentication: RequestAuthentication;
|
|
|
|
metaSortKey: number;
|
|
|
|
isPrivate: boolean;
|
2017-07-18 22:10:57 +00:00
|
|
|
// Settings
|
2021-05-12 06:35:00 +00:00
|
|
|
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;
|
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isRequest = (model: Pick<BaseModel, 'type'>): model is Request => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init(): BaseRequest {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2016-10-02 20:57:00 +00:00
|
|
|
url: '',
|
|
|
|
name: 'New Request',
|
2017-06-01 13:54:46 +00:00
|
|
|
description: '',
|
2016-10-02 20:57:00 +00:00
|
|
|
method: METHOD_GET,
|
2016-11-22 19:42:10 +00:00
|
|
|
body: {},
|
2016-10-02 20:57:00 +00:00
|
|
|
parameters: [],
|
|
|
|
headers: [],
|
|
|
|
authentication: {},
|
2017-03-28 22:45:23 +00:00
|
|
|
metaSortKey: -1 * Date.now(),
|
2018-03-06 03:05:57 +00:00
|
|
|
isPrivate: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
// Settings
|
|
|
|
settingStoreCookies: true,
|
|
|
|
settingSendCookies: true,
|
2017-03-29 23:09:28 +00:00
|
|
|
settingDisableRenderRequestBody: false,
|
2018-03-26 17:43:42 +00:00
|
|
|
settingEncodeUrl: true,
|
2018-06-06 20:48:14 +00:00
|
|
|
settingRebuildPath: true,
|
2019-10-31 16:02:03 +00:00
|
|
|
settingFollowRedirects: 'global',
|
2016-11-10 01:15:27 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
export function newAuth(type: string, oldAuth: RequestAuthentication = {}): RequestAuthentication {
|
2017-03-23 22:10:42 +00:00
|
|
|
switch (type) {
|
2017-03-28 22:45:23 +00:00
|
|
|
// No Auth
|
|
|
|
case AUTH_NONE:
|
|
|
|
return {};
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// HTTP Basic Authentication
|
|
|
|
case AUTH_BASIC:
|
2020-09-24 20:58:28 +00:00
|
|
|
return {
|
|
|
|
type,
|
|
|
|
useISO88591: oldAuth.useISO88591 || false,
|
|
|
|
disabled: oldAuth.disabled || false,
|
|
|
|
username: oldAuth.username || '',
|
|
|
|
password: oldAuth.password || '',
|
|
|
|
};
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
case AUTH_DIGEST:
|
|
|
|
case AUTH_NTLM:
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
disabled: oldAuth.disabled || false,
|
|
|
|
username: oldAuth.username || '',
|
2018-12-12 17:36:11 +00:00
|
|
|
password: oldAuth.password || '',
|
2017-03-28 22:45:23 +00:00
|
|
|
};
|
2017-03-23 22:10:42 +00:00
|
|
|
|
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: '',
|
2018-01-16 06:06:26 +00:00
|
|
|
privateKey: '',
|
2017-11-06 21:43:00 +00:00
|
|
|
version: '1.0',
|
|
|
|
nonce: '',
|
|
|
|
timestamp: '',
|
2018-12-12 17:36:11 +00:00
|
|
|
callback: '',
|
2017-11-06 19:26:31 +00:00
|
|
|
};
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// OAuth 2.0
|
|
|
|
case AUTH_OAUTH_2:
|
2017-11-07 18:19:16 +00:00
|
|
|
return {
|
|
|
|
type,
|
2018-12-12 17:36:11 +00:00
|
|
|
grantType: GRANT_TYPE_AUTHORIZATION_CODE,
|
2017-11-07 18:19:16 +00:00
|
|
|
};
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2017-07-31 17:29:36 +00:00
|
|
|
// Aws IAM
|
2017-07-12 21:01:14 +00:00
|
|
|
case AUTH_AWS_IAM:
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
disabled: oldAuth.disabled || false,
|
|
|
|
accessKeyId: oldAuth.accessKeyId || '',
|
2018-01-24 17:51:13 +00:00
|
|
|
secretAccessKey: oldAuth.secretAccessKey || '',
|
2018-12-12 17:36:11 +00:00
|
|
|
sessionToken: oldAuth.sessionToken || '',
|
2017-07-12 21:01:14 +00:00
|
|
|
};
|
|
|
|
|
2017-11-07 18:19:16 +00:00
|
|
|
// Hawk
|
2017-08-21 17:43:12 +00:00
|
|
|
case AUTH_HAWK:
|
2017-11-07 18:19:16 +00:00
|
|
|
return {
|
|
|
|
type,
|
2018-12-12 17:36:11 +00:00
|
|
|
algorithm: HAWK_ALGORITHM_SHA256,
|
2017-11-07 18:19:16 +00:00
|
|
|
};
|
2017-08-21 17:43:12 +00:00
|
|
|
|
2017-11-07 18:14:08 +00:00
|
|
|
// Atlassian ASAP
|
|
|
|
case AUTH_ASAP:
|
2017-11-07 18:19:16 +00:00
|
|
|
return {
|
|
|
|
type,
|
|
|
|
issuer: '',
|
|
|
|
subject: '',
|
|
|
|
audience: '',
|
2018-03-28 23:27:21 +00:00
|
|
|
additionalClaims: '',
|
2017-11-07 18:19:16 +00:00
|
|
|
keyId: '',
|
2018-12-12 17:36:11 +00:00
|
|
|
privateKey: '',
|
2017-11-07 18:19:16 +00:00
|
|
|
};
|
2017-11-07 18:14:08 +00:00
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
// Types needing no defaults
|
2017-11-07 18:19:16 +00:00
|
|
|
case AUTH_NETRC:
|
2017-03-23 22:10:42 +00:00
|
|
|
default:
|
2021-05-12 06:35:00 +00:00
|
|
|
return {
|
|
|
|
type,
|
|
|
|
};
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function newBodyNone(): RequestBody {
|
2017-04-11 21:20:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function newBodyRaw(rawBody: string, contentType?: string): RequestBody {
|
2016-11-25 19:01:41 +00:00
|
|
|
if (typeof contentType !== 'string') {
|
2021-05-12 06:35:00 +00:00
|
|
|
return {
|
|
|
|
text: rawBody,
|
|
|
|
};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mimeType = contentType.split(';')[0];
|
2021-05-12 06:35:00 +00:00
|
|
|
return {
|
|
|
|
mimeType,
|
|
|
|
text: rawBody,
|
|
|
|
};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:00:53 +00:00
|
|
|
export function newBodyGraphQL(rawBody: string): RequestBody {
|
|
|
|
try {
|
|
|
|
// Only strip the newlines if rawBody is a parsable JSON
|
|
|
|
JSON.parse(rawBody);
|
2021-05-12 06:35:00 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_GRAPHQL,
|
|
|
|
text: rawBody.replace(/\\\\n/g, ''),
|
|
|
|
};
|
2021-02-09 01:00:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof SyntaxError) {
|
2021-05-12 06:35:00 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_GRAPHQL,
|
|
|
|
text: rawBody,
|
|
|
|
};
|
2021-02-09 01:00:53 +00:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function newBodyFormUrlEncoded(parameters: RequestBodyParameter[] | null): RequestBody {
|
2016-11-22 19:42:10 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_URLENCODED,
|
2018-12-12 17:36:11 +00:00
|
|
|
params: parameters || [],
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2018-12-12 17:36:11 +00:00
|
|
|
fileName: path,
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 20:32:18 +00:00
|
|
|
export function newBodyForm(parameters: RequestBodyParameter[]): RequestBody {
|
2016-11-22 19:42:10 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_DATA,
|
2018-12-12 17:36:11 +00:00
|
|
|
params: parameters || [],
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function migrate(doc: Request): Request {
|
2016-12-08 20:29:40 +00:00
|
|
|
doc = migrateBody(doc);
|
|
|
|
doc = migrateWeirdUrls(doc);
|
2017-03-23 22:10:42 +00:00
|
|
|
doc = migrateAuthType(doc);
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<Request>(type, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getById(id: string): Promise<Request | null> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.get(type, id);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function findByParentId(parentId: string) {
|
|
|
|
return db.find<Request>(type, { parentId: parentId });
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function update(request: Request, patch: Partial<Request>) {
|
|
|
|
return db.docUpdate<Request>(request, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
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,
|
2021-05-12 06:35:00 +00:00
|
|
|
doCreate = false,
|
2018-12-12 17:36:11 +00:00
|
|
|
savedBody: RequestBody = {},
|
2021-05-12 06:35:00 +00:00
|
|
|
) {
|
2016-11-27 21:42:38 +00:00
|
|
|
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
|
|
|
|
2017-08-10 19:50:37 +00:00
|
|
|
// GraphQL must be POST
|
|
|
|
if (mimeType === CONTENT_TYPE_GRAPHQL) {
|
|
|
|
request.method = METHOD_POST;
|
|
|
|
}
|
|
|
|
|
2017-04-20 17:18:00 +00:00
|
|
|
// Check if we are converting to/from variants of XML or JSON
|
|
|
|
let leaveContentTypeAlone = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-04-20 17:18:00 +00:00
|
|
|
if (contentTypeHeader && mimeType) {
|
|
|
|
const current = contentTypeHeader.value;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-04-20 17:18:00 +00:00
|
|
|
if (current.includes('xml') && mimeType.includes('xml')) {
|
|
|
|
leaveContentTypeAlone = true;
|
|
|
|
} else if (current.includes('json') && mimeType.includes('json')) {
|
|
|
|
leaveContentTypeAlone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 1. Update Content-Type header //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2017-04-20 01:37:40 +00:00
|
|
|
const hasBody = typeof mimeType === 'string';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2018-10-20 14:24:27 +00:00
|
|
|
if (!hasBody) {
|
|
|
|
headers = headers.filter(h => h !== contentTypeHeader);
|
|
|
|
} else if (mimeType === CONTENT_TYPE_OTHER) {
|
2017-12-12 19:05:45 +00:00
|
|
|
// Leave headers alone
|
2017-04-20 17:18:00 +00:00
|
|
|
} else if (mimeType && contentTypeHeader && !leaveContentTypeAlone) {
|
2017-07-25 18:00:30 +00:00
|
|
|
contentTypeHeader.value = contentTypeHeaderValue;
|
2017-04-20 01:37:40 +00:00
|
|
|
} else if (mimeType && !contentTypeHeader) {
|
2021-05-12 06:35:00 +00:00
|
|
|
headers.push({
|
|
|
|
name: 'Content-Type',
|
|
|
|
value: contentTypeHeaderValue,
|
|
|
|
});
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 2. Make a new request body //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2016-11-23 19:33:24 +00:00
|
|
|
let body;
|
2018-10-17 16:42:33 +00:00
|
|
|
const oldBody = Object.keys(savedBody).length === 0 ? request.body : savedBody;
|
2018-02-16 16:24:54 +00:00
|
|
|
|
2017-07-25 18:00:30 +00:00
|
|
|
if (mimeType === CONTENT_TYPE_FORM_URLENCODED) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Urlencoded
|
2018-02-16 16:24:54 +00:00
|
|
|
body = oldBody.params
|
|
|
|
? newBodyFormUrlEncoded(oldBody.params)
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2018-02-16 16:24:54 +00:00
|
|
|
: newBodyFormUrlEncoded(deconstructQueryStringToParams(oldBody.text));
|
2021-05-27 18:00:32 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
|
|
|
|
// Form Data
|
|
|
|
body = oldBody.params
|
2018-02-16 16:24:54 +00:00
|
|
|
? newBodyForm(oldBody.params)
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2018-02-16 16:24:54 +00:00
|
|
|
: newBodyForm(deconstructQueryStringToParams(oldBody.text));
|
2016-11-22 22:26:52 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FILE) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// File
|
2016-11-23 19:33:24 +00:00
|
|
|
body = newBodyFile('');
|
2017-07-25 04:15:24 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_GRAPHQL) {
|
|
|
|
if (contentTypeHeader) {
|
|
|
|
contentTypeHeader.value = CONTENT_TYPE_JSON;
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-02-09 01:00:53 +00:00
|
|
|
body = newBodyGraphQL(oldBody.text || '');
|
2016-12-21 23:37:48 +00:00
|
|
|
} else if (typeof mimeType !== 'string') {
|
2017-01-27 01:00:27 +00:00
|
|
|
// No body
|
2017-04-11 21:20:01 +00:00
|
|
|
body = newBodyNone();
|
2016-11-22 19:42:10 +00:00
|
|
|
} else {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Raw Content-Type (ex: application/json)
|
2018-11-06 02:31:02 +00:00
|
|
|
body = newBodyRaw(oldBody.text || '', mimeType);
|
2016-09-21 20:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 2. create/update request //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2016-11-27 21:42:38 +00:00
|
|
|
if (doCreate) {
|
2021-05-12 06:35:00 +00:00
|
|
|
const newRequest: Request = Object.assign({}, request, {
|
|
|
|
headers,
|
|
|
|
body,
|
|
|
|
});
|
2017-07-18 22:10:57 +00:00
|
|
|
return create(newRequest);
|
2016-11-27 21:42:38 +00:00
|
|
|
} else {
|
2021-05-12 06:35:00 +00:00
|
|
|
return update(request, {
|
|
|
|
headers,
|
|
|
|
body,
|
|
|
|
});
|
2016-11-27 21:42:38 +00:00
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function duplicate(request: Request, patch: Partial<Request> = {}) {
|
2020-06-30 20:53:01 +00:00
|
|
|
// 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) {
|
2019-12-11 17:48:56 +00:00
|
|
|
patch.name = `${request.name} (Copy)`;
|
|
|
|
}
|
2017-02-27 22:54:56 +00:00
|
|
|
|
|
|
|
// Get sort key of next request
|
2021-05-12 06:35:00 +00:00
|
|
|
const q = {
|
|
|
|
metaSortKey: {
|
|
|
|
$gt: request.metaSortKey,
|
|
|
|
},
|
|
|
|
};
|
2017-02-27 22:54:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
// @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;
|
2017-02-27 22:54:56 +00:00
|
|
|
// Calculate new sort key
|
|
|
|
const sortKeyIncrement = (nextSortKey - request.metaSortKey) / 2;
|
|
|
|
const metaSortKey = request.metaSortKey + sortKeyIncrement;
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.duplicate<Request>(request, {
|
|
|
|
name,
|
|
|
|
metaSortKey,
|
|
|
|
...patch,
|
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function remove(request: Request) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.remove(request);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function all() {
|
|
|
|
return db.all<Request>(type);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~ //
|
|
|
|
// Migrations //
|
|
|
|
// ~~~~~~~~~~ //
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
/**
|
|
|
|
* Migrate old body (string) to new body (object)
|
|
|
|
* @param request
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
function migrateBody(request: Request) {
|
2016-12-01 18:48:49 +00:00
|
|
|
if (request.body && typeof request.body === 'object') {
|
|
|
|
return request;
|
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
// 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);
|
2016-11-22 19:42:10 +00:00
|
|
|
|
|
|
|
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 = {};
|
2016-11-22 19:42:10 +00:00
|
|
|
} else {
|
2017-07-18 22:10:57 +00:00
|
|
|
const body: string = typeof request.body === 'string' ? request.body : '';
|
|
|
|
request.body = newBodyRaw(body, contentType);
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
2016-12-08 20:29:40 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
/**
|
|
|
|
* Fix some weird URLs that were caused by an old bug
|
|
|
|
* @param request
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
function migrateWeirdUrls(request: Request) {
|
2016-12-08 20:29:40 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure the request.authentication.type property is added
|
|
|
|
* @param request
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
function migrateAuthType(request: Request) {
|
2017-03-23 22:10:42 +00:00
|
|
|
const isAuthSet = request.authentication && request.authentication.username;
|
|
|
|
|
|
|
|
if (isAuthSet && !request.authentication.type) {
|
|
|
|
request.authentication.type = AUTH_BASIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|