fixing typescript errors

This commit is contained in:
Mke Jongbloet 2023-04-05 21:44:51 +01:00
parent fe979c63b5
commit f88ef64625
2 changed files with 32 additions and 32 deletions

8
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "stock-info", "name": "get-stock-information",
"version": "0.0.0", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "stock-info", "name": "get-stock-information",
"version": "0.0.0", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"yahoo-stock-api": "^2.1.3" "yahoo-stock-api": "^2.1.3"

View file

@ -27,6 +27,7 @@ export default class StockInfoPlugin extends Plugin {
if (n >= 1e12) return +(n / 1e12).toFixed(1) + "T"; if (n >= 1e12) return +(n / 1e12).toFixed(1) + "T";
}; };
// helper function: log error
const logError = (msg: string) => { const logError = (msg: string) => {
console.error("Error occurred:", msg); console.error("Error occurred:", msg);
new Notice( new Notice(
@ -35,6 +36,7 @@ export default class StockInfoPlugin extends Plugin {
); );
}; };
// function to call current stock prices
async function callCurrentPrices(ticker: string) { async function callCurrentPrices(ticker: string) {
try { try {
return yahoo.getSymbol({ return yahoo.getSymbol({
@ -50,42 +52,40 @@ export default class StockInfoPlugin extends Plugin {
} }
// await the results of both async calls // await the results of both async calls
const currentStockInfo = await callCurrentPrices(ticker); const callCurrentPricesResponse = await callCurrentPrices(
ticker
console.log(currentStockInfo); );
// create object to store stock return values // create object to store stock return values
const stock: { [key: string]: any } = {}; const stock: { [key: string]: number | string | null } = {};
// check the API sent back something
if (callCurrentPricesResponse) {
if (!callCurrentPricesResponse.error) {
const currentStockInfo: getSymbolResponse =
callCurrentPricesResponse.response as getSymbolResponse;
if (currentStockInfo) {
// check the API sent back something
if (!currentStockInfo.error) {
// if the API didn't send back an error // if the API didn't send back an error
stock["name"] = currentStockInfo.name ?? null; // Name of the stock stock["name"] =
callCurrentPricesResponse.name ?? null; // Name of the stock
stock["currency"] = stock["currency"] =
currentStockInfo.currency ?? null; // Currency of the stock callCurrentPricesResponse.currency ?? null; // Currency of the stock
stock["bid"] = stock["bid"] = currentStockInfo.bid.value ?? null; // Bid price of the stock
currentStockInfo.response.bid.value ?? null; // Bid price of the stock stock["ask"] = currentStockInfo.ask.value ?? null; // Ask price of the stock
stock["ask"] =
currentStockInfo.response.ask.value ?? null; // Ask price of the stock
stock["marketCap"] = stock["marketCap"] =
currentStockInfo.response.marketCap ?? null; // Market cap of the stock currentStockInfo.marketCap ?? null; // Market cap of the stock
stock["previousClose"] = stock["previousClose"] =
currentStockInfo.response.previousClose ?? null; // Previous close price of the stock currentStockInfo.previousClose ?? null; // Previous close price of the stock
stock["volume"] = stock["volume"] = currentStockInfo.volume ?? null; // Volume of shares for the stock
currentStockInfo.response.volume ?? null; // Volume of shares for the stock
stock["fiftytwo_high"] = stock["fiftytwo_high"] =
currentStockInfo.response.fiftyTwoWeekRange currentStockInfo.fiftyTwoWeekRange.high ?? null; // 52 week high
.high ?? null; // 52 week high
stock["fiftytwo_low"] = stock["fiftytwo_low"] =
currentStockInfo.response.fiftyTwoWeekRange currentStockInfo.fiftyTwoWeekRange.low ?? null; // 52 week low
.low ?? null; // 52 week low
stock["dayRange_high"] = stock["dayRange_high"] =
currentStockInfo.response.dayRange.high ?? null; // Day range high currentStockInfo.dayRange.high ?? null; // Day range high
stock["dayRange_low"] = stock["dayRange_low"] =
currentStockInfo.response.dayRange.low ?? null; // Day range low currentStockInfo.dayRange.low ?? null; // Day range low
stock["updated"] = stock["updated"] = currentStockInfo.updated ?? null; // Date and time of provided information
currentStockInfo.response.updated ?? null; // Date and time of provided information
// BUILD OUTPUT // BUILD OUTPUT
let output = "> [!info]- " + ticker + " "; let output = "> [!info]- " + ticker + " ";
@ -144,11 +144,11 @@ export default class StockInfoPlugin extends Plugin {
editor.replaceSelection(`${output}` + "\n\n"); editor.replaceSelection(`${output}` + "\n\n");
for (const r in stock) delete stock[r]; for (const r in stock) delete stock[r];
for (const r in currentStockInfo)
delete currentStockInfo[r];
} else { } else {
// API call returned an error // API call returned an error
logError(currentStockInfo.message as string); logError(
callCurrentPricesResponse.message as string
);
} }
} else { } else {
// API call returned null // API call returned null