Skip to content

Commit

Permalink
Modularize lodestar-api
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jun 14, 2022
1 parent 04155c6 commit 56d1fcf
Show file tree
Hide file tree
Showing 63 changed files with 282 additions and 144 deletions.
17 changes: 13 additions & 4 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@
".": {
"import": "./lib/index.js"
},
"./beacon": {
"import": "./lib/beacon/index.js"
},
"./beacon/server": {
"import": "./lib/beacon/server/index.js"
},
"./builder": {
"import": "./lib/builder/index.js"
},
"./builder/server": {
"import": "./lib/builder/index.js"
},
"./keymanager": {
"import": "./lib/keymanager/index.js"
},
"./keymanager/server": {
"import": "./lib/keymanager/server.js"
},
"./server": {
"import": "./lib/server/index.js"
"import": "./lib/keymanager/index.js"
}
},
"typesVersions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/beacon/index.js";
import {IHttpClient, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for beacon routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/config.js";
import {IHttpClient, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for config routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes, StateFormat} from "../routes/debug.js";
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for debug routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventSource from "eventsource";
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, BeaconEvent, routesData, getEventSerdes} from "../routes/events.js";
import {stringifyQuery} from "./utils/format.js";
import {stringifyQuery} from "../../utils/client/format.js";

