@narumitw/pi-ticker 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -47
- package/dist/chunks/{chunk-U5NETXSC.js → chunk-5QPNCY3F.ts} +1 -1
- package/dist/chunks/chunk-5QPNCY3F.ts.map +7 -0
- package/dist/chunks/{menu-WR7QV3P4.js → menu-L3RY6JMO.ts} +7 -10
- package/dist/chunks/menu-L3RY6JMO.ts.map +7 -0
- package/dist/chunks/{tui-menu-KQLX7NJ5.js → tui-menu-26V6EKZK.ts} +12 -33
- package/dist/chunks/tui-menu-26V6EKZK.ts.map +7 -0
- package/dist/index.ts +5 -17
- package/dist/index.ts.map +2 -2
- package/package.json +54 -55
- package/src/menu.ts +207 -226
- package/src/quotes.ts +67 -73
- package/src/render.ts +107 -114
- package/src/settings.ts +105 -111
- package/src/ticker.ts +263 -286
- package/src/tui-menu.ts +416 -457
- package/dist/chunks/chunk-U5NETXSC.js.map +0 -7
- package/dist/chunks/menu-WR7QV3P4.js.map +0 -7
- package/dist/chunks/tui-menu-KQLX7NJ5.js.map +0 -7
package/src/menu.ts
CHANGED
|
@@ -2,240 +2,221 @@ import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
|
2
2
|
import { parseSymbols } from "./settings.js";
|
|
3
3
|
|
|
4
4
|
type Screen = "tickers" | "order" | "move";
|
|
5
|
-
type Action =
|
|
6
|
-
| "toggle"
|
|
7
|
-
| "addCustom"
|
|
8
|
-
| "toggleWidget"
|
|
9
|
-
| "chooseTicker"
|
|
10
|
-
| "moveUp"
|
|
11
|
-
| "moveDown"
|
|
12
|
-
| "refresh";
|
|
5
|
+
type Action = "toggle" | "addCustom" | "toggleWidget" | "chooseTicker" | "moveUp" | "moveDown" | "refresh";
|
|
13
6
|
|
|
14
7
|
export interface TickerMenuOptions {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
signal: AbortSignal;
|
|
9
|
+
isCurrent(): boolean;
|
|
10
|
+
getSymbols(): readonly string[];
|
|
11
|
+
applySymbols(symbols: readonly string[], signal: AbortSignal): Promise<void>;
|
|
12
|
+
getWidgetEnabled(): boolean;
|
|
13
|
+
applyWidgetEnabled(widgetEnabled: boolean, signal: AbortSignal): Promise<void>;
|
|
14
|
+
refresh(): void;
|
|
22
15
|
}
|
|
23
16
|
|
|
24
|
-
export async function showTickerMenu(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
await showTickerTuiMenu(ctx, options);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
17
|
+
export async function showTickerMenu(ctx: ExtensionCommandContext, options: TickerMenuOptions): Promise<void> {
|
|
18
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
19
|
+
if (ctx.mode === "tui") {
|
|
20
|
+
const { showTickerTuiMenu } = await import("./tui-menu.js");
|
|
21
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
22
|
+
await showTickerTuiMenu(ctx, options);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
35
25
|
|
|
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
|
-
moveUp: ({ signal }) => moveSelectedTicker(-1, signal),
|
|
200
|
-
moveDown: ({ signal }) => moveSelectedTicker(1, signal),
|
|
201
|
-
refresh: () => {
|
|
202
|
-
options.refresh();
|
|
203
|
-
return { kind: "stay" };
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
});
|
|
26
|
+
const { defineMenu, runMenu, sanitizeTerminalText } = await import("@narumitw/pi-tui-kit");
|
|
27
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
28
|
+
let menuCandidates = [...options.getSymbols()];
|
|
29
|
+
let movingSymbol: string | undefined;
|
|
30
|
+
const candidates = () => {
|
|
31
|
+
for (const symbol of options.getSymbols()) {
|
|
32
|
+
if (!menuCandidates.includes(symbol)) menuCandidates.push(symbol);
|
|
33
|
+
}
|
|
34
|
+
return menuCandidates;
|
|
35
|
+
};
|
|
36
|
+
const menu = defineMenu<undefined, Screen, Action, ExtensionCommandContext>({
|
|
37
|
+
start: "tickers",
|
|
38
|
+
screens: {
|
|
39
|
+
tickers: () => {
|
|
40
|
+
const selected = new Set(options.getSymbols());
|
|
41
|
+
return {
|
|
42
|
+
kind: "multiSelect",
|
|
43
|
+
title: "Manage tickers",
|
|
44
|
+
lines: [`${selected.size} selected · uncheck to remove · changes save immediately`],
|
|
45
|
+
items: candidates().map((symbol) => ({
|
|
46
|
+
id: symbol,
|
|
47
|
+
label: symbol,
|
|
48
|
+
description: "Display this Yahoo Finance symbol in the editor widget.",
|
|
49
|
+
selected: selected.has(symbol),
|
|
50
|
+
searchText: symbol.replaceAll("-", " "),
|
|
51
|
+
})),
|
|
52
|
+
action: "toggle",
|
|
53
|
+
enableSearch: true,
|
|
54
|
+
viewportSize: 10,
|
|
55
|
+
actions: [
|
|
56
|
+
{
|
|
57
|
+
id: "add-custom",
|
|
58
|
+
label: "Add custom ticker…",
|
|
59
|
+
description: "Add and select one Yahoo Finance symbol.",
|
|
60
|
+
action: "addCustom",
|
|
61
|
+
},
|
|
62
|
+
...(selected.size > 1
|
|
63
|
+
? [
|
|
64
|
+
{
|
|
65
|
+
id: "reorder",
|
|
66
|
+
label: "Reorder tickers…",
|
|
67
|
+
description: "Change the order shown in the editor widget.",
|
|
68
|
+
to: "order" as const,
|
|
69
|
+
},
|
|
70
|
+
]
|
|
71
|
+
: []),
|
|
72
|
+
{
|
|
73
|
+
id: "widget-enabled",
|
|
74
|
+
label: `Widget: ${options.getWidgetEnabled() ? "On" : "Off"}`,
|
|
75
|
+
description: "Show or hide the editor widget and persist this setting.",
|
|
76
|
+
action: "toggleWidget",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "refresh",
|
|
80
|
+
label: "Refresh now",
|
|
81
|
+
description: options.getWidgetEnabled()
|
|
82
|
+
? "Fetch the selected symbols immediately."
|
|
83
|
+
: "Enable the widget before refreshing.",
|
|
84
|
+
action: "refresh",
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
hint: "close",
|
|
88
|
+
doneLabel: "Close",
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
order: () => ({
|
|
92
|
+
kind: "choice",
|
|
93
|
+
title: "Ticker order",
|
|
94
|
+
lines: ["Choose a ticker to move."],
|
|
95
|
+
items: options.getSymbols().map((symbol, index) => ({
|
|
96
|
+
id: symbol,
|
|
97
|
+
label: `${index + 1}. ${symbol}`,
|
|
98
|
+
description: "Select this ticker, then move it up or down.",
|
|
99
|
+
})),
|
|
100
|
+
action: "chooseTicker",
|
|
101
|
+
viewportSize: 10,
|
|
102
|
+
}),
|
|
103
|
+
move: () => {
|
|
104
|
+
const symbols = options.getSymbols();
|
|
105
|
+
const index = movingSymbol ? symbols.indexOf(movingSymbol) : -1;
|
|
106
|
+
const available = index >= 0;
|
|
107
|
+
return {
|
|
108
|
+
kind: "actions",
|
|
109
|
+
title: movingSymbol ? `Move ${movingSymbol}` : "Move ticker",
|
|
110
|
+
lines: [
|
|
111
|
+
available ? `Position ${index + 1} of ${symbols.length}` : "Ticker is no longer selected.",
|
|
112
|
+
`Order: ${symbols.join(" · ")}`,
|
|
113
|
+
],
|
|
114
|
+
items: [
|
|
115
|
+
{
|
|
116
|
+
id: "move-up",
|
|
117
|
+
label: "Move up",
|
|
118
|
+
description: "Move this ticker one position toward the start.",
|
|
119
|
+
disabled: !available || index === 0,
|
|
120
|
+
disabledReason: !available ? "Ticker is unavailable" : index === 0 ? "Already first" : undefined,
|
|
121
|
+
action: "moveUp",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: "move-down",
|
|
125
|
+
label: "Move down",
|
|
126
|
+
description: "Move this ticker one position toward the end.",
|
|
127
|
+
disabled: !available || index === symbols.length - 1,
|
|
128
|
+
disabledReason: !available
|
|
129
|
+
? "Ticker is unavailable"
|
|
130
|
+
: index === symbols.length - 1
|
|
131
|
+
? "Already last"
|
|
132
|
+
: undefined,
|
|
133
|
+
action: "moveDown",
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
actions: {
|
|
140
|
+
toggle: async ({ itemId, selected, signal }) => {
|
|
141
|
+
if (!candidates().includes(itemId)) return { kind: "rejected" };
|
|
142
|
+
const next = new Set(options.getSymbols());
|
|
143
|
+
if (selected) next.add(itemId);
|
|
144
|
+
else next.delete(itemId);
|
|
145
|
+
await options.applySymbols(orderedSymbols(next, candidates()), signal);
|
|
146
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "stay" };
|
|
147
|
+
if (!selected) menuCandidates = menuCandidates.filter((symbol) => symbol !== itemId);
|
|
148
|
+
return { kind: "stay" };
|
|
149
|
+
},
|
|
150
|
+
addCustom: async ({ ctx: actionCtx, signal }) => {
|
|
151
|
+
const entered = await actionCtx.ui.input("Add custom ticker", "Yahoo symbol, for example MSFT or SOL-USD", {
|
|
152
|
+
signal,
|
|
153
|
+
});
|
|
154
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "stay" };
|
|
155
|
+
if (!entered?.trim()) return { kind: "stay" };
|
|
156
|
+
const parsed = parseSymbols([entered]);
|
|
157
|
+
if (parsed.length !== 1) throw new Error("Enter exactly one Yahoo Finance symbol.");
|
|
158
|
+
const symbol = parsed[0];
|
|
159
|
+
if (!symbol) return { kind: "stay" };
|
|
160
|
+
const selected = new Set(options.getSymbols());
|
|
161
|
+
if (selected.has(symbol)) {
|
|
162
|
+
actionCtx.ui.notify(`${symbol} is already selected.`, "info");
|
|
163
|
+
return { kind: "stay" };
|
|
164
|
+
}
|
|
165
|
+
selected.add(symbol);
|
|
166
|
+
await options.applySymbols([...selected], signal);
|
|
167
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "stay" };
|
|
168
|
+
if (!menuCandidates.includes(symbol)) menuCandidates.push(symbol);
|
|
169
|
+
return { kind: "stay" };
|
|
170
|
+
},
|
|
171
|
+
toggleWidget: async ({ signal }) => {
|
|
172
|
+
await options.applyWidgetEnabled(!options.getWidgetEnabled(), signal);
|
|
173
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "stay" };
|
|
174
|
+
return { kind: "stay" };
|
|
175
|
+
},
|
|
176
|
+
chooseTicker: ({ itemId }) => {
|
|
177
|
+
if (!options.getSymbols().includes(itemId)) return { kind: "rejected" };
|
|
178
|
+
movingSymbol = itemId;
|
|
179
|
+
return { kind: "to", screen: "move" };
|
|
180
|
+
},
|
|
181
|
+
moveUp: ({ signal }) => moveSelectedTicker(-1, signal),
|
|
182
|
+
moveDown: ({ signal }) => moveSelectedTicker(1, signal),
|
|
183
|
+
refresh: () => {
|
|
184
|
+
options.refresh();
|
|
185
|
+
return { kind: "stay" };
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
});
|
|
207
189
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
190
|
+
async function moveSelectedTicker(direction: -1 | 1, signal: AbortSignal) {
|
|
191
|
+
const current = options.getSymbols();
|
|
192
|
+
const from = movingSymbol ? current.indexOf(movingSymbol) : -1;
|
|
193
|
+
const to = from + direction;
|
|
194
|
+
if (from < 0 || to < 0 || to >= current.length) return { kind: "rejected" } as const;
|
|
195
|
+
const next = [...current];
|
|
196
|
+
const [symbol] = next.splice(from, 1);
|
|
197
|
+
if (!symbol) return { kind: "rejected" } as const;
|
|
198
|
+
next.splice(to, 0, symbol);
|
|
199
|
+
await options.applySymbols(next, signal);
|
|
200
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "stay" } as const;
|
|
201
|
+
menuCandidates = [...next];
|
|
202
|
+
return { kind: "stay" } as const;
|
|
203
|
+
}
|
|
222
204
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
});
|
|
205
|
+
await runMenu(ctx, menu, {
|
|
206
|
+
getState: () => undefined,
|
|
207
|
+
signal: options.signal,
|
|
208
|
+
isCurrent: options.isCurrent,
|
|
209
|
+
onError: (menuCtx, error) => {
|
|
210
|
+
if (!menuCtx.hasUI) return;
|
|
211
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
212
|
+
menuCtx.ui.notify(sanitizeTerminalText(message), "error");
|
|
213
|
+
},
|
|
214
|
+
onUnsupportedMode: (menuCtx, mode) => {
|
|
215
|
+
if (menuCtx.hasUI) menuCtx.ui.notify(`Ticker menu is unavailable in ${mode} mode.`, "warning");
|
|
216
|
+
},
|
|
217
|
+
});
|
|
237
218
|
}
|
|
238
219
|
|
|
239
220
|
function orderedSymbols(selected: ReadonlySet<string>, candidates: readonly string[]): string[] {
|
|
240
|
-
|
|
221
|
+
return candidates.filter((symbol) => selected.has(symbol));
|
|
241
222
|
}
|
package/src/quotes.ts
CHANGED
|
@@ -1,104 +1,98 @@
|
|
|
1
1
|
export const QUOTE_ENDPOINT = "https://query1.finance.yahoo.com/v8/finance/chart";
|
|
2
2
|
|
|
3
3
|
export interface Quote {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
symbol: string;
|
|
5
|
+
price: number;
|
|
6
|
+
previousClose: number;
|
|
7
|
+
change: number;
|
|
8
|
+
changePercent: number;
|
|
9
|
+
currency?: string;
|
|
10
|
+
marketTime?: number;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export interface QuoteResult {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
symbol: string;
|
|
15
|
+
quote?: Quote;
|
|
16
|
+
error?: string;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export class QuoteRequestTimeoutError extends Error {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
constructor() {
|
|
21
|
+
super("Quote request timed out.");
|
|
22
|
+
this.name = "QuoteRequestTimeoutError";
|
|
23
|
+
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export async function fetchQuotes(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
symbols: readonly string[],
|
|
28
|
+
signal: AbortSignal,
|
|
29
|
+
fetchImplementation: typeof fetch = fetch,
|
|
30
30
|
): Promise<QuoteResult[]> {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
31
|
+
return Promise.all(
|
|
32
|
+
symbols.map(async (symbol): Promise<QuoteResult> => {
|
|
33
|
+
try {
|
|
34
|
+
const quote = await fetchQuote(symbol, signal, fetchImplementation);
|
|
35
|
+
return { symbol, quote };
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (signal.aborted) {
|
|
38
|
+
if (!(signal.reason instanceof QuoteRequestTimeoutError)) throw error;
|
|
39
|
+
return { symbol, error: signal.reason.message };
|
|
40
|
+
}
|
|
41
|
+
return { symbol, error: errorMessage(error) };
|
|
42
|
+
}
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
async function fetchQuote(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
signal,
|
|
56
|
-
});
|
|
57
|
-
if (!response.ok) throw new Error(`Quote request failed with HTTP ${response.status}.`);
|
|
58
|
-
return parseQuote(symbol, await response.json());
|
|
47
|
+
async function fetchQuote(symbol: string, signal: AbortSignal, fetchImplementation: typeof fetch): Promise<Quote> {
|
|
48
|
+
const url = `${QUOTE_ENDPOINT}/${encodeURIComponent(symbol)}?interval=1m&range=1d`;
|
|
49
|
+
const response = await fetchImplementation(url, {
|
|
50
|
+
headers: { "User-Agent": "pi-ticker/1.0" },
|
|
51
|
+
signal,
|
|
52
|
+
});
|
|
53
|
+
if (!response.ok) throw new Error(`Quote request failed with HTTP ${response.status}.`);
|
|
54
|
+
return parseQuote(symbol, await response.json());
|
|
59
55
|
}
|
|
60
56
|
|
|
61
57
|
export function parseQuote(requestedSymbol: string, payload: unknown): Quote {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
58
|
+
const chart = asRecord(payload)?.chart;
|
|
59
|
+
const result = asRecord(chart)?.result;
|
|
60
|
+
const first = Array.isArray(result) ? asRecord(result[0]) : undefined;
|
|
61
|
+
const meta = asRecord(first?.meta);
|
|
62
|
+
const price = finiteNumber(meta?.regularMarketPrice);
|
|
63
|
+
const previousClose = finiteNumber(meta?.previousClose) ?? finiteNumber(meta?.chartPreviousClose);
|
|
64
|
+
if (price === undefined || previousClose === undefined || previousClose === 0) {
|
|
65
|
+
throw new Error("Quote response did not include a usable price and previous close.");
|
|
66
|
+
}
|
|
67
|
+
const change = price - previousClose;
|
|
68
|
+
const currency = safeCurrency(meta?.currency);
|
|
69
|
+
const marketTimeSeconds = finiteNumber(meta?.regularMarketTime);
|
|
70
|
+
return {
|
|
71
|
+
symbol: requestedSymbol,
|
|
72
|
+
price,
|
|
73
|
+
previousClose,
|
|
74
|
+
change,
|
|
75
|
+
changePercent: (change / previousClose) * 100,
|
|
76
|
+
...(currency ? { currency } : {}),
|
|
77
|
+
...(marketTimeSeconds === undefined ? {} : { marketTime: marketTimeSeconds * 1000 }),
|
|
78
|
+
};
|
|
83
79
|
}
|
|
84
80
|
|
|
85
81
|
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
86
|
-
|
|
87
|
-
? (value as Record<string, unknown>)
|
|
88
|
-
: undefined;
|
|
82
|
+
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined;
|
|
89
83
|
}
|
|
90
84
|
|
|
91
85
|
function finiteNumber(value: unknown): number | undefined {
|
|
92
|
-
|
|
86
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
93
87
|
}
|
|
94
88
|
|
|
95
89
|
function safeCurrency(value: unknown): string | undefined {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
90
|
+
if (value === "GBp") return value;
|
|
91
|
+
if (typeof value !== "string") return undefined;
|
|
92
|
+
const normalized = value.toUpperCase();
|
|
93
|
+
return /^[A-Z]{3}$/.test(normalized) ? normalized : undefined;
|
|
100
94
|
}
|
|
101
95
|
|
|
102
96
|
function errorMessage(error: unknown): string {
|
|
103
|
-
|
|
97
|
+
return error instanceof Error ? error.message : String(error);
|
|
104
98
|
}
|