fastify 4.17.0 → 4.18.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/GOVERNANCE.md +2 -2
- package/README.md +13 -10
- package/docs/Guides/Ecosystem.md +6 -0
- package/docs/Guides/Serverless.md +12 -1
- package/docs/Reference/Errors.md +5 -0
- package/docs/Reference/LTS.md +5 -5
- package/docs/Reference/Lifecycle.md +3 -0
- package/docs/Reference/Logging.md +34 -1
- package/docs/Reference/Reply.md +19 -0
- package/docs/Reference/Request.md +1 -1
- package/docs/Reference/Server.md +20 -2
- package/docs/Reference/Validation-and-Serialization.md +3 -2
- package/examples/benchmark/body.json +3 -0
- package/fastify.d.ts +6 -1
- package/fastify.js +17 -12
- package/lib/contentTypeParser.js +3 -1
- package/lib/errors.js +5 -0
- package/lib/handleRequest.js +15 -4
- package/lib/reply.js +3 -2
- package/lib/route.js +14 -3
- package/lib/schemas.js +18 -21
- package/lib/server.js +9 -4
- package/lib/validation.js +98 -16
- package/package.json +33 -33
- package/test/custom-parser.0.test.js +777 -0
- package/test/{custom-parser.test.js → custom-parser.1.test.js} +1 -742
- package/test/fastify-instance.test.js +39 -0
- package/test/hooks-async.test.js +154 -0
- package/test/hooks.test.js +29 -29
- package/test/internals/reply.test.js +114 -45
- package/test/internals/validation.test.js +58 -0
- package/test/reply-error.test.js +7 -7
- package/test/request-error.test.js +3 -0
- package/test/route-hooks.test.js +38 -0
- package/test/route.test.js +192 -74
- package/test/schema-special-usage.test.js +569 -0
- package/test/types/fastify.test-d.ts +4 -1
- package/test/types/instance.test-d.ts +1 -0
- package/test/types/logger.test-d.ts +10 -4
- package/test/types/reply.test-d.ts +4 -1
- package/test/types/route.test-d.ts +12 -0
- package/test/url-rewriting.test.js +3 -0
- package/types/.eslintrc.json +1 -1
- package/types/instance.d.ts +1 -1
- package/types/logger.d.ts +10 -1
- package/types/reply.d.ts +7 -1
- package/types/type-provider.d.ts +0 -1
- package/types/utils.d.ts +3 -1
- /package/types/{tsconfig.json → tsconfig.eslint.json} +0 -0
|
@@ -744,3 +744,572 @@ test('The default schema compilers should not be called when overwritte by the u
|
|
|
744
744
|
|
|
745
745
|
await fastify.ready()
|
|
746
746
|
})
|
|
747
|
+
|
|
748
|
+
test('Supports async JOI validation', t => {
|
|
749
|
+
t.plan(7)
|
|
750
|
+
|
|
751
|
+
const schemaValidator = ({ schema }) => async data => {
|
|
752
|
+
const validationResult = await schema.validateAsync(data)
|
|
753
|
+
return validationResult
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
const fastify = Fastify({
|
|
757
|
+
exposeHeadRoutes: false
|
|
758
|
+
})
|
|
759
|
+
fastify.setValidatorCompiler(schemaValidator)
|
|
760
|
+
|
|
761
|
+
fastify.get('/', {
|
|
762
|
+
schema: {
|
|
763
|
+
headers: Joi.object({
|
|
764
|
+
'user-agent': Joi.string().external(async (val) => {
|
|
765
|
+
if (val !== 'lightMyRequest') {
|
|
766
|
+
throw new Error('Invalid user-agent')
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
t.equal(val, 'lightMyRequest')
|
|
770
|
+
return val
|
|
771
|
+
}),
|
|
772
|
+
host: Joi.string().required()
|
|
773
|
+
})
|
|
774
|
+
}
|
|
775
|
+
}, (request, reply) => {
|
|
776
|
+
reply.send(request.headers)
|
|
777
|
+
})
|
|
778
|
+
|
|
779
|
+
fastify.inject('/', (err, res) => {
|
|
780
|
+
t.error(err)
|
|
781
|
+
t.equal(res.statusCode, 200)
|
|
782
|
+
t.same(res.json(), {
|
|
783
|
+
'user-agent': 'lightMyRequest',
|
|
784
|
+
host: 'localhost:80'
|
|
785
|
+
})
|
|
786
|
+
})
|
|
787
|
+
|
|
788
|
+
fastify.inject({
|
|
789
|
+
url: '/',
|
|
790
|
+
headers: {
|
|
791
|
+
'user-agent': 'invalid'
|
|
792
|
+
}
|
|
793
|
+
}, (err, res) => {
|
|
794
|
+
t.error(err)
|
|
795
|
+
t.equal(res.statusCode, 400)
|
|
796
|
+
t.same(res.json(), {
|
|
797
|
+
statusCode: 400,
|
|
798
|
+
code: 'FST_ERR_VALIDATION',
|
|
799
|
+
error: 'Bad Request',
|
|
800
|
+
message: 'Invalid user-agent (user-agent)'
|
|
801
|
+
})
|
|
802
|
+
})
|
|
803
|
+
})
|
|
804
|
+
|
|
805
|
+
test('Supports async AJV validation', t => {
|
|
806
|
+
t.plan(12)
|
|
807
|
+
|
|
808
|
+
const fastify = Fastify({
|
|
809
|
+
exposeHeadRoutes: false,
|
|
810
|
+
ajv: {
|
|
811
|
+
customOptions: {
|
|
812
|
+
allErrors: true,
|
|
813
|
+
keywords: [
|
|
814
|
+
{
|
|
815
|
+
keyword: 'idExists',
|
|
816
|
+
async: true,
|
|
817
|
+
type: 'number',
|
|
818
|
+
validate: checkIdExists
|
|
819
|
+
}
|
|
820
|
+
]
|
|
821
|
+
},
|
|
822
|
+
plugins: [
|
|
823
|
+
[ajvErrors, { singleError: '@@@@' }]
|
|
824
|
+
]
|
|
825
|
+
}
|
|
826
|
+
})
|
|
827
|
+
|
|
828
|
+
async function checkIdExists (schema, data) {
|
|
829
|
+
const res = await Promise.resolve(data)
|
|
830
|
+
switch (res) {
|
|
831
|
+
case 42:
|
|
832
|
+
return true
|
|
833
|
+
|
|
834
|
+
case 500:
|
|
835
|
+
throw new Error('custom error')
|
|
836
|
+
|
|
837
|
+
default:
|
|
838
|
+
return false
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
const schema = {
|
|
843
|
+
$async: true,
|
|
844
|
+
type: 'object',
|
|
845
|
+
properties: {
|
|
846
|
+
userId: {
|
|
847
|
+
type: 'integer',
|
|
848
|
+
idExists: { table: 'users' }
|
|
849
|
+
},
|
|
850
|
+
postId: {
|
|
851
|
+
type: 'integer',
|
|
852
|
+
idExists: { table: 'posts' }
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
fastify.post('/', {
|
|
858
|
+
schema: {
|
|
859
|
+
body: schema
|
|
860
|
+
},
|
|
861
|
+
handler (req, reply) { reply.send(req.body) }
|
|
862
|
+
})
|
|
863
|
+
|
|
864
|
+
fastify.inject({
|
|
865
|
+
method: 'POST',
|
|
866
|
+
url: '/',
|
|
867
|
+
payload: { userId: 99 }
|
|
868
|
+
}, (err, res) => {
|
|
869
|
+
t.error(err)
|
|
870
|
+
t.equal(res.statusCode, 400)
|
|
871
|
+
t.same(res.json(), {
|
|
872
|
+
statusCode: 400,
|
|
873
|
+
code: 'FST_ERR_VALIDATION',
|
|
874
|
+
error: 'Bad Request',
|
|
875
|
+
message: 'validation failed'
|
|
876
|
+
})
|
|
877
|
+
})
|
|
878
|
+
|
|
879
|
+
fastify.inject({
|
|
880
|
+
method: 'POST',
|
|
881
|
+
url: '/',
|
|
882
|
+
payload: { userId: 500 }
|
|
883
|
+
}, (err, res) => {
|
|
884
|
+
t.error(err)
|
|
885
|
+
t.equal(res.statusCode, 400)
|
|
886
|
+
t.same(res.json(), {
|
|
887
|
+
statusCode: 400,
|
|
888
|
+
code: 'FST_ERR_VALIDATION',
|
|
889
|
+
error: 'Bad Request',
|
|
890
|
+
message: 'custom error'
|
|
891
|
+
})
|
|
892
|
+
})
|
|
893
|
+
|
|
894
|
+
fastify.inject({
|
|
895
|
+
method: 'POST',
|
|
896
|
+
url: '/',
|
|
897
|
+
payload: { userId: 42 }
|
|
898
|
+
}, (err, res) => {
|
|
899
|
+
t.error(err)
|
|
900
|
+
t.equal(res.statusCode, 200)
|
|
901
|
+
t.same(res.json(), { userId: 42 })
|
|
902
|
+
})
|
|
903
|
+
|
|
904
|
+
fastify.inject({
|
|
905
|
+
method: 'POST',
|
|
906
|
+
url: '/',
|
|
907
|
+
payload: { userId: 42, postId: 19 }
|
|
908
|
+
}, (err, res) => {
|
|
909
|
+
t.error(err)
|
|
910
|
+
t.equal(res.statusCode, 400)
|
|
911
|
+
t.same(res.json(), {
|
|
912
|
+
statusCode: 400,
|
|
913
|
+
code: 'FST_ERR_VALIDATION',
|
|
914
|
+
error: 'Bad Request',
|
|
915
|
+
message: 'validation failed'
|
|
916
|
+
})
|
|
917
|
+
})
|
|
918
|
+
})
|
|
919
|
+
|
|
920
|
+
test('Check all the async AJV validation paths', t => {
|
|
921
|
+
const fastify = Fastify({
|
|
922
|
+
exposeHeadRoutes: false,
|
|
923
|
+
ajv: {
|
|
924
|
+
customOptions: {
|
|
925
|
+
allErrors: true,
|
|
926
|
+
keywords: [
|
|
927
|
+
{
|
|
928
|
+
keyword: 'idExists',
|
|
929
|
+
async: true,
|
|
930
|
+
type: 'number',
|
|
931
|
+
validate: checkIdExists
|
|
932
|
+
}
|
|
933
|
+
]
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
})
|
|
937
|
+
|
|
938
|
+
async function checkIdExists (schema, data) {
|
|
939
|
+
const res = await Promise.resolve(data)
|
|
940
|
+
switch (res) {
|
|
941
|
+
case 200:
|
|
942
|
+
return true
|
|
943
|
+
|
|
944
|
+
default:
|
|
945
|
+
return false
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
const schema = {
|
|
950
|
+
$async: true,
|
|
951
|
+
type: 'object',
|
|
952
|
+
properties: {
|
|
953
|
+
id: {
|
|
954
|
+
type: 'integer',
|
|
955
|
+
idExists: { table: 'posts' }
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
fastify.post('/:id', {
|
|
961
|
+
schema: {
|
|
962
|
+
params: schema,
|
|
963
|
+
body: schema,
|
|
964
|
+
query: schema,
|
|
965
|
+
headers: schema
|
|
966
|
+
},
|
|
967
|
+
handler (req, reply) { reply.send(req.body) }
|
|
968
|
+
})
|
|
969
|
+
|
|
970
|
+
const testCases = [
|
|
971
|
+
{
|
|
972
|
+
params: 400,
|
|
973
|
+
body: 200,
|
|
974
|
+
querystring: 200,
|
|
975
|
+
headers: 200,
|
|
976
|
+
response: 400
|
|
977
|
+
},
|
|
978
|
+
{
|
|
979
|
+
params: 200,
|
|
980
|
+
body: 400,
|
|
981
|
+
querystring: 200,
|
|
982
|
+
headers: 200,
|
|
983
|
+
response: 400
|
|
984
|
+
},
|
|
985
|
+
{
|
|
986
|
+
params: 200,
|
|
987
|
+
body: 200,
|
|
988
|
+
querystring: 400,
|
|
989
|
+
headers: 200,
|
|
990
|
+
response: 400
|
|
991
|
+
},
|
|
992
|
+
{
|
|
993
|
+
params: 200,
|
|
994
|
+
body: 200,
|
|
995
|
+
querystring: 200,
|
|
996
|
+
headers: 400,
|
|
997
|
+
response: 400
|
|
998
|
+
},
|
|
999
|
+
{
|
|
1000
|
+
params: 200,
|
|
1001
|
+
body: 200,
|
|
1002
|
+
querystring: 200,
|
|
1003
|
+
headers: 200,
|
|
1004
|
+
response: 200
|
|
1005
|
+
}
|
|
1006
|
+
]
|
|
1007
|
+
t.plan(testCases.length * 2)
|
|
1008
|
+
testCases.forEach(validate)
|
|
1009
|
+
|
|
1010
|
+
function validate ({
|
|
1011
|
+
params,
|
|
1012
|
+
body,
|
|
1013
|
+
querystring,
|
|
1014
|
+
headers,
|
|
1015
|
+
response
|
|
1016
|
+
}) {
|
|
1017
|
+
fastify.inject({
|
|
1018
|
+
method: 'POST',
|
|
1019
|
+
url: `/${params}`,
|
|
1020
|
+
headers: { id: headers },
|
|
1021
|
+
query: { id: querystring },
|
|
1022
|
+
payload: { id: body }
|
|
1023
|
+
}, (err, res) => {
|
|
1024
|
+
t.error(err)
|
|
1025
|
+
t.equal(res.statusCode, response)
|
|
1026
|
+
})
|
|
1027
|
+
}
|
|
1028
|
+
})
|
|
1029
|
+
|
|
1030
|
+
test('Check mixed sync and async AJV validations', t => {
|
|
1031
|
+
const fastify = Fastify({
|
|
1032
|
+
exposeHeadRoutes: false,
|
|
1033
|
+
ajv: {
|
|
1034
|
+
customOptions: {
|
|
1035
|
+
allErrors: true,
|
|
1036
|
+
keywords: [
|
|
1037
|
+
{
|
|
1038
|
+
keyword: 'idExists',
|
|
1039
|
+
async: true,
|
|
1040
|
+
type: 'number',
|
|
1041
|
+
validate: checkIdExists
|
|
1042
|
+
}
|
|
1043
|
+
]
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
})
|
|
1047
|
+
|
|
1048
|
+
async function checkIdExists (schema, data) {
|
|
1049
|
+
const res = await Promise.resolve(data)
|
|
1050
|
+
switch (res) {
|
|
1051
|
+
case 200:
|
|
1052
|
+
return true
|
|
1053
|
+
|
|
1054
|
+
default:
|
|
1055
|
+
return false
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
const schemaSync = {
|
|
1060
|
+
type: 'object',
|
|
1061
|
+
properties: {
|
|
1062
|
+
id: { type: 'integer' }
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
const schemaAsync = {
|
|
1067
|
+
$async: true,
|
|
1068
|
+
type: 'object',
|
|
1069
|
+
properties: {
|
|
1070
|
+
id: {
|
|
1071
|
+
type: 'integer',
|
|
1072
|
+
idExists: { table: 'posts' }
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
fastify.post('/queryAsync/:id', {
|
|
1078
|
+
schema: {
|
|
1079
|
+
params: schemaSync,
|
|
1080
|
+
body: schemaSync,
|
|
1081
|
+
query: schemaAsync,
|
|
1082
|
+
headers: schemaSync
|
|
1083
|
+
},
|
|
1084
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1085
|
+
})
|
|
1086
|
+
|
|
1087
|
+
fastify.post('/paramsAsync/:id', {
|
|
1088
|
+
schema: {
|
|
1089
|
+
params: schemaAsync,
|
|
1090
|
+
body: schemaSync
|
|
1091
|
+
},
|
|
1092
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1093
|
+
})
|
|
1094
|
+
|
|
1095
|
+
fastify.post('/bodyAsync/:id', {
|
|
1096
|
+
schema: {
|
|
1097
|
+
params: schemaAsync,
|
|
1098
|
+
body: schemaAsync,
|
|
1099
|
+
query: schemaSync
|
|
1100
|
+
},
|
|
1101
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1102
|
+
})
|
|
1103
|
+
|
|
1104
|
+
fastify.post('/headersSync/:id', {
|
|
1105
|
+
schema: {
|
|
1106
|
+
params: schemaSync,
|
|
1107
|
+
body: schemaSync,
|
|
1108
|
+
query: schemaAsync,
|
|
1109
|
+
headers: schemaSync
|
|
1110
|
+
},
|
|
1111
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1112
|
+
})
|
|
1113
|
+
|
|
1114
|
+
fastify.post('/noHeader/:id', {
|
|
1115
|
+
schema: {
|
|
1116
|
+
params: schemaSync,
|
|
1117
|
+
body: schemaSync,
|
|
1118
|
+
query: schemaAsync
|
|
1119
|
+
},
|
|
1120
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1121
|
+
})
|
|
1122
|
+
|
|
1123
|
+
fastify.post('/noBody/:id', {
|
|
1124
|
+
schema: {
|
|
1125
|
+
params: schemaSync,
|
|
1126
|
+
query: schemaAsync,
|
|
1127
|
+
headers: schemaSync
|
|
1128
|
+
},
|
|
1129
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1130
|
+
})
|
|
1131
|
+
|
|
1132
|
+
const testCases = [
|
|
1133
|
+
{
|
|
1134
|
+
url: '/queryAsync',
|
|
1135
|
+
params: 200,
|
|
1136
|
+
body: 200,
|
|
1137
|
+
querystring: 200,
|
|
1138
|
+
headers: 'not a number sync',
|
|
1139
|
+
response: 400
|
|
1140
|
+
},
|
|
1141
|
+
{
|
|
1142
|
+
url: '/paramsAsync',
|
|
1143
|
+
params: 200,
|
|
1144
|
+
body: 'not a number sync',
|
|
1145
|
+
querystring: 200,
|
|
1146
|
+
headers: 200,
|
|
1147
|
+
response: 400
|
|
1148
|
+
},
|
|
1149
|
+
{
|
|
1150
|
+
url: '/bodyAsync',
|
|
1151
|
+
params: 200,
|
|
1152
|
+
body: 200,
|
|
1153
|
+
querystring: 'not a number sync',
|
|
1154
|
+
headers: 200,
|
|
1155
|
+
response: 400
|
|
1156
|
+
},
|
|
1157
|
+
{
|
|
1158
|
+
url: '/headersSync',
|
|
1159
|
+
params: 200,
|
|
1160
|
+
body: 200,
|
|
1161
|
+
querystring: 200,
|
|
1162
|
+
headers: 'not a number sync',
|
|
1163
|
+
response: 400
|
|
1164
|
+
},
|
|
1165
|
+
{
|
|
1166
|
+
url: '/noHeader',
|
|
1167
|
+
params: 200,
|
|
1168
|
+
body: 200,
|
|
1169
|
+
querystring: 200,
|
|
1170
|
+
headers: 'not a number sync, but not validated',
|
|
1171
|
+
response: 200
|
|
1172
|
+
},
|
|
1173
|
+
{
|
|
1174
|
+
url: '/noBody',
|
|
1175
|
+
params: 200,
|
|
1176
|
+
body: 'not a number sync, but not validated',
|
|
1177
|
+
querystring: 200,
|
|
1178
|
+
headers: 'not a number sync',
|
|
1179
|
+
response: 400
|
|
1180
|
+
}
|
|
1181
|
+
]
|
|
1182
|
+
t.plan(testCases.length * 2)
|
|
1183
|
+
testCases.forEach(validate)
|
|
1184
|
+
|
|
1185
|
+
function validate ({
|
|
1186
|
+
url,
|
|
1187
|
+
params,
|
|
1188
|
+
body,
|
|
1189
|
+
querystring,
|
|
1190
|
+
headers,
|
|
1191
|
+
response
|
|
1192
|
+
}) {
|
|
1193
|
+
fastify.inject({
|
|
1194
|
+
method: 'POST',
|
|
1195
|
+
url: `${url}/${params || ''}`,
|
|
1196
|
+
headers: { id: headers },
|
|
1197
|
+
query: { id: querystring },
|
|
1198
|
+
payload: { id: body }
|
|
1199
|
+
}, (err, res) => {
|
|
1200
|
+
t.error(err)
|
|
1201
|
+
t.equal(res.statusCode, response)
|
|
1202
|
+
})
|
|
1203
|
+
}
|
|
1204
|
+
})
|
|
1205
|
+
|
|
1206
|
+
test('Check if hooks and attachValidation work with AJV validations', t => {
|
|
1207
|
+
const fastify = Fastify({
|
|
1208
|
+
exposeHeadRoutes: false,
|
|
1209
|
+
ajv: {
|
|
1210
|
+
customOptions: {
|
|
1211
|
+
allErrors: true,
|
|
1212
|
+
keywords: [
|
|
1213
|
+
{
|
|
1214
|
+
keyword: 'idExists',
|
|
1215
|
+
async: true,
|
|
1216
|
+
type: 'number',
|
|
1217
|
+
validate: checkIdExists
|
|
1218
|
+
}
|
|
1219
|
+
]
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
})
|
|
1223
|
+
|
|
1224
|
+
async function checkIdExists (schema, data) {
|
|
1225
|
+
const res = await Promise.resolve(data)
|
|
1226
|
+
switch (res) {
|
|
1227
|
+
case 200:
|
|
1228
|
+
return true
|
|
1229
|
+
|
|
1230
|
+
default:
|
|
1231
|
+
return false
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
const schemaAsync = {
|
|
1236
|
+
$async: true,
|
|
1237
|
+
type: 'object',
|
|
1238
|
+
properties: {
|
|
1239
|
+
id: {
|
|
1240
|
+
type: 'integer',
|
|
1241
|
+
idExists: { table: 'posts' }
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
fastify.post('/:id', {
|
|
1247
|
+
preHandler: function hook (request, reply, done) {
|
|
1248
|
+
t.equal(request.validationError.message, 'validation failed')
|
|
1249
|
+
t.pass('preHandler called')
|
|
1250
|
+
|
|
1251
|
+
reply.code(400).send(request.body)
|
|
1252
|
+
},
|
|
1253
|
+
attachValidation: true,
|
|
1254
|
+
schema: {
|
|
1255
|
+
params: schemaAsync,
|
|
1256
|
+
body: schemaAsync,
|
|
1257
|
+
query: schemaAsync,
|
|
1258
|
+
headers: schemaAsync
|
|
1259
|
+
},
|
|
1260
|
+
handler (req, reply) { reply.send(req.body) }
|
|
1261
|
+
})
|
|
1262
|
+
|
|
1263
|
+
const testCases = [
|
|
1264
|
+
{
|
|
1265
|
+
params: 200,
|
|
1266
|
+
body: 200,
|
|
1267
|
+
querystring: 200,
|
|
1268
|
+
headers: 400,
|
|
1269
|
+
response: 400
|
|
1270
|
+
},
|
|
1271
|
+
{
|
|
1272
|
+
params: 200,
|
|
1273
|
+
body: 400,
|
|
1274
|
+
querystring: 200,
|
|
1275
|
+
headers: 200,
|
|
1276
|
+
response: 400
|
|
1277
|
+
},
|
|
1278
|
+
{
|
|
1279
|
+
params: 200,
|
|
1280
|
+
body: 200,
|
|
1281
|
+
querystring: 400,
|
|
1282
|
+
headers: 200,
|
|
1283
|
+
response: 400
|
|
1284
|
+
},
|
|
1285
|
+
{
|
|
1286
|
+
params: 200,
|
|
1287
|
+
body: 200,
|
|
1288
|
+
querystring: 200,
|
|
1289
|
+
headers: 400,
|
|
1290
|
+
response: 400
|
|
1291
|
+
}
|
|
1292
|
+
]
|
|
1293
|
+
t.plan(testCases.length * 4)
|
|
1294
|
+
testCases.forEach(validate)
|
|
1295
|
+
|
|
1296
|
+
function validate ({
|
|
1297
|
+
url,
|
|
1298
|
+
params,
|
|
1299
|
+
body,
|
|
1300
|
+
querystring,
|
|
1301
|
+
headers,
|
|
1302
|
+
response
|
|
1303
|
+
}) {
|
|
1304
|
+
fastify.inject({
|
|
1305
|
+
method: 'POST',
|
|
1306
|
+
url: `/${params}`,
|
|
1307
|
+
headers: { id: headers },
|
|
1308
|
+
query: { id: querystring },
|
|
1309
|
+
payload: { id: body }
|
|
1310
|
+
}, (err, res) => {
|
|
1311
|
+
t.error(err)
|
|
1312
|
+
t.equal(res.statusCode, response)
|
|
1313
|
+
})
|
|
1314
|
+
}
|
|
1315
|
+
})
|
|
@@ -198,7 +198,10 @@ expectAssignable<FastifyInstance>(fastify({
|
|
|
198
198
|
}))
|
|
199
199
|
expectAssignable<FastifyInstance>(fastify({ frameworkErrors: () => { } }))
|
|
200
200
|
expectAssignable<FastifyInstance>(fastify({
|
|
201
|
-
rewriteUrl: (req)
|
|
201
|
+
rewriteUrl: function (req) {
|
|
202
|
+
this.log.debug('rewrite url')
|
|
203
|
+
return req.url === '/hi' ? '/hello' : req.url!
|
|
204
|
+
}
|
|
202
205
|
}))
|
|
203
206
|
expectAssignable<FastifyInstance>(fastify({
|
|
204
207
|
schemaErrorFormatter: (errors, dataVar) => {
|
|
@@ -31,6 +31,7 @@ expectType<AddressInfo[]>(server.addresses())
|
|
|
31
31
|
expectType<unknown>(server.getSchema('SchemaId'))
|
|
32
32
|
expectType<string>(server.printRoutes())
|
|
33
33
|
expectType<string>(server.printPlugins())
|
|
34
|
+
expectType<string>(server.listeningOrigin)
|
|
34
35
|
|
|
35
36
|
expectAssignable<FastifyInstance>(
|
|
36
37
|
server.setErrorHandler(function (error, request, reply) {
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import { expectDeprecated, expectError, expectType } from 'tsd'
|
|
1
|
+
import { expectAssignable, expectDeprecated, expectError, expectNotAssignable, expectType } from 'tsd'
|
|
2
2
|
import fastify, {
|
|
3
3
|
FastifyLogFn,
|
|
4
4
|
LogLevel,
|
|
5
5
|
FastifyLoggerInstance,
|
|
6
6
|
FastifyRequest,
|
|
7
7
|
FastifyReply,
|
|
8
|
-
FastifyBaseLogger
|
|
8
|
+
FastifyBaseLogger,
|
|
9
|
+
FastifyInstance
|
|
9
10
|
} from '../../fastify'
|
|
10
11
|
import { Server, IncomingMessage, ServerResponse } from 'http'
|
|
11
12
|
import * as fs from 'fs'
|
|
12
13
|
import P from 'pino'
|
|
14
|
+
import { ResSerializerReply } from '../../types/logger'
|
|
13
15
|
|
|
14
16
|
expectType<FastifyLoggerInstance>(fastify().log)
|
|
15
17
|
|
|
@@ -127,7 +129,9 @@ const serverAutoInferredSerializerResponseObjectOption = fastify({
|
|
|
127
129
|
logger: {
|
|
128
130
|
serializers: {
|
|
129
131
|
res (ServerResponse) {
|
|
130
|
-
expectType<FastifyReply
|
|
132
|
+
expectType<ResSerializerReply<Server, FastifyReply>>(ServerResponse)
|
|
133
|
+
expectAssignable<Partial<FastifyReply> & Pick<FastifyReply, 'statusCode'>>(ServerResponse)
|
|
134
|
+
expectNotAssignable<FastifyReply>(ServerResponse)
|
|
131
135
|
return {
|
|
132
136
|
status: '200'
|
|
133
137
|
}
|
|
@@ -154,7 +158,9 @@ const serverAutoInferredSerializerObjectOption = fastify({
|
|
|
154
158
|
}
|
|
155
159
|
},
|
|
156
160
|
res (ServerResponse) {
|
|
157
|
-
expectType<FastifyReply
|
|
161
|
+
expectType<ResSerializerReply<Server, FastifyReply>>(ServerResponse)
|
|
162
|
+
expectAssignable<Partial<FastifyReply> & Pick<FastifyReply, 'statusCode'>>(ServerResponse)
|
|
163
|
+
expectNotAssignable<FastifyReply>(ServerResponse)
|
|
158
164
|
return {
|
|
159
165
|
statusCode: 'statusCode'
|
|
160
166
|
}
|
|
@@ -23,7 +23,7 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
|
23
23
|
expectType<(values: {[key: string]: any}) => FastifyReply>(reply.headers)
|
|
24
24
|
expectType<(key: string) => number | string | string[] | undefined>(reply.getHeader)
|
|
25
25
|
expectType<() => { [key: string]: number | string | string[] | undefined }>(reply.getHeaders)
|
|
26
|
-
expectType<(key: string) =>
|
|
26
|
+
expectType<(key: string) => FastifyReply>(reply.removeHeader)
|
|
27
27
|
expectType<(key: string) => boolean>(reply.hasHeader)
|
|
28
28
|
expectType<{(statusCode: number, url: string): FastifyReply; (url: string): FastifyReply }>(reply.redirect)
|
|
29
29
|
expectType<() => FastifyReply>(reply.hijack)
|
|
@@ -33,6 +33,9 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
|
33
33
|
expectType<(fn: (payload: any) => string) => FastifyReply>(reply.serializer)
|
|
34
34
|
expectType<(payload: any) => string | ArrayBuffer | Buffer>(reply.serialize)
|
|
35
35
|
expectType<(fulfilled: () => void, rejected: (err: Error) => void) => void>(reply.then)
|
|
36
|
+
expectType<(key: string, fn: ((reply: FastifyReply, payload: string | Buffer | null) => Promise<string>) | ((reply: FastifyReply, payload: string | Buffer | null, done: (err: Error | null, value?: string) => void) => void)) => FastifyReply>(reply.trailer)
|
|
37
|
+
expectType<(key: string) => boolean>(reply.hasTrailer)
|
|
38
|
+
expectType<(key: string) => FastifyReply>(reply.removeTrailer)
|
|
36
39
|
expectType<FastifyInstance>(reply.server)
|
|
37
40
|
expectAssignable<((httpStatus: string) => DefaultSerializationFunction)>(reply.getSerializationFunction)
|
|
38
41
|
expectAssignable<((schema: {[key: string]: unknown}) => DefaultSerializationFunction)>(reply.getSerializationFunction)
|
|
@@ -342,3 +342,15 @@ expectType<boolean>(fastify().hasRoute({
|
|
|
342
342
|
method: 'GET',
|
|
343
343
|
constraints: { version: '1.2.0' }
|
|
344
344
|
}))
|
|
345
|
+
|
|
346
|
+
expectType<FastifyInstance>(fastify().route({
|
|
347
|
+
url: '/',
|
|
348
|
+
method: 'get',
|
|
349
|
+
handler: routeHandlerWithReturnValue
|
|
350
|
+
}))
|
|
351
|
+
|
|
352
|
+
expectType<FastifyInstance>(fastify().route({
|
|
353
|
+
url: '/',
|
|
354
|
+
method: ['put', 'patch'],
|
|
355
|
+
handler: routeHandlerWithReturnValue
|
|
356
|
+
}))
|
|
@@ -10,6 +10,7 @@ test('Should rewrite url', t => {
|
|
|
10
10
|
const fastify = Fastify({
|
|
11
11
|
rewriteUrl (req) {
|
|
12
12
|
t.equal(req.url, '/this-would-404-without-url-rewrite')
|
|
13
|
+
this.log.info('rewriting url')
|
|
13
14
|
return '/'
|
|
14
15
|
}
|
|
15
16
|
})
|
|
@@ -43,6 +44,7 @@ test('Should not rewrite if the url is the same', t => {
|
|
|
43
44
|
const fastify = Fastify({
|
|
44
45
|
rewriteUrl (req) {
|
|
45
46
|
t.equal(req.url, '/this-would-404-without-url-rewrite')
|
|
47
|
+
this.log.info('rewriting url')
|
|
46
48
|
return req.url
|
|
47
49
|
}
|
|
48
50
|
})
|
|
@@ -74,6 +76,7 @@ test('Should throw an error', t => {
|
|
|
74
76
|
const fastify = Fastify({
|
|
75
77
|
rewriteUrl (req) {
|
|
76
78
|
t.equal(req.url, '/this-would-404-without-url-rewrite')
|
|
79
|
+
this.log.info('rewriting url')
|
|
77
80
|
return undefined
|
|
78
81
|
}
|
|
79
82
|
})
|
package/types/.eslintrc.json
CHANGED
package/types/instance.d.ts
CHANGED
|
@@ -99,7 +99,7 @@ export interface FastifyInstance<
|
|
|
99
99
|
prefix: string;
|
|
100
100
|
version: string;
|
|
101
101
|
log: Logger;
|
|
102
|
-
|
|
102
|
+
listeningOrigin: string;
|
|
103
103
|
addresses(): AddressInfo[]
|
|
104
104
|
withTypeProvider<Provider extends FastifyTypeProvider>(): FastifyInstance<RawServer, RawRequest, RawReply, Logger, Provider>;
|
|
105
105
|
|