Skip to content

Commit

Permalink
add rest api interface
Browse files Browse the repository at this point in the history
change variables to more consistence
  • Loading branch information
Zewasik committed Oct 4, 2024
1 parent e5c5827 commit 942d591
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
11 changes: 9 additions & 2 deletions idea/explorer/src/decorators/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ export interface IJsonRpc {
handleRequest({ method, params, id }: JsonRpcRequest): Promise<JsonRpcResponse | JsonRpcResponse[]>;
}

export class JsonRpcBase implements IJsonRpc {
export interface IRestApi {
createRestRouter(): Router;
}

export class HybridApiBase implements IJsonRpc, IRestApi {
_getMethod(name: string): (...args: any[]) => Promise<void> {
throw new Error('Method not implemented.');
}
handleRequest({ method, params, id }: JsonRpcRequest): Promise<JsonRpcResponse | JsonRpcResponse[]> {
throw new Error('Method not implemented.');
}
createRestRouter(): Router {
throw new Error('Method not implemented.');
}
}

export function JsonRpc<TBase extends Constructor<JsonRpcBase>>(Base: TBase) {
export function HybridApi<TBase extends Constructor<HybridApiBase>>(Base: TBase) {
return class Jsonrpc extends Base {
private __methods = new Set(Object.keys(rpcMethods));
private __genesises: Set<string>;
Expand Down
4 changes: 2 additions & 2 deletions idea/explorer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createDbConnection, DbConfig } from 'indexer-db';
import { readFileSync } from 'fs';
import { AllInOneService } from './services/all-in-one';
import { config } from './config';
import { JsonRpcServer } from './jsonrpc';
import { HybridApiServer } from './server';
import { retryMethodsJob } from './middlewares/retry';

const main = async () => {
Expand All @@ -15,7 +15,7 @@ const main = async () => {
services.set(genesis, new AllInOneService(dataSource));
}

const server = new JsonRpcServer(services);
const server = new HybridApiServer(services);

await server.run();

Expand Down
5 changes: 2 additions & 3 deletions idea/explorer/src/jsonrpc.ts → idea/explorer/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express, { Express } from 'express';
import { JsonRpc, JsonRpcBase, JsonRpcMethod, RestHandler } from './decorators/method';
import { HybridApi, HybridApiBase, JsonRpcMethod, RestHandler } from './decorators/method';
import { AllInOneService } from './services/all-in-one';
import {
ParamGetCode,
Expand All @@ -19,9 +19,8 @@ import {
import { Cache } from './middlewares/caching';
import { redisConnect } from './middlewares/redis';
import { Retry } from './middlewares/retry';
import { VoucherNotFound } from './errors';

export class JsonRpcServer extends JsonRpc(JsonRpcBase) {
export class HybridApiServer extends HybridApi(HybridApiBase) {
private _app: Express;

constructor(private _services: Map<string, AllInOneService>) {
Expand Down

0 comments on commit 942d591

Please sign in to comment.