Skip to content

Commit

Permalink
switch to fast-json-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecsl committed Feb 21, 2025
1 parent 96f7927 commit 3b6d440
Show file tree
Hide file tree
Showing 34 changed files with 413 additions and 1,164 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@ yarn build

# Running the test app

```
# if you already have an access token and refresh token
DEBUG_DEPTH=5 DEBUG=* \
TOS_ACCESS_TOKEN=<access_token> \
TOS_REFRESH_TOKEN=<refresh_token> \
node dist/example/testApp.js
Create a `.env` file with the following:

```
DEBUG=*
DEBUG_DEPTH=5
# either the following
TOS_ACCESS_TOKEN=<access_token>
TOS_REFRESH_TOKEN=<refresh_token>
# or the following
# if you don't have an access token and refresh token, you can use your username and password
# this will launch a browser to authenticate and then save the access token and refresh token to .env
DEBUG_DEPTH=5 DEBUG=* \
TOS_USERNAME=<username> \
TOS_PASSWORD=<password> \
node dist/example/testApp.js
TOS_USERNAME=<username>
TOS_PASSWORD=<password>
```

```
node --env-file=.env dist/example/testApp.js
```

## Running the proxy server

```
node dist/example/wsProxyServer.js
node --env-file=.env dist/example/wsProxyServer.js
```

## Running the proxy client
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"dependencies": {
"debug": "^4.3.4",
"fast-json-patch": "^3.1.1",
"isomorphic-ws": "^5.0.0",
"lodash-es": "^4.17.21",
"obgen": "^0.5.2",
Expand All @@ -51,15 +52,15 @@
"author": "Felipe Lima",
"license": "MIT",
"devDependencies": {
"@types/lodash": "^4.14.186",
"@types/node": "^20.3.0",
"@babel/core": "^7.15.8",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@types/debug": "^4.1.8",
"@types/jest": "^29.4.0",
"@types/lodash": "^4.14.186",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.3.0",
"@types/node-fetch": "^2.6.4",
"@types/ws": "^8.5.5",
"@typescript-eslint/eslint-plugin": "^6.13.2",
Expand Down
164 changes: 70 additions & 94 deletions src/client/mockWsJsonClient.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import { WsJsonClient } from "./wsJsonClient.js";
import { PositionsResponse } from "./services/positionsMessageHandler.js";
import { RawLoginResponseBody } from "./services/loginMessageHandler.js";
import { CancelOrderResponse } from "./services/cancelOrderMessageHandler.js";
import {
ChartRequestParams,
ChartResponse,
} from "./services/chartMessageHandler.js";
import { ChartRequestParams } from "./services/chartMessageHandler.js";
import { CreateAlertRequestParams } from "./services/createAlertMessageHandler.js";
import { OptionChainResponse } from "./services/optionSeriesMessageHandler.js";
import {
OptionChainDetailsRequest,
OptionChainDetailsResponse,
} from "./services/optionChainDetailsMessageHandler.js";
import { OptionSeriesQuotesResponse } from "./services/optionSeriesQuotesMessageHandler.js";
import {
OptionQuotesRequestParams,
OptionQuotesResponse,
} from "./services/optionQuotesMessageHandler.js";
import {
PlaceLimitOrderRequestParams,
PlaceOrderSnapshotResponse,
} from "./services/placeOrderMessageHandler.js";
import { OrderEventsResponse } from "./services/orderEventsMessageHandler.js";
import { QuotesResponse } from "./services/quotesMessageHandler.js";
import { InstrumentSearchResponse } from "./services/instrumentSearchMessageHandler.js";
import { UserPropertiesResponse } from "./services/userPropertiesMessageHandler.js";
import {
CancelAlertResponse,
CreateAlertResponse,
LookupAlertsResponse,
} from "./types/alertTypes.js";
import { MarketDepthResponse } from "./services/marketDepthMessageHandler.js";
import { GetWatchlistResponse } from "./services/getWatchlistMessageHandler.js";
import { RawLoginResponseBody } from "./services/loginMessageHandler.js";
import { OptionChainDetailsRequest } from "./services/optionChainDetailsMessageHandler.js";
import { OptionQuotesRequestParams } from "./services/optionQuotesMessageHandler.js";
import { PlaceLimitOrderRequestParams } from "./services/placeOrderMessageHandler.js";
import { ParsedPayloadResponse } from "./tdaWsJsonTypes.js";
import { WsJsonClient } from "./wsJsonClient.js";

export default class MockWsJsonClient implements WsJsonClient {
authenticateWithAuthCode(
Expand All @@ -47,17 +22,17 @@ export default class MockWsJsonClient implements WsJsonClient {
throw new Error("Method not implemented.");
}

async *accountPositions(_: string): AsyncIterable<PositionsResponse> {
async *accountPositions(_: string): AsyncIterable<ParsedPayloadResponse> {

Check warning on line 25 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
return yield {
service: "positions",
positions: [],
body: { positions: [] },
};
}

cancelAlert(_: number): Promise<CancelAlertResponse> {
cancelAlert(_: number): Promise<ParsedPayloadResponse> {

Check warning on line 32 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
return Promise.resolve({
service: "alerts/cancel",
alerts: [],
body: { alerts: [] },
});
}

Expand All @@ -70,18 +45,17 @@ export default class MockWsJsonClient implements WsJsonClient {
});
}

async *chart(_: ChartRequestParams): AsyncIterable<ChartResponse> {
async *chart(_: ChartRequestParams): AsyncIterable<ParsedPayloadResponse> {

Check warning on line 48 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
return yield {
service: "chart",
symbol: "",
candles: [],
body: { symbol: "", candles: [] },
};
}

createAlert(_: CreateAlertRequestParams): Promise<CreateAlertResponse> {
createAlert(_: CreateAlertRequestParams): Promise<ParsedPayloadResponse> {

Check warning on line 55 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
return Promise.resolve({
service: "alerts/create",
alerts: [],
body: { alerts: [] },
});
}

Expand All @@ -99,127 +73,129 @@ export default class MockWsJsonClient implements WsJsonClient {
return false;
}

async *lookupAlerts(): AsyncIterable<LookupAlertsResponse> {
async *lookupAlerts(): AsyncIterable<ParsedPayloadResponse> {
return yield {
service: "alerts/lookup",
alerts: [],
body: {},
};
}

optionChain(_: string): Promise<OptionChainResponse> {
optionChain(_: string): Promise<ParsedPayloadResponse> {

Check warning on line 83 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
return Promise.resolve({
series: [],
service: "optionSeries",
body: {},
});
}

optionChainDetails(
_: OptionChainDetailsRequest

Check warning on line 91 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
): Promise<OptionChainDetailsResponse> {
): Promise<ParsedPayloadResponse> {
return Promise.resolve({
service: "option_chain/get",
seriesDetails: [],
body: {},
});
}

async *optionChainQuotes(
_: string
): AsyncIterable<OptionSeriesQuotesResponse> {
async *optionChainQuotes(_: string): AsyncIterable<ParsedPayloadResponse> {

Check warning on line 99 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (23.x)

'_' is defined but never used
return yield {
series: [],
service: "optionSeries/quotes",
body: {},
};
}

async *optionQuotes(
_: OptionQuotesRequestParams
): AsyncIterable<OptionQuotesResponse> {
): AsyncIterable<ParsedPayloadResponse> {
return yield {
items: [],
service: "quotes/options",
body: {},
};
}

placeOrder(
_: PlaceLimitOrderRequestParams
): Promise<PlaceOrderSnapshotResponse> {
placeOrder(_: PlaceLimitOrderRequestParams): Promise<ParsedPayloadResponse> {
return Promise.resolve({
service: "place_order",
orders: [],
body: {},
});
}

async *quotes(_: string[]): AsyncIterable<QuotesResponse> {
async *quotes(_: string[]): AsyncIterable<ParsedPayloadResponse> {
return yield {
service: "quotes",
quotes: [],
body: {},
};
}

replaceOrder(
_: Required<PlaceLimitOrderRequestParams>
): Promise<OrderEventsResponse> {
): Promise<ParsedPayloadResponse> {
return Promise.resolve({
service: "order_events",
orders: [],
body: {},
});
}

searchInstruments(_: string): Promise<InstrumentSearchResponse> {
searchInstruments(_: string): Promise<ParsedPayloadResponse> {
return Promise.resolve({
service: "instrument_search",
instruments: [],
body: {},
});
}

userProperties(): Promise<UserPropertiesResponse> {
userProperties(): Promise<ParsedPayloadResponse> {
return Promise.resolve({
service: "user_properties",
defaultAccountCode: "foo",
nickname: "foo",
plDisplayMethod: "foo",
stocksOrderDefaultType: "foo",
stocksOrderDefaultQuantity: 0,
stocksOrderQuantityIncrement: 0,
optionsOrderDefaultType: "foo",
optionsOrderDefaultQuantity: 0,
optionsOrderQuantityIncrement: 0,
futuresOrderOrderDefaultType: "foo",
futuresOrderDefaultType: "foo",
futuresOrderDefaultQuantity: 0,
futuresOrderQuantityIncrement: 0,
futureOptionsOrderDefaultType: "foo",
futureOptionsOrderDefaultQuantity: 0,
futureOptionsOrderQuantityIncrement: 0,
forexOrderDefaultType: "foo",
forexOrderDefaultQuantity: 0,
forexOrderQuantityIncrement: 0,
body: {
defaultAccountCode: "foo",
nickname: "foo",
plDisplayMethod: "foo",
stocksOrderDefaultType: "foo",
stocksOrderDefaultQuantity: 0,
stocksOrderQuantityIncrement: 0,
optionsOrderDefaultType: "foo",
optionsOrderDefaultQuantity: 0,
optionsOrderQuantityIncrement: 0,
futuresOrderOrderDefaultType: "foo",
futuresOrderDefaultType: "foo",
futuresOrderDefaultQuantity: 0,
futuresOrderQuantityIncrement: 0,
futureOptionsOrderDefaultType: "foo",
futureOptionsOrderDefaultQuantity: 0,
futureOptionsOrderQuantityIncrement: 0,
forexOrderDefaultType: "foo",
forexOrderDefaultQuantity: 0,
forexOrderQuantityIncrement: 0,
},
});
}

async *workingOrders(_: string): AsyncIterable<OrderEventsResponse> {
async *workingOrders(_: string): AsyncIterable<ParsedPayloadResponse> {
return yield {
orders: [],
body: {},
service: "order_events",
};
}

async *marketDepth(_: string): AsyncIterable<MarketDepthResponse> {
async *marketDepth(_: string): AsyncIterable<ParsedPayloadResponse> {
return yield {
bidQuotes: [],
askQuotes: [],
body: {
bidQuotes: [],
askQuotes: [],
},
service: "market_depth",
};
}

watchlist(watchlistId: number): Promise<GetWatchlistResponse> {
watchlist(watchlistId: number): Promise<ParsedPayloadResponse> {
return Promise.resolve({
service: "watchlist/get",
watchlist: {
id: watchlistId,
name: "foo",
type: "STATIC",
symbols: ["AAPL", "GOOG", "MSFT"],
body: {
watchlist: {
id: watchlistId,
name: "foo",
type: "STATIC",
symbols: ["AAPL", "GOOG", "MSFT"],
},
},
});
}
Expand Down
Loading

0 comments on commit 3b6d440

Please sign in to comment.