|
| 1 | +import WebSocketApiMessageHandler, { |
| 2 | + newPayload, |
| 3 | +} from "./webSocketApiMessageHandler"; |
| 4 | +import { RawPayloadRequest, RawPayloadResponse } from "../tdaWsJsonTypes"; |
| 5 | +import { ApiService } from "./apiService"; |
| 6 | +import { isObject } from "lodash"; |
| 7 | + |
| 8 | +export type GetWatchListResponseItem = { |
| 9 | + id: number; |
| 10 | + name: string; |
| 11 | + type?: string; |
| 12 | + symbols?: string[]; |
| 13 | +}; |
| 14 | + |
| 15 | +export type GetWatchlistSnapshotResponse = { |
| 16 | + watchlist: GetWatchListResponseItem; |
| 17 | + service: "watchlist/get"; |
| 18 | +}; |
| 19 | + |
| 20 | +export type GetWatchlistPatchResponse = { |
| 21 | + patches: { |
| 22 | + op: string; |
| 23 | + path: string; |
| 24 | + value: { watchlist: GetWatchListResponseItem } | number | string | string[]; |
| 25 | + }[]; |
| 26 | + service: "watchlist/get"; |
| 27 | +}; |
| 28 | + |
| 29 | +export type GetWatchlistResponse = GetWatchlistSnapshotResponse; |
| 30 | + |
| 31 | +export default class GetWatchlistMessageHandler |
| 32 | + implements WebSocketApiMessageHandler<number, GetWatchlistResponse | null> |
| 33 | +{ |
| 34 | + buildRequest(watchlistId: number): RawPayloadRequest { |
| 35 | + return newPayload({ |
| 36 | + header: { |
| 37 | + service: "watchlist/get", |
| 38 | + id: "watchlist/get", |
| 39 | + ver: 0, |
| 40 | + }, |
| 41 | + params: { watchlistId }, |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + parseResponse(message: RawPayloadResponse): GetWatchlistResponse | null { |
| 46 | + const [{ header, body }] = message.payload; |
| 47 | + switch (header.type) { |
| 48 | + case "snapshot": { |
| 49 | + if ("watchlist" in body) { |
| 50 | + const { watchlist } = body as { watchlist: GetWatchListResponseItem }; |
| 51 | + return { watchlist, service: "watchlist/get" }; |
| 52 | + } else { |
| 53 | + console.warn( |
| 54 | + "Unexpected watchlist/get snapshot response with missing `watchlist` object", |
| 55 | + message |
| 56 | + ); |
| 57 | + return null; |
| 58 | + } |
| 59 | + } |
| 60 | + case "patch": { |
| 61 | + return parseGetWatchlistPatchResponse( |
| 62 | + body as GetWatchlistPatchResponse |
| 63 | + ); |
| 64 | + } |
| 65 | + default: |
| 66 | + console.warn("Unexpected watchlist/get response", message); |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + service: ApiService = "watchlist/get"; |
| 72 | +} |
| 73 | + |
| 74 | +function parseGetWatchlistPatchResponse( |
| 75 | + body: GetWatchlistPatchResponse |
| 76 | +): GetWatchlistResponse { |
| 77 | + const { patches } = body as GetWatchlistPatchResponse; |
| 78 | + const { path, value } = patches[0]; |
| 79 | + if (path === "" && isObject(value)) { |
| 80 | + const { watchlist } = value as { |
| 81 | + watchlist: GetWatchListResponseItem; |
| 82 | + }; |
| 83 | + return { watchlist, service: "watchlist/get" }; |
| 84 | + } |
| 85 | + const watchlist: Record<string, any> = {}; |
| 86 | + patches.forEach(({ path, value }) => { |
| 87 | + const pathParts = path.split("/"); |
| 88 | + const key = pathParts[pathParts.length - 1]; |
| 89 | + watchlist[key] = value; |
| 90 | + }); |
| 91 | + return { |
| 92 | + watchlist: watchlist as GetWatchListResponseItem, |
| 93 | + service: "watchlist/get", |
| 94 | + }; |
| 95 | +} |
0 commit comments