/
request.test-d.ts
92 lines (79 loc) · 3.09 KB
/
request.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { expectType } from 'tsd'
import fastify, { RouteHandler, RawRequestDefaultExpression, RequestBodyDefault, RequestGenericInterface } from '../../fastify'
import { RequestParamsDefault, RequestHeadersDefault, RequestQuerystringDefault } from '../../types/utils'
import { FastifyLoggerInstance } from '../../types/logger'
import { FastifyRequest } from '../../types/request'
import { FastifyReply } from '../../types/reply'
import { FastifyInstance } from '../../types/instance'
interface RequestBody {
content: string;
}
interface RequestQuerystring {
from: string;
}
interface RequestParams {
id: number;
}
interface RequestHeaders {
'x-foobar': string;
}
interface RequestData extends RequestGenericInterface {
Body: RequestBody;
Querystring: RequestQuerystring;
Params: RequestParams;
Headers: RequestHeaders;
}
type Handler = RouteHandler<RequestData>
type CustomRequest = FastifyRequest<{
Body: RequestBody;
Querystring: RequestQuerystring;
Params: RequestParams;
Headers: RequestHeaders;
}>
const getHandler: RouteHandler = function (request, _reply) {
expectType<string>(request.url)
expectType<string>(request.method)
expectType<string>(request.routerPath)
expectType<string>(request.routerMethod)
expectType<boolean>(request.is404)
expectType<string>(request.hostname)
expectType<string>(request.ip)
expectType<string[] | undefined>(request.ips)
expectType<RawRequestDefaultExpression>(request.raw)
expectType<RequestBodyDefault>(request.body)
expectType<RequestParamsDefault>(request.params)
expectType<RequestHeadersDefault & RawRequestDefaultExpression['headers']>(request.headers)
request.headers = {}
expectType<RequestQuerystringDefault>(request.query)
expectType<any>(request.id)
expectType<FastifyLoggerInstance>(request.log)
expectType<RawRequestDefaultExpression['socket']>(request.socket)
expectType<Error & { validation: any; validationContext: string } | undefined>(request.validationError)
expectType<FastifyInstance>(request.server)
}
const postHandler: Handler = function (request) {
expectType<RequestBody>(request.body)
expectType<RequestParams>(request.params)
expectType<RequestHeaders & RawRequestDefaultExpression['headers']>(request.headers)
expectType<RequestQuerystring>(request.query)
expectType<string>(request.body.content)
expectType<string>(request.query.from)
expectType<number>(request.params.id)
expectType<string>(request.headers['x-foobar'])
expectType<FastifyInstance>(request.server)
}
function putHandler (request: CustomRequest, reply: FastifyReply) {
expectType<RequestBody>(request.body)
expectType<RequestParams>(request.params)
expectType<RequestHeaders & RawRequestDefaultExpression['headers']>(request.headers)
expectType<RequestQuerystring>(request.query)
expectType<string>(request.body.content)
expectType<string>(request.query.from)
expectType<number>(request.params.id)
expectType<string>(request.headers['x-foobar'])
expectType<FastifyInstance>(request.server)
}
const server = fastify()
server.get('/get', getHandler)
server.post('/post', postHandler)
server.put('/put', putHandler)