Skip to content

Commit df0618b

Browse files
authored
Merge pull request #10 from firstandthird/refactor
refactor
2 parents a60a00d + 53c19c6 commit df0618b

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

index.js

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,26 @@ module.exports = (server, options, allDone) => {
2323
return reply.redirect(redirectTo).code(options.statusCode);
2424
};
2525

26-
if (options.method === 'append') {
27-
server.ext('onPreResponse', (request, reply) => {
28-
const statusCode = request.response.output ? request.response.output.statusCode : request.response.statusCode;
29-
// if the route was already found by hapi then just ignore it:
30-
if (statusCode !== 404) {
31-
return reply.continue();
32-
}
33-
const method = request.method.toLowerCase();
34-
// before failing, first check if there's a slashed route we can redirect to:
35-
if (['get', 'head'].indexOf(method) !== -1 && request.path[request.path.length - 1] !== '/') {
36-
const slashedPath = `${request.path}/`;
37-
return doRedirect(slashedPath, request, reply);
38-
}
39-
// otherwise it really is a 404:
26+
// if (options.method === 'append') {
27+
server.ext('onPreResponse', (request, reply) => {
28+
const statusCode = request.response.output ? request.response.output.statusCode : request.response.statusCode;
29+
// if the route was already found by hapi then just ignore it:
30+
if (statusCode !== 404) {
4031
return reply.continue();
41-
});
42-
} else if (options.method === 'remove') {
43-
server.ext('onPreResponse', (request, reply) => {
44-
const statusCode = request.response.output ? request.response.output.statusCode : request.response.statusCode;
45-
// if the route was already found by hapi then just ignore it:
46-
if (statusCode !== 404) {
47-
return reply.continue();
48-
}
49-
// before failing, check if there's an unslashed route we can redirect to:
50-
const method = request.method.toLowerCase();
51-
if (['get', 'head'].indexOf(method) !== -1 && request.path !== '/' && request.path[request.path.length - 1] === '/') {
52-
const slashlessPath = request.path.replace(/\/$/, '');
53-
return doRedirect(slashlessPath, request, reply);
54-
}
55-
// otherwise it really is a 404:
56-
return reply.continue();
57-
});
58-
}
32+
}
33+
const method = request.method.toLowerCase();
34+
// pick a condition checker based on either 'append' or 'remove' mode:
35+
const condition = options.method === 'append' ? () => request.path[request.path.length - 1] !== '/' :
36+
() => request.path !== '/' && request.path[request.path.length - 1] === '/';
37+
// see if we need to do a redirect for either slashed/slashless path:
38+
if (['get', 'head'].indexOf(method) !== -1 && condition()) {
39+
// pick a redirection based on either 'append' or 'remove' mode:
40+
const redirectPath = options.method === 'append' ? `${request.path}/` : request.path.replace(/\/$/, '');
41+
return doRedirect(redirectPath, request, reply);
42+
}
43+
// otherwise it really is a 404:
44+
return reply.continue();
45+
});
5946
allDone();
6047
};
6148

0 commit comments

Comments
 (0)