2021-12-14 14:26:36 +00:00
import { HttpVersions , Settings as BaseSettings , UpdateChannel } from 'insomnia-common' ;
2021-10-14 14:59:45 +00:00
2021-03-13 03:38:54 +00:00
import {
getAppDefaultDarkTheme ,
2021-07-22 23:04:56 +00:00
getAppDefaultLightTheme ,
getAppDefaultTheme ,
2021-03-13 03:38:54 +00:00
} from '../common/constants' ;
2021-07-22 23:04:56 +00:00
import { database as db } from '../common/database' ;
2019-03-12 16:38:30 +00:00
import * as hotkeys from '../common/hotkeys' ;
2021-10-14 14:59:45 +00:00
import { getMonkeyPatchedControlledSettings , omitControlledSettings } from './helpers/settings' ;
2021-07-22 23:04:56 +00:00
import type { BaseModel } from './index' ;
2016-09-21 20:32:45 +00:00
2017-11-18 22:47:54 +00:00
export type Settings = BaseModel & BaseSettings ;
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
2021-07-13 23:49:32 +00:00
export type ThemeSettings = Pick < Settings , ' autoDetectColorScheme ' | ' lightTheme ' | ' darkTheme ' | ' theme ' > ;
2021-06-16 19:19:00 +00:00
export const isSettings = ( model : Pick < BaseModel , ' type ' > ) : model is Settings = > (
model . type === type
) ;
2018-06-25 17:42:50 +00:00
export function init ( ) : BaseSettings {
2016-11-10 01:15:27 +00:00
return {
2021-10-14 14:59:45 +00:00
autoDetectColorScheme : false ,
2019-12-17 19:10:42 +00:00
autoHideMenuBar : false ,
autocompleteDelay : 1200 ,
2021-10-14 14:59:45 +00:00
allowNotificationRequests : true ,
clearOAuth2SessionOnRestart : true ,
darkTheme : getAppDefaultDarkTheme ( ) ,
2019-12-17 19:10:42 +00:00
deviceId : null ,
disableHtmlPreviewJs : false ,
2021-10-14 14:59:45 +00:00
disablePaidFeatureAds : 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' ,
2021-12-13 07:45:08 +00:00
showVariableSourceAndValue : false ,
2020-06-10 18:15:56 +00:00
filterResponsesByEnv : false ,
2019-12-17 19:10:42 +00:00
followRedirects : true ,
fontInterface : null ,
fontMonospace : null ,
fontSize : 13 ,
fontVariantLigatures : false ,
forceVerticalLayout : false ,
2021-10-14 14:59:45 +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 ,
// 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 ,
hasPromptedToMigrateFromDesigner : false ,
2019-12-17 19:10:42 +00:00
hotKeyRegistry : hotkeys.newDefaultRegistry ( ) ,
2016-10-02 20:57:00 +00:00
httpProxy : '' ,
httpsProxy : '' ,
2021-10-14 14:59:45 +00:00
incognitoMode : false ,
lightTheme : getAppDefaultLightTheme ( ) ,
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 ,
2021-12-14 14:26:36 +00:00
updateChannel : UpdateChannel.stable ,
2019-12-17 19:10:42 +00:00
useBulkHeaderEditor : false ,
useBulkParametersEditor : false ,
2021-09-01 21:08:44 +00:00
validateAuthSSL : true ,
2019-12-17 19:10:42 +00:00
validateSSL : 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
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() {
2021-10-14 14:59:45 +00:00
let settingsList = await db . all < Settings > ( type ) ;
2021-05-12 06:35:00 +00:00
2021-10-14 14:59:45 +00:00
if ( settingsList ? . length === 0 ) {
settingsList = [ await getOrCreate ( ) ] ;
2016-11-16 17:18:39 +00:00
}
2021-10-14 14:59:45 +00:00
return settingsList . map ( getMonkeyPatchedControlledSettings ) ;
2016-11-16 17:18:39 +00:00
}
2021-10-14 14:59:45 +00:00
async function create() {
const settings = await db . docCreate < Settings > ( type ) ;
return getMonkeyPatchedControlledSettings ( settings ) ;
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 > ) {
2021-12-14 14:26:36 +00:00
const sanitizedPatch = omitControlledSettings ( settings , patch ) ;
const updatedSettings = await db . docUpdate < Settings > ( settings , sanitizedPatch ) ;
2021-10-14 14:59:45 +00:00
return getMonkeyPatchedControlledSettings ( updatedSettings ) ;
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-10-14 14:59:45 +00:00
const sanitizedPatch = omitControlledSettings ( settings , patch ) ;
const updatedSettings = await db . docUpdate < Settings > ( settings , sanitizedPatch ) ;
return getMonkeyPatchedControlledSettings ( updatedSettings ) ;
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-12-14 14:26:36 +00:00
return await create ( ) ;
2016-10-02 20:57:00 +00:00
}
2021-12-14 14:26:36 +00:00
return getMonkeyPatchedControlledSettings ( results [ 0 ] ) ;
2016-10-02 20:57:00 +00:00
}
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 ;
}