2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
2021-03-13 03:38:54 +00:00
|
|
|
import {
|
|
|
|
getAppDefaultTheme,
|
|
|
|
getAppDefaultLightTheme,
|
|
|
|
getAppDefaultDarkTheme,
|
|
|
|
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
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface PluginConfig {
|
|
|
|
disabled: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PluginConfigMap = Record<string, PluginConfig>;
|
|
|
|
|
|
|
|
interface BaseSettings {
|
|
|
|
autoHideMenuBar: boolean;
|
|
|
|
autocompleteDelay: number;
|
|
|
|
deviceId: string | null;
|
|
|
|
disableHtmlPreviewJs: boolean;
|
|
|
|
disableResponsePreviewLinks: boolean;
|
|
|
|
disableUpdateNotification: boolean;
|
|
|
|
editorFontSize: number;
|
|
|
|
editorIndentSize: number;
|
|
|
|
editorIndentWithTabs: boolean;
|
|
|
|
editorKeyMap: string;
|
|
|
|
editorLineWrapping: boolean;
|
|
|
|
enableAnalytics: boolean;
|
|
|
|
environmentHighlightColorStyle: string;
|
|
|
|
filterResponsesByEnv: boolean;
|
|
|
|
followRedirects: boolean;
|
|
|
|
clearOAuth2SessionOnRestart: boolean;
|
|
|
|
fontInterface: string | null;
|
|
|
|
fontMonospace: string | null;
|
|
|
|
fontSize: number;
|
|
|
|
fontVariantLigatures: boolean;
|
|
|
|
forceVerticalLayout: boolean;
|
|
|
|
hotKeyRegistry: hotkeys.HotKeyRegistry;
|
|
|
|
httpProxy: string;
|
|
|
|
httpsProxy: string;
|
|
|
|
lineWrapping?: boolean;
|
|
|
|
maxHistoryResponses: number;
|
|
|
|
maxRedirects: number;
|
|
|
|
maxTimelineDataSizeKB: number;
|
|
|
|
noProxy: string;
|
|
|
|
nunjucksPowerUserMode: boolean;
|
|
|
|
pluginConfig: PluginConfigMap;
|
|
|
|
pluginPath: string;
|
|
|
|
preferredHttpVersion: HttpVersion;
|
|
|
|
proxyEnabled: boolean;
|
|
|
|
showPasswords: boolean;
|
|
|
|
theme: string;
|
|
|
|
autoDetectColorScheme: boolean;
|
|
|
|
lightTheme: string;
|
|
|
|
darkTheme: string;
|
|
|
|
timeout: number;
|
|
|
|
updateAutomatically: boolean;
|
|
|
|
updateChannel: string;
|
|
|
|
useBulkHeaderEditor: boolean;
|
|
|
|
useBulkParametersEditor: boolean;
|
|
|
|
validateSSL: boolean;
|
|
|
|
hasPromptedToMigrateFromDesigner: boolean;
|
|
|
|
hasPromptedOnboarding: boolean;
|
|
|
|
hasPromptedAnalytics: 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';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Settings';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const prefix = 'set';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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(),
|
2021-03-13 03:38:54 +00:00
|
|
|
autoDetectColorScheme: false,
|
|
|
|
lightTheme: getAppDefaultLightTheme(),
|
|
|
|
darkTheme: getAppDefaultDarkTheme(),
|
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,
|
2021-04-15 14:33:21 +00:00
|
|
|
// Only existing users updating from an older version should see the analytics prompt
|
|
|
|
// So by default this flag is set to false, and is toggled to true during initialization
|
|
|
|
// for new users
|
|
|
|
hasPromptedAnalytics: false,
|
2016-11-10 01:15:27 +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 function migrate(doc: Settings) {
|
2019-04-26 20:58:05 +00:00
|
|
|
doc = migrateEnsureHotKeys(doc);
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function all() {
|
|
|
|
const settings = await db.all<Settings>(type);
|
|
|
|
|
|
|
|
if (settings?.length === 0) {
|
2016-11-16 17:18:39 +00:00
|
|
|
return [await getOrCreate()];
|
|
|
|
} else {
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function create(patch: Partial<Settings> = {}) {
|
|
|
|
return db.docCreate<Settings>(type, 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 async function update(settings: Settings, patch: Partial<Settings>) {
|
|
|
|
return db.docUpdate<Settings>(settings, 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 async function patch(patch: Partial<Settings>) {
|
2021-02-25 20:13:37 +00:00
|
|
|
const settings = await getOrCreate();
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docUpdate<Settings>(settings, patch);
|
2021-02-25 20:13:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function getOrCreate() {
|
|
|
|
const results = await db.all<Settings>(type) || [];
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
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 {
|
2021-05-12 06:35:00 +00:00
|
|
|
settings.hotKeyRegistry = { ...hotkeys.newDefaultRegistry(), ...settings.hotKeyRegistry };
|
2019-04-26 20:58:05 +00:00
|
|
|
return settings;
|
|
|
|
}
|