Update test to match ticket version · github/codeql@125836d · GitHub
Skip to content

Commit 125836d

Browse files
committed
Update test to match ticket version
1 parent 05914f2 commit 125836d

5 files changed

Lines changed: 87 additions & 49 deletions

File tree

Lines changed: 7 additions & 0 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const endpoints = [];
2+
3+
function apiEndpoint(method, path, handler) {
4+
endpoints.push({ method, path, handler });
5+
}
6+
7+
function createRouteConfig(method, path, handler) {
8+
const hapiHandler = async function (request, h) {
9+
return handler(request.params, request.query, request.payload);
10+
};
11+
12+
return {
13+
method,
14+
path,
15+
handler: hapiHandler,
16+
};
17+
}
18+
19+
function registerEndpoints(server, serviceInstance) {
20+
for (const definition of endpoints) {
21+
const wrapped = async (params, query, payload) => {
22+
return definition.handler.call(serviceInstance, params, query, payload);
23+
};
24+
25+
const config = createRouteConfig(definition.method, definition.path, wrapped);
26+
server.route(config);
27+
}
28+
}
29+
30+
module.exports = { apiEndpoint, registerEndpoints };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { createCached } = require("./wrapped-route-cache");
2+
3+
class ReportService {
4+
async runQuery(filter) {
5+
sink(filter);
6+
}
7+
}
8+
9+
const service = new ReportService();
10+
11+
const getReportCached = createCached(async function (filter) {
12+
return service.runQuery(filter);
13+
});
14+
15+
module.exports = { getReportCached };
Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
1-
const Hapi = require("hapi");
2-
3-
const endpoints = [];
4-
5-
function endpoint(handler) {
6-
endpoints.push({ handler });
7-
}
8-
9-
function routeConfig(handler) {
10-
return {
11-
handler: async function (request, h) {
12-
return handler(request.query);
13-
},
14-
};
15-
}
16-
17-
function createCached(fn) {
18-
return async function (...args) {
19-
return fn(...args);
20-
};
21-
}
22-
23-
const cached = createCached(function (filter) {
24-
sink(filter);
25-
});
26-
27-
class Routes {
28-
get(query) {
29-
return cached(query.filter);
1+
const Hapi = require("@hapi/hapi");
2+
const {
3+
apiEndpoint,
4+
registerEndpoints,
5+
} = require("./wrapped-route-configurator");
6+
const { getReportCached } = require("./wrapped-route-service");
7+
8+
class ReportRoutes {
9+
async getReport(params, query) {
10+
return getReportCached(query.filter);
3011
}
3112
}
3213

33-
endpoint(Routes.prototype.get);
14+
apiEndpoint("GET", "/reports", ReportRoutes.prototype.getReport);
3415

35-
function register(server, instance) {
36-
for (const definition of endpoints) {
37-
const wrapped = async (query) => definition.handler.call(instance, query);
38-
server.route(routeConfig(wrapped));
39-
}
16+
async function main() {
17+
const server = Hapi.server();
18+
registerEndpoints(server, new ReportRoutes());
19+
return server;
4020
}
4121

42-
register(new Hapi.Server(), new Routes());
22+
main();

‎javascript/ql/test/library-tests/frameworks/hapi/tests.expected‎

Lines changed: 19 additions & 13 deletions

0 commit comments

Comments
 (0)