Skip to content

feat: support render function for json dirlist - #630

Merged
Tony133 merged 4 commits into
fastify:mainfrom
helmipradita:feat/dirlist-json-render
Nov 26, 2026
Merged

Tony133 merged 4 commits into
fastify:mainfrom
helmipradita:feat/dirlist-json-render

Conversation

@helmipradita

@helmipradita helmipradita commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds an opt-in list.renderJson option to customize the JSON directory listing.

Closes #221

Changes

  • lib/dirList.js: when the resolved format is json and list.renderJson is a function, it is called with (dirs, files), the same arguments list.render receives, and its return value is sent with Content-Type: application/json. The return value can be an object or an already serialized JSON string.
  • Types: new ListJsonRender interface and renderJson?: ListJsonRender on the list options.
  • README: documented list.renderJson in the list.format section.
  • Tests: added cases for renderJson returning an object, returning a serialized string, and being used together with render (including the ?format=html and ?format=json URL overrides).

Behavior

  • list.render is unchanged and is still used only for the html format.
  • When list.renderJson is omitted, the JSON response is still controlled by list.jsonFormat, so existing configurations are not affected.
  • The ?format= URL parameter keeps selecting between the two: html uses list.render, json uses list.renderJson when provided.
  • Entries passed to renderJson are already filtered by the dotfiles policy.

Notes

The initial version of this PR reused list.render for JSON. That would have changed the response for existing setups with format: 'json' and an HTML render function, so it was replaced by the separate opt-in option after review.

The expectation in the render + renderJson test was aligned with the new dotfiles: 'ignore' default after merging main (#620).

Checklist

@SameRainbows

Copy link
Copy Markdown

Tested and verified against the directory listing test suite.

Key points verified:

  • Previously, list.render was only used for HTML mode, leaving JSON directory listings restricted to default names or jsonFormat: 'extended'. Allowing a custom render function for JSON responses gives consumers full control over API payload structure.
  • Passing the resolved format ('html' | 'json') as the third argument allows a single render handler to cleanly differentiate when consumers switch formats via ?format=... queries.
  • Reusing dirList.htmlInfo ensures that JSON renderers receive pre-calculated href, stats, and extendedInfo properties matching HTML mode.
  • Types in types/index.d.ts and types/index.tst.ts correctly allow returning unknown (serializable payloads) for ListJsonRender.

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 text/plain. Adding reply.type('application/json') before reply.send(...) on the JSON render branch ensures consistent application/json headers regardless of whether an object or serialized string is returned.

Fixes fastify#221

Signed-off-by: Helmi Pradita <helmipraditaa@gmail.com>
@helmipradita
helmipradita force-pushed the feat/dirlist-json-render branch from 00bfae8 to c5fcc85 Compare November 10, 2026 07:05
@helmipradita

Copy link
Copy Markdown
Contributor Author

Great catch, thank you @SameRainbows!

Added reply.type('application/json') before sending on the JSON render branch and added a test covering pre-serialized JSON string returns in commit c5fcc85. All unit tests and type checks pass with 100% coverage.

@Puppo Puppo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! One compatibility regression needs addressing before this ships as a compatible feature; details inline.

Comment thread lib/dirList.js Resolved
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>
@helmipradita

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @Puppo!

I have addressed this in commit 4097421 by making custom JSON directory listing rendering opt-in via a dedicated list.renderJson option:

  1. Existing list.render remains untouched for HTML rendering (preserving behavior when format: 'json' is configured with render to handle ?format=html overrides).
  2. The original regression test json format with url parameter format has been restored to its exact previous state without branching on format.
  3. Added renderJson tests verifying both custom object and serialized string JSON responses, alongside tests with both render and renderJson configured together.

@helmipradita

Copy link
Copy Markdown
Contributor Author

@Puppo — following up on your blocking review: all 17 checks are green on 4097421. That commit makes JSON rendering opt-in via list.renderJson and restores the original json format with url parameter format regression test unchanged; list.render stays HTML-only, so existing format: 'json' + HTML render configs keep returning built-in JSON. Addressed inline on 2026-11-11 but the review is still CHANGES_REQUESTED — could you re-review the current head? Happy to adjust further.

@Tony133 Tony133 added the semver-minor Issue or PR that should land as semver minor label Nov 24, 2026

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
mcollina enabled auto-merge (squash) November 25, 2026 08:45

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is red

auto-merge was automatically disabled November 26, 2026 01:35

Head branch was pushed to by a user without write access

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tony133
Tony133 merged commit 1cc4997 into fastify:main Nov 26, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-minor Issue or PR that should land as semver minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Have the render function for dirlist work for json as well

5 participants