mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
7.9 KiB
200 lines
7.9 KiB
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.YahooFinance = void 0;
|
|
exports.default = createYahooFinance;
|
|
/**
|
|
* @module createYahooFinance
|
|
*/
|
|
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
const options_js_1 = require("./lib/options/options.js");
|
|
const yahooFinanceFetch_js_1 = __importDefault(require("./lib/yahooFinanceFetch.js"));
|
|
const moduleExec_js_1 = __importDefault(require("./lib/moduleExec.js"));
|
|
const notices_js_1 = __importDefault(require("./lib/notices.js"));
|
|
const runtime_detect_js_1 = require("./lib/runtime-detect.js");
|
|
const defaults_js_1 = __importDefault(require("./lib/options/defaults.js"));
|
|
const deepmerge_js_1 = require("./lib/deepmerge.js");
|
|
const MIN_SUPPORTED_RUNTIMES = {
|
|
node: "22.0.0",
|
|
deno: "2.0.0",
|
|
bun: "1.0.0",
|
|
cloudflare: {
|
|
requireFeatures: [],
|
|
},
|
|
};
|
|
/**
|
|
* Instantiate a new YahooFinance client.
|
|
*
|
|
* See {@linkcode YahooFinanceOptions} for available options.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const yahooFinance = new YahooFinance(); // or YahooFinance({ ...options });
|
|
* console.log(await yahooFinance.quote("AAPL"));
|
|
* ```
|
|
*
|
|
* Internal / private properties (prefixed `_`) and shown below are not part of the public API and should not be depended on.
|
|
* You're welcome to inspect or make use of them but they might change or disappear without notice.
|
|
*
|
|
* @see The full list of {@link [modules] main modules} and {@link [other] other modules}.
|
|
* @see {@linkcode [createYahooFinance].createYahooFinance createYahooFinance} for creating an API client with custom modules (advanced use-cases only).
|
|
* @see The {@link ../../~/default.html | default} entry point that includes all modules.
|
|
*/
|
|
class YahooFinance {
|
|
_setOpts(options) {
|
|
options_js_1.setOptions.call(this, options);
|
|
}
|
|
constructor(options) {
|
|
Object.defineProperty(this, "_opts", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, "_fetch", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, "_moduleExec", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, "_notices", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
// XXX TODO remove
|
|
Object.defineProperty(this, "_env", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, "_logObj", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
this._fetch = yahooFinanceFetch_js_1.default;
|
|
this._moduleExec = moduleExec_js_1.default;
|
|
// XXX TODO remove
|
|
this._env = {
|
|
fetch: null,
|
|
};
|
|
if ("_createOpts" in this) {
|
|
const createOpts = this._createOpts;
|
|
this._opts = (0, deepmerge_js_1.deepMerge)(defaults_js_1.default, createOpts["_opts"] ?? {}, options ?? {});
|
|
if ("_allowAdditionalProps" in createOpts) {
|
|
if (!this._opts.validation)
|
|
this._opts.validation = {};
|
|
this._opts.validation.allowAdditionalProps = false;
|
|
}
|
|
if ("fetchDevel" in createOpts) {
|
|
// @ts-expect-error: later
|
|
this._env.fetchDevel = createOpts.fetchDevel;
|
|
}
|
|
}
|
|
else {
|
|
this._opts = (0, deepmerge_js_1.deepMerge)(defaults_js_1.default, options ?? {});
|
|
}
|
|
// The following rely on this._opts being set
|
|
this._notices = new notices_js_1.default(this);
|
|
// deno-coverage-ignore-start
|
|
// @ts-ignore: relevant for ts-json-schema-generator
|
|
this._logObj = dntShim.Deno.stdout.isTerminal()
|
|
// deno-lint-ignore no-explicit-any
|
|
? (obj, opts) => this._opts.logger.dir(obj, { depth: opts?.depth ?? 4, colors: true })
|
|
// deno-lint-ignore no-explicit-any
|
|
: (obj) => this._opts.logger.info(JSON.stringify(obj, null, 2));
|
|
// deno-coverage-ignore-stop
|
|
// Validate options after setting up logger, etc.
|
|
options_js_1.validateOptions.call(this, options ?? {}, "#constructor");
|
|
try {
|
|
(0, runtime_detect_js_1.assertSupported)(MIN_SUPPORTED_RUNTIMES);
|
|
}
|
|
catch (error) {
|
|
const warning = ". Things might break or work unexpectedly!";
|
|
if (error instanceof Error) {
|
|
this._opts.logger.warn("[yahoo-finance2] " + error.message + warning);
|
|
}
|
|
else {
|
|
this._opts.logger.warn("[yahoo-finance2] " + JSON.stringify(error) + warning);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
exports.YahooFinance = YahooFinance;
|
|
/**
|
|
* Create a new YahooFinance **class** with the given options (usually a list of modules,
|
|
* or special options useful for testing).
|
|
*
|
|
* @example Basic Example
|
|
* ```ts
|
|
* import quote from "yahoo-finance2/modules/quote.ts";
|
|
* import search from "yahoo-finance2/modules/search.ts";
|
|
*
|
|
* // Create a YahooFinance instance with the quote and search modules only.
|
|
* const yahooFinance = createYahooFinance({
|
|
* modules: { quote, search }
|
|
* });
|
|
* ```
|
|
*
|
|
* By using only the modules you need, you'll have a small bundle size. But remember,
|
|
* `yahoo-finance2` is never bundled to the client (browser), so your savings will be
|
|
* will be marginal (e.g. a marginally faster serverless cold start time).
|
|
*
|
|
* @param createOpts The {@link CreateYahooFinanceOptions} that influence the class creation.
|
|
* @returns A {@link YahooFinance} class that you can call with `new YahooFinance()`.
|
|
*/
|
|
function createYahooFinance(createOpts) {
|
|
const { modules, ...rest } = createOpts;
|
|
Object.assign(YahooFinance.prototype, modules);
|
|
Object.assign(YahooFinance.prototype, { _createOpts: rest });
|
|
Object.assign(YahooFinance, Object.fromEntries(Object.keys(modules).map((key) => [key, function () {
|
|
throw new Error("Call `const yahooFinance = new YahooFinance()` first. Upgrading from v2? See " +
|
|
"https://github.com/gadicc/yahoo-finance2/blob/dev/docs/UPGRADING.md.");
|
|
}])));
|
|
return YahooFinance;
|
|
}
|
|
|