Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
feat: add custom response header for easy debug
Browse files Browse the repository at this point in the history
  • Loading branch information
RetricSu committed Dec 9, 2022
1 parent 9161b7c commit a7a3f9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/api-server/src/middlewares/header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Response } from "express";

export interface ResponseHeader {
instantFinality: boolean;
// add more below if needed
}

export function setResponseHeader(res: Response, header: ResponseHeader) {
res.setHeader("X-Instant-Finality", header.instantFinality.toString());
}
11 changes: 10 additions & 1 deletion packages/api-server/src/middlewares/jayson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { instantFinalityHackMethods, methods } from "../methods/index";
import { Request, Response, NextFunction } from "express";
import createServer from "connect";
import { isInstantFinalityHackMode } from "../util";
import { ResponseHeader, setResponseHeader } from "./header";

const server = new jayson.Server(methods);
const instantFinalityHackServer = new jayson.Server(instantFinalityHackMethods);
Expand All @@ -18,8 +19,16 @@ export const jaysonMiddleware = (
req.body.params = [] as any[];
}

const enableInstantFinality = isInstantFinalityHackMode(req);

// manage response header here
const header: ResponseHeader = {
instantFinality: enableInstantFinality,
};
setResponseHeader(res, header);

// enable additional feature for special URL
if (isInstantFinalityHackMode(req)) {
if (enableInstantFinality) {
const middleware =
instantFinalityHackServer.middleware() as createServer.NextHandleFunction;
return middleware(req, res, next);
Expand Down

0 comments on commit a7a3f9b

Please sign in to comment.