Skip to content

Commit 00bfae8

Browse files
committed
feat: support render function for json dirlist
Fixes #221
1 parent a006a4f commit 00bfae8

5 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ Default: `json`
334334
Options: `html`, `json`
335335
336336
Directory list can be in `html` format; in that case, `list.render` function is required.
337+
Directory list in `json` format can also use `list.render` to customize the JSON response.
338+
When `list.render` is omitted, the JSON response is controlled by `list.jsonFormat`.
339+
The `list.render` function receives `(dirs, files, format)`, where `format` is `html` or `json`.
337340
338341
This option can be overridden by the URL parameter `format`. Options are `html` and `json`.
339342

‎lib/dirList.js‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ const dirList = {
135135
}
136136

137137
const format = reply.request.query.format || options.format
138+
const renderFormat = format === 'html' ? 'html' : 'json'
138139
if (format !== 'html') {
140+
if (typeof options.render === 'function' && options.format !== 'html') {
141+
await reply.send(dirList.render(entries, route, prefix, options, renderFormat))
142+
return
143+
}
144+
139145
if (options.jsonFormat !== 'extended') {
140146
const nameEntries = { dirs: [], files: [] }
141147
entries.dirs.forEach(entry => nameEntries.dirs.push(entry.name))
@@ -148,12 +154,26 @@ const dirList = {
148154
return
149155
}
150156

151-
const html = options.render(
152-
entries.dirs.map(entry => dirList.htmlInfo(entry, route, prefix, options)),
153-
entries.files.map(entry => dirList.htmlInfo(entry, route, prefix, options)))
157+
const html = dirList.render(entries, route, prefix, options, renderFormat)
154158
await reply.type('text/html').send(html)
155159
},
156160

161+
/**
162+
* render directory entries
163+
* @param {ReturnType<typeof dirList.list>} entries directory list entries
164+
* @param {string} route request route
165+
* @param {string} prefix static prefix
166+
* @param {(ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
167+
* @param {'html' | 'json'} format response format
168+
* @return {unknown}
169+
*/
170+
render: function (entries, route, prefix, options, format) {
171+
return options.render(
172+
entries.dirs.map(entry => dirList.htmlInfo(entry, route, prefix, options)),
173+
entries.files.map(entry => dirList.htmlInfo(entry, route, prefix, options)),
174+
format)
175+
},
176+
157177
/**
158178
* provide the html information about entry and route, to get name and full route
159179
* @param entry file or dir name and stats

‎test/dir-list.test.js‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,50 @@ test('dir list json format - extended info', async t => {
561561
})
562562
})
563563

564+
test('dir list json format - render', async t => {
565+
t.plan(1)
566+
567+
const options = {
568+
root: path.join(__dirname, '/static'),
569+
prefix: '/public',
570+
prefixAvoidTrailingSlash: true,
571+
list: {
572+
format: 'json',
573+
names: ['index', 'index.json', '/'],
574+
render (dirs, files) {
575+
return {
576+
dirs: dirs.map(dir => dir.name),
577+
images: files
578+
.filter(file => file.name.endsWith('.jpg'))
579+
.map(file => ({ name: file.name, href: file.href }))
580+
}
581+
}
582+
}
583+
}
584+
const route = '/public/shallow/'
585+
const content = {
586+
dirs: ['empty'],
587+
images: [
588+
{
589+
name: 'sample.jpg',
590+
href: '/public/shallow/sample.jpg'
591+
}
592+
]
593+
}
594+
595+
await helper.arrange(t, options, async (url) => {
596+
await t.test(route, async t => {
597+
t.plan(4)
598+
599+
const response = await fetch(url + route)
600+
t.assert.ok(response.ok)
601+
t.assert.deepStrictEqual(response.status, 200)
602+
t.assert.deepStrictEqual(await response.json(), content)
603+
t.assert.ok(response.headers.get('content-type').includes('application/json'))
604+
})
605+
})
606+
})
607+
564608
test('json format with url parameter format', async t => {
565609
t.plan(12)
566610

@@ -570,8 +614,15 @@ test('json format with url parameter format', async t => {
570614
index: false,
571615
list: {
572616
format: 'json',
573-
render () {
574-
return 'html'
617+
render (dirs, files, format) {
618+
if (format === 'html') {
619+
return 'html'
620+
}
621+
622+
return {
623+
dirs: dirs.map(dir => dir.name),
624+
files: files.map(file => file.name)
625+
}
575626
}
576627
}
577628
}

‎types/index.d.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ declare namespace fastifyStatic {
3737
stats: Stats;
3838
}
3939

40+
export type ListFormat = 'html' | 'json'
41+
4042
export interface ListRender {
41-
(dirs: ListDir[], files: ListFile[]): string;
43+
(dirs: ListDir[], files: ListFile[], format?: ListFormat): string;
44+
}
45+
46+
export interface ListJsonRender {
47+
(dirs: ListDir[], files: ListFile[], format?: ListFormat): unknown;
4248
}
4349

4450
export interface ListOptions {
@@ -49,8 +55,7 @@ declare namespace fastifyStatic {
4955

5056
export interface ListOptionsJsonFormat extends ListOptions {
5157
format: 'json';
52-
// Required when the URL parameter `format=html` exists
53-
render?: ListRender;
58+
render?: ListJsonRender;
5459
}
5560

5661
export interface ListOptionsHtmlFormat extends ListOptions {

‎types/index.tst.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ expect<FastifyStaticOptions>()
101101
}
102102
})
103103

104+
expect<FastifyStaticOptions>()
105+
.type.toBeAssignableFrom({
106+
root: '',
107+
list: {
108+
format: 'json' as const,
109+
render: () => ({ custom: [] })
110+
}
111+
})
112+
104113
expect<FastifyStaticOptions>()
105114
.type.toBeAssignableFrom({
106115
root: '',

0 commit comments

Comments
 (0)