Skip to content

Commit

Permalink
feat(quote): allow ?fields= argument, see docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Mar 11, 2021
1 parent 6815add commit b597954
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 8 deletions.
13 changes: 12 additions & 1 deletion docs/modules/quote.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ an array of symbols, and you'll receive an array of results back.

### Query Options

The `quote` module doesn't have any options. Find some? Let us know!
| Name | Type | Default | Description |
| ------------- | ----------| ---------- | --------------------------------- |
| `fields` | string[] | (all) | Which fields to return in query

```js
// Don't return all fields, only return these two + other essentials.
await quote('TSLA', { fields: [ "symbol", "displayName" ] });
```

Note: some fields are *always* returned, e.g. `language`, `quoteType`, etc.
For a list of all fields, see the `Quote` interface in
[quote.ts](https://github.com/gadicc/node-yahoo-finance2/blob/devel/src/modules/quote.ts).

### Module Options

Expand Down
109 changes: 104 additions & 5 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2767,7 +2767,6 @@
"marketState",
"tradeable",
"exchange",
"shortName",
"exchangeTimezoneName",
"exchangeTimezoneShortName",
"gmtOffSetMilliseconds",
Expand Down Expand Up @@ -3085,7 +3084,6 @@
"priceHint",
"quoteType",
"region",
"shortName",
"sourceInterval",
"startDate",
"symbol",
Expand Down Expand Up @@ -3373,7 +3371,6 @@
"priceHint",
"quoteType",
"region",
"shortName",
"sourceInterval",
"symbol",
"tradeable",
Expand Down Expand Up @@ -3657,14 +3654,109 @@
"priceHint",
"quoteType",
"region",
"shortName",
"sourceInterval",
"symbol",
"tradeable",
"triggerable"
],
"type": "object"
},
"QuoteField": {
"enum": [
"quoteType",
"language",
"region",
"quoteSourceName",
"triggerable",
"currency",
"marketState",
"tradeable",
"exchange",
"shortName",
"longName",
"messageBoardId",
"exchangeTimezoneName",
"exchangeTimezoneShortName",
"gmtOffSetMilliseconds",
"market",
"esgPopulated",
"fiftyTwoWeekLowChange",
"fiftyTwoWeekLowChangePercent",
"fiftyTwoWeekRange",
"fiftyTwoWeekHighChange",
"fiftyTwoWeekHighChangePercent",
"fiftyTwoWeekLow",
"fiftyTwoWeekHigh",
"dividendDate",
"earningsTimestamp",
"earningsTimestampStart",
"earningsTimestampEnd",
"trailingAnnualDividendRate",
"trailingPE",
"trailingAnnualDividendYield",
"epsTrailingTwelveMonths",
"epsForward",
"epsCurrentYear",
"priceEpsCurrentYear",
"sharesOutstanding",
"bookValue",
"fiftyDayAverage",
"fiftyDayAverageChange",
"fiftyDayAverageChangePercent",
"twoHundredDayAverage",
"twoHundredDayAverageChange",
"twoHundredDayAverageChangePercent",
"marketCap",
"forwardPE",
"priceToBook",
"sourceInterval",
"exchangeDataDelayedBy",
"firstTradeDateMilliseconds",
"priceHint",
"postMarketChangePercent",
"postMarketTime",
"postMarketPrice",
"postMarketChange",
"regularMarketChange",
"regularMarketChangePercent",
"regularMarketTime",
"regularMarketPrice",
"regularMarketDayHigh",
"regularMarketDayRange",
"regularMarketDayLow",
"regularMarketVolume",
"regularMarketPreviousClose",
"preMarketChange",
"preMarketChangePercent",
"preMarketTime",
"preMarketPrice",
"bid",
"ask",
"bidSize",
"askSize",
"fullExchangeName",
"financialCurrency",
"regularMarketOpen",
"averageDailyVolume3Month",
"averageDailyVolume10Day",
"displayName",
"symbol",
"ytdReturn",
"trailingThreeMonthReturns",
"trailingThreeMonthNavReturns",
"ipoExpectedDate",
"newListingDate",
"circulatingSupply",
"fromCurrency",
"toCurrency",
"lastMarket",
"coinImageUrl",
"volume24Hr",
"volumeAllCurrencies",
"startDate"
],
"type": "string"
},
"QuoteMutualfund": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -3941,7 +4033,6 @@
"priceHint",
"quoteType",
"region",
"shortName",
"sourceInterval",
"symbol",
"tradeable",
Expand All @@ -3951,6 +4042,14 @@
},
"QuoteOptions": {
"additionalProperties": false,
"properties": {
"fields": {
"items": {
"$ref": "#/definitions/QuoteField"
},
"type": "array"
}
},
"type": "object"
},
"QuoteResponse": {
Expand Down
16 changes: 16 additions & 0 deletions src/modules/quote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ describe("quote", () => {
yf.quote("AAPL", {}, { devel: "weirdJsonResult.fake.json" })
).rejects.toThrow(/Unexpected result/);
});

