Skip to content

Commit

Permalink
fix(crumb): needsCrumb option, and in quote() only
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed May 7, 2023
1 parent c21f025 commit cf92258
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/lib/moduleExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ interface ModuleExecOptions {
* Default: 'json'. Can be 'text' or 'csv' (augments fetch's "text").
*/
fetchType?: string;
/**
* Default: false. This request requires Yahoo cookies & crumb.
*/
needsCrumb: boolean;
};

result: {
Expand Down Expand Up @@ -145,7 +149,8 @@ async function moduleExec(this: ThisWithModExec, opts: ModuleExecOptions) {
queryOpts.url,
queryOptions,
moduleOpts,
queryOpts.fetchType
queryOpts.fetchType,
queryOpts.needsCrumb
);

if (queryOpts.fetchType === "csv") result = csv2json(result);
Expand Down
9 changes: 6 additions & 3 deletions src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ async function yahooFinanceFetch(
urlBase: string,
params = {},
moduleOpts: YahooFinanceFetchModuleOptions = {},
func = "json"
func = "json",
needsCrumb = false
) {
if (!(this && this._env))
throw new errors.NoEnvironmentError(
Expand All @@ -91,8 +92,10 @@ async function yahooFinanceFetch(
},
};

// @ts-expect-error: TODO, crumb string type for partial params
params.crumb = await getCrumb(fetchFunc, fetchOptionsBase);
if (needsCrumb) {
// @ts-expect-error: TODO, crumb string type for partial params
params.crumb = await getCrumb(fetchFunc, fetchOptionsBase);
}

// @ts-expect-error: TODO copy interface? @types lib?
const urlSearchParams = new URLSearchParams(params);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ export default async function quote(

query: {
url: "https://${YF_QUERY_HOST}/v7/finance/quote",
schemaKey: "#/definitions/QuoteOptions",
needsCrumb: true,
schemaKey: "#/definitions/QuoteOptions",
defaults: queryOptionsDefaults,
runtime: { symbols },
overrides: queryOptionsOverrides,
Expand Down

0 comments on commit cf92258

Please sign in to comment.