2017-07-19 04:48:28 +00:00
|
|
|
// @flow
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2020-06-10 18:15:56 +00:00
|
|
|
import { getAppDefaultTheme, HttpVersions, UPDATE_CHANNEL_STABLE } from '../common/constants';
|
2019-03-12 16:38:30 +00:00
|
|
|
import * as hotkeys from '../common/hotkeys';
|
2020-06-10 18:15:56 +00:00
|
|
|
import type { HttpVersion } from '../common/constants';
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2020-01-23 18:43:04 +00:00
|
|
|
export type PluginConfig = {
|
|
|
|
disabled: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type PluginConfigMap = {
|
|
|
|
[string]: PluginConfig,
|
|
|
|
};
|
|
|
|
|
2017-07-19 04:48:28 +00:00
|
|
|
type BaseSettings = {
|
2019-12-17 19:10:42 +00:00
|
|
|
autoHideMenuBar: boolean,
|
|
|
|
autocompleteDelay: number,
|
|
|
|
deviceId: string | null,
|
|
|
|
disableHtmlPreviewJs: boolean,
|
2020-01-07 16:35:21 +00:00
|
|
|
disableResponsePreviewLinks: boolean,
|
2020-06-10 18:15:56 +00:00
|
|
|
disableUpdateNotification: boolean,
|
2017-07-19 04:48:28 +00:00
|
|
|
editorFontSize: number,
|
|
|
|
editorIndentSize: number,
|
2018-09-26 23:08:32 +00:00
|
|
|
editorIndentWithTabs: boolean,
|
2019-12-17 19:10:42 +00:00
|
|
|
editorKeyMap: string,
|
|
|
|
editorLineWrapping: boolean,
|
2020-04-26 20:33:39 +00:00
|
|
|
enableAnalytics: boolean,
|
2019-12-17 19:10:42 +00:00
|
|
|
environmentHighlightColorStyle: string,
|
2020-06-10 18:15:56 +00:00
|
|
|
filterResponsesByEnv: boolean,
|
2019-12-17 19:10:42 +00:00
|
|
|
followRedirects: boolean,
|
2020-10-09 20:33:21 +00:00
|
|
|
clearOAuth2SessionOnRestart: boolean,
|
2019-12-17 19:10:42 +00:00
|
|
|
fontInterface: string | null,
|
|
|
|
fontMonospace: string | null,
|
|
|
|
fontSize: number,
|
|
|
|
fontVariantLigatures: boolean,
|
|
|
|
forceVerticalLayout: boolean,
|
|
|
|
hotKeyRegistry: hotkeys.HotKeyRegistry,
|
2017-07-19 04:48:28 +00:00
|
|
|
httpProxy: string,
|
|
|
|
httpsProxy: string,
|
2019-12-17 19:10:42 +00:00
|
|
|
maxHistoryResponses: number,
|
|
|
|
maxRedirects: number,
|
|
|
|
maxTimelineDataSizeKB: number,
|
2017-07-19 04:48:28 +00:00
|
|
|
noProxy: string,
|
2019-12-17 19:10:42 +00:00
|
|
|
nunjucksPowerUserMode: boolean,
|
2020-06-10 18:15:56 +00:00
|
|
|
pluginConfig: PluginConfigMap,
|
2019-12-17 19:10:42 +00:00
|
|
|
pluginPath: string,
|
2020-06-10 18:15:56 +00:00
|
|
|
preferredHttpVersion: HttpVersion,
|
2017-07-19 04:48:28 +00:00
|
|
|
proxyEnabled: boolean,
|
2019-12-17 19:10:42 +00:00
|
|
|
showPasswords: boolean,
|
2017-07-19 04:48:28 +00:00
|
|
|
theme: string,
|
2019-12-17 19:10:42 +00:00
|
|
|
timeout: number,
|
2018-07-12 20:38:51 +00:00
|
|
|
updateAutomatically: boolean,
|
2019-12-17 19:10:42 +00:00
|
|
|
updateChannel: string,
|
|
|
|
useBulkHeaderEditor: boolean,
|
|
|
|
useBulkParametersEditor: boolean,
|
|
|
|
validateSSL: boolean,
|
2021-02-09 19:55:30 +00:00
|
|
|
hasPromptedToMigrateFromDesigner: boolean,
|
2021-02-25 20:13:37 +00:00
|
|
|
hasPromptedOnboarding: boolean,
|
2017-07-19 04:48:28 +00:00
|
|
|
};
|
|
|
|
|
2017-11-18 22:47:54 +00:00
|
|
|
export type Settings = BaseModel & BaseSettings;
|
2017-07-19 04:48:28 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Settings';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Settings';
|
|
|
|
export const prefix = 'set';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = false;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init(): BaseSettings {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2019-12-17 19:10:42 +00:00
|
|
|
autoHideMenuBar: false,
|
|
|
|
autocompleteDelay: 1200,
|
|
|
|
deviceId: null,
|
|
|
|
disableHtmlPreviewJs: false,
|
2020-01-07 16:35:21 +00:00
|
|
|
disableResponsePreviewLinks: false,
|
2019-12-17 19:10:42 +00:00
|
|
|
disableUpdateNotification: false,
|
2017-04-21 04:30:52 +00:00
|
|
|
editorFontSize: 11,
|
|
|
|
editorIndentSize: 2,
|
2018-09-26 23:08:32 +00:00
|
|
|
editorIndentWithTabs: true,
|
2019-12-17 19:10:42 +00:00
|
|
|
editorKeyMap: 'default',
|
|
|
|
editorLineWrapping: true,
|
2020-04-26 20:33:39 +00:00
|
|
|
enableAnalytics: false,
|
2019-12-17 19:10:42 +00:00
|
|
|
environmentHighlightColorStyle: 'sidebar-indicator',
|
2020-06-10 18:15:56 +00:00
|
|
|
filterResponsesByEnv: false,
|
2019-12-17 19:10:42 +00:00
|
|
|
followRedirects: true,
|
2020-10-09 20:33:21 +00:00
|
|
|
clearOAuth2SessionOnRestart: true,
|
2019-12-17 19:10:42 +00:00
|
|
|
fontInterface: null,
|
|
|
|
fontMonospace: null,
|
|
|
|
fontSize: 13,
|
|
|
|
fontVariantLigatures: false,
|
|
|
|
forceVerticalLayout: false,
|
|
|
|
hotKeyRegistry: hotkeys.newDefaultRegistry(),
|
2016-10-02 20:57:00 +00:00
|
|
|
httpProxy: '',
|
|
|
|
httpsProxy: '',
|
2019-04-18 04:29:42 +00:00
|
|
|
maxHistoryResponses: 20,
|
2019-12-17 19:10:42 +00:00
|
|
|
maxRedirects: -1,
|
|
|
|
maxTimelineDataSizeKB: 10,
|
|
|
|
noProxy: '',
|
|
|
|
nunjucksPowerUserMode: false,
|
2020-04-26 20:33:39 +00:00
|
|
|
pluginConfig: {},
|
2019-12-17 19:10:42 +00:00
|
|
|
pluginPath: '',
|
2020-06-10 18:15:56 +00:00
|
|
|
preferredHttpVersion: HttpVersions.default,
|
2017-03-28 22:45:23 +00:00
|
|
|
proxyEnabled: false,
|
2019-12-17 19:10:42 +00:00
|
|
|
showPasswords: false,
|
2020-04-26 20:33:39 +00:00
|
|
|
theme: getAppDefaultTheme(),
|
2019-12-17 19:10:42 +00:00
|
|
|
timeout: 0,
|
2018-07-12 20:38:51 +00:00
|
|
|
updateAutomatically: true,
|
2019-12-17 19:10:42 +00:00
|
|
|
updateChannel: UPDATE_CHANNEL_STABLE,
|
|
|
|
useBulkHeaderEditor: false,
|
|
|
|
useBulkParametersEditor: false,
|
|
|
|
validateSSL: true,
|
2021-02-09 19:55:30 +00:00
|
|
|
|
|
|
|
hasPromptedToMigrateFromDesigner: false,
|
2021-02-25 20:13:37 +00:00
|
|
|
|
|
|
|
// Users should only see onboarding during first launch, and anybody updating from an
|
|
|
|
// older version should not see it, so by default this flag is set to true, and is toggled
|
|
|
|
// to false during initialization
|
|
|
|
hasPromptedOnboarding: true,
|
2016-11-10 01:15:27 +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 function migrate(doc: Settings): Settings {
|
2019-04-26 20:58:05 +00:00
|
|
|
doc = migrateEnsureHotKeys(doc);
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export async function all(patch: $Shape<Settings> = {}): Promise<Array<Settings>> {
|
2016-11-16 17:18:39 +00:00
|
|
|
const settings = await db.all(type);
|
|
|
|
if (settings.length === 0) {
|
|
|
|
return [await getOrCreate()];
|
|
|
|
} else {
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export async function create(patch: $Shape<Settings> = {}): Promise<Settings> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export async function update(settings: Settings, patch: $Shape<Settings>): Promise<Settings> {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(settings, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-02-25 20:13:37 +00:00
|
|
|
export async function patch(patch: $Shape<Settings>): Promise<Settings> {
|
|
|
|
const settings = await getOrCreate();
|
|
|
|
return db.docUpdate(settings, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getOrCreate(): Promise<Settings> {
|
2016-10-02 20:57:00 +00:00
|
|
|
const results = await db.all(type);
|
|
|
|
if (results.length === 0) {
|
2021-02-25 20:13:37 +00:00
|
|
|
return create();
|
2016-10-02 20:57:00 +00:00
|
|
|
} else {
|
|
|
|
return results[0];
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 20:58:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure map is updated when new hotkeys are added
|
|
|
|
*/
|
|
|
|
function migrateEnsureHotKeys(settings: Settings): Settings {
|
|
|
|
settings.hotKeyRegistry = {
|
|
|
|
...hotkeys.newDefaultRegistry(),
|
|
|
|
...settings.hotKeyRegistry,
|
|
|
|
};
|
|
|
|
return settings;
|
|
|
|
}
|