Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gergas3 committed Oct 27, 2023
1 parent 2a56aee commit 5e06237
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
8 changes: 4 additions & 4 deletions client/__tests__/filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jpFilter as jqFilter } from "../filter";
import { jqTransform } from "../filter";

const exampleResponse = {
status: 200,
Expand Down Expand Up @@ -46,7 +46,7 @@ const exampleResponse = {

describe("json path filter", () => {
it("returns only selected fields", async () => {
const result = await jqFilter(
const result = await jqTransform(
exampleResponse,
"{ status: .status, data: { kind: .data.kind, items: [.data.items[] | { metadata: { name: .metadata.name } }] } }"
);
Expand All @@ -56,13 +56,13 @@ describe("json path filter", () => {

describe("returns original input data", () => {
it("on error", async () => {
const result = await jqFilter(exampleResponse, "{ { }");
const result = await jqTransform(exampleResponse, "{ { }");

expect(result).toMatchSnapshot();
});

it("when no filter supplied", async () => {
const result = await jqFilter(exampleResponse, undefined);
const result = await jqTransform(exampleResponse, undefined);

expect(result).toMatchSnapshot();
});
Expand Down
8 changes: 5 additions & 3 deletions client/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { createLogger } from "../log";

const logger = createLogger({ name: "filter" });

// jq-node doesn't support `import` syntax
// import { jq } from "node-jq";
// TODO fix node config because jq-node cannot be imported with the `import` syntax above
// Error:
// Module '"node-jq"' has no exported member 'jq'.
const jq = require("node-jq");

export const jpFilter = async (
export const jqTransform = async (
data: any,
jqHeader: string | string[] | undefined
): Promise<any> => {
Expand All @@ -16,7 +19,6 @@ export const jpFilter = async (
return data;
}
try {
// jq.run() returns a Promise
return await jq.run(query, data, { input: "json", output: "json" });
} catch (error: any) {
logger.error(
Expand Down
9 changes: 6 additions & 3 deletions client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createLogger } from "../log";
import { ForwardedRequest, ForwardedResponse, JQ_HEADER } from "../types";
import { deferral } from "../util/deferral";
import { Backoff } from "./backoff";
import { jpFilter as jqFilter } from "./filter";
import { jqTransform } from "./filter";
import { jwt } from "./jwks";

/**
Expand Down Expand Up @@ -160,8 +160,11 @@ export class JsonRpcClient {
});
const jpSelectHeader = request.headers[JQ_HEADER];
this.#logger.debug({ response }, "forwarded response before filters");
const data = await jqFilter(response.data, jpSelectHeader);
this.#logger.debug({ data }, "forwarded response data after filters");
const data = await jqTransform(response.data, jpSelectHeader);
this.#logger.debug(
{ response: data },
"forwarded response data after filters"
);
return {
headers: response.headers,
status: response.status,
Expand Down
1 change: 1 addition & 0 deletions log/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const createLogger = <T extends LoggerOptions>(
"response.config.headers.authorization", // Axios intercepted response object
"response.request.headers.authorization", // Axios intercepted response object contains the request as well
"request.headers.authorization", // Axios intercepted request object + forwarded request object (JSON RPC request)
"headers.authorization",
],
formatters: {
level: (label) => {
Expand Down
2 changes: 1 addition & 1 deletion types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Request } from "express";
import core from "express-serve-static-core";
import { IncomingHttpHeaders } from "node:http";

export const JQ_HEADER = "braekhus-jq-response-filter";
export const JQ_HEADER = "braekhus-jq-response-transform";

export type IncomingRequest = Request<
core.ParamsDictionary,
Expand Down

0 comments on commit 5e06237

Please sign in to comment.