-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the api client to response the status code
- Loading branch information
1 parent
e99f6ba
commit 923137f
Showing
36 changed files
with
1,648 additions
and
338 deletions.
There are no files selected for viewing
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,13 +1,25 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/beacon/index.js"; | ||
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {IHttpClient, generateGenericJsonClient, ClientOptions} from "../../utils/client/index.js"; | ||
import {ReturnTypes} from "../../utils/types.js"; | ||
import {defaultClientOptions} from "./index.js"; | ||
|
||
/** | ||
* REST HTTP client for beacon routes | ||
*/ | ||
export function getClient(config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(config); | ||
const returnTypes = getReturnTypes(); | ||
// All routes return JSON, use a client auto-generator | ||
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
return generateGenericJsonClient<Api<ErrorAsResponse>, ReqTypes, ErrorAsResponse>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes as ReturnTypes<Api<ErrorAsResponse>>, | ||
httpClient, | ||
options ?? (defaultClientOptions as ClientOptions<ErrorAsResponse>) | ||
); | ||
} |
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,13 +1,25 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/config.js"; | ||
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {ClientOptions, generateGenericJsonClient, IHttpClient} from "../../utils/client/index.js"; | ||
import {ReturnTypes} from "../../utils/types.js"; | ||
import {Api, getReqSerializers, getReturnTypes, ReqTypes, routesData} from "../routes/config.js"; | ||
import {defaultClientOptions} from "./index.js"; | ||
|
||
/** | ||
* REST HTTP client for config routes | ||
*/ | ||
export function getClient(config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
// All routes return JSON, use a client auto-generator | ||
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
return generateGenericJsonClient<Api<ErrorAsResponse>, ReqTypes, ErrorAsResponse>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes as ReturnTypes<Api<ErrorAsResponse>>, | ||
httpClient, | ||
options ?? (defaultClientOptions as ClientOptions<ErrorAsResponse>) | ||
); | ||
} |
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,49 +1,77 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes, StateFormat} from "../routes/debug.js"; | ||
import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import { | ||
ClientOptions, | ||
generateGenericJsonClient, | ||
getFetchOptsSerializers, | ||
IHttpClient, | ||
} from "../../utils/client/index.js"; | ||
import {ReturnTypes} from "../../utils/types.js"; | ||
import {StateId} from "../routes/beacon/state.js"; | ||
import {Api, getReqSerializers, getReturnTypes, ReqTypes, routesData, StateFormat} from "../routes/debug.js"; | ||
import {defaultClientOptions} from "./index.js"; | ||
|
||
// As Jul 2022, it takes up to 3 mins to download states so make this 5 mins for reservation | ||
const GET_STATE_TIMEOUT_MS = 5 * 60 * 1000; | ||
|
||
/** | ||
* REST HTTP client for debug routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
_config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
// Some routes return JSON, use a client auto-generator | ||
const client = generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
const client = generateGenericJsonClient<Api<ErrorAsResponse>, ReqTypes, ErrorAsResponse>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes as ReturnTypes<Api<ErrorAsResponse>>, | ||
httpClient, | ||
options ?? (defaultClientOptions as ClientOptions<ErrorAsResponse>) | ||
); | ||
// For `getState()` generate request serializer | ||
const fetchOptsSerializers = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
...client, | ||
|
||
// TODO: Debug the type issue | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
async getState(stateId: string, format?: StateFormat) { | ||
if (format === "ssz") { | ||
const buffer = await httpClient.arrayBuffer({ | ||
const res = await httpClient.arrayBuffer({ | ||
...fetchOptsSerializers.getState(stateId, format), | ||
timeoutMs: GET_STATE_TIMEOUT_MS, | ||
}); | ||
// Casting to any otherwise Typescript doesn't like the multi-type return | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any | ||
return new Uint8Array(buffer) as any; | ||
} else { | ||
return client.getState(stateId, format); | ||
return { | ||
ok: true, | ||
res: {data: new Uint8Array(res.body)}, | ||
status: 200, | ||
}; | ||
} | ||
return client.getState(stateId, format); | ||
}, | ||
async getStateV2(stateId: string, format?: StateFormat) { | ||
|
||
// TODO: Debug the type issue | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
async getStateV2(stateId: StateId, format?: StateFormat) { | ||
if (format === "ssz") { | ||
const buffer = await httpClient.arrayBuffer({ | ||
const res = await httpClient.arrayBuffer({ | ||
...fetchOptsSerializers.getStateV2(stateId, format), | ||
timeoutMs: GET_STATE_TIMEOUT_MS, | ||
}); | ||
// Casting to any otherwise Typescript doesn't like the multi-type return | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any | ||
return new Uint8Array(buffer) as any; | ||
} else { | ||
return client.getStateV2(stateId, format); | ||
return {ok: true, res: {data: new Uint8Array(res.body)}, status: res.status}; | ||
} | ||
|
||
return client.getStateV2(stateId, format); | ||
}, | ||
}; | ||
} |
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
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,13 +1,25 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lightclient.js"; | ||
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {IHttpClient, generateGenericJsonClient, ClientOptions} from "../../utils/client/index.js"; | ||
import {ReturnTypes} from "../../utils/types.js"; | ||
import {defaultClientOptions} from "./index.js"; | ||
|
||
/** | ||
* REST HTTP client for lightclient routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
_config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
// All routes return JSON, use a client auto-generator | ||
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
return generateGenericJsonClient<Api<ErrorAsResponse>, ReqTypes, ErrorAsResponse>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes as ReturnTypes<Api<ErrorAsResponse>>, | ||
httpClient, | ||
options ?? (defaultClientOptions as ClientOptions<ErrorAsResponse>) | ||
); | ||
} |
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,13 +1,25 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lodestar.js"; | ||
import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; | ||
import {IHttpClient, generateGenericJsonClient, ClientOptions} from "../../utils/client/index.js"; | ||
import {ReturnTypes} from "../../utils/types.js"; | ||
import {defaultClientOptions} from "./index.js"; | ||
|
||
/** | ||
* REST HTTP client for lodestar routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
_config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
// All routes return JSON, use a client auto-generator | ||
return generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
return generateGenericJsonClient<Api<ErrorAsResponse>, ReqTypes, ErrorAsResponse>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes as ReturnTypes<Api<ErrorAsResponse>>, | ||
httpClient, | ||
options ?? (defaultClientOptions as ClientOptions<ErrorAsResponse>) | ||
); | ||
} |
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,21 +1,25 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {generateGenericJsonClient, getFetchOptsSerializers, IHttpClient} from "../../utils/client/index.js"; | ||
import {Api, getReqSerializers, getReturnTypes, NodeHealth, ReqTypes, routesData} from "../routes/node.js"; | ||
import {ClientOptions, generateGenericJsonClient, IHttpClient} from "../../utils/client/index.js"; | ||
import {APIServerHandlers} from "../../utils/types.js"; | ||
import {Api, getReqSerializers, getReturnTypes, ReqTypes, routesData} from "../routes/node.js"; | ||
import {defaultClientOptions} from "./index.js"; | ||
|
||
/** | ||
* REST HTTP client for beacon routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
_config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(); | ||
const returnTypes = getReturnTypes(); | ||
// All routes return JSON, use a client auto-generator | ||
const client = generateGenericJsonClient<Api, ReqTypes>(routesData, reqSerializers, returnTypes, httpClient); | ||
const fetchOptsSerializers = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
...client, | ||
async getHealth(): Promise<NodeHealth> { | ||
return httpClient.request({...fetchOptsSerializers.getHealth()}); | ||
}, | ||
}; | ||
return generateGenericJsonClient<Api<ErrorAsResponse>, ReqTypes, ErrorAsResponse>( | ||
routesData, | ||
reqSerializers, | ||
returnTypes, | ||
httpClient, | ||
options ?? (defaultClientOptions as ClientOptions<ErrorAsResponse>) | ||
); | ||
} |
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,22 +1,48 @@ | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {deserializeProof} from "@chainsafe/persistent-merkle-tree"; | ||
import {deserializeProof, Proof} from "@chainsafe/persistent-merkle-tree"; | ||
import {Api, ReqTypes, routesData, getReqSerializers} from "../routes/proof.js"; | ||
import {IHttpClient, getFetchOptsSerializers} from "../../utils/client/index.js"; | ||
import {IHttpClient, getFetchOptsSerializers, ClientOptions, HttpError} from "../../utils/client/index.js"; | ||
import {HttpStatusCode} from "../../utils/client/httpStatusCode.js"; | ||
import {APIClientResponse} from "../../utils/types.js"; | ||
|
||
/** | ||
* REST HTTP client for lightclient routes | ||
*/ | ||
export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { | ||
export function getClient<ErrorAsResponse extends boolean = false>( | ||
_config: IChainForkConfig, | ||
httpClient: IHttpClient, | ||
options?: ClientOptions<ErrorAsResponse> | ||
): Api<ErrorAsResponse> { | ||
const reqSerializers = getReqSerializers(); | ||
|
||
// For `getStateProof()` generate request serializer | ||
const fetchOptsSerializers = getFetchOptsSerializers<Api, ReqTypes>(routesData, reqSerializers); | ||
|
||
return { | ||
async getStateProof(stateId, paths) { | ||
const buffer = await httpClient.arrayBuffer(fetchOptsSerializers.getStateProof(stateId, paths)); | ||
const proof = deserializeProof(new Uint8Array(buffer)); | ||
return {data: proof}; | ||
try { | ||
const res = await httpClient.arrayBuffer(fetchOptsSerializers.getStateProof(stateId, paths)); | ||
const proof = deserializeProof(new Uint8Array(res.body)); | ||
|
||
return {ok: true, res: {data: proof}, status: HttpStatusCode.OK} as APIClientResponse< | ||
{[HttpStatusCode.OK]: {data: Proof}}, | ||
HttpStatusCode.INTERNAL_SERVER_ERROR, | ||
ErrorAsResponse | ||
>; | ||
} catch (err) { | ||
if (err instanceof HttpError && options?.errorAsResponse) { | ||
return { | ||
ok: false, | ||
res: {code: err.status, message: err.message}, | ||
status: err.status, | ||
} as APIClientResponse< | ||
{[HttpStatusCode.OK]: {data: Proof}}, | ||
HttpStatusCode.INTERNAL_SERVER_ERROR, | ||
ErrorAsResponse | ||
>; | ||
} | ||
throw err; | ||
} | ||
}, | ||
}; | ||
} |
Oops, something went wrong.