Skip to content

Commit

Permalink
refactor(core-api): dot-separated-query hapi plugin (#3965)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainydio authored Aug 17, 2020
1 parent 3565ef5 commit d680391
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
36 changes: 36 additions & 0 deletions __tests__/unit/core-api/plugins/dot-separated-query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { dotSeparatedQuery } from "../../../../packages/core-api/src/plugins/dot-separated-query";

describe("dotSeparatedQuery.register", () => {
it("should register onRequest extension", () => {
const server = {
ext: jest.fn(),
};

dotSeparatedQuery.register(server);

expect(server.ext).toBeCalledWith("onRequest", dotSeparatedQuery.onRequest);
});
});

describe("dotSeparatedQuery.onRequest", () => {
it("should replace query object", () => {
const request = {
query: {
"balance.from": "100",
"balance.to": "200",
},
};

const h = {
continue: Symbol,
};

const ret = dotSeparatedQuery.onRequest(request, h);

expect(request.query).toEqual({
balance: { from: "100", to: "200" },
});

expect(ret).toBe(h.continue);
});
});
20 changes: 20 additions & 0 deletions packages/core-api/src/plugins/dot-separated-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Utils } from "@arkecosystem/core-kernel";
import Hapi from "@hapi/hapi";

export const dotSeparatedQuery = {
name: "dot-separated-query",
version: "1.0.0",

register(server: Hapi.Server): void {
server.ext("onRequest", this.onRequest);
},

onRequest(request: Hapi.Request, h: Hapi.ResponseToolkit): Hapi.Lifecycle.ReturnValue {
const query = {};
for (const [key, value] of Object.entries(request.query)) {
Utils.set(query, key, value);
}
request.query = query;
return h.continue;
},
};
2 changes: 2 additions & 0 deletions packages/core-api/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dotSeparatedQuery } from "./dot-separated-query";
import { hapiAjv } from "./hapi-ajv";
import { whitelist } from "./whitelist";

Expand All @@ -9,6 +10,7 @@ export const preparePlugins = (config) => [
},
},
{ plugin: hapiAjv },
{ plugin: dotSeparatedQuery },
{
plugin: require("./cache"),
options: config.cache,
Expand Down

0 comments on commit d680391

Please sign in to comment.