-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsearch.ts
304 lines (282 loc) · 8.32 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { StaticDecode, Type } from "@sinclair/typebox";
import type {
ModuleOptions,
ModuleOptionsWithValidateTrue,
ModuleOptionsWithValidateFalse,
ModuleThis,
} from "../lib/moduleCommon.js";
import { YahooFinanceDate, YahooNumber } from "../lib/yahooFinanceTypes.js";
const SearchQuoteYahoo = Type.Object(
{
symbol: Type.String(), // "BABA"
isYahooFinance: Type.Literal(true), // true
exchange: Type.String(), // "NYQ"
exchDisp: Type.Optional(Type.String()), // "London", e.g. with BJ0CDD2
shortname: Type.Optional(Type.String()), // "Alibaba Group Holding Limited"
longname: Type.Optional(Type.String()), // "Alibaba Group Holding Limited"
index: Type.Literal("quotes"), // "quotes"
score: YahooNumber, // 1111958.0
newListingDate: Type.Optional(YahooFinanceDate), // "2021-02-16"
prevName: Type.Optional(Type.String()),
nameChangeDate: Type.Optional(YahooFinanceDate),
sector: Type.Optional(Type.String()), // "Industrials"
industry: Type.Optional(Type.String()), // "Building Products & Equipment"
dispSecIndFlag: Type.Optional(Type.Boolean()), // true
},
{
additionalProperties: Type.Any(),
},
);
const SearchQuoteYahooEquity = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("EQUITY"),
typeDisp: Type.Literal("Equity"),
}),
],
{
title: "SearchQuoteYahooEntity",
},
);
const SearchQuoteYahooOption = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("OPTION"),
typeDisp: Type.Literal("Option"),
}),
],
{
title: "SearchQuoteYahooOption",
},
);
const SearchQuoteYahooETF = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("ETF"),
typeDisp: Type.Literal("ETF"),
}),
],
{
title: "SearchQuoteYahooETF",
},
);
const SearchQuoteYahooFund = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("MUTUALFUND"),
typeDisp: Type.Literal("Fund"),
}),
],
{
title: "SearchQuoteYahooFund",
},
);
const SearchQuoteYahooIndex = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("INDEX"),
typeDisp: Type.Literal("Index"),
}),
],
{
title: "SearchQuoteYahooIndex",
},
);
const SearchQuoteYahooCurrency = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("CURRENCY"),
typeDisp: Type.Literal("Currency"),
}),
],
{
title: "SearchQuoteYahooCurrency",
},
);
const SearchQuoteYahooCryptocurrency = Type.Composite([
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("CRYPTOCURRENCY"),
typeDisp: Type.Literal("Cryptocurrency"),
}),
]);
const SearchQuoteYahooFuture = Type.Composite(
[
SearchQuoteYahoo,
Type.Object({
quoteType: Type.Literal("FUTURE"),
typeDisp: Type.Union([Type.Literal("Future"), Type.Literal("Futures")]),
}),
],
{
title: "SearchQuoteYahooFuture",
},
);
const SearchQuoteNonYahoo = Type.Object(
{
index: Type.String(), // '78ddc07626ff4bbcae663e88514c23a0'
name: Type.String(), // 'AAPlasma'
permalink: Type.String(), // 'aaplasma'
isYahooFinance: Type.Literal(false), // false
},
{
additionalProperties: Type.Any(),
title: "SearchQuoteNonYahoo",
},
);
const SearchNewsThumbnailResolution = Type.Object(
{
url: Type.String(),
width: YahooNumber,
height: YahooNumber,
tag: Type.String(),
},
{
title: "SearchNewsThumbnailResolution",
},
);
const SearchNews = Type.Object(
{
uuid: Type.String(), // "9aff624a-e84c-35f3-9c23-db39852006dc"
title: Type.String(), // "Analyst Report: Alibaba Group Holding Limited"
publisher: Type.String(), // "Morningstar Research"
link: Type.String(), // "https://finance.yahoo.com/m/9aff624a-e84c-35f3-9c23-db39852006dc/analyst-report%3A-alibaba-group.html"
providerPublishTime: YahooFinanceDate, // coerced to New Date(1611285342 * 1000)
type: Type.String(), // "STORY" TODO "STORY" | ???
thumbnail: Type.Optional(
Type.Object({
resolutions: Type.Array(SearchNewsThumbnailResolution),
}),
),
relatedTickers: Type.Optional(Type.Array(Type.String())), // [ "AAPL" ]
},
{
additionalProperties: Type.Any(),
title: "SearchNews",
},
);
export type SearchResult = StaticDecode<typeof SearchResultSchema>;
const SearchResultSchema = Type.Object(
{
explains: Type.Array(Type.Any()),
count: YahooNumber,
quotes: Type.Array(
Type.Union([
SearchQuoteYahooEquity,
SearchQuoteYahooOption,
SearchQuoteYahooETF,
SearchQuoteYahooFund,
SearchQuoteYahooIndex,
SearchQuoteYahooCurrency,
SearchQuoteYahooCryptocurrency,
SearchQuoteNonYahoo,
SearchQuoteYahooFuture,
]),
),
news: Type.Array(SearchNews),
nav: Type.Array(Type.Any()),
lists: Type.Array(Type.Any()),
researchReports: Type.Array(Type.Any()),
totalTime: YahooNumber,
// ALWAYS present, but TEMPORARILY marked optional ("?") since its
// sudden appearance, let's make sure it doesn't get suddenly removed.
// Array<any> until we can find some examples of what it actually looks
// like (#255).
screenerFieldResults: Type.Optional(Type.Array(Type.Any())),
// ALWAYS present, but TEMPORARILY marked optional ("?") since its
// sudden appearance, let's make sure it doesn't get suddenly removed.
// Array<any> until we can find some examples of what it actually looks
// like (#399).
culturalAssets: Type.Optional(Type.Array(Type.Any())),
timeTakenForQuotes: YahooNumber, // 26
timeTakenForNews: YahooNumber, // 419
timeTakenForAlgowatchlist: YahooNumber, // 700
timeTakenForPredefinedScreener: YahooNumber, // 400
timeTakenForCrunchbase: YahooNumber, // 400
timeTakenForNav: YahooNumber, // 400
timeTakenForResearchReports: YahooNumber, // 0
// ALWAYS present, but TEMPORARILY marked optional ("?") since its
// sudden appearance, let's make sure it doesn't get suddenly removed.
timeTakenForScreenerField: Type.Optional(YahooNumber),
// ALWAYS present, but TEMPORARILY marked optional ("?") since its
// sudden appearance, let's make sure it doesn't get suddenly removed.
timeTakenForCulturalAssets: Type.Optional(YahooNumber),
},
{
additionalProperties: Type.Any(),
title: "SearchResults",
},
);
const SearchOptionsSchema = Type.Object(
{
lang: Type.Optional(Type.String()),
region: Type.Optional(Type.String()),
quotesCount: Type.Optional(YahooNumber),
newsCount: Type.Optional(YahooNumber),
enableFuzzyQuery: Type.Optional(Type.Boolean()),
quotesQueryId: Type.Optional(Type.String()),
multiQuoteQueryId: Type.Optional(Type.String()),
newsQueryId: Type.Optional(Type.String()),
enableCb: Type.Optional(Type.Boolean()),
enableNavLinks: Type.Optional(Type.Boolean()),
enableEnhancedTrivialQuery: Type.Optional(Type.Boolean()),
},
{
title: "SearchOptions",
additionalProperties: false,
},
);
export type SearchOptions = StaticDecode<typeof SearchOptionsSchema>;
const queryOptionsDefaults: SearchOptions = {
lang: "en-US",
region: "US",
quotesCount: 6,
newsCount: 4,
enableFuzzyQuery: false,
quotesQueryId: "tss_match_phrase_query",
multiQuoteQueryId: "multi_quote_single_token_query",
newsQueryId: "news_cie_vespa",
enableCb: true,
enableNavLinks: true,
enableEnhancedTrivialQuery: true,
};
export default function search(
this: ModuleThis,
query: string,
queryOptionsOverrides?: SearchOptions,
moduleOptions?: ModuleOptionsWithValidateTrue,
): Promise<SearchResult>;
export default function search(
this: ModuleThis,
query: string,
queryOptionsOverrides?: SearchOptions,
moduleOptions?: ModuleOptionsWithValidateFalse,
): Promise<any>;
export default function search(
this: ModuleThis,
query: string,
queryOptionsOverrides?: SearchOptions,
moduleOptions?: ModuleOptions,
): Promise<any> {
return this._moduleExec({
moduleName: "searchTypebox",
query: {
url: "https://${YF_QUERY_HOST}/v1/finance/search",
schema: SearchOptionsSchema,
defaults: queryOptionsDefaults,
runtime: { q: query },
overrides: queryOptionsOverrides,
needsCrumb: false,
},
result: {
schema: SearchResultSchema,
},
moduleOptions,
});
}