Skip to content

Commit

Permalink
fix: correct ListOptions type (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
is2ei committed May 18, 2022
1 parent 4cafcb2 commit c293d8c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
15 changes: 11 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ interface ListRender {
}

interface ListOptions {
format: 'json' | 'html';
names: string[];
render: ListRender;
names?: string[];
extendedFolderInfo?: boolean;
}

interface ListOptionsJsonFormat extends ListOptions {
format: 'json';
jsonFormat?: 'names' | 'extended';
}

interface ListOptionsHtmlFormat extends ListOptions {
format: 'html';
render: ListRender;
}

// Passed on to `send`
interface SendOptions {
acceptRanges?: boolean;
Expand All @@ -73,7 +80,7 @@ export interface FastifyStaticOptions extends SendOptions {
setHeaders?: (...args: any[]) => void;
redirect?: boolean;
wildcard?: boolean;
list?: boolean | ListOptions;
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
allowedPath?: (pathName: string, root?: string) => boolean;
/**
* @description
Expand Down
12 changes: 9 additions & 3 deletions lib/dirList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const dirList = {
/**
* get files and dirs from dir, or error
* @param {string} dir full path fs dir
* @param {ListOptions} options
* @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
* @param {string} dotfiles
* note: can't use glob because don't get error on non existing dir
*/
Expand Down Expand Up @@ -99,7 +99,7 @@ const dirList = {
* send dir list content, or 404 on error
* @param {Fastify.Reply} reply
* @param {string} dir full path fs dir
* @param {ListOptions} options
* @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
* @param {string} route request route
* @param {string} dotfiles
*/
Expand Down Expand Up @@ -151,7 +151,7 @@ const dirList = {
/**
* say if the route can be handled by dir list or not
* @param {string} route request route
* @param {ListOptions} options
* @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
* @return {boolean}
*/
handle: function (route, options) {
Expand Down Expand Up @@ -194,9 +194,15 @@ const dirList = {
if (options.list.names && !Array.isArray(options.list.names)) {
return new TypeError('The `list.names` option must be an array')
}
if (options.list.jsonFormat != null && options.list.jsonFormat !== 'names' && options.list.jsonFormat !== 'extended') {
return new TypeError('The `list.jsonFormat` option must be name or extended')
}
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
18 changes: 17 additions & 1 deletion test/dir-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ t.test('throws when `root` is an array', t => {
t.equal(err.message, 'multi-root with list option is not supported')
})

t.test('throws when `list.format option` is invalid', t => {
t.test('throws when `list.format` option is invalid', t => {
t.plan(2)

const err = dirList.validateOptions({ list: { format: 'hello' } })
Expand All @@ -56,6 +56,14 @@ t.test('throws when `list.names option` is not an array', t => {
t.equal(err.message, 'The `list.names` option must be an array')
})

t.test('throws when `list.jsonFormat` option is invalid', t => {
t.plan(2)

const err = dirList.validateOptions({ list: { jsonFormat: 'hello' } })
t.type(err, TypeError)
t.equal(err.message, 'The `list.jsonFormat` option must be name or extended')
})

t.test('throws when `list.format` is html and `list render` is not a function', t => {
t.plan(2)

Expand All @@ -64,6 +72,14 @@ 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
32 changes: 31 additions & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastify from 'fastify'
import { expectError } from 'tsd'
import { expectAssignable, expectError } from 'tsd'
import fastifyStatic, { FastifyStaticOptions } from '../..'

const appWithImplicitHttp = fastify()
Expand Down Expand Up @@ -32,6 +32,36 @@ expectError<FastifyStaticOptions>({
wildcard: '**/**'
})

expectAssignable<FastifyStaticOptions>({
root: '',
list: {
format: 'json',
}
})

expectError<FastifyStaticOptions>({
root: '',
list: {
format: 'json',
render: () => ''
}
})

expectAssignable<FastifyStaticOptions>({
root: '',
list: {
format: 'html',
render: () => ''
}
})

expectError<FastifyStaticOptions>({
root: '',
list: {
format: 'html',
}
})

appWithImplicitHttp
.register(fastifyStatic, options)
.after(() => {
Expand Down

0 comments on commit c293d8c

Please sign in to comment.