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

Commit

Permalink
add types for promise api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Miller committed Aug 5, 2021
1 parent 0e14594 commit 95f95d4
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 255 deletions.
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
254 changes: 0 additions & 254 deletions robinhood.d.ts

This file was deleted.

44 changes: 44 additions & 0 deletions types/promises.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// drawback: requires typescript 4.1+
// drawback: Parameters<> doesn't preserve parameter/tuple labels,
// and probably not jsdoc annotations either

import robinhood = require("./robinhood");

type RemoveFnParam<T extends unknown[]> = T extends []
? []
: T extends [infer H, ...infer R]
? H extends Function
? RemoveFnParam<R>
: [H, ...RemoveFnParam<R>]
: T;

type LastTupleElement<T extends unknown[]> = T extends [infer X]
? X
: T extends [infer H, ...infer R]
? LastTupleElement<R>
: T;

type RobinhoodPromiseAPI = (
options: robinhood.Options.WebApiOpts
) => Promise<
{
[key in keyof robinhood.RobinhoodWebApi]: (
...args: RemoveFnParam<Parameters<robinhood.RobinhoodWebApi[key]>>
) => Promise<
LastTupleElement<
Parameters<robinhood.RobinhoodWebApi[key]>
> extends Function
? LastTupleElement<
Parameters<
// @ts-expect-error: this must be a function in this block
LastTupleElement<
Parameters<robinhood.RobinhoodWebApi[key]>
>
>
>
: ReturnType<robinhood.RobinhoodWebApi[key]>
>;
}
>;

export = RobinhoodPromiseAPI;
15 changes: 15 additions & 0 deletions types/promises.v3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// for older typescripts

import robinhood = require("./robinhood");

type RobinhoodPromiseAPI = (
options: robinhood.Options.WebApiOpts
) => Promise<
{
[key in keyof robinhood.RobinhoodWebApi]: (
...args: any[]
) => Promise<any>;
}
>;

export = RobinhoodPromiseAPI;
Loading

0 comments on commit 95f95d4

Please sign in to comment.