2017-07-18 22:10:57 +00:00
|
|
|
// @flow
|
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
|
|
|
|
} from '../common/constants';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { getContentTypeHeader } from '../common/misc';
|
|
|
|
import {
|
|
|
|
buildQueryStringFromParams,
|
|
|
|
deconstructQueryStringToParams
|
|
|
|
} from 'insomnia-url';
|
|
|
|
import { GRANT_TYPE_AUTHORIZATION_CODE } from '../network/o-auth-2/constants';
|
|
|
|
import { SIGNATURE_METHOD_HMAC_SHA1 } from '../network/o-auth-1/constants';
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Request';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Request';
|
|
|
|
export const prefix = 'req';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = true;
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2017-07-18 22:10:57 +00:00
|
|
|
export type RequestAuthentication = Object;
|
2017-11-06 20:44:55 +00:00
|
|
|
|
2017-07-18 23:38:19 +00:00
|
|
|
export type RequestHeader = {
|
|
|
|
name: string,
|
|
|
|
value: string,
|
|
|
|
disabled?: boolean
|
|
|
|
};
|
2017-07-18 22:10:57 +00:00
|
|
|
|
2017-07-18 23:38:19 +00:00
|
|
|
export type RequestParameter = {
|
2017-07-18 22:10:57 +00:00
|
|
|
name: string,
|
|
|
|
value: string,
|
2017-07-18 23:38:19 +00:00
|
|
|
disabled?: boolean,
|
2017-07-18 22:10:57 +00:00
|
|
|
id?: string,
|
2017-07-18 23:38:19 +00:00
|
|
|
fileName?: string
|
|
|
|
};
|
|
|
|
|
|
|
|
export type RequestBodyParameter = {
|
|
|
|
name: string,
|
|
|
|
value: string,
|
|
|
|
disabled?: boolean,
|
2018-03-06 04:50:25 +00:00
|
|
|
multiline?: string,
|
2017-07-18 23:38:19 +00:00
|
|
|
id?: string,
|
2017-08-14 19:28:49 +00:00
|
|
|
fileName?: string,
|
|
|
|
type?: string
|
2017-07-18 22:10:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type RequestBody = {
|
2017-08-14 19:28:49 +00:00
|
|
|
mimeType?: string | null,
|
2017-07-18 22:10:57 +00:00
|
|
|
text?: string,
|
|
|
|
fileName?: string,
|
2017-07-18 23:38:19 +00:00
|
|
|
params?: Array<RequestBodyParameter>
|
2017-07-18 22:10:57 +00:00
|
|
|
};
|
|
|
|
|
2017-07-19 01:55:47 +00:00
|
|
|
type BaseRequest = {
|
2017-07-18 22:10:57 +00:00
|
|
|
url: string,
|
|
|
|
name: string,
|
|
|
|
description: string,
|
|
|
|
method: string,
|
|
|
|
body: RequestBody,
|
2017-07-18 23:38:19 +00:00
|
|
|
parameters: Array<RequestParameter>,
|
|
|
|
headers: Array<RequestHeader>,
|
2017-07-18 22:10:57 +00:00
|
|
|
authentication: RequestAuthentication,
|
|
|
|
metaSortKey: number,
|
2018-03-06 03:05:57 +00:00
|
|
|
isPrivate: boolean,
|
2017-07-18 22:10:57 +00:00
|
|
|
|
|
|
|
// Settings
|
|
|
|
settingStoreCookies: boolean,
|
|
|
|
settingSendCookies: boolean,
|
|
|
|
settingDisableRenderRequestBody: boolean,
|
2018-03-26 17:43:42 +00:00
|
|
|
settingEncodeUrl: boolean,
|
2018-06-06 20:48:14 +00:00
|
|
|
settingRebuildPath: boolean,
|
|
|
|
settingMaxTimelineDataSize: number
|
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 {
|
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,
|
|
|
|
settingMaxTimelineDataSize: 1000
|
2016-11-10 01:15:27 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +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:
|
2017-03-28 22:45:23 +00:00
|
|
|
case AUTH_DIGEST:
|
|
|
|
case AUTH_NTLM:
|
|
|
|
return {
|
|
|
|
type,
|
|
|
|
disabled: oldAuth.disabled || false,
|
|
|
|
username: oldAuth.username || '',
|
|
|
|
password: oldAuth.password || ''
|
|
|
|
};
|
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: '',
|
|
|
|
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,
|
|
|
|
grantType: GRANT_TYPE_AUTHORIZATION_CODE
|
|
|
|
};
|
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 || '',
|
|
|
|
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,
|
|
|
|
algorithm: HAWK_ALGORITHM_SHA256
|
|
|
|
};
|
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: '',
|
|
|
|
privateKey: ''
|
|
|
|
};
|
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:
|
2018-06-25 17:42:50 +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') {
|
2018-06-25 17:42:50 +00:00
|
|
|
return { text: rawBody };
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mimeType = contentType.split(';')[0];
|
2018-06-25 17:42:50 +00:00
|
|
|
return { mimeType, text: rawBody };
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function newBodyFormUrlEncoded(
|
|
|
|
parameters: Array<RequestBodyParameter> | null
|
|
|
|
): RequestBody {
|
2016-11-22 19:42:10 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_URLENCODED,
|
2018-03-06 04:50:25 +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,
|
|
|
|
fileName: path
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-11-22 22:26:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function newBodyForm(
|
|
|
|
parameters: Array<RequestBodyParameter>
|
|
|
|
): RequestBody {
|
2016-11-22 19:42:10 +00:00
|
|
|
return {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_DATA,
|
2017-01-27 01:00:27 +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;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function create(patch: Object = {}): Promise<Request> {
|
2016-09-21 20:32:45 +00:00
|
|
|
if (!patch.parentId) {
|
2018-06-25 17:42:50 +00:00
|
|
|
throw new Error(
|
|
|
|
`New Requests missing \`parentId\`: ${JSON.stringify(patch)}`
|
|
|
|
);
|
2016-09-21 20:32:45 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(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> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.get(type, id);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function findByParentId(parentId: string): Promise<Array<Request>> {
|
|
|
|
return db.find(type, { parentId: parentId });
|
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 update(request: Request, patch: Object): Promise<Request> {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(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,
|
2018-02-16 16:24:54 +00:00
|
|
|
doCreate: boolean = false,
|
|
|
|
savedBody: RequestBody = {}
|
2017-07-18 22:10:57 +00:00
|
|
|
): Promise<Request> {
|
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);
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-07-25 18:00:30 +00:00
|
|
|
// GraphQL uses JSON content-type
|
2018-06-25 17:42:50 +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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// 1. Update Content-Type header //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-04-20 01:37:40 +00:00
|
|
|
const hasBody = typeof mimeType === 'string';
|
2017-05-15 05:36:14 +00:00
|
|
|
if (!hasBody || 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) {
|
2018-06-25 17:42:50 +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;
|
2017-01-27 01:00:27 +00:00
|
|
|
|
2018-06-25 17:42:50 +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)
|
|
|
|
: newBodyFormUrlEncoded(deconstructQueryStringToParams(oldBody.text));
|
2016-11-22 19:42:10 +00:00
|
|
|
} else if (mimeType === CONTENT_TYPE_FORM_DATA) {
|
2017-01-27 01:00:27 +00:00
|
|
|
// Form Data
|
2018-02-16 16:24:54 +00:00
|
|
|
body = oldBody.params
|
|
|
|
? newBodyForm(oldBody.params)
|
|
|
|
: 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;
|
|
|
|
}
|
2018-02-16 16:24:54 +00:00
|
|
|
body = newBodyRaw(oldBody.text || '', CONTENT_TYPE_GRAPHQL);
|
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-02-16 16:24:54 +00:00
|
|
|
body = oldBody.params
|
|
|
|
? newBodyRaw(buildQueryStringFromParams(oldBody.params, false), mimeType)
|
|
|
|
: 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) {
|
2018-06-25 17:42:50 +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 {
|
2018-06-25 17:42:50 +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
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function duplicate(request: Request): Promise<Request> {
|
2016-09-21 20:32:45 +00:00
|
|
|
const name = `${request.name} (Copy)`;
|
2017-02-27 22:54:56 +00:00
|
|
|
|
|
|
|
// Get sort key of next request
|
2018-06-25 17:42:50 +00:00
|
|
|
const q = { metaSortKey: { $gt: request.metaSortKey } };
|
|
|
|
const [nextRequest] = await db.find(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;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
return db.duplicate(request, { name, metaSortKey });
|
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 remove(request: Request): Promise<void> {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.remove(request);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function all() {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.all(type);
|
|
|
|
}
|
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
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
function migrateBody(request: 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-06-25 17:42:50 +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-06-25 17:42:50 +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
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
function migrateWeirdUrls(request: 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
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
function migrateAuthType(request: 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;
|
|
|
|
}
|