Skip to content

Commit

Permalink
feat(notices): notices lib, refactor, use for "yahooSurvey" (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Aug 7, 2024
1 parent 8e6407e commit b4c016b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/index-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import yahooFinanceFetch from "./lib/yahooFinanceFetch.js";
import options from "./lib/options.js";
import errors from "./lib/errors.js";
import setGlobalConfig from "./lib/setGlobalConfig.js";
import moduleExec from "./lib/moduleExec.js";
import { suppressNotices } from "./lib/notices.js";

// modules
import autoc from "./modules/autoc.js";
Expand Down Expand Up @@ -33,6 +33,7 @@ export default {
// common
errors,
setGlobalConfig,
suppressNotices,

// modules,
autoc,
Expand Down
12 changes: 2 additions & 10 deletions src/lib/getCrumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ExtendedCookieJar } from "./cookieJar";
import pkg from "../../package.json";
import { Logger } from "./options.js";
import { Cookie } from "tough-cookie";
import { showNotice } from "./notices.js";

const CONFIG_FAKE_URL = "http://config.yf2/";

Expand Down Expand Up @@ -342,8 +343,6 @@ export async function getCrumbClear(cookieJar: ExtendedCookieJar) {
await cookieJar.removeAllCookies();
}

let shownYahooSurvey = false;

export default function getCrumb(
cookieJar: ExtendedCookieJar,
fetch: (url: RequestInfo, init?: RequestInit) => Promise<Response>,
Expand All @@ -352,14 +351,7 @@ export default function getCrumb(
url = "https://finance.yahoo.com/quote/AAPL",
__getCrumb = _getCrumb
) {
if (!shownYahooSurvey) {
logger.info(
"Please consider completing the survey at https://bit.ly/yahoo-finance-api-feedback " +
"if you haven't already; for more info see " +
"https://github.com/gadicc/node-yahoo-finance2/issues/764#issuecomment-2056623851."
);
shownYahooSurvey = true;
}
showNotice("yahooSurvey");

if (!promise)
promise = __getCrumb(cookieJar, fetch, fetchOptionsBase, logger, url);
Expand Down
46 changes: 46 additions & 0 deletions src/lib/notices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import options from "./options.js";
const logger = options.logger || console;

type Notice = {
id: string;
text: string;
level?: keyof typeof logger;
onceOnly?: boolean;
suppress?: boolean;
};

const notices = {
yahooSurvey: {
id: "yahooSurvey",
text:
"Please consider completing the survey at https://bit.ly/yahoo-finance-api-feedback " +
"if you haven't already; for more info see " +
"https://github.com/gadicc/node-yahoo-finance2/issues/764#issuecomment-2056623851.",
onceOnly: true,
} as Notice,
};

export function showNotice(id: keyof typeof notices) {
const n = notices[id];
if (!n) throw new Error(`Unknown notice id: ${id}`);

if (n.suppress) return;
if (n.onceOnly) n.suppress = true;

const text =
n.text +
// (n.onceOnly ? " (only shown once)" : "") +
" You can supress this message in future with `yahooFinance.supressNotices(['" +
id +
"'])`.";
const level = n.level || "info";
logger[level](text);
}

export function suppressNotices(noticeIds: (keyof typeof notices)[]) {
noticeIds.forEach((id) => {
const n = notices[id];
if (!n) logger.error(`Unknown notice id: ${id}`);
n.suppress = true;
});
}
3 changes: 3 additions & 0 deletions tests/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { suppressNotices } from "../src/lib/notices";
suppressNotices(["yahooSurvey"]);

import { toBeType } from "jest-tobetype";

expect.extend({
Expand Down

0 comments on commit b4c016b

Please sign in to comment.