Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
feat: add typescript support via typings file (#83)
Browse files Browse the repository at this point in the history
* feat: add typescript support via typings file

* fix: test and add newline in gitignore

* fix: export all types and enums. remove set_mfa_code

* fix: update to remove mfa reference in login
  • Loading branch information
evanshortiss authored and aurbano committed Sep 19, 2018
1 parent 01dd0e1 commit 344dbfb
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tells the .editorconfg plugin to stop searching once it finds this file
root = true

[*]
indent_size = 2
indent_style = space
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ node_modules
.idea

.nyc_output

package-lock.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![npm](https://img.shields.io/npm/dm/robinhood.svg)](https://www.npmjs.com/package/robinhood)
[![Coverage Status](https://coveralls.io/repos/github/aurbano/robinhood-node/badge.svg?branch=master)](https://coveralls.io/github/aurbano/robinhood-node?branch=master)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Faurbano%2Frobinhood-node.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Faurbano%2Frobinhood-node?ref=badge_shield)
[![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://www.typescriptlang.org/)

NodeJS Framework to make trades with the private [Robinhood](https://www.robinhood.com/) API. Using this API is not encouraged, since it's not officially available and it has been reverse engineered.
See @Sanko's [Unofficial Documentation](https://github.com/sanko/Robinhood) for more information.
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"version": "1.5.3",
"description": "Comprehensive NodeJS API wrapper for the Robinhood API",
"main": "src/robinhood.js",
"typings": "robinhood.d.ts",
"scripts": {
"test": "nyc ava",
"test": "nyc ava && npm run typescript",
"typescript": "tsc --project test/types/tsconfig.json",
"release": "standard-version"
},
"repository": {
Expand All @@ -28,9 +30,11 @@
"ava": "^0.17.0",
"coveralls": "^2.11.15",
"nyc": "^10.0.0",
"standard-version": "^4.3.0"
"standard-version": "^4.3.0",
"typescript": "~3.0.3"
},
"dependencies": {
"@types/request": "~2.47.1",
"lodash": "^4.17.4",
"promise": "^7.1.1",
"query-string": "^4.3.2",
Expand Down
254 changes: 254 additions & 0 deletions robinhood.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
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 enum TagTypes {
TopTen = '10-most-popular',
TopOneHundred = '100-most-popular'
}

export enum TriggerTypes {
Immediate = 'immediate',
Day = 'day'
}

export enum TimeInForceTypes {
GoodForDay = 'gfd',
GoodTillCancelled = 'gtc',
OrderCancelsOther = 'oco'
}

export enum OrderTypes {
Limit = 'limit',
Market = 'market'
}

export enum IntervalTypes {
FiveMinute = '5minute',
TenMinute = '10minute'
}

export enum SpanTypes {
Week = 'week',
Day = '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
39 changes: 39 additions & 0 deletions test/types/robinhood-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as robinhood from '../../'

const username = 'robinhood-user'
const password = 'robinhood-pass'
const mfaCode = '12345'

const onLoginSuccess = () => {
console.log('Login success, auth token is: ', api.auth_token)

api.positions((err, res, body) => {
if (err) {
console.error('error fetching positions', err)
} else if (res.statusCode !== 200) {
console.log(`error fetching positions. received status code ${res.statusCode}`)
} else {
console.log('positions are: ', body)
}
})

api.historicals(
'APPL',
robinhood.IntervalTypes.FiveMinute,
robinhood.SpanTypes.Day,
(err, res, body) => {
if (err) {
console.error('error fetching historicals', err)
} else if (res.statusCode !== 200) {
console.log(`error fetching historicals. received status code ${res.statusCode}`)
} else {
console.log('historicals are: ', body)
}
}
)
}

const api = robinhood({
username,
password
}, onLoginSuccess)
11 changes: 11 additions & 0 deletions test/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noEmit": true,
"strict": true,
},
"files": [
"./robinhood-types.test.ts"
]
}

0 comments on commit 344dbfb

Please sign in to comment.