hapi-transform-table
Advanced tools
Comparing version
22
index.js
const jsonToTable = require('json-to-table'); | ||
const os = require('os'); | ||
const qs = require('querystring'); | ||
@@ -18,3 +19,11 @@ const register = (server, pluginOptions) => { | ||
request.headers.accept = 'text/html'; | ||
request.setUrl(request.path.replace('.html', '')); | ||
let newUrl = request.path.replace('.html', ''); | ||
// save the original path info: | ||
request.app.transformTable = { | ||
originalPath: newUrl | ||
}; | ||
if (Object.keys(query).length) { | ||
newUrl = `${newUrl}?${qs.stringify(query)}`; | ||
} | ||
request.setUrl(newUrl); | ||
request.query = query; | ||
@@ -26,8 +35,11 @@ } | ||
const response = request.response; | ||
if (response.isBoom) { | ||
if (response.isBoom || response.statusCode !== 200) { | ||
// if this was originally a .html request and it got redirected, | ||
// add back the .html before returning it | ||
if (request.app.transformTable && [301, 302].includes(response.statusCode)) { | ||
const originalPath = request.app.transformTable.originalPath; | ||
response.headers.location = response.headers.location.replace(originalPath, `${originalPath}.html`); | ||
} | ||
return h.continue; | ||
} | ||
if (response.statusCode !== 200) { | ||
return h.continue; | ||
} | ||
if (request.headers.accept === 'text/html') { | ||
@@ -34,0 +46,0 @@ const routeOptions = request.route.settings.plugins['hapi-transform-table'] || {}; |
{ | ||
"name": "hapi-transform-table", | ||
"version": "1.5.3", | ||
"version": "1.5.4", | ||
"description": "hapi plugin for routes that need to return json as HTML tables ", | ||
@@ -27,4 +27,5 @@ "main": "index.js", | ||
"hapi": "^17.2.0", | ||
"hapi-password": "^5.1.0", | ||
"tap": "^11.0.1" | ||
} | ||
} |
@@ -344,2 +344,3 @@ const Hapi = require('hapi'); | ||
handler(request, h) { | ||
t.equal(request.url.path, '/path1?test=1'); | ||
t.equal(request.query.test, '1', 'query param forwarded'); | ||
@@ -359,1 +360,30 @@ return []; | ||
}); | ||
tap.test('auth schemes that do redirects will preserve the original .html route', async(t) => { | ||
const server = await new Hapi.Server({ port: 8080 }); | ||
await server.register(plugin, {}); | ||
await server.register({ | ||
plugin: require('hapi-password'), | ||
options: { | ||
salt: 'aSalt', | ||
password: 'password', | ||
cookieName: 'demo-login' | ||
} | ||
}); | ||
server.route({ | ||
method: 'GET', | ||
path: '/success', | ||
config: { | ||
plugins: { | ||
'hapi-transform-table': {} | ||
}, | ||
handler: (request, h) => 'success!' | ||
} | ||
}); | ||
await server.start(); | ||
const redirectResponse = await server.inject({ url: '/success.html' }); | ||
t.equal(redirectResponse.statusCode, 302); | ||
t.equal(redirectResponse.headers.location, '/login?next=/success.html'); | ||
await server.stop(); | ||
t.end(); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16370
10.17%449
10.05%0
-100%6
20%