it("passes through single ?fields", async () => {
const devel = "quote-TSLA-fields-symbol.json";
const queryOpts = { fields: ["symbol"] };
const result = await yf.quote("TSLA", queryOpts, { devel });
expect(result.symbol).toBe("TSLA");
expect(result.displayName).not.toBeDefined();
});

it("passes through multiple ?fields", async () => {
const devel = "quote-TSLA-fields-symbol-shortName.json";
const queryOpts = { fields: ["symbol", "displayName"] };
const result = await yf.quote("TSLA", queryOpts, { devel });
expect(result.symbol).toBe("TSLA");
expect(result.displayName).toBeDefined();
});
});
31 changes: 29 additions & 2 deletions src/modules/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface QuoteBase {
marketState: "REGULAR" | "CLOSED" | "PRE" | "PREPRE" | "POST" | "POSTPOST";
tradeable: boolean; // false,
exchange: string; // "NMS",
shortName: string; // "NVIDIA Corporation",
shortName?: string; // "NVIDIA Corporation",
longName?: string; // "NVIDIA Corporation",
messageBoardId?: string; // "finmb_32307",
exchangeTimezoneName: string; // "America/New_York",
Expand Down Expand Up @@ -95,6 +95,24 @@ export interface QuoteBase {
newListingDate?: Date; // "2021-02-16",
}

/*
* [TODO] Fields seen in a query but not in this module yet:
*
* - extendedMarketChange
* - extendedMarketChangePercent
* - extendedMarketPrice
* - extendedMarketTime
* - dayHigh (separate to regularMarketDayHigh, etc)
* - dayLow (separate to regularMarketDayLow, etc)
* - volume (separaet to regularMarketVolume, etc)
*
* i.e. on yahoo site, with ?fields=dayHigh,dayLow,etc.
*/

/*
* Guaranteed fields, even we don't ask for them
*/

export interface QuoteEquity extends QuoteBase {
quoteType: "EQUITY";
}
Expand Down Expand Up @@ -127,7 +145,11 @@ export type Quote =

export type QuoteResponse = Quote[];

export interface QuoteOptions {}
export type QuoteField = keyof Quote;

export interface QuoteOptions {
fields?: QuoteField[];
}

const queryOptionsDefaults = {};

Expand Down Expand Up @@ -169,6 +191,11 @@ export default function quote(
defaults: queryOptionsDefaults,
runtime: { symbols },
overrides: queryOptionsOverrides,
transformWith(queryOptions: QuoteOptions) {
// Options validation ensures this is a string[]
if (queryOptions.fields) queryOptions.fields.join(",");
return queryOptions;
},
},

result: {
Expand Down
101 changes: 101 additions & 0 deletions tests/http/quote-TSLA-fields-symbol-shortName.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"request": {
"url": "https://query2.finance.yahoo.com/v7/finance/quote?symbols=TSLA&fields=symbol%2CdisplayName"
},
"response": {
"ok": true,
"status": 200,
"statusText": "OK",
"headers": {
"content-type": [
"application/json"
],
"cache-control": [
"public, max-age=1, stale-while-revalidate=9"
],
"vary": [
"Origin,Accept-Encoding"
],
"y-rid": [
"78bujftg4k00j"
],
"x-yahoo-request-id": [
"78bujftg4k00j"
],
"x-request-id": [
"ba6aaf23-46cb-4468-9e9d-1b1887cd9dce"
],
"content-encoding": [
"gzip"
],
"content-length": [
"342"
],
"x-envoy-upstream-service-time": [
"2"
],
"date": [
"Thu, 11 Mar 2021 11:33:39 GMT"
],
"server": [
"ATS"
],
"x-envoy-decorator-operation": [
"finance-quote-api--mtls-production-ir2.finance-k8s.svc.yahoo.local:4080/*"
],
"age": [
"0"
],
"strict-transport-security": [
"max-age=15552000"
],
"referrer-policy": [
"no-referrer-when-downgrade"
],
"x-frame-options": [
"SAMEORIGIN"
],
"connection": [
"close"
],
"expect-ct": [
"max-age=31536000, report-uri=\"http://csp.yahoo.com/beacon/csp?src=yahoocom-expect-ct-report-only\""
],
"x-xss-protection": [
"1; mode=block"
],
"x-content-type-options": [
"nosniff"
]
},
"bodyJson": {
"quoteResponse": {
"result": [
{
"language": "en-US",
"region": "US",
"quoteType": "EQUITY",
"quoteSourceName": "Delayed Quote",
"triggerable": true,
"firstTradeDateMilliseconds": 1277818200000,
"priceHint": 2,
"fullExchangeName": "NasdaqGS",
"exchange": "NMS",
"sourceInterval": 15,
"exchangeDataDelayedBy": 0,
"tradeable": false,
"exchangeTimezoneName": "America/New_York",
"exchangeTimezoneShortName": "EST",
"gmtOffSetMilliseconds": -18000000,
"market": "us_market",
"esgPopulated": false,
"marketState": "PRE",
"displayName": "Tesla",
"symbol": "TSLA"
}
],
"error": null
}
}
}
}
Loading

0 comments on commit b597954

Please sign in to comment.