Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(setGlobalConfig): add setGlobalConfig function #133

Merged
merged 4 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ symbol, topHoldings, upgradeDowngradeHistory),
[coming soon](https://github.com/gadicc/node-yahoo-finance2/issues/8).

Extras: [`quoteCombine`](./docs/other/quoteCombine.md).
Utils: [`setGlobalConfig`](./docs/utils/setGlobalConfig.md).

See the [Full Documentation](./docs/README.md).

Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
1. [Common Options](#common-options)
1. [Modules](#modules)
1. [Other Methods](#other)
1. [Util Methods](#utils)
1. [Error Handling](#error-handling)
1. [Validation](./validation.md)
1. [Concurrency](./concurrency.md)
Expand Down Expand Up @@ -44,6 +45,9 @@ const result = await yahooFinance.module(query, queryOpts, moduleOpts);

1. [quoteCombine](./other/quoteCombine.md) - debounce and combine multiple quote calls.

<a name="utils"></a>
1. [setGlobalConfig](./utils/setGlobalConfig.md) - set global config options.

<a name="error-handling"></a>
## Error Handling

Expand Down
31 changes: 31 additions & 0 deletions docs/other/setGlobalConfig.md
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
}
});
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"scripts": {
"coverage": "jest --coverage",
"lint": "eslint . --ext .js,.ts",
"schema": "ts-json-schema-generator -f tsconfig.json -p 'src/modules/**/*.ts' -t '*' | node bin/schema-tweak.js > schema.json",
"schema": "ts-json-schema-generator -f tsconfig.json -p 'src/{modules,typings}/**/*.ts' -t '*' | node bin/schema-tweak.js > schema.json",
"generateSchema": "yarn schema",
"prepublishOnly": "tsc && yarn generateSchema",
"test": "jest",
Expand Down
166 changes: 166 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,18 @@
],
"type": "object"
},
"Options": {
"additionalProperties": false,
"properties": {
"queue": {
"$ref": "#/definitions/QueueOptions"
},
"validation": {
"$ref": "#/definitions/ValidationOptions"
}
},
"type": "object"
},
"OptionsOptions": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -2188,6 +2200,18 @@
],
"type": "object"
},
"PartialOptions": {
"additionalProperties": false,
"properties": {
"queue": {
"$ref": "#/definitions/QueueOptions"
},
"validation": {
"$ref": "#/definitions/ValidationOptions"
}
},
"type": "object"
},
"PeriodRange": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -2380,6 +2404,112 @@
],
"type": "object"
},
"Queue": {
"additionalProperties": false,
"properties": {
"_queue": {
"items": {
"additionalProperties": false,
"properties": {
"func": {
"additionalProperties": false,
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
},
"length": {
"yahooFinanceType": "number"
},
"prototype": {}
},
"required": [
"prototype",
"length",
"arguments",
"caller"
],
"type": "object"
},
"reject": {
"additionalProperties": false,
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
},
"length": {
"yahooFinanceType": "number"
},
"prototype": {}
},
"required": [
"prototype",
"length",
"arguments",
"caller"
],
"type": "object"
},
"resolve": {
"additionalProperties": false,
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
},
"length": {
"yahooFinanceType": "number"
},
"prototype": {}
},
"required": [
"prototype",
"length",
"arguments",
"caller"
],
"type": "object"
}
},
"required": [
"func",
"resolve",
"reject"
],
"type": "object"
},
"type": "array"
},
"_running": {
"yahooFinanceType": "number"
},
"concurrency": {
"yahooFinanceType": "number"
}
},
"required": [
"concurrency",
"_running",
"_queue"
],
"type": "object"
},
"QueueOptions": {
"additionalProperties": false,
"properties": {
"_queue": {
"$ref": "#/definitions/Queue"
},
"concurrency": {
"yahooFinanceType": "number"
},
"timeout": {
"yahooFinanceType": "number"
}
},
"type": "object"
},
"Quote": {
"anyOf": [
{
Expand Down Expand Up @@ -6058,6 +6188,22 @@
],
"type": "object"
},
"ValidationOptions": {
"additionalProperties": false,
"properties": {
"logErrors": {
"type": "boolean"
},
"logOptionsErrors": {
"type": "boolean"
}
},
"required": [
"logErrors",
"logOptionsErrors"
],
"type": "object"
},
"Yearly": {
"additionalProperties": false,
"properties": {
Expand All @@ -6077,6 +6223,26 @@
"earnings"
],
"type": "object"
},
"interface-2073358172-9814-11278-2073358172-0-212312": {
"additionalProperties": false,
"properties": {
"arguments": {},
"caller": {
"$ref": "#/definitions/interface-2073358172-9814-11278-2073358172-0-212312"
},
"length": {
"yahooFinanceType": "number"
},
"prototype": {}
},
"required": [
"prototype",
"length",
"arguments",
"caller"
],
"type": "object"
}
}
}
9 changes: 3 additions & 6 deletions src/lib/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// TODO, keep defaults there too?
import type { ValidationOptions } from "./validateAndCoerceTypes";
import type { QueueOptions } from "./queue";
// import type { ValidationOptions } from "./validateAndCoerceTypes";
// import type { QueueOptions } from "./queue";

export interface Options {
queue: QueueOptions;
validation: ValidationOptions;
}
import { Options } from "../typings/interfaces";

const options: Options = {
queue: {
Expand Down
30 changes: 30 additions & 0 deletions src/lib/setGlobalConfig.spec.ts
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\./
);
});
});
36 changes: 36 additions & 0 deletions src/lib/setGlobalConfig.ts
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];
}
}
}
2 changes: 1 addition & 1 deletion src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Queue from "./queue";

import type { Options } from "./options";
import type { Options } from "../typings/interfaces";
import type { QueueOptions } from "./queue";

import errors from "./errors";
Expand Down
9 changes: 9 additions & 0 deletions src/typings/interfaces.ts
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>;