From aaafc25a532f02b99348139596487e9fe0c8765c Mon Sep 17 00:00:00 2001 From: shrey Date: Sat, 3 Mar 2018 10:11:00 -0500 Subject: [PATCH 1/2] Add typings for `settings` module. --- types/settings/index.d.ts | 30 +++++++++++++++++++++++++++++ types/settings/settings-tests.ts | 33 ++++++++++++++++++++++++++++++++ types/settings/tsconfig.json | 23 ++++++++++++++++++++++ types/settings/tslint.json | 3 +++ 4 files changed, 89 insertions(+) create mode 100644 types/settings/index.d.ts create mode 100644 types/settings/settings-tests.ts create mode 100644 types/settings/tsconfig.json create mode 100644 types/settings/tslint.json diff --git a/types/settings/index.d.ts b/types/settings/index.d.ts new file mode 100644 index 0000000000..1c7ec115de --- /dev/null +++ b/types/settings/index.d.ts @@ -0,0 +1,30 @@ +// Type definitions for settings 0.1 +// Project: https://github.com/mgutz/node-settings +// Definitions by: Shrey Jain +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +// This wierd looking class definition is necessary since the function that the module +// exposes NEEDS (will throw error otherwise) to be called with the `new` keyword +// BUT returns a random object...not an instance of the class +declare class Settings { + constructor(pathOrModule: Settings.PathOrModule, options?: Settings.Options); + + [setting: string]: any; +} + +declare namespace Settings { + interface Options { + env?: string; + root?: string; + defaults?: any; + } + + type PathOrModule = string | + { + forceEnv?: string, + common: any, // error is thrown if 'common' object is not provided + [envName: string]: any + }; +} +export = Settings; diff --git a/types/settings/settings-tests.ts b/types/settings/settings-tests.ts new file mode 100644 index 0000000000..e872298796 --- /dev/null +++ b/types/settings/settings-tests.ts @@ -0,0 +1,33 @@ +import Settings = require('settings'); + +// works without options +new Settings('foo'); + +// works with path to module containing settings +const settings = new Settings('./path/to/my/config'); +// any prop is accessible on returned value +settings.hello = 'world'; +const foo = settings.bar; + +// works with settings object +const settings2 = new Settings({ + common: {setting: 'mySetting'}, production: { + hello: 'bar' + } +}); +settings2.hello = 'world'; + +// allows for forceEnv in settings object +const settings3 = new Settings({ + common: {}, + forceEnv: 'production' +}); + +// allows options +const settings4 = new Settings('./path/to/my/settings', { + root: 'someRoot', + env: 'development', + defaults: { + someSetting: 'settingValue' + } +}); diff --git a/types/settings/tsconfig.json b/types/settings/tsconfig.json new file mode 100644 index 0000000000..5fefeda97d --- /dev/null +++ b/types/settings/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "settings-tests.ts" + ] +} \ No newline at end of file diff --git a/types/settings/tslint.json b/types/settings/tslint.json new file mode 100644 index 0000000000..30a1bdde2e --- /dev/null +++ b/types/settings/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file From c1404a4a14f2d6a2bde7af7daf46cabcbb7ae5a6 Mon Sep 17 00:00:00 2001 From: shrey Date: Sat, 3 Mar 2018 10:26:30 -0500 Subject: [PATCH 2/2] Add new lines to end of file. --- types/settings/tsconfig.json | 2 +- types/settings/tslint.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/settings/tsconfig.json b/types/settings/tsconfig.json index 5fefeda97d..d565388a77 100644 --- a/types/settings/tsconfig.json +++ b/types/settings/tsconfig.json @@ -20,4 +20,4 @@ "index.d.ts", "settings-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/settings/tslint.json b/types/settings/tslint.json index 30a1bdde2e..d88586e5bd 100644 --- a/types/settings/tslint.json +++ b/types/settings/tslint.json @@ -1,3 +1,3 @@ { "extends": "dtslint/dt.json" -} \ No newline at end of file +}