-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix readme, fix example, lack swagger ui support * feat: add swagger example * fix: update to use the internal fastify Ajv * Apply suggestions from code review Co-authored-by: aj-gameon <113391687+aj-gameon@users.noreply.github.com> Co-authored-by: Fernando Toledo <fernando@ftoledo.com> --------- Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: aj-gameon <113391687+aj-gameon@users.noreply.github.com> Co-authored-by: Fernando Toledo <fernando@ftoledo.com>
- Loading branch information
1 parent
2c41fda
commit 15afc65
Showing
4 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
const ajvFilePlugin = (ajv, options = {}) => { | ||
return ajv.addKeyword({ | ||
keyword: 'isFile', | ||
compile: (_schema, parent, _it) => { | ||
parent.type = 'file' | ||
delete parent.isFile | ||
return () => true | ||
} | ||
}) | ||
} | ||
const fastify = require('fastify')({ | ||
logger: true, | ||
ajv: { | ||
plugins: [ajvFilePlugin] | ||
} | ||
}) | ||
|
||
fastify.register(require('..'), { | ||
attachFieldsToBody: true | ||
}) | ||
fastify.register(require('fastify-swagger')) | ||
fastify.register(require('@fastify/swagger-ui')) | ||
fastify.post( | ||
'/upload/files', | ||
{ | ||
schema: { | ||
consumes: ['multipart/form-data'], | ||
body: { | ||
type: 'object', | ||
required: ['myField'], | ||
properties: { | ||
myField: { isFile: true } | ||
} | ||
} | ||
} | ||
}, | ||
function (req, reply) { | ||
console.log({ body: req.body }) | ||
reply.send('done') | ||
} | ||
) | ||
|
||
fastify.listen({ port: 3000 }, (err) => { | ||
if (err) throw err | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters