diff --git a/packages/api/src/app.ts b/packages/api/src/app.ts index 5de36eaa..dfedc702 100644 --- a/packages/api/src/app.ts +++ b/packages/api/src/app.ts @@ -9,6 +9,7 @@ import logger from "./logger"; import { BundlerRPCMethods, CustomRPCMethods, + HttpStatus, RedirectedRPCMethods, } from "./constants"; import { EthAPI, DebugAPI, Web3API, RedirectAPI } from "./modules"; @@ -139,9 +140,9 @@ export class ApiApp { if (this.redirectRpc && method in RedirectedRPCMethods) { const body = await redirectApi.redirect(method, params); if (body.error) { - return res.status(200).send({ ...body, id }); + return res.status(HttpStatus.OK).send({ ...body, id }); } - return res.status(200).send({ jsonrpc, id, ...body }); + return res.status(HttpStatus.OK).send({ jsonrpc, id, ...body }); } if (result === undefined) { @@ -200,7 +201,7 @@ export class ApiApp { case CustomRPCMethods.skandha_config: result = await skandhaApi.getConfig(); // skip hexlify for this particular rpc - return res.status(200).send({ + return res.status(HttpStatus.OK).send({ jsonrpc, id, result, @@ -214,7 +215,7 @@ export class ApiApp { } result = deepHexlify(result); - return res.status(200).send({ + return res.status(HttpStatus.OK).send({ jsonrpc, id, result, diff --git a/packages/api/src/constants.ts b/packages/api/src/constants.ts index e9631e72..019f59fe 100644 --- a/packages/api/src/constants.ts +++ b/packages/api/src/constants.ts @@ -61,3 +61,54 @@ export const RedirectedRPCMethods = { eth_maxPriorityFeePerGas: "eth_maxPriorityFeePerGas", eth_sendRawTransaction: "eth_sendRawTransaction", }; + +export enum HttpStatus { + CONTINUE = 100, + SWITCHING_PROTOCOLS = 101, + PROCESSING = 102, + EARLYHINTS = 103, + OK = 200, + CREATED = 201, + ACCEPTED = 202, + NON_AUTHORITATIVE_INFORMATION = 203, + NO_CONTENT = 204, + RESET_CONTENT = 205, + PARTIAL_CONTENT = 206, + AMBIGUOUS = 300, + MOVED_PERMANENTLY = 301, + FOUND = 302, + SEE_OTHER = 303, + NOT_MODIFIED = 304, + TEMPORARY_REDIRECT = 307, + PERMANENT_REDIRECT = 308, + BAD_REQUEST = 400, + UNAUTHORIZED = 401, + PAYMENT_REQUIRED = 402, + FORBIDDEN = 403, + NOT_FOUND = 404, + METHOD_NOT_ALLOWED = 405, + NOT_ACCEPTABLE = 406, + PROXY_AUTHENTICATION_REQUIRED = 407, + REQUEST_TIMEOUT = 408, + CONFLICT = 409, + GONE = 410, + LENGTH_REQUIRED = 411, + PRECONDITION_FAILED = 412, + PAYLOAD_TOO_LARGE = 413, + URI_TOO_LONG = 414, + UNSUPPORTED_MEDIA_TYPE = 415, + REQUESTED_RANGE_NOT_SATISFIABLE = 416, + EXPECTATION_FAILED = 417, + I_AM_A_TEAPOT = 418, + MISDIRECTED = 421, + UNPROCESSABLE_ENTITY = 422, + FAILED_DEPENDENCY = 424, + PRECONDITION_REQUIRED = 428, + TOO_MANY_REQUESTS = 429, + INTERNAL_SERVER_ERROR = 500, + NOT_IMPLEMENTED = 501, + BAD_GATEWAY = 502, + SERVICE_UNAVAILABLE = 503, + GATEWAY_TIMEOUT = 504, + HTTP_VERSION_NOT_SUPPORTED = 505, +} diff --git a/packages/api/src/server.ts b/packages/api/src/server.ts index 2bef36f0..dea6c8ff 100644 --- a/packages/api/src/server.ts +++ b/packages/api/src/server.ts @@ -3,6 +3,7 @@ import cors from "@fastify/cors"; import RpcError from "types/lib/api/errors/rpc-error"; import { ServerConfig } from "types/lib/api/interfaces"; import logger from "./logger"; +import { HttpStatus } from "./constants"; export class Server { constructor(private app: FastifyInstance, private config: ServerConfig) { @@ -70,16 +71,18 @@ export class Server { data: err.data, code: err.code, }; - return res.status(200).send({ + return res.status(HttpStatus.OK).send({ jsonrpc: body.jsonrpc, id: body.id, error, }); } - return res.status(err.statusCode ?? 500).send({ - error: "Unexpected behaviour", - }); + return res + .status(err.statusCode ?? HttpStatus.INTERNAL_SERVER_ERROR) + .send({ + error: "Unexpected behaviour", + }); }); await this.app.listen({