From 106da627add7ef25fa3de3a81e862754482c07fb Mon Sep 17 00:00:00 2001 From: Tyler Miller Date: Thu, 5 Aug 2021 11:12:26 -0700 Subject: [PATCH] introduce native promise support incl. documentation & types --- package.json | 14 ++- robinhood.d.ts | 254 ---------------------------------------- types/promises.d.ts | 42 +++++++ types/promises.v3.d.ts | 15 +++ types/robinhood.d.ts | 255 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 325 insertions(+), 255 deletions(-) delete mode 100644 robinhood.d.ts create mode 100644 types/promises.d.ts create mode 100644 types/promises.v3.d.ts create mode 100644 types/robinhood.d.ts diff --git a/package.json b/package.json index 37a8fac..d7344c2 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,19 @@ "version": "1.7.0", "description": "Comprehensive NodeJS API wrapper for the Robinhood API", "main": "src/robinhood.js", - "typings": "robinhood.d.ts", + "typings": "types/robinhood.d.ts", + "typesVersions": { + "<4.1": { + "promises": [ + "types/promises.v3.d.ts" + ] + }, + ">=4.1": { + "promises": [ + "types/promises.d.ts" + ] + } + }, "exports": { ".": "./src/robinhood.js", "./promises": "./src/promises.js", diff --git a/robinhood.d.ts b/robinhood.d.ts deleted file mode 100644 index 8f025a0..0000000 --- a/robinhood.d.ts +++ /dev/null @@ -1,254 +0,0 @@ -import request = require('request') - -declare function robinhood(options: robinhood.Options.WebApiOpts, callback: robinhood.InitCallback): robinhood.RobinhoodWebApi; - -declare namespace robinhood { - - export namespace Options { - interface WebApiOpts { - username?: string - password?: string - token?: string - } - - interface OrdersOptions { - updated_at: string - instrument: string - } - - interface EarningsOptionsWithSymbol { - range?: number - symbol: string - } - - interface EarningsOptionsWithInstrument { - range?: number - instrument: string - } - - interface BuySellOptions { - type: OrderType - quantity: number - bid_price: number - instrument: { - url?: string - symbol?: string - } - trigger: TriggerType - time: TimeInForceType - } - } - - export type TagType = TagTypes - export type OrderType = OrderTypes - export type IntervalType = IntervalTypes - export type SpanType = SpanTypes - export type TriggerType = TriggerTypes - export type TimeInForceType = TimeInForceTypes - export type InitCallback = () => void - - export type TagTypes = - '10-most-popular' - | '100-most-popular' - ; - - export type TriggerTypes = - 'immediate' - | 'day' - ; - - export type TimeInForceTypes = - 'gfd' - | 'gtc' - | 'oco' - ; - - export type OrderTypes = - 'limit' - | 'market' - ; - - export type IntervalTypes = - '5minute' - | '10minute' - ; - - export type SpanTypes = - 'week' - | 'day' - ; - - - export interface RobinhoodWebApi { - /** - * Revokes the current token for this session. - * @param callback - */ - expire_token (callback: request.RequestCallback): void - - /** - * Returns the token for the current session. - */ - auth_token (): string|null - - /** - * Get the current user's investment profile. - * @param callback - */ - investment_profile (callback: request.RequestCallback): void - - /** - * Return all instruments, or those for a given symbol. - * @param callback - */ - instruments(callback: request.RequestCallback): void - instruments(symbol: string, callback: request.RequestCallback): void - - /** - * Get fundamental data about a symbol. - * @param symbol - * @param callback - */ - fundamentals (symbol: string, callback: request.RequestCallback): void - - /** - * Get the popularity for a specified stock. - * @param symbol - * @param callback - */ - popularity (symbol: string, callback: request.RequestCallback): void - - /** - * Returns account information for the current user session. - * @param callback - */ - accounts (callback: request.RequestCallback): void - - /** - * Get the user's quote data for a specified stock. - * @param symbol - * @param callback - */ - quote_data (symbol: string, callback: request.RequestCallback): void - quote_data (symbol: string[], callback: request.RequestCallback): void - - /** - * Get the user's order information for the given options or specific order. - * @param options - * @param callback - */ - orders (options: Options.OrdersOptions, callback: request.RequestCallback): void - orders (orderId: string, callback: request.RequestCallback): void - - /** - * Get the user's position information. - * @param callback - */ - positions (callback: request.RequestCallback): void - - /** - * Get the user's nonzero position information only. - * @param callback - */ - nonzero_positions (callback: request.RequestCallback): void - - /** - * Place a buy order on a specified stock. - * @param options - * @param callback - */ - place_buy_order (options: Options.BuySellOptions, callback: request.RequestCallback): void - - /** - * Place a sell order on a specified stock. - * @param options - * @param callback - */ - place_sell_order (options: Options.BuySellOptions, callback: request.RequestCallback): void - - /** - * Cancel an order with the order object or order ID. - * @param order - * @param callback - */ - cancel_order (order: object, callback: request.RequestCallback): void - cancel_order (orderId: string, callback: request.RequestCallback): void - - /** - * Get historical information for the given symbol. - * @param symbol - * @param intv - * @param span - * @param callback - */ - historicals (symbol: string, intv: IntervalType, span: SpanType, callback: request.RequestCallback): void - - /** - * Get user information. - * @param callback - */ - user (callback: request.RequestCallback): void - - /** - * Returns the user's watchlists - * @param callback - */ - watchlists (callback: request.RequestCallback): void - - /** - * Get the earnings information using either the symbol or instrument. - * @param options - * @param callback - */ - earnings (options: Options.EarningsOptionsWithInstrument|Options.EarningsOptionsWithSymbol, callback: request.RequestCallback): void - - /** - * Get the user's dividends information. - * @param callback - */ - dividends (callback: request.RequestCallback): void - - /** - * Fetch splits for the given instrument. - * @param instrument - * @param callback - */ - splits (instrument: string, callback: request.RequestCallback): void - - /** - * Returns news for a given symbol. - * @param symbol - * @param callback - */ - news (symbol: string, callback: request.RequestCallback): void - - /** - * Returns information for the given tag. - * Retrieve Robinhood's new Tags: In 2018, Robinhood Web will expose more - * Social and Informational tools. You'll see how popular a security is with - * other Robinhood users, MorningStar ratings, etc. - * @param tag - * @param callback - */ - tag (tag: TagType, callback: request.RequestCallback): void - - /** - * Perform a GET request against the given URL. - * Used to get continued or paginated data from the API. Queries with long - * results return a reference to the next set. - * @param url - * @param callback - */ - url (url: string, callback: request.RequestCallback): void - - /** - * - * @param callback - */ - sp500_down (callback: request.RequestCallback): void - - sp500_up (callback: request.RequestCallback): void - } -} - -export = robinhood diff --git a/types/promises.d.ts b/types/promises.d.ts new file mode 100644 index 0000000..f190333 --- /dev/null +++ b/types/promises.d.ts @@ -0,0 +1,42 @@ +// requires typescript 4.1+ + +import robinhood = require("./robinhood"); + +type RemoveFnParam = T extends [] + ? [] + : T extends [infer H, ...infer R] + ? H extends Function + ? RemoveFnParam + : [H, ...RemoveFnParam] + : T; + +type LastTupleElement = T extends [infer X] + ? X + : T extends [infer H, ...infer R] + ? LastTupleElement + : T; + +declare function RobinhoodPromiseAPI( + options: robinhood.Options.WebApiOpts +): Promise< + { + [key in keyof robinhood.RobinhoodWebApi]: ( + ...args: RemoveFnParam> + ) => Promise< + LastTupleElement< + Parameters + > extends Function + ? LastTupleElement< + Parameters< + // @ts-expect-error: this must be a function in this block + LastTupleElement< + Parameters + > + > + > + : ReturnType + >; + } +>; + +export = RobinhoodPromiseAPI; diff --git a/types/promises.v3.d.ts b/types/promises.v3.d.ts new file mode 100644 index 0000000..c67b533 --- /dev/null +++ b/types/promises.v3.d.ts @@ -0,0 +1,15 @@ +// for typescript <4.1 + +import robinhood = require("./robinhood"); + +declare function RobinhoodPromiseAPI( + options: robinhood.Options.WebApiOpts +): Promise< + { + [key in keyof robinhood.RobinhoodWebApi]: ( + ...args: any[] + ) => Promise; + } +>; + +export = RobinhoodPromiseAPI; diff --git a/types/robinhood.d.ts b/types/robinhood.d.ts new file mode 100644 index 0000000..99371b0 --- /dev/null +++ b/types/robinhood.d.ts @@ -0,0 +1,255 @@ +import request = require("request"); + +declare function robinhood( + options: robinhood.Options.WebApiOpts, + callback: robinhood.InitCallback +): robinhood.RobinhoodWebApi; + +declare namespace robinhood { + export namespace Options { + interface WebApiOpts { + username?: string; + password?: string; + token?: string; + } + + interface OrdersOptions { + updated_at: string; + instrument: string; + } + + interface EarningsOptionsWithSymbol { + range?: number; + symbol: string; + } + + interface EarningsOptionsWithInstrument { + range?: number; + instrument: string; + } + + interface BuySellOptions { + type: OrderType; + quantity: number; + bid_price: number; + instrument: { + url?: string; + symbol?: string; + }; + trigger: TriggerType; + time: TimeInForceType; + } + } + + export type TagType = TagTypes; + export type OrderType = OrderTypes; + export type IntervalType = IntervalTypes; + export type SpanType = SpanTypes; + export type TriggerType = TriggerTypes; + export type TimeInForceType = TimeInForceTypes; + export type InitCallback = () => void; + + export type TagTypes = "10-most-popular" | "100-most-popular"; + + export type TriggerTypes = "immediate" | "day"; + + export type TimeInForceTypes = "gfd" | "gtc" | "oco"; + + export type OrderTypes = "limit" | "market"; + + export type IntervalTypes = "5minute" | "10minute"; + + export type SpanTypes = "week" | "day"; + + export interface RobinhoodWebApi { + /** + * Revokes the current token for this session. + * @param callback + */ + expire_token(callback: request.RequestCallback): void; + + /** + * Returns the token for the current session. + */ + auth_token(): string | null; + + /** + * Get the current user's investment profile. + * @param callback + */ + investment_profile(callback: request.RequestCallback): void; + + /** + * Return all instruments, or those for a given symbol. + * @param callback + */ + instruments(callback: request.RequestCallback): void; + instruments(symbol: string, callback: request.RequestCallback): void; + + /** + * Get fundamental data about a symbol. + * @param symbol + * @param callback + */ + fundamentals(symbol: string, callback: request.RequestCallback): void; + + /** + * Get the popularity for a specified stock. + * @param symbol + * @param callback + */ + popularity(symbol: string, callback: request.RequestCallback): void; + + /** + * Returns account information for the current user session. + * @param callback + */ + accounts(callback: request.RequestCallback): void; + + /** + * Get the user's quote data for a specified stock. + * @param symbol + * @param callback + */ + quote_data(symbol: string, callback: request.RequestCallback): void; + quote_data(symbol: string[], callback: request.RequestCallback): void; + + /** + * Get the user's order information for the given options or specific order. + * @param options + * @param callback + */ + orders( + options: Options.OrdersOptions, + callback: request.RequestCallback + ): void; + orders(orderId: string, callback: request.RequestCallback): void; + + /** + * Get the user's position information. + * @param callback + */ + positions(callback: request.RequestCallback): void; + + /** + * Get the user's nonzero position information only. + * @param callback + */ + nonzero_positions(callback: request.RequestCallback): void; + + /** + * Place a buy order on a specified stock. + * @param options + * @param callback + */ + place_buy_order( + options: Options.BuySellOptions, + callback: request.RequestCallback + ): void; + + /** + * Place a sell order on a specified stock. + * @param options + * @param callback + */ + place_sell_order( + options: Options.BuySellOptions, + callback: request.RequestCallback + ): void; + + /** + * Cancel an order with the order object or order ID. + * @param order + * @param callback + */ + cancel_order(order: object, callback: request.RequestCallback): void; + cancel_order(orderId: string, callback: request.RequestCallback): void; + + /** + * Get historical information for the given symbol. + * @param symbol + * @param intv + * @param span + * @param callback + */ + historicals( + symbol: string, + intv: IntervalType, + span: SpanType, + callback: request.RequestCallback + ): void; + + /** + * Get user information. + * @param callback + */ + user(callback: request.RequestCallback): void; + + /** + * Returns the user's watchlists + * @param callback + */ + watchlists(callback: request.RequestCallback): void; + + /** + * Get the earnings information using either the symbol or instrument. + * @param options + * @param callback + */ + earnings( + options: + | Options.EarningsOptionsWithInstrument + | Options.EarningsOptionsWithSymbol, + callback: request.RequestCallback + ): void; + + /** + * Get the user's dividends information. + * @param callback + */ + dividends(callback: request.RequestCallback): void; + + /** + * Fetch splits for the given instrument. + * @param instrument + * @param callback + */ + splits(instrument: string, callback: request.RequestCallback): void; + + /** + * Returns news for a given symbol. + * @param symbol + * @param callback + */ + news(symbol: string, callback: request.RequestCallback): void; + + /** + * Returns information for the given tag. + * Retrieve Robinhood's new Tags: In 2018, Robinhood Web will expose more + * Social and Informational tools. You'll see how popular a security is with + * other Robinhood users, MorningStar ratings, etc. + * @param tag + * @param callback + */ + tag(tag: TagType, callback: request.RequestCallback): void; + + /** + * Perform a GET request against the given URL. + * Used to get continued or paginated data from the API. Queries with long + * results return a reference to the next set. + * @param url + * @param callback + */ + url(url: string, callback: request.RequestCallback): void; + + /** + * + * @param callback + */ + sp500_down(callback: request.RequestCallback): void; + + sp500_up(callback: request.RequestCallback): void; + } +} + +export = robinhood;