Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow render option with format:json #308

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ interface ListRender {
interface ListOptions {
names?: string[];
extendedFolderInfo?: boolean;
jsonFormat?: 'names' | 'extended';
}

interface ListOptionsJsonFormat extends ListOptions {
format: 'json';
jsonFormat?: 'names' | 'extended';
// Required when the URL parameter `format=html` exists
render?: ListRender;
}

interface ListOptionsHtmlFormat extends ListOptions {
Expand Down
7 changes: 4 additions & 3 deletions lib/dirList.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ const dirList = {
* @param {string} dotfiles
*/
send: async function ({ reply, dir, options, route, prefix, dotfiles }) {
if (reply.request.query.format === 'html' && typeof options.render !== 'function') {
throw new Error('The `list.render` option must be a function and is required with the URL parameter `format=html`')
}

let entries
try {
entries = await dirList.list(dir, options, dotfiles)
Expand Down Expand Up @@ -200,9 +204,6 @@ const dirList = {
if (options.list.format === 'html' && typeof options.list.render !== 'function') {
return new TypeError('The `list.render` option must be a function and is required with html format')
}
if (options.list.format === 'html' && options.list.jsonFormat != null) {
return new TypeError('The `list.jsonFormat` option must be with json format')
}
}

}
Expand Down
109 changes: 99 additions & 10 deletions test/dir-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ t.test('throws when `list.format` is html and `list render` is not a function',
t.equal(err.message, 'The `list.render` option must be a function and is required with html format')
})

t.test('throws when `list.format` is html and `list.jsonFormat` is given', t => {
t.plan(2)

const err = dirList.validateOptions({ list: { format: 'html', render: () => '', jsonFormat: 'extended' } })
t.type(err, TypeError)
t.equal(err.message, 'The `list.jsonFormat` option must be with json format')
})

t.test('dir list wrong options', t => {
t.plan(3)

Expand Down Expand Up @@ -485,7 +477,103 @@ t.test('dir list json format - extended info', t => {
})
})

t.test('dir list - url parameter format', t => {
t.test('json format with url parameter format', t => {
t.plan(13)

const options = {
root: path.join(__dirname, '/static'),
prefix: '/public',
index: false,
list: {
format: 'json',
render (dirs, files) {
return 'html'
}
}
}
const route = '/public/'
const jsonContent = { dirs: ['deep', 'shallow'], files: ['.example', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }

helper.arrange(t, options, (url) => {
simple.concat({
method: 'GET',
url: url + route
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), JSON.stringify(jsonContent))
t.ok(response.headers['content-type'].includes('application/json'))
})

simple.concat({
method: 'GET',
url: url + route + '?format=html'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), 'html')
t.ok(response.headers['content-type'].includes('text/html'))
})

simple.concat({
method: 'GET',
url: url + route + '?format=json'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), JSON.stringify(jsonContent))
t.ok(response.headers['content-type'].includes('application/json'))
})
})
})

t.test('json format with url parameter format and without render option', t => {
t.plan(12)

const options = {
root: path.join(__dirname, '/static'),
prefix: '/public',
index: false,
list: {
format: 'json'
}
}
const route = '/public/'
const jsonContent = { dirs: ['deep', 'shallow'], files: ['.example', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }

helper.arrange(t, options, (url) => {
simple.concat({
method: 'GET',
url: url + route
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), JSON.stringify(jsonContent))
t.ok(response.headers['content-type'].includes('application/json'))
})

simple.concat({
method: 'GET',
url: url + route + '?format=html'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 500)
t.equal(JSON.parse(body.toString()).message, 'The `list.render` option must be a function and is required with the URL parameter `format=html`')
})

simple.concat({
method: 'GET',
url: url + route + '?format=json'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), JSON.stringify(jsonContent))
t.ok(response.headers['content-type'].includes('application/json'))
})
})
})

t.test('html format with url parameter format', t => {
t.plan(13)

const options = {
Expand All @@ -500,6 +588,7 @@ t.test('dir list - url parameter format', t => {
}
}
const route = '/public/'
const jsonContent = { dirs: ['deep', 'shallow'], files: ['.example', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }

helper.arrange(t, options, (url) => {
simple.concat({
Expand Down Expand Up @@ -528,7 +617,7 @@ t.test('dir list - url parameter format', t => {
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.ok(body.toString())
t.equal(body.toString(), JSON.stringify(jsonContent))
t.ok(response.headers['content-type'].includes('application/json'))
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ expectAssignable<FastifyStaticOptions>({
}
})

expectError<FastifyStaticOptions>({
expectAssignable<FastifyStaticOptions>({
root: '',
list: {
format: 'json',
Expand Down