fastify 3.26.0 → 4.0.0-alpha.1
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/README.md +5 -4
- package/build/build-error-serializer.js +27 -0
- package/build/build-validation.js +49 -35
- package/docs/Guides/Ecosystem.md +2 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Migration-Guide-V4.md +12 -0
- package/docs/Reference/ContentTypeParser.md +8 -1
- package/docs/Reference/Errors.md +51 -6
- package/docs/Reference/Hooks.md +4 -7
- package/docs/Reference/LTS.md +5 -4
- package/docs/Reference/Reply.md +23 -22
- package/docs/Reference/Request.md +1 -3
- package/docs/Reference/Routes.md +17 -10
- package/docs/Reference/Server.md +98 -63
- package/docs/Reference/TypeScript.md +11 -13
- package/docs/Reference/Validation-and-Serialization.md +32 -54
- package/docs/Type-Providers.md +257 -0
- package/examples/hooks.js +1 -1
- package/examples/simple-stream.js +18 -0
- package/fastify.d.ts +36 -22
- package/fastify.js +72 -53
- package/lib/configValidator.js +902 -1023
- package/lib/contentTypeParser.js +6 -16
- package/lib/context.js +36 -10
- package/lib/decorate.js +5 -3
- package/lib/error-handler.js +158 -0
- package/lib/error-serializer.js +257 -0
- package/lib/errors.js +49 -10
- package/lib/fourOhFour.js +31 -20
- package/lib/handleRequest.js +10 -13
- package/lib/hooks.js +14 -9
- package/lib/noop-set.js +10 -0
- package/lib/pluginOverride.js +0 -3
- package/lib/pluginUtils.js +3 -2
- package/lib/reply.js +44 -163
- package/lib/request.js +13 -10
- package/lib/route.js +158 -139
- package/lib/schema-controller.js +3 -3
- package/lib/schemas.js +27 -1
- package/lib/server.js +219 -116
- package/lib/symbols.js +6 -4
- package/lib/validation.js +2 -1
- package/lib/warnings.js +2 -12
- package/lib/wrapThenable.js +4 -11
- package/package.json +40 -45
- package/test/404s.test.js +265 -108
- package/test/500s.test.js +2 -2
- package/test/async-await.test.js +15 -71
- package/test/close.test.js +39 -1
- package/test/content-parser.test.js +32 -0
- package/test/context-config.test.js +56 -4
- package/test/custom-http-server.test.js +14 -7
- package/test/custom-parser-async.test.js +0 -65
- package/test/custom-parser.test.js +54 -121
- package/test/decorator.test.js +1 -3
- package/test/delete.test.js +5 -5
- package/test/encapsulated-error-handler.test.js +50 -0
- package/test/esm/index.test.js +0 -14
- package/test/fastify-instance.test.js +4 -4
- package/test/fluent-schema.test.js +4 -4
- package/test/get.test.js +3 -3
- package/test/helper.js +18 -3
- package/test/hooks-async.test.js +14 -47
- package/test/hooks.on-ready.test.js +9 -4
- package/test/hooks.test.js +58 -99
- package/test/http2/closing.test.js +5 -11
- package/test/http2/unknown-http-method.test.js +3 -9
- package/test/https/custom-https-server.test.js +12 -6
- package/test/inject.test.js +1 -1
- package/test/input-validation.js +2 -2
- package/test/internals/all.test.js +2 -2
- package/test/internals/contentTypeParser.test.js +4 -4
- package/test/internals/handleRequest.test.js +9 -46
- package/test/internals/initialConfig.test.js +33 -12
- package/test/internals/logger.test.js +1 -1
- package/test/internals/reply.test.js +245 -3
- package/test/internals/request.test.js +13 -7
- package/test/internals/server.test.js +88 -0
- package/test/listen.test.js +84 -1
- package/test/logger.test.js +98 -58
- package/test/maxRequestsPerSocket.test.js +8 -6
- package/test/middleware.test.js +2 -25
- package/test/noop-set.test.js +19 -0
- package/test/nullable-validation.test.js +51 -14
- package/test/plugin.test.js +31 -5
- package/test/pretty-print.test.js +22 -10
- package/test/reply-error.test.js +123 -12
- package/test/request-error.test.js +2 -5
- package/test/route-hooks.test.js +17 -17
- package/test/route-prefix.test.js +2 -1
- package/test/route.test.js +216 -20
- package/test/router-options.test.js +1 -1
- package/test/schema-examples.test.js +11 -5
- package/test/schema-feature.test.js +24 -19
- package/test/schema-serialization.test.js +50 -9
- package/test/schema-special-usage.test.js +14 -81
- package/test/schema-validation.test.js +9 -9
- package/test/skip-reply-send.test.js +8 -8
- package/test/stream.test.js +23 -12
- package/test/throw.test.js +8 -5
- package/test/trust-proxy.test.js +1 -1
- package/test/type-provider.test.js +20 -0
- package/test/types/fastify.test-d.ts +12 -18
- package/test/types/hooks.test-d.ts +7 -3
- package/test/types/import.js +2 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +61 -15
- package/test/types/logger.test-d.ts +44 -15
- package/test/types/route.test-d.ts +8 -2
- package/test/types/schema.test-d.ts +2 -39
- package/test/types/type-provider.test-d.ts +417 -0
- package/test/validation-error-handling.test.js +9 -9
- package/test/versioned-routes.test.js +29 -17
- package/test/wrapThenable.test.js +7 -6
- package/types/.eslintrc.json +1 -1
- package/types/content-type-parser.d.ts +17 -8
- package/types/hooks.d.ts +107 -60
- package/types/instance.d.ts +137 -105
- package/types/logger.d.ts +18 -104
- package/types/plugin.d.ts +10 -4
- package/types/register.d.ts +1 -1
- package/types/reply.d.ts +16 -11
- package/types/request.d.ts +10 -5
- package/types/route.d.ts +42 -31
- package/types/schema.d.ts +15 -1
- package/types/type-provider.d.ts +99 -0
- package/types/utils.d.ts +1 -1
- package/lib/schema-compilers.js +0 -12
- package/test/emit-warning.test.js +0 -166
|
@@ -10,6 +10,7 @@ import { expectAssignable, expectError, expectType } from 'tsd'
|
|
|
10
10
|
import { FastifyRequest } from '../../types/request'
|
|
11
11
|
import { FastifyReply } from '../../types/reply'
|
|
12
12
|
import { HookHandlerDoneFunction } from '../../types/hooks'
|
|
13
|
+
import { FastifySchemaControllerOptions } from '../../types/schema'
|
|
13
14
|
|
|
14
15
|
const server = fastify()
|
|
15
16
|
|
|
@@ -30,6 +31,7 @@ expectType<string>(server.printPlugins())
|
|
|
30
31
|
|
|
31
32
|
expectAssignable<FastifyInstance>(
|
|
32
33
|
server.setErrorHandler(function (error, request, reply) {
|
|
34
|
+
expectType<FastifyError>(error)
|
|
33
35
|
expectAssignable<FastifyInstance>(this)
|
|
34
36
|
})
|
|
35
37
|
)
|
|
@@ -46,14 +48,18 @@ server.setErrorHandler(fastifyErrorHandler)
|
|
|
46
48
|
async function asyncFastifyErrorHandler (this: FastifyInstance, error: FastifyError) {}
|
|
47
49
|
server.setErrorHandler(asyncFastifyErrorHandler)
|
|
48
50
|
|
|
49
|
-
function nodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
51
|
+
function nodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
52
|
+
if (error) { throw error }
|
|
53
|
+
}
|
|
50
54
|
server.setErrorHandler(nodeJSErrorHandler)
|
|
51
55
|
|
|
52
|
-
function asyncNodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
56
|
+
function asyncNodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
57
|
+
if (error) { throw error }
|
|
58
|
+
}
|
|
53
59
|
server.setErrorHandler(asyncNodeJSErrorHandler)
|
|
54
60
|
|
|
55
61
|
class CustomError extends Error {
|
|
56
|
-
private __brand: any
|
|
62
|
+
private __brand: any
|
|
57
63
|
}
|
|
58
64
|
interface ReplyPayload {
|
|
59
65
|
Reply: {
|
|
@@ -81,20 +87,20 @@ expectError(server.setErrorHandler<CustomError, ReplyPayload>((error, request, r
|
|
|
81
87
|
reply.send({ test: 'foo' })
|
|
82
88
|
}))
|
|
83
89
|
// typed sync error handler return error
|
|
84
|
-
|
|
90
|
+
server.setErrorHandler<CustomError, ReplyPayload>((error, request, reply) => {
|
|
85
91
|
expectType<CustomError>(error)
|
|
86
92
|
return { test: 'foo' }
|
|
87
|
-
})
|
|
93
|
+
})
|
|
88
94
|
// typed async error handler send error
|
|
89
95
|
expectError(server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
|
|
90
96
|
expectType<CustomError>(error)
|
|
91
97
|
reply.send({ test: 'foo' })
|
|
92
98
|
}))
|
|
93
99
|
// typed async error handler return error
|
|
94
|
-
|
|
100
|
+
server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
|
|
95
101
|
expectType<CustomError>(error)
|
|
96
102
|
return { test: 'foo' }
|
|
97
|
-
})
|
|
103
|
+
})
|
|
98
104
|
|
|
99
105
|
function notFoundHandler (request: FastifyRequest, reply: FastifyReply) {}
|
|
100
106
|
function notFoundpreHandlerHandler (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) { done() }
|
|
@@ -109,9 +115,34 @@ server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFo
|
|
|
109
115
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundHandler)
|
|
110
116
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler }, notFoundHandler)
|
|
111
117
|
|
|
112
|
-
function invalidErrorHandler (error: number) {
|
|
118
|
+
function invalidErrorHandler (error: number) {
|
|
119
|
+
if (error) throw error
|
|
120
|
+
}
|
|
113
121
|
expectError(server.setErrorHandler(invalidErrorHandler))
|
|
114
122
|
|
|
123
|
+
server.setSchemaController({
|
|
124
|
+
bucket: (parentSchemas: unknown) => {
|
|
125
|
+
return {
|
|
126
|
+
addSchema (schema: unknown) {
|
|
127
|
+
expectType<unknown>(schema)
|
|
128
|
+
expectType<FastifyInstance>(server.addSchema({ type: 'null' }))
|
|
129
|
+
return server.addSchema({ type: 'null' })
|
|
130
|
+
},
|
|
131
|
+
getSchema (schemaId: string) {
|
|
132
|
+
expectType<string>(schemaId)
|
|
133
|
+
return server.getSchema('SchemaId')
|
|
134
|
+
},
|
|
135
|
+
getSchemas () {
|
|
136
|
+
expectType<Record<string, unknown>>(server.getSchemas())
|
|
137
|
+
return server.getSchemas()
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
function invalidSchemaController (schemaControllerOptions: FastifySchemaControllerOptions) {}
|
|
144
|
+
expectError(server.setSchemaController(invalidSchemaController))
|
|
145
|
+
|
|
115
146
|
server.setReplySerializer(function (payload, statusCode) {
|
|
116
147
|
expectType<unknown>(payload)
|
|
117
148
|
expectType<number>(statusCode)
|
|
@@ -124,16 +155,30 @@ expectError(server.setReplySerializer(invalidReplySerializer))
|
|
|
124
155
|
function serializerWithInvalidReturn (payload: unknown, statusCode: number) {}
|
|
125
156
|
expectError(server.setReplySerializer(serializerWithInvalidReturn))
|
|
126
157
|
|
|
127
|
-
function invalidSchemaErrorFormatter () {
|
|
158
|
+
function invalidSchemaErrorFormatter (err: Error) {
|
|
159
|
+
if (err) { throw err }
|
|
160
|
+
}
|
|
128
161
|
expectError(server.setSchemaErrorFormatter(invalidSchemaErrorFormatter))
|
|
129
162
|
|
|
130
163
|
// test listen method callback
|
|
131
|
-
expectAssignable<void>(server.listen(3000, '', 0, (err, address) => {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
expectAssignable<void>(server.listen('3000', '', (err, address) => {
|
|
135
|
-
|
|
136
|
-
|
|
164
|
+
expectAssignable<void>(server.listen(3000, '', 0, (err, address) => {
|
|
165
|
+
expectType<Error | null>(err)
|
|
166
|
+
}))
|
|
167
|
+
expectAssignable<void>(server.listen('3000', '', 0, (err, address) => {
|
|
168
|
+
expectType<Error | null>(err)
|
|
169
|
+
}))
|
|
170
|
+
expectAssignable<void>(server.listen(3000, '', (err, address) => {
|
|
171
|
+
expectType<Error | null>(err)
|
|
172
|
+
}))
|
|
173
|
+
expectAssignable<void>(server.listen('3000', '', (err, address) => {
|
|
174
|
+
expectType<Error | null>(err)
|
|
175
|
+
}))
|
|
176
|
+
expectAssignable<void>(server.listen(3000, (err, address) => {
|
|
177
|
+
expectType<Error | null>(err)
|
|
178
|
+
}))
|
|
179
|
+
expectAssignable<void>(server.listen('3000', (err, address) => {
|
|
180
|
+
expectType<Error | null>(err)
|
|
181
|
+
}))
|
|
137
182
|
|
|
138
183
|
// test listen method callback types
|
|
139
184
|
expectAssignable<void>(server.listen('3000', (err, address) => {
|
|
@@ -171,6 +216,7 @@ type InitialConfig = Readonly<{
|
|
|
171
216
|
keepAliveTimeout?: number,
|
|
172
217
|
bodyLimit?: number,
|
|
173
218
|
caseSensitive?: boolean,
|
|
219
|
+
forceCloseConnections?: boolean,
|
|
174
220
|
http2?: boolean,
|
|
175
221
|
https?: boolean | Readonly<{ allowHTTP1: boolean }>,
|
|
176
222
|
ignoreTrailingSlash?: boolean,
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { expectType } from 'tsd'
|
|
2
|
-
import fastify, {
|
|
2
|
+
import fastify, {
|
|
3
|
+
FastifyLogFn,
|
|
4
|
+
LogLevel,
|
|
5
|
+
FastifyLoggerInstance,
|
|
6
|
+
FastifyRequest,
|
|
7
|
+
FastifyReply,
|
|
8
|
+
FastifyBaseLogger
|
|
9
|
+
} from '../../fastify'
|
|
3
10
|
import { Server, IncomingMessage, ServerResponse } from 'http'
|
|
4
|
-
import pino from 'pino'
|
|
5
11
|
import * as fs from 'fs'
|
|
12
|
+
import P from 'pino'
|
|
6
13
|
|
|
7
14
|
expectType<FastifyLoggerInstance>(fastify().log)
|
|
8
15
|
|
|
@@ -15,14 +22,17 @@ class Foo {}
|
|
|
15
22
|
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({ foo: 'bar' }))
|
|
16
23
|
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Error()))
|
|
17
24
|
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Foo()))
|
|
18
|
-
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](0))
|
|
19
25
|
})
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
/*
|
|
28
|
+
// TODO make pino export BaseLogger again
|
|
29
|
+
interface CustomLogger extends FastifyBaseLogger {
|
|
22
30
|
customMethod(msg: string, ...args: unknown[]): void;
|
|
23
31
|
}
|
|
24
32
|
|
|
33
|
+
// // ToDo https://github.com/pinojs/pino/issues/1100
|
|
25
34
|
class CustomLoggerImpl implements CustomLogger {
|
|
35
|
+
level = 'info'
|
|
26
36
|
customMethod (msg: string, ...args: unknown[]) { console.log(msg, args) }
|
|
27
37
|
|
|
28
38
|
// Implementation signature must be compatible with all overloads of FastifyLogFn
|
|
@@ -35,7 +45,9 @@ class CustomLoggerImpl implements CustomLogger {
|
|
|
35
45
|
fatal (...args: unknown[]) { console.log(args) }
|
|
36
46
|
trace (...args: unknown[]) { console.log(args) }
|
|
37
47
|
debug (...args: unknown[]) { console.log(args) }
|
|
38
|
-
|
|
48
|
+
silent (...args: unknown[]) { }
|
|
49
|
+
|
|
50
|
+
child (bindings: P.Bindings, options?: P.ChildLoggerOptions): CustomLoggerImpl { return new CustomLoggerImpl() }
|
|
39
51
|
}
|
|
40
52
|
|
|
41
53
|
const customLogger = new CustomLoggerImpl()
|
|
@@ -48,20 +60,21 @@ CustomLoggerImpl
|
|
|
48
60
|
>({ logger: customLogger })
|
|
49
61
|
|
|
50
62
|
expectType<CustomLoggerImpl>(serverWithCustomLogger.log)
|
|
63
|
+
*/
|
|
51
64
|
|
|
52
65
|
const serverWithPino = fastify<
|
|
53
66
|
Server,
|
|
54
67
|
IncomingMessage,
|
|
55
68
|
ServerResponse,
|
|
56
|
-
|
|
69
|
+
P.Logger
|
|
57
70
|
>({
|
|
58
|
-
logger:
|
|
71
|
+
logger: P({
|
|
59
72
|
level: 'info',
|
|
60
73
|
redact: ['x-userinfo']
|
|
61
74
|
})
|
|
62
75
|
})
|
|
63
76
|
|
|
64
|
-
expectType<
|
|
77
|
+
expectType<P.Logger>(serverWithPino.log)
|
|
65
78
|
|
|
66
79
|
const serverWithLogOptions = fastify<
|
|
67
80
|
Server,
|
|
@@ -94,16 +107,16 @@ const serverAutoInferringTypes = fastify({
|
|
|
94
107
|
}
|
|
95
108
|
})
|
|
96
109
|
|
|
97
|
-
expectType<
|
|
110
|
+
expectType<FastifyBaseLogger>(serverAutoInferringTypes.log)
|
|
98
111
|
|
|
99
112
|
const serverWithAutoInferredPino = fastify({
|
|
100
|
-
logger:
|
|
113
|
+
logger: P({
|
|
101
114
|
level: 'info',
|
|
102
115
|
redact: ['x-userinfo']
|
|
103
116
|
})
|
|
104
117
|
})
|
|
105
118
|
|
|
106
|
-
expectType<
|
|
119
|
+
expectType<P.Logger>(serverWithAutoInferredPino.log)
|
|
107
120
|
|
|
108
121
|
const serverAutoInferredFileOption = fastify({
|
|
109
122
|
logger: {
|
|
@@ -112,7 +125,7 @@ const serverAutoInferredFileOption = fastify({
|
|
|
112
125
|
}
|
|
113
126
|
})
|
|
114
127
|
|
|
115
|
-
expectType<
|
|
128
|
+
expectType<FastifyBaseLogger>(serverAutoInferredFileOption.log)
|
|
116
129
|
|
|
117
130
|
const serverAutoInferredPinoPrettyBooleanOption = fastify({
|
|
118
131
|
logger: {
|
|
@@ -120,7 +133,7 @@ const serverAutoInferredPinoPrettyBooleanOption = fastify({
|
|
|
120
133
|
}
|
|
121
134
|
})
|
|
122
135
|
|
|
123
|
-
expectType<
|
|
136
|
+
expectType<FastifyBaseLogger>(serverAutoInferredPinoPrettyBooleanOption.log)
|
|
124
137
|
|
|
125
138
|
const serverAutoInferredPinoPrettyObjectOption = fastify({
|
|
126
139
|
logger: {
|
|
@@ -141,7 +154,7 @@ const serverAutoInferredPinoPrettyObjectOption = fastify({
|
|
|
141
154
|
}
|
|
142
155
|
})
|
|
143
156
|
|
|
144
|
-
expectType<
|
|
157
|
+
expectType<FastifyBaseLogger>(serverAutoInferredPinoPrettyObjectOption.log)
|
|
145
158
|
|
|
146
159
|
const serverAutoInferredSerializerObjectOption = fastify({
|
|
147
160
|
logger: {
|
|
@@ -176,10 +189,26 @@ const serverAutoInferredSerializerObjectOption = fastify({
|
|
|
176
189
|
}
|
|
177
190
|
})
|
|
178
191
|
|
|
179
|
-
expectType<
|
|
192
|
+
expectType<FastifyBaseLogger>(serverAutoInferredSerializerObjectOption.log)
|
|
180
193
|
|
|
181
194
|
const passStreamAsOption = fastify({
|
|
182
195
|
logger: {
|
|
183
196
|
stream: fs.createWriteStream('/tmp/stream.out')
|
|
184
197
|
}
|
|
185
198
|
})
|
|
199
|
+
|
|
200
|
+
expectType<FastifyBaseLogger>(passStreamAsOption.log)
|
|
201
|
+
|
|
202
|
+
const passPinoOption = fastify({
|
|
203
|
+
logger: {
|
|
204
|
+
redact: ['custom'],
|
|
205
|
+
messageKey: 'msg',
|
|
206
|
+
nestedKey: 'nested',
|
|
207
|
+
prettyPrint: {
|
|
208
|
+
|
|
209
|
+
},
|
|
210
|
+
enabled: true
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
expectType<FastifyBaseLogger>(passPinoOption.log)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fastify, { FastifyInstance, FastifyRequest, FastifyReply, RouteHandlerMethod } from '../../fastify'
|
|
2
|
-
import { expectType, expectError, expectAssignable } from 'tsd'
|
|
2
|
+
import { expectType, expectError, expectAssignable, printType } from 'tsd'
|
|
3
3
|
import { HTTPMethods } from '../../types/utils'
|
|
4
4
|
import * as http from 'http'
|
|
5
5
|
import { RequestPayload } from '../../types/hooks'
|
|
@@ -44,7 +44,13 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
|
|
|
44
44
|
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {}, routeHandler))
|
|
45
45
|
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', { handler: routeHandler }))
|
|
46
46
|
|
|
47
|
-
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {
|
|
47
|
+
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {
|
|
48
|
+
handler: routeHandler,
|
|
49
|
+
errorHandler: (error, request, reply) => {
|
|
50
|
+
expectType<FastifyError>(error)
|
|
51
|
+
reply.send('error')
|
|
52
|
+
}
|
|
53
|
+
}))
|
|
48
54
|
|
|
49
55
|
interface BodyInterface { prop: string }
|
|
50
56
|
interface QuerystringInterface { prop: number }
|
|
@@ -3,7 +3,7 @@ import fastify, { FastifyInstance, FastifyRequest, FastifySchema } from '../../f
|
|
|
3
3
|
import { RouteGenericInterface } from '../../types/route'
|
|
4
4
|
import { ContextConfigDefault } from '../../types/utils'
|
|
5
5
|
import { FastifyReply } from '../../types/reply'
|
|
6
|
-
import Ajv
|
|
6
|
+
import Ajv from 'ajv'
|
|
7
7
|
|
|
8
8
|
const server = fastify()
|
|
9
9
|
|
|
@@ -35,33 +35,6 @@ expectAssignable<FastifyInstance>(server.get(
|
|
|
35
35
|
() => { }
|
|
36
36
|
))
|
|
37
37
|
|
|
38
|
-
expectAssignable<FastifyInstance>(server.get<RouteGenericInterface, ContextConfigDefault, { validate:(data: any) => any }>(
|
|
39
|
-
'/no-schema',
|
|
40
|
-
{
|
|
41
|
-
schema: {},
|
|
42
|
-
validatorCompiler: ({ schema }) => {
|
|
43
|
-
// Error: Property 'validate' does not exist on type 'FastifySchema'.
|
|
44
|
-
return (data: any) => schema.validate(data)
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
() => { }
|
|
48
|
-
))
|
|
49
|
-
|
|
50
|
-
expectAssignable<FastifyInstance>(
|
|
51
|
-
server.route<RouteGenericInterface, ContextConfigDefault, { validate:(data: any) => any }>(
|
|
52
|
-
{
|
|
53
|
-
schema: {},
|
|
54
|
-
validatorCompiler: ({ schema }) => {
|
|
55
|
-
// Error: Property 'validate' does not exist on type 'FastifySchema'.
|
|
56
|
-
return (data: any) => schema.validate(data)
|
|
57
|
-
},
|
|
58
|
-
method: 'POST',
|
|
59
|
-
url: '/',
|
|
60
|
-
handler: async (_request: FastifyRequest, _reply: FastifyReply) => {}
|
|
61
|
-
}
|
|
62
|
-
)
|
|
63
|
-
)
|
|
64
|
-
|
|
65
38
|
expectAssignable<FastifyInstance>(server.setValidatorCompiler(({ schema }) => {
|
|
66
39
|
return new Ajv().compile(schema)
|
|
67
40
|
}))
|
|
@@ -83,20 +56,10 @@ expectAssignable<FastifyInstance>(server.post('/test', {
|
|
|
83
56
|
|
|
84
57
|
expectAssignable<FastifyInstance>(server.setValidatorCompiler<FastifySchema & { validate: Record<string, unknown> }>(
|
|
85
58
|
function ({ schema }) {
|
|
86
|
-
return new Ajv().compile(schema
|
|
59
|
+
return new Ajv().compile(schema)
|
|
87
60
|
}
|
|
88
61
|
))
|
|
89
62
|
|
|
90
63
|
expectAssignable<FastifyInstance>(server.setSerializerCompiler<FastifySchema & { validate: string }>(
|
|
91
64
|
() => data => JSON.stringify(data)
|
|
92
65
|
))
|
|
93
|
-
|
|
94
|
-
expectError(server.get(
|
|
95
|
-
'/unknown-schema-prop',
|
|
96
|
-
{
|
|
97
|
-
schema: {
|
|
98
|
-
unknown: { type: 'null' }
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
() => { }
|
|
102
|
-
))
|