Skip to content

Commit

Permalink
Merge pull request #41 from lightninglabs/add-types
Browse files Browse the repository at this point in the history
types: add strong types for all API methods
  • Loading branch information
kaloudis authored Jun 21, 2022
2 parents 0320fc7 + 92d58a3 commit d3a291f
Show file tree
Hide file tree
Showing 128 changed files with 11,850 additions and 162,876 deletions.
29 changes: 0 additions & 29 deletions lib/api/base.ts

This file was deleted.

47 changes: 25 additions & 22 deletions lib/api/createRpc.ts
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;
17 changes: 17 additions & 0 deletions lib/api/faraday.ts
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;
25 changes: 0 additions & 25 deletions lib/api/faraday/index.ts

This file was deleted.

107 changes: 0 additions & 107 deletions lib/api/grpc.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as GrpcClient } from './grpc';
export { default as LndApi } from './lnd';
export { default as LoopApi } from './loop';
export { default as PoolApi } from './pool';
Expand Down
44 changes: 44 additions & 0 deletions lib/api/lnd.ts
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;
Loading

0 comments on commit d3a291f

Please sign in to comment.