-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(setGlobalConfig): add setGlobalConfig function (#133)
* feat(setGlobalConfig): add setGlobalConfig function * chore: remove unneeded todo note * docs(setGlobalConfig): add docs for setGlobalConfig * fix(setGlobalConfig): merge multiple levels deep * Merges config multiple levels deep. Also fixes docs and tests.
- Loading branch information
1 parent
bc20d16
commit 43ebaa4
Showing
10 changed files
with
282 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# setGlobalConfig | ||
|
||
This util function sets global config options, merging with the defaults. These options are then the defaults used for every request. It is the **recommended** way of setting global config. Setting global config directly is not recommended. | ||
|
||
## Usage: | ||
|
||
```js | ||
import yahooFinance from 'yahoo-finance2'; | ||
|
||
yahooFinance.setGlobalConfig({ | ||
queue: { | ||
// some options here | ||
} | ||
}); | ||
``` | ||
|
||
Notes: | ||
|
||
- Config provided to this function is validated. | ||
|
||
- Options are merged infinite levels deep: | ||
```js | ||
import yahooFinance from 'yahoo-finance2'; | ||
|
||
yahooFinance.setGlobalConfig({ | ||
queue: { | ||
concurrency: 2, | ||
// timeout is still set | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import testYf from "../../tests/testYf"; | ||
import options from "./options"; | ||
import setGlobalConfig from "./setGlobalConfig"; | ||
const yf = testYf({ setGlobalConfig }); | ||
|
||
describe("setGlobalConfig", () => { | ||
const optionsBackup = JSON.parse(JSON.stringify(options)); | ||
beforeEach(() => { | ||
yf._opts = JSON.parse(JSON.stringify(optionsBackup)); | ||
}); | ||
|
||
it("sets config options and passes validation", () => { | ||
const configOverrides = { queue: { concurrency: 10, timeout: 90 } }; | ||
yf.setGlobalConfig(configOverrides); | ||
expect(yf._opts).toEqual({ ...optionsBackup, ...configOverrides }); | ||
}); | ||
it("sets config options multiple levels deep", () => { | ||
const configOverrides = { queue: { concurrency: 10 } }; | ||
yf.setGlobalConfig(configOverrides); | ||
expect(yf._opts.queue).toEqual({ | ||
concurrency: 10, | ||
timeout: optionsBackup.queue.timeout, | ||
}); | ||
}); | ||
it("should throw on invalid config", () => { | ||
expect(() => yf.setGlobalConfig({ queue: { abc: "" } })).toThrow( | ||
/yahooFinance.setGlobalConfig called with invalid options\./ | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Options } from "../typings/interfaces"; | ||
import { ModuleThis } from "./moduleCommon"; | ||
import validateAndCoerceTypes from "./validateAndCoerceTypes"; | ||
|
||
export default function setGlobalConfig( | ||
this: ModuleThis, | ||
config: Partial<Options> | ||
): void { | ||
validateAndCoerceTypes({ | ||
object: config, | ||
source: "setGlobalConfig", | ||
type: "options", | ||
options: this._opts.validation, | ||
schemaKey: "#/definitions/PartialOptions", | ||
}); | ||
mergeObjects(this._opts, config as Obj); | ||
} | ||
|
||
type Obj = Record<string, string | ObjRecurse>; | ||
|
||
// This is fine, since this is just a hack for recursive types | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface ObjRecurse extends Obj {} | ||
|
||
function mergeObjects(original: Obj, objToMerge: Obj) { | ||
const ownKeys: (keyof typeof objToMerge)[] = Reflect.ownKeys( | ||
objToMerge | ||
) as string[]; | ||
for (const key of ownKeys) { | ||
if (typeof objToMerge[key] === "object") { | ||
mergeObjects(original[key] as Obj, objToMerge[key] as Obj); | ||
} else { | ||
original[key] = objToMerge[key]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { QueueOptions } from "../lib/queue"; | ||
import { ValidationOptions } from "../lib/validateAndCoerceTypes"; | ||
|
||
export interface PartialOptions { | ||
queue?: QueueOptions; | ||
validation?: ValidationOptions; | ||
} | ||
|
||
export type Options = Required<PartialOptions>; |