2016-07-16 02:06:10 +00:00
|
|
|
import electron from 'electron';
|
2016-07-23 08:22:52 +00:00
|
|
|
import NeDB from 'nedb';
|
2016-07-16 02:06:10 +00:00
|
|
|
import * as fsPath from 'path';
|
|
|
|
import * as methods from '../lib/constants';
|
2016-09-02 05:45:12 +00:00
|
|
|
import {DB_PERSIST_INTERVAL, DEFAULT_SIDEBAR_WIDTH} from '../lib/constants';
|
2016-07-16 02:06:10 +00:00
|
|
|
import {generateId} from './util';
|
|
|
|
import {PREVIEW_MODE_SOURCE} from '../lib/previewModes';
|
2016-08-15 17:04:36 +00:00
|
|
|
import {isDevelopment} from '../lib/appInfo';
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-07-25 22:27:29 +00:00
|
|
|
export const TYPE_STATS = 'Stats';
|
2016-07-19 19:13:51 +00:00
|
|
|
export const TYPE_SETTINGS = 'Settings';
|
2016-04-26 07:29:24 +00:00
|
|
|
export const TYPE_WORKSPACE = 'Workspace';
|
2016-08-15 17:04:36 +00:00
|
|
|
export const TYPE_ENVIRONMENT = 'Environment';
|
|
|
|
export const TYPE_COOKIE_JAR = 'CookieJar';
|
2016-04-26 07:29:24 +00:00
|
|
|
export const TYPE_REQUEST_GROUP = 'RequestGroup';
|
|
|
|
export const TYPE_REQUEST = 'Request';
|
|
|
|
export const TYPE_RESPONSE = 'Response';
|
2016-07-19 19:13:51 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
const BASE_MODEL_DEFAULTS = () => ({
|
|
|
|
modified: Date.now(),
|
|
|
|
created: Date.now(),
|
|
|
|
parentId: null
|
|
|
|
});
|
|
|
|
|
2016-07-19 19:13:51 +00:00
|
|
|
const MODEL_DEFAULTS = {
|
2016-07-25 22:27:29 +00:00
|
|
|
[TYPE_STATS]: () => ({
|
|
|
|
lastLaunch: Date.now(),
|
|
|
|
lastVersion: null,
|
|
|
|
launches: 0
|
|
|
|
}),
|
2016-07-19 19:13:51 +00:00
|
|
|
[TYPE_SETTINGS]: () => ({
|
|
|
|
showPasswords: true,
|
|
|
|
useBulkHeaderEditor: false,
|
2016-07-28 20:10:26 +00:00
|
|
|
followRedirects: false,
|
2016-08-22 22:22:22 +00:00
|
|
|
editorFontSize: 12,
|
2016-07-19 22:28:29 +00:00
|
|
|
editorLineWrapping: true,
|
2016-08-15 17:04:36 +00:00
|
|
|
httpProxy: '',
|
|
|
|
httpsProxy: '',
|
2016-07-28 20:10:26 +00:00
|
|
|
timeout: 0,
|
|
|
|
validateSSL: true
|
2016-07-19 19:13:51 +00:00
|
|
|
}),
|
|
|
|
[TYPE_WORKSPACE]: () => ({
|
|
|
|
name: 'New Workspace',
|
|
|
|
metaSidebarWidth: DEFAULT_SIDEBAR_WIDTH,
|
2016-08-15 22:31:30 +00:00
|
|
|
metaActiveEnvironmentId: null,
|
|
|
|
metaActiveRequestId: null,
|
|
|
|
metaFilter: ''
|
2016-07-19 19:13:51 +00:00
|
|
|
}),
|
2016-08-15 17:04:36 +00:00
|
|
|
[TYPE_ENVIRONMENT]: () => ({
|
2016-08-22 19:05:17 +00:00
|
|
|
name: 'New Environment',
|
2016-08-15 17:04:36 +00:00
|
|
|
data: {},
|
|
|
|
}),
|
|
|
|
[TYPE_COOKIE_JAR]: () => ({
|
|
|
|
name: 'Default Jar',
|
|
|
|
cookies: []
|
|
|
|
}),
|
2016-07-19 19:13:51 +00:00
|
|
|
[TYPE_REQUEST_GROUP]: () => ({
|
2016-07-21 19:15:35 +00:00
|
|
|
name: 'New Folder',
|
2016-07-19 19:13:51 +00:00
|
|
|
environment: {},
|
|
|
|
metaCollapsed: false,
|
|
|
|
metaSortKey: -1 * Date.now()
|
|
|
|
}),
|
|
|
|
[TYPE_REQUEST]: () => ({
|
|
|
|
url: '',
|
|
|
|
name: 'New Request',
|
|
|
|
method: methods.METHOD_GET,
|
|
|
|
body: '',
|
|
|
|
parameters: [],
|
2016-08-31 20:50:27 +00:00
|
|
|
headers: [],
|
2016-07-19 19:13:51 +00:00
|
|
|
authentication: {},
|
|
|
|
metaPreviewMode: PREVIEW_MODE_SOURCE,
|
|
|
|
metaSortKey: -1 * Date.now()
|
|
|
|
}),
|
|
|
|
[TYPE_RESPONSE]: () => ({
|
|
|
|
statusCode: 0,
|
|
|
|
statusMessage: '',
|
|
|
|
contentType: 'text/plain',
|
|
|
|
url: '',
|
2016-07-22 20:38:28 +00:00
|
|
|
bytesRead: 0,
|
|
|
|
elapsedTime: 0,
|
2016-07-19 19:13:51 +00:00
|
|
|
headers: [],
|
2016-08-15 17:04:36 +00:00
|
|
|
cookies: [],
|
2016-07-19 19:13:51 +00:00
|
|
|
body: '',
|
|
|
|
error: ''
|
|
|
|
}),
|
|
|
|
};
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
export const ALL_TYPES = Object.keys(MODEL_DEFAULTS);
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
let db = null;
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
function getDBFilePath (modelType) {
|
2016-08-04 04:12:45 +00:00
|
|
|
// NOTE: Do not EVER change this. EVER!
|
2016-04-28 07:30:26 +00:00
|
|
|
const basePath = electron.remote.app.getPath('userData');
|
2016-07-23 08:22:52 +00:00
|
|
|
return fsPath.join(basePath, `insomnia.${modelType}.db`);
|
2016-04-28 00:04:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
/**
|
|
|
|
* Initialize the database. This should be called once on app start.
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
let initialized = false;
|
|
|
|
export function initDB () {
|
|
|
|
// Only init once
|
|
|
|
if (initialized) {
|
|
|
|
return new Promise(resolve => resolve());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
2016-08-15 17:04:36 +00:00
|
|
|
db = {};
|
|
|
|
|
|
|
|
if (isDevelopment()) {
|
|
|
|
global.db = db;
|
|
|
|
}
|
2016-05-11 05:43:51 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
// Fill in the defaults
|
2016-06-18 21:02:27 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
const modelTypes = Object.keys(MODEL_DEFAULTS);
|
|
|
|
modelTypes.map(t => {
|
|
|
|
const filename = getDBFilePath(t);
|
|
|
|
const autoload = true;
|
2016-05-11 05:43:51 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
db[t] = new NeDB({filename, autoload});
|
|
|
|
db[t].persistence.setAutocompactionInterval(DB_PERSIST_INTERVAL)
|
2016-04-27 03:17:05 +00:00
|
|
|
});
|
2016-07-16 07:22:08 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
// Done
|
2016-07-16 07:22:08 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
initialized = true;
|
|
|
|
console.log(`-- Initialize DB at ${getDBFilePath('t')} --`);
|
|
|
|
resolve();
|
|
|
|
});
|
2016-07-16 07:22:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
let changeListeners = {};
|
|
|
|
|
|
|
|
export function onChange (id, callback) {
|
|
|
|
console.log(`-- Added DB Listener ${id} -- `);
|
|
|
|
changeListeners[id] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function offChange (id) {
|
|
|
|
console.log(`-- Removed DB Listener ${id} -- `);
|
|
|
|
delete changeListeners[id];
|
|
|
|
}
|
|
|
|
|
2016-09-02 05:45:12 +00:00
|
|
|
function getMostRecentlyModified (type, query = {}) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db[type].find(query).sort({modified: -1}).limit(1).exec((err, docs) => {
|
|
|
|
resolve(docs.length ? docs[0] : null);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
function find (type, query = {}) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db[type].find(query, (err, rawDocs) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const modelDefaults = MODEL_DEFAULTS[type]();
|
|
|
|
const docs = rawDocs.map(rawDoc => {
|
|
|
|
return Object.assign({}, modelDefaults, rawDoc);
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(docs);
|
|
|
|
});
|
2016-07-19 16:59:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
function all (type) {
|
|
|
|
return find(type);
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
function getWhere (type, query) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db[type].find(query, (err, rawDocs) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-05-11 05:43:51 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
if (rawDocs.length === 0) {
|
2016-07-25 22:27:29 +00:00
|
|
|
// Not found. Too bad!
|
|
|
|
return resolve(null);
|
2016-07-23 08:22:52 +00:00
|
|
|
}
|
2016-05-11 05:43:51 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
const modelDefaults = MODEL_DEFAULTS[type]();
|
|
|
|
resolve(Object.assign({}, modelDefaults, rawDocs[0]));
|
|
|
|
});
|
|
|
|
});
|
2016-07-19 16:59:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
function get (type, id) {
|
|
|
|
return getWhere(type, {_id: id});
|
2016-07-19 04:01:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
function count (type, query = {}) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db[type].count(query, (err, count) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-05-11 05:43:51 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
resolve(count);
|
|
|
|
});
|
|
|
|
});
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function insert (doc) {
|
2016-07-23 08:22:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db[doc.type].insert(doc, (err, newDoc) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-06-18 21:02:27 +00:00
|
|
|
|
2016-07-23 08:22:52 +00:00
|
|
|
resolve(newDoc);
|
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('insert', doc));
|
|
|
|
});
|
|
|
|
});
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function update (doc) {
|
2016-07-19 16:59:26 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-07-23 08:22:52 +00:00
|
|
|
db[doc.type].update({_id: doc._id}, doc, err => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
resolve(doc);
|
2016-07-19 16:59:26 +00:00
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('update', doc));
|
2016-07-23 08:22:52 +00:00
|
|
|
});
|
2016-07-19 16:59:26 +00:00
|
|
|
});
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function remove (doc) {
|
2016-08-15 22:31:30 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
withChildren(doc).then(docs => {
|
|
|
|
const promises = docs.map(d => (
|
|
|
|
db[d.type].remove({_id: d._id}, {multi: true})
|
|
|
|
));
|
|
|
|
|
|
|
|
Promise.all(promises).then(() => {
|
|
|
|
for (const doc of docs) {
|
|
|
|
Object.keys(changeListeners).map(k => changeListeners[k]('remove', doc));
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
});
|
2016-07-23 08:22:52 +00:00
|
|
|
});
|
|
|
|
});
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 05:45:12 +00:00
|
|
|
/**
|
|
|
|
* Remove a lot of documents quickly and silently
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @param query
|
|
|
|
* @returns {Promise.<T>}
|
|
|
|
*/
|
|
|
|
function removeBulkSilently (type, query) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
db[type].remove(query, {multi: true}, err => resolve());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// DEFAULT MODEL STUFF //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~ //
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
function docUpdate (originalDoc, patch = {}) {
|
|
|
|
const doc = Object.assign(
|
2016-08-15 22:31:30 +00:00
|
|
|
BASE_MODEL_DEFAULTS(),
|
2016-04-27 03:17:05 +00:00
|
|
|
originalDoc,
|
|
|
|
patch,
|
|
|
|
{modified: Date.now()}
|
|
|
|
);
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
return update(doc);
|
2016-07-19 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 23:05:39 +00:00
|
|
|
function docCreate (type, idPrefix, patch = {}) {
|
2016-04-26 07:29:24 +00:00
|
|
|
const doc = Object.assign(
|
2016-08-15 22:31:30 +00:00
|
|
|
BASE_MODEL_DEFAULTS(),
|
|
|
|
{_id: generateId(idPrefix)},
|
|
|
|
MODEL_DEFAULTS[type](),
|
2016-04-16 23:24:57 +00:00
|
|
|
patch,
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
// Fields that the user can't touch
|
2016-04-16 23:24:57 +00:00
|
|
|
{
|
2016-04-18 04:39:15 +00:00
|
|
|
type: type,
|
2016-04-16 23:24:57 +00:00
|
|
|
modified: Date.now()
|
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
);
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
return insert(doc);
|
2016-04-17 20:35:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
// ~~~~~~~ //
|
|
|
|
// GENERAL //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
|
|
|
export function withChildren (doc = null) {
|
|
|
|
let docsToReturn = doc ? [doc] : [];
|
|
|
|
|
|
|
|
const next = (docs) => {
|
|
|
|
const promises = [];
|
|
|
|
for (const doc of docs) {
|
|
|
|
for (const type of ALL_TYPES) {
|
|
|
|
// If the doc is null, we want to search for parentId === null
|
|
|
|
const parentId = doc ? doc._id : null;
|
|
|
|
promises.push(find(type, {parentId}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises).then(results => {
|
|
|
|
let newDocs = [];
|
|
|
|
|
|
|
|
// Gather up the docs from each type
|
|
|
|
for (const docs of results) {
|
|
|
|
for (const doc of docs) {
|
|
|
|
newDocs.push(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newDocs.length === 0) {
|
|
|
|
// Didn't find anything. We're done
|
|
|
|
return new Promise(resolve => resolve(docsToReturn));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue searching for children
|
|
|
|
docsToReturn = [...docsToReturn, ...newDocs];
|
|
|
|
return next(newDocs);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return next([doc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
// ~~~~~~~ //
|
|
|
|
// REQUEST //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
export function requestCreateAndActivate (workspace, patch = {}) {
|
|
|
|
return requestCreate(patch).then(r => {
|
2016-07-21 03:58:52 +00:00
|
|
|
workspaceUpdate(workspace, {metaActiveRequestId: r._id});
|
2016-07-06 20:18:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestCopyAndActivate (workspace, request) {
|
2016-08-17 21:05:14 +00:00
|
|
|
return requestDuplicate(request).then(r => {
|
2016-07-21 03:58:52 +00:00
|
|
|
workspaceUpdate(workspace, {metaActiveRequestId: r._id});
|
2016-07-06 20:18:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export function requestCreate (patch = {}) {
|
2016-07-20 23:16:28 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Requests missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
2016-07-18 23:05:39 +00:00
|
|
|
return docCreate(TYPE_REQUEST, 'req', patch);
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function requestGetById (id) {
|
2016-04-29 08:15:37 +00:00
|
|
|
return get(TYPE_REQUEST, id);
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function requestFindByParentId (parentId) {
|
2016-07-23 08:22:52 +00:00
|
|
|
return find(TYPE_REQUEST, {parentId: parentId});
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function requestUpdate (request, patch) {
|
|
|
|
return docUpdate(request, patch);
|
2016-07-19 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:02:17 +00:00
|
|
|
export function requestUpdateContentType (request, contentType) {
|
|
|
|
let headers = [...request.headers];
|
|
|
|
const contentTypeHeader = headers.find(
|
|
|
|
h => h.name.toLowerCase() === 'content-type'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!contentType) {
|
|
|
|
// Remove the contentType header if we are unsetting it
|
|
|
|
headers = headers.filter(h => h !== contentTypeHeader);
|
|
|
|
} else if (contentTypeHeader) {
|
|
|
|
contentTypeHeader.value = contentType;
|
|
|
|
} else {
|
|
|
|
headers.push({name: 'Content-Type', value: contentType})
|
|
|
|
}
|
|
|
|
|
|
|
|
return docUpdate(request, {headers});
|
|
|
|
}
|
|
|
|
|
2016-08-17 21:05:14 +00:00
|
|
|
export function requestDuplicate (request) {
|
2016-04-26 07:29:24 +00:00
|
|
|
const name = `${request.name} (Copy)`;
|
2016-08-17 21:05:14 +00:00
|
|
|
const newRequest = Object.assign({}, request, {name});
|
|
|
|
|
|
|
|
// Remove the old Id
|
|
|
|
delete newRequest._id;
|
|
|
|
|
|
|
|
return requestCreate(newRequest);
|
2016-04-16 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function requestRemove (request) {
|
|
|
|
return remove(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_REQUEST);
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 04:12:45 +00:00
|
|
|
export function requestGetAncestors (request) {
|
|
|
|
return new Promise(resolve => {
|
2016-08-15 17:04:36 +00:00
|
|
|
let ancestors = [];
|
2016-08-04 04:12:45 +00:00
|
|
|
|
|
|
|
const next = (doc) => {
|
|
|
|
Promise.all([
|
|
|
|
requestGroupGetById(doc.parentId),
|
|
|
|
workspaceGetById(doc.parentId)
|
|
|
|
]).then(([requestGroup, workspace]) => {
|
|
|
|
if (requestGroup) {
|
2016-08-15 17:04:36 +00:00
|
|
|
ancestors = [requestGroup, ...ancestors];
|
2016-08-04 04:12:45 +00:00
|
|
|
next(requestGroup);
|
|
|
|
} else if (workspace) {
|
2016-08-15 17:04:36 +00:00
|
|
|
ancestors = [workspace, ...ancestors];
|
2016-08-04 04:12:45 +00:00
|
|
|
next(workspace);
|
|
|
|
// We could be done here, but let's have there only be one finish case
|
|
|
|
} else {
|
|
|
|
// We're finished
|
|
|
|
resolve(ancestors);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
next(request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
|
2016-04-16 23:24:57 +00:00
|
|
|
// ~~~~~~~~~~~~~ //
|
|
|
|
// REQUEST GROUP //
|
|
|
|
// ~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
export function requestGroupCreate (patch = {}) {
|
2016-07-20 23:16:28 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Requests missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
return docCreate(TYPE_REQUEST_GROUP, 'fdr', patch);
|
2016-04-17 20:35:35 +00:00
|
|
|
}
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function requestGroupUpdate (requestGroup, patch) {
|
|
|
|
return docUpdate(requestGroup, patch);
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function requestGroupGetById (id) {
|
|
|
|
return get(TYPE_REQUEST_GROUP, id);
|
2016-07-19 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function requestGroupFindByParentId (parentId) {
|
2016-07-27 02:49:51 +00:00
|
|
|
return find(TYPE_REQUEST_GROUP, {parentId});
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function requestGroupRemove (requestGroup) {
|
|
|
|
return remove(requestGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestGroupAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_REQUEST_GROUP);
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
// ~~~~~~~~ //
|
2016-04-17 20:35:35 +00:00
|
|
|
// RESPONSE //
|
2016-04-23 06:08:52 +00:00
|
|
|
// ~~~~~~~~ //
|
2016-04-16 23:24:57 +00:00
|
|
|
|
2016-04-17 20:35:35 +00:00
|
|
|
export function responseCreate (patch = {}) {
|
2016-07-20 23:16:28 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Requests missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
2016-09-02 05:45:12 +00:00
|
|
|
removeBulkSilently(TYPE_RESPONSE, {parentId: patch.parentId});
|
2016-07-18 23:05:39 +00:00
|
|
|
return docCreate(TYPE_RESPONSE, 'res', patch);
|
2016-04-17 20:35:35 +00:00
|
|
|
}
|
2016-04-18 04:39:15 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function responseAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_RESPONSE);
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 05:45:12 +00:00
|
|
|
export function responseGetLatestByParentId (parentId) {
|
|
|
|
return getMostRecentlyModified(TYPE_RESPONSE, {parentId});
|
|
|
|
}
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
// ~~~~~~~ //
|
|
|
|
// COOKIES //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
|
|
|
export function cookieJarCreate (patch = {}) {
|
|
|
|
return docCreate(TYPE_COOKIE_JAR, 'jar', patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cookieJarGetOrCreateForWorkspace (workspace) {
|
|
|
|
const parentId = workspace._id;
|
|
|
|
return find(TYPE_COOKIE_JAR, {parentId}).then(cookieJars => {
|
|
|
|
if (cookieJars.length === 0) {
|
|
|
|
return cookieJarCreate({parentId})
|
|
|
|
} else {
|
|
|
|
return new Promise(resolve => resolve(cookieJars[0]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cookieJarAll () {
|
|
|
|
return all(TYPE_COOKIE_JAR);
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
export function cookieJarGetById (id) {
|
|
|
|
return get(TYPE_COOKIE_JAR, id);
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
export function cookieJarUpdate (cookieJar, patch) {
|
|
|
|
return docUpdate(cookieJar, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
// ~~~~~~~~~ //
|
|
|
|
// WORKSPACE //
|
|
|
|
// ~~~~~~~~~ //
|
|
|
|
|
2016-08-04 04:12:45 +00:00
|
|
|
export function workspaceGetById (id) {
|
|
|
|
return get(TYPE_WORKSPACE, id);
|
|
|
|
}
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
export function workspaceCreate (patch = {}) {
|
2016-07-18 23:05:39 +00:00
|
|
|
return docCreate(TYPE_WORKSPACE, 'wrk', patch);
|
2016-04-23 06:08:52 +00:00
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
export function workspaceAll () {
|
2016-05-11 05:43:51 +00:00
|
|
|
return all(TYPE_WORKSPACE).then(workspaces => {
|
2016-04-27 03:17:05 +00:00
|
|
|
if (workspaces.length === 0) {
|
2016-07-19 19:13:51 +00:00
|
|
|
return workspaceCreate({name: 'Insomnia'}).then(workspaceAll);
|
2016-04-26 07:29:24 +00:00
|
|
|
} else {
|
2016-04-27 03:17:05 +00:00
|
|
|
return new Promise(resolve => resolve(workspaces))
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|
2016-04-27 03:17:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function workspaceCount () {
|
|
|
|
return count(TYPE_WORKSPACE)
|
2016-04-27 03:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:59:26 +00:00
|
|
|
export function workspaceUpdate (workspace, patch) {
|
|
|
|
return docUpdate(workspace, patch);
|
2016-07-19 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
export function workspaceRemove (workspace) {
|
|
|
|
return remove(workspace);
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|
2016-07-19 19:13:51 +00:00
|
|
|
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
// ~~~~~~~~~~~ //
|
|
|
|
// ENVIRONMENT //
|
|
|
|
// ~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
export function environmentCreate (patch = {}) {
|
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Environment missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return docCreate(TYPE_ENVIRONMENT, 'env', patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function environmentUpdate (environment, patch) {
|
|
|
|
return docUpdate(environment, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function environmentFindByParentId (parentId) {
|
|
|
|
return find(TYPE_ENVIRONMENT, {parentId});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function environmentGetOrCreateForWorkspace (workspace) {
|
|
|
|
const parentId = workspace._id;
|
|
|
|
return find(TYPE_ENVIRONMENT, {parentId}).then(environments => {
|
|
|
|
if (environments.length === 0) {
|
2016-08-22 19:05:17 +00:00
|
|
|
return environmentCreate({parentId, name: 'Base Environment'})
|
2016-08-15 17:04:36 +00:00
|
|
|
} else {
|
|
|
|
return new Promise(resolve => resolve(environments[0]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function environmentGetById (id) {
|
|
|
|
return get(TYPE_ENVIRONMENT, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function environmentRemove (environment) {
|
|
|
|
return remove(environment);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function environmentAll () {
|
|
|
|
return all(TYPE_ENVIRONMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ~~~~~~~~ //
|
|
|
|
// SETTINGS //
|
|
|
|
// ~~~~~~~~ //
|
2016-07-19 19:13:51 +00:00
|
|
|
|
|
|
|
export function settingsCreate (patch = {}) {
|
2016-07-25 22:27:29 +00:00
|
|
|
return docCreate(TYPE_SETTINGS, 'set', patch);
|
2016-07-19 19:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function settingsUpdate (settings, patch) {
|
|
|
|
return docUpdate(settings, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function settingsGet () {
|
|
|
|
return all(TYPE_SETTINGS).then(results => {
|
|
|
|
if (results.length === 0) {
|
|
|
|
return settingsCreate().then(settingsGet);
|
|
|
|
} else {
|
|
|
|
return new Promise(resolve => resolve(results[0]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-07-25 22:27:29 +00:00
|
|
|
|
|
|
|
// ~~~~ //
|
|
|
|
// USER //
|
|
|
|
// ~~~~ //
|
|
|
|
|
|
|
|
export function statsCreate (patch = {}) {
|
|
|
|
return docCreate(TYPE_STATS, 'sta', patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function statsUpdate (patch) {
|
|
|
|
return statsGet().then(stats => {
|
|
|
|
return docUpdate(stats, patch);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function statsGet () {
|
|
|
|
return all(TYPE_STATS).then(results => {
|
|
|
|
if (results.length === 0) {
|
|
|
|
return statsCreate().then(statsGet);
|
|
|
|
} else {
|
|
|
|
return new Promise(resolve => resolve(results[0]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function statsIncrement (key) {
|
|
|
|
return statsGet().then(stats => {
|
|
|
|
if (stats[key] === undefined) {
|
|
|
|
throw new Error(`Stats[${key}] doesn't exist for increment`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = {
|
|
|
|
[key]: stats[key] + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return docUpdate(stats, patch);
|
|
|
|
});
|
|
|
|
}
|