Skip to content

Commit 39e74ae

Browse files
committed
use custom date reviver for date objects
1 parent d74b007 commit 39e74ae

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tda-wsjson-client",
3-
"version": "0.10.1",
3+
"version": "0.10.2",
44
"description": "WebSocket client for the TD Ameritrade wsjson API",
55
"main": "dist/web.bundle.js",
66
"types": "dist/web.d.ts",

src/client/wsJsonClientProxy.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export default class WsJsonClientProxy implements WsJsonClient {
9898
const { buffer, socket } = this;
9999
return new Promise((resolve, reject) => {
100100
socket.onmessage = ({ data }) => {
101-
buffer.emit(JSON.parse(data as string) as ProxiedResponse);
101+
// make sure date objects are reconstructed across the wire
102+
const parsedMsg = JSON.parse(data as string, dateReviver);
103+
buffer.emit(parsedMsg as ProxiedResponse);
102104
};
103105
socket.onopen = () => {
104106
logger("proxy ws connection opened");
@@ -271,3 +273,14 @@ export default class WsJsonClientProxy implements WsJsonClient {
271273
.map(({ response }) => response as T);
272274
}
273275
}
276+
277+
function dateReviver(_: string, value: any): any {
278+
if (typeof value === "string") {
279+
// Regular expression to check if the string matches ISO 8601 date format
280+
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;
281+
if (isoDateRegex.test(value)) {
282+
return new Date(value);
283+
}
284+
}
285+
return value; // return the value unchanged if not a date string
286+
}

src/example/testApp.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ async function run() {
177177
const { client } = await authClient.authenticateWithRetry(token);
178178
const app = new TestApp(client);
179179
await Promise.all([
180-
app.quotes(["ABNB", "UBER"]),
181-
app.accountPositions(),
180+
// app.quotes(["ABNB", "UBER"]),
181+
// app.accountPositions(),
182182
app.optionChain("TSLA"),
183+
// app.optionChainQuotes("AAPL"),
183184
]);
184185
}
185186

0 commit comments

Comments
 (0)