/**
* REST HTTP client for events routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api} from "../interface.js";
import {IHttpClient, HttpClient, HttpClientOptions, HttpClientModules, HttpError} from "./utils/index.js";
export {HttpClient, HttpClientOptions, HttpError};
import {Api} from "../../interface.js";
import {IHttpClient, HttpClient, HttpClientOptions, HttpClientModules} from "../../utils/client/index.js";

import * as beacon from "./beacon.js";
import * as configApi from "./config.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {deserializeProof} from "@chainsafe/persistent-merkle-tree";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lightclient.js";
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for lightclient routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lodestar.js";
import {IHttpClient, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for lodestar routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/node.js";
import {IHttpClient, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for beacon routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/validator.js";
import {IHttpClient, generateGenericJsonClient} from "./utils/index.js";
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js";

/**
* REST HTTP client for validator routes
Expand Down
38 changes: 38 additions & 0 deletions packages/api/src/beacon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {Api as BeaconApi} from "./routes/beacon/index.js";
import {Api as ConfigApi} from "./routes/config.js";
import {Api as DebugApi} from "./routes/debug.js";
import {Api as EventsApi} from "./routes/events.js";
import {Api as LightclientApi} from "./routes/lightclient.js";
import {Api as LodestarApi} from "./routes/lodestar.js";
import {Api as NodeApi} from "./routes/node.js";
import {Api as ValidatorApi} from "./routes/validator.js";

// NOTE: Don't export server here so it's not bundled to all consumers

export * as routes from "./routes/index.js";
export {getClient} from "./client/index.js";

export type Api = {
beacon: BeaconApi;
config: ConfigApi;
debug: DebugApi;
events: EventsApi;
lightclient: LightclientApi;
lodestar: LodestarApi;
node: NodeApi;
validator: ValidatorApi;
};

// Declare namespaces for CLI options
export type ApiNamespace = keyof Api;
const allNamespacesObj: {[K in keyof Api]: true} = {
beacon: true,
config: true,
debug: true,
events: true,
lightclient: true,
lodestar: true,
node: true,
validator: true,
};
export const allNamespaces = Object.keys(allNamespacesObj) as ApiNamespace[];
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TypeJson,
ReqSerializers,
ReqSerializer,
} from "../../utils/index.js";
} from "../../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {phase0, ssz} from "@chainsafe/lodestar-types";
import {RoutesData, ReturnTypes, reqEmpty, ContainerData} from "../../utils/index.js";
import {RoutesData, ReturnTypes, reqEmpty, ContainerData} from "../../../utils/index.js";
import * as block from "./block.js";
import * as pool from "./pool.js";
import * as state from "./state.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ReqSerializers,
reqEmpty,
ReqEmpty,
} from "../../utils/index.js";
} from "../../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Schema,
ReqSerializers,
ReqSerializer,
} from "../../utils/index.js";
} from "../../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ReqSerializers,
RoutesData,
sameType,
} from "../utils/index.js";
} from "../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ReqSerializers,
ReqEmpty,
ReqSerializer,
} from "../utils/index.js";
} from "../../utils/index.js";
import {StateId} from "./beacon/state.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Epoch, phase0, Slot, ssz, StringType, RootHex, altair, UintNum64} from "@chainsafe/lodestar-types";
import {ContainerType, Type, VectorCompositeType} from "@chainsafe/ssz";
import {FINALIZED_ROOT_DEPTH} from "@chainsafe/lodestar-params";
import {RouteDef, TypeJson} from "../utils/index.js";
import {RouteDef, TypeJson} from "../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
ReqSerializers,
reqEmpty,
ReqEmpty,
} from "../utils/index.js";
import {queryParseProofPathsArr, querySerializeProofPathsArr} from "../utils/serdes.js";
} from "../../utils/index.js";
import {queryParseProofPathsArr, querySerializeProofPathsArr} from "../../utils/serdes.js";
import {LightclientHeaderUpdate, LightclientFinalizedUpdate} from "./events.js";

// Re-export for convenience when importing routes.lightclient.LightclientHeaderUpdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RoutesData,
sameType,
Schema,
} from "../utils/index.js";
} from "../../utils/index.js";
import {FilterGetPeers, NodePeer, PeerDirection, PeerState} from "./node.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ReqSerializers,
ReqEmpty,
sameType,
} from "../utils/index.js";
} from "../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
reqOnlyBody,
ReqSerializers,
jsonType,
} from "../utils/index.js";
} from "../../utils/index.js";

// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/beacon/index.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
// All routes return JSON, use a server auto-generator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/config.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
// All routes return JSON, use a server auto-generator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/debug.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
const reqSerializers = getReqSerializers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getEventSerdes} from "../routes/events.js";
import {ServerRoutes} from "./utils/index.js";
import {ServerRoutes} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
const eventSerdes = getEventSerdes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {FastifyInstance} from "fastify";
import {IChainForkConfig} from "@chainsafe/lodestar-config";
// eslint-disable-next-line import/no-extraneous-dependencies
import {Api} from "../interface.js";
import {ServerRoute} from "./utils/index.js";
import {Api} from "../../interface.js";
import {ServerInstance, ServerRoute, RouteConfig, registerRoute} from "../../utils/server/index.js";

import * as beacon from "./beacon.js";
import * as configApi from "./config.js";
Expand All @@ -13,12 +11,11 @@ import * as lodestar from "./lodestar.js";
import * as node from "./node.js";
import * as validator from "./validator.js";

export type RouteConfig = {
operationId: ServerRoute["id"];
};
// Re-export for convenience
export {RouteConfig};

export function registerRoutes(
server: FastifyInstance,
server: ServerInstance,
config: IChainForkConfig,
api: Api,
enabledNamespaces: (keyof Api)[]
Expand Down Expand Up @@ -48,22 +45,8 @@ export function registerRoutes(
throw Error(`Unknown api namespace ${namespace}`);
}

registerRoutesGroup(server, routes());
}
}

export function registerRoutesGroup(
fastify: FastifyInstance,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
routes: Record<string, ServerRoute<any>>
): void {
for (const route of Object.values(routes)) {
fastify.route({
url: route.url,
method: route.method,
handler: route.handler,
schema: route.schema,
config: {operationId: route.id} as RouteConfig,
});
for (const route of Object.values(routes())) {
registerRoute(server, route);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {serializeProof} from "@chainsafe/persistent-merkle-tree";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/lightclient.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
const reqSerializers = getReqSerializers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/lodestar.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
// All routes return JSON, use a server auto-generator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/node.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
// All routes return JSON, use a server auto-generator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/validator.js";
import {ServerRoutes, getGenericJsonServer} from "./utils/index.js";
import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";

export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes<Api, ReqTypes> {
// All routes return JSON, use a server auto-generator
Expand Down
13 changes: 13 additions & 0 deletions packages/api/src/builder/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {IHttpClient, generateGenericJsonClient} from "../utils/client/index.js";
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "./routes.js";

/**
* REST HTTP client for builder routes
*/
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api {
const reqSerializers = getReqSerializers();
const returnTypes = getReturnTypes();
// All routes return JSON, use a client auto-generator
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient);
}
22 changes: 22 additions & 0 deletions packages/api/src/builder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {IChainForkConfig} from "@chainsafe/lodestar-config";
import {HttpClient, HttpClientModules, HttpClientOptions, IHttpClient} from "../utils/client/httpClient.js";
import {Api} from "./routes.js";
import * as builder from "./client.js";

// NOTE: Don't export server here so it's not bundled to all consumers

export {Api};

// Note: build API does not have namespaces as routes are declared at the "root" namespace

type ClientModules = HttpClientModules & {
config: IChainForkConfig;
httpClient?: IHttpClient;
};

export function getClient(opts: HttpClientOptions, modules: ClientModules): Api {
const {config} = modules;
const httpClient = modules.httpClient ?? new HttpClient(opts, modules);

return builder.getClient(config, httpClient);
}
Loading

0 comments on commit 56d1fcf

Please sign in to comment.