Repository navigation
feat: support render function for json dirlist - #630
helmipradita wants to merge 2 commits into
Conversation
|
Tested and verified against the directory listing test suite. Key points verified:
One small note: when returning a custom formatted JSON payload, if the user returns a pre-serialized JSON string (rather than an object or array), Fastify will default to |
Fixes fastify#221 Signed-off-by: Helmi Pradita <helmipraditaa@gmail.com>
00bfae8 to
c5fcc85
Compare
|
Great catch, thank you @SameRainbows! Added |
Puppo
left a comment
There was a problem hiding this comment.
Thanks for the PR! One compatibility regression needs addressing before this ships as a compatible feature; details inline.
| const renderFormat = format === 'html' ? 'html' : 'json' | ||
| if (format !== 'html') { | ||
| if (typeof options.render === 'function' && options.format !== 'html') { | ||
| await reply.type('application/json').send(dirList.render(entries, route, prefix, options, renderFormat)) |
There was a problem hiding this comment.
[Blocking] Preserve existing JSON-default listings with an HTML renderer.
list: { format: 'json', render: () => '<html>listing</html>' } was already supported: ordinary requests and ?format=json returned the built-in JSON listing, and only ?format=html invoked the renderer. This branch now invokes that existing renderer for JSON requests too. I reproduced HTTP 200 with Content-Type: application/json and the raw body <html>listing</html>, so existing clients calling response.json() break.
The base revision's json format with url parameter format test covers precisely this configuration; changing its callback to branch on format hides the compatibility regression. Can we preserve the original regression case and make JSON rendering opt-in separately, or explicitly treat this as a breaking change with migration guidance?
There was a problem hiding this comment.
Great point, thank you @Puppo!
Addressed in commit 4097421:
- Preserved
list.renderexclusively for HTML directory listings, restoring the originaljson format with url parameter formattest case so that existing configurations withlist: { format: 'json', render: () => '<html>' }continue returning built-in JSON for default/?format=jsonrequests and only invokerenderwhen?format=htmlis explicitly requested. - Introduced
list.renderJsonas the separate, opt-in custom renderer for JSON responses. - Added dedicated tests for
list.renderJson, serialized JSON string returns, and simultaneousrender+renderJsonconfiguration. All 394 unit tests and type checks pass with 100% coverage.
Preserve existing list.render behavior for HTML listings when format is json (such as ?format=html override), and provide dedicated list.renderJson for customizing JSON listings. Signed-off-by: Helmi Pradita <helmipraditaa@gmail.com>
|
Thanks for the thorough review @Puppo! I have addressed this in commit
|
|
@Puppo — following up on your blocking review: all 17 checks are green on |
Fixes #221
Checklist
npm run testandnpm run benchmarkDescription
jsonformat to uselist.renderto customize the output response.'html' | 'json') to thelist.renderfunction so consumers can handle both or differentiate when query parameters (?format=...) override default options.ListJsonRender), and unit test suite with 100% coverage.