-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.test-d.ts
119 lines (106 loc) · 2.99 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import fastify, { FastifyInstance } from 'fastify'
import { createReadStream } from 'fs'
import { expectError, expectType } from 'tsd'
import * as zlib from 'zlib'
import fastifyCompress, { FastifyCompressOptions } from '..'
const stream = createReadStream('./package.json')
const withGlobalOptions: FastifyCompressOptions = {
global: true,
threshold: 10,
zlib: zlib,
brotliOptions: {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
}
},
zlibOptions: { level: 1 },
inflateIfDeflated: true,
customTypes: /x-protobuf$/,
encodings: ['gzip', 'br', 'identity', 'deflate'],
requestEncodings: ['gzip', 'br', 'identity', 'deflate'],
forceRequestEncoding: 'gzip',
removeContentLengthHeader: true
}
const app: FastifyInstance = fastify()
app.register(fastifyCompress, withGlobalOptions)
app.register(fastifyCompress, {
customTypes: value => value === 'application/json'
})
app.get('/test-one', async (request, reply) => {
expectType<void>(reply.compress(stream))
})
app.get('/test-two', async (request, reply) => {
expectError(reply.compress())
})
// Instantiation of an app without global
const appWithoutGlobal: FastifyInstance = fastify()
appWithoutGlobal.register(fastifyCompress, { global: false })
appWithoutGlobal.get('/one', {
compress: {
zlib: {
createGzip: () => zlib.createGzip()
},
removeContentLengthHeader: false
},
decompress: {
forceRequestEncoding: 'gzip',
zlib: {
createGunzip: () => zlib.createGunzip()
}
}
}, (request, reply) => {
expectType<void>(reply.type('text/plain').compress(stream))
})
appWithoutGlobal.get('/two', {
config: {
compress: {
zlib: {
createGzip: () => zlib.createGzip()
}
},
decompress: {
forceRequestEncoding: 'gzip',
zlib: {
createGunzip: () => zlib.createGunzip()
}
}
}
}, (request, reply) => {
expectType<void>(reply.type('text/plain').compress(stream))
})
expectError(
appWithoutGlobal.get('/throw-a-ts-arg-error-on-shorthand-route', {
compress: 'bad compress route option value',
decompress: 'bad decompress route option value'
}, (request, reply) => {
expectType<void>(reply.type('text/plain').compress(stream))
})
)
expectError(
appWithoutGlobal.route({
method: 'GET',
path: '/throw-a-ts-arg-error',
compress: 'bad compress route option value',
decompress: 'bad decompress route option value',
handler: (request, reply) => { expectType<void>(reply.type('text/plain').compress(stream)) }
})
)
appWithoutGlobal.inject(
{
method: 'GET',
path: '/throw-a-ts-arg-error',
headers: {
'accept-encoding': 'gzip'
}
},
(err) => {
expectType<Error | undefined>(err)
}
)
// Instantiation of an app that should trigger a typescript error
const appThatTriggerAnError = fastify()
expectError(appThatTriggerAnError.register(fastifyCompress, {
global: true,
thisOptionDoesNotExist: 'trigger a typescript error'
}))