@fastify/static 6.2.0 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -43,13 +43,20 @@ interface ListRender {
43
43
  }
44
44
 
45
45
  interface ListOptions {
46
- format: 'json' | 'html';
47
- names: string[];
48
- render: ListRender;
46
+ names?: string[];
49
47
  extendedFolderInfo?: boolean;
48
+ }
49
+
50
+ interface ListOptionsJsonFormat extends ListOptions {
51
+ format: 'json';
50
52
  jsonFormat?: 'names' | 'extended';
51
53
  }
52
54
 
55
+ interface ListOptionsHtmlFormat extends ListOptions {
56
+ format: 'html';
57
+ render: ListRender;
58
+ }
59
+
53
60
  // Passed on to `send`
54
61
  interface SendOptions {
55
62
  acceptRanges?: boolean;
@@ -73,7 +80,7 @@ export interface FastifyStaticOptions extends SendOptions {
73
80
  setHeaders?: (...args: any[]) => void;
74
81
  redirect?: boolean;
75
82
  wildcard?: boolean;
76
- list?: boolean | ListOptions;
83
+ list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
77
84
  allowedPath?: (pathName: string, root?: string) => boolean;
78
85
  /**
79
86
  * @description
package/lib/dirList.js CHANGED
@@ -8,7 +8,7 @@ const dirList = {
8
8
  /**
9
9
  * get files and dirs from dir, or error
10
10
  * @param {string} dir full path fs dir
11
- * @param {ListOptions} options
11
+ * @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
12
12
  * @param {string} dotfiles
13
13
  * note: can't use glob because don't get error on non existing dir
14
14
  */
@@ -99,7 +99,7 @@ const dirList = {
99
99
  * send dir list content, or 404 on error
100
100
  * @param {Fastify.Reply} reply
101
101
  * @param {string} dir full path fs dir
102
- * @param {ListOptions} options
102
+ * @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
103
103
  * @param {string} route request route
104
104
  * @param {string} dotfiles
105
105
  */
@@ -151,7 +151,7 @@ const dirList = {
151
151
  /**
152
152
  * say if the route can be handled by dir list or not
153
153
  * @param {string} route request route
154
- * @param {ListOptions} options
154
+ * @param {(boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat)} options
155
155
  * @return {boolean}
156
156
  */
157
157
  handle: function (route, options) {
@@ -194,9 +194,15 @@ const dirList = {
194
194
  if (options.list.names && !Array.isArray(options.list.names)) {
195
195
  return new TypeError('The `list.names` option must be an array')
196
196
  }
197
+ if (options.list.jsonFormat != null && options.list.jsonFormat !== 'names' && options.list.jsonFormat !== 'extended') {
198
+ return new TypeError('The `list.jsonFormat` option must be name or extended')
199
+ }
197
200
  if (options.list.format === 'html' && typeof options.list.render !== 'function') {
198
201
  return new TypeError('The `list.render` option must be a function and is required with html format')
199
202
  }
203
+ if (options.list.format === 'html' && options.list.jsonFormat != null) {
204
+ return new TypeError('The `list.jsonFormat` option must be with json format')
205
+ }
200
206
  }
201
207
 
202
208
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "6.2.0",
3
+ "version": "6.3.0",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -40,7 +40,7 @@ t.test('throws when `root` is an array', t => {
40
40
  t.equal(err.message, 'multi-root with list option is not supported')
41
41
  })
42
42
 
43
- t.test('throws when `list.format option` is invalid', t => {
43
+ t.test('throws when `list.format` option is invalid', t => {
44
44
  t.plan(2)
45
45
 
46
46
  const err = dirList.validateOptions({ list: { format: 'hello' } })
@@ -56,6 +56,14 @@ t.test('throws when `list.names option` is not an array', t => {
56
56
  t.equal(err.message, 'The `list.names` option must be an array')
57
57
  })
58
58
 
59
+ t.test('throws when `list.jsonFormat` option is invalid', t => {
60
+ t.plan(2)
61
+
62
+ const err = dirList.validateOptions({ list: { jsonFormat: 'hello' } })
63
+ t.type(err, TypeError)
64
+ t.equal(err.message, 'The `list.jsonFormat` option must be name or extended')
65
+ })
66
+
59
67
  t.test('throws when `list.format` is html and `list render` is not a function', t => {
60
68
  t.plan(2)
61
69
 
@@ -64,6 +72,14 @@ t.test('throws when `list.format` is html and `list render` is not a function',
64
72
  t.equal(err.message, 'The `list.render` option must be a function and is required with html format')
65
73
  })
66
74
 
75
+ t.test('throws when `list.format` is html and `list.jsonFormat` is given', t => {
76
+ t.plan(2)
77
+
78
+ const err = dirList.validateOptions({ list: { format: 'html', render: () => '', jsonFormat: 'extended' } })
79
+ t.type(err, TypeError)
80
+ t.equal(err.message, 'The `list.jsonFormat` option must be with json format')
81
+ })
82
+
67
83
  t.test('dir list wrong options', t => {
68
84
  t.plan(3)
69
85
 
@@ -1,5 +1,5 @@
1
1
  import fastify from 'fastify'
2
- import { expectError } from 'tsd'
2
+ import { expectAssignable, expectError } from 'tsd'
3
3
  import fastifyStatic, { FastifyStaticOptions } from '../..'
4
4
 
5
5
  const appWithImplicitHttp = fastify()
@@ -32,6 +32,36 @@ expectError<FastifyStaticOptions>({
32
32
  wildcard: '**/**'
33
33
  })
34
34
 
35
+ expectAssignable<FastifyStaticOptions>({
36
+ root: '',
37
+ list: {
38
+ format: 'json',
39
+ }
40
+ })
41
+
42
+ expectError<FastifyStaticOptions>({
43
+ root: '',
44
+ list: {
45
+ format: 'json',
46
+ render: () => ''
47
+ }
48
+ })
49
+
50
+ expectAssignable<FastifyStaticOptions>({
51
+ root: '',
52
+ list: {
53
+ format: 'html',
54
+ render: () => ''
55
+ }
56
+ })
57
+
58
+ expectError<FastifyStaticOptions>({
59
+ root: '',
60
+ list: {
61
+ format: 'html',
62
+ }
63
+ })
64
+
35
65
  appWithImplicitHttp
36
66
  .register(fastifyStatic, options)
37
67
  .after(() => {