-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from lightninglabs/add-types
types: add strong types for all API methods
- Loading branch information
Showing
128 changed files
with
11,850 additions
and
162,876 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
import WasmClient from './../index'; | ||
import { capitalize } from '../util/strings'; | ||
import LNC from '../lnc'; | ||
import { subscriptionMethods } from '../types/proto/schema'; | ||
|
||
// capitalize the first letter in the string | ||
const capitalize = (s: string) => s && s[0].toUpperCase() + s.slice(1); | ||
|
||
/** | ||
* An API wrapper to communicate with the LND node via GRPC | ||
* Creates a typed Proxy object which calls the WASM request or | ||
* subscribe methods depending on which function is called on the object | ||
*/ | ||
function createRpc<T extends unknown>( | ||
wasm: WasmClient, | ||
service: any, | ||
subscriptions?: any | ||
): any { | ||
return new Proxy(service, { | ||
export function createRpc<T extends object>(packageName: string, lnc: LNC): T { | ||
const rpc = {}; | ||
return new Proxy(rpc, { | ||
get(target, key, c) { | ||
// make sure funcs are camelcased | ||
const requestName = capitalize(key.toString()); | ||
const methodName = capitalize(key.toString()); | ||
// the full name of the method (ex: lnrpc.Lightning.OpenChannel) | ||
const method = `${packageName}.${methodName}`; | ||
|
||
const call = service[requestName]; | ||
if (call.responseStream) { | ||
if (subscriptions && subscriptions[key]) | ||
return subscriptions[key]; | ||
return (request: any): any => { | ||
const res = wasm.request(call, request); | ||
return res; | ||
if (subscriptionMethods.includes(method)) { | ||
// call subscribe for streaming methods | ||
return ( | ||
request: object, | ||
callback: (msg: object) => void, | ||
errCallback?: (err: Error) => void | ||
): void => { | ||
lnc.subscribe(method, request, callback, errCallback); | ||
}; | ||
} else { | ||
return async (request: any): Promise<any> => { | ||
const res = await wasm.request(call, request); | ||
return res.toObject(); | ||
// call request for unary methods | ||
return async (request: object): Promise<any> => { | ||
return await lnc.request(method, request); | ||
}; | ||
} | ||
} | ||
}); | ||
}) as T; | ||
} | ||
|
||
export default createRpc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import LNC from '../lnc'; | ||
import { FaradayServer } from '../types/proto/faraday/faraday'; | ||
import { serviceNames as sn } from '../types/proto/schema'; | ||
import { createRpc } from './createRpc'; | ||
|
||
/** | ||
* An API wrapper to communicate with the Faraday node via GRPC | ||
*/ | ||
class FaradayApi { | ||
faradayServer: FaradayServer; | ||
|
||
constructor(lnc: LNC) { | ||
this.faradayServer = createRpc(sn.frdrpc.FaradayServer, lnc); | ||
} | ||
} | ||
|
||
export default FaradayApi; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import LNC from '../lnc'; | ||
import { Autopilot } from '../types/proto/lnd/autopilotrpc/autopilot'; | ||
import { ChainNotifier } from '../types/proto/lnd/chainrpc/chainnotifier'; | ||
import { Invoices } from '../types/proto/lnd/invoicesrpc/invoices'; | ||
import { Lightning } from '../types/proto/lnd/lightning'; | ||
import { Router } from '../types/proto/lnd/routerrpc/router'; | ||
import { Signer } from '../types/proto/lnd/signrpc/signer'; | ||
import { WalletKit } from '../types/proto/lnd/walletrpc/walletkit'; | ||
import { WalletUnlocker } from '../types/proto/lnd/walletunlocker'; | ||
import { Watchtower } from '../types/proto/lnd/watchtowerrpc/watchtower'; | ||
import { WatchtowerClient } from '../types/proto/lnd/wtclientrpc/wtclient'; | ||
import { serviceNames as sn } from '../types/proto/schema'; | ||
import { createRpc } from './createRpc'; | ||
|
||
/** | ||
* An API wrapper to communicate with the LND node via GRPC | ||
*/ | ||
class LndApi { | ||
autopilot: Autopilot; | ||
chainNotifier: ChainNotifier; | ||
invoices: Invoices; | ||
lightning: Lightning; | ||
router: Router; | ||
signer: Signer; | ||
walletKit: WalletKit; | ||
walletUnlocker: WalletUnlocker; | ||
watchtower: Watchtower; | ||
watchtowerClient: WatchtowerClient; | ||
|
||
constructor(lnc: LNC) { | ||
this.autopilot = createRpc(sn.autopilotrpc.Autopilot, lnc); | ||
this.chainNotifier = createRpc(sn.chainrpc.ChainNotifier, lnc); | ||
this.invoices = createRpc(sn.invoicesrpc.Invoices, lnc); | ||
this.lightning = createRpc(sn.lnrpc.Lightning, lnc); | ||
this.router = createRpc(sn.routerrpc.Router, lnc); | ||
this.signer = createRpc(sn.signrpc.Signer, lnc); | ||
this.walletKit = createRpc(sn.walletrpc.WalletKit, lnc); | ||
this.walletUnlocker = createRpc(sn.lnrpc.WalletUnlocker, lnc); | ||
this.watchtower = createRpc(sn.watchtowerrpc.Watchtower, lnc); | ||
this.watchtowerClient = createRpc(sn.wtclientrpc.WatchtowerClient, lnc); | ||
} | ||
} | ||
|
||
export default LndApi; |
Oops, something went wrong.