-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core-api): dot-separated-query hapi plugin (#3965)
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
__tests__/unit/core-api/plugins/dot-separated-query.test.ts
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -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; | ||
}, | ||
}; |
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