forked from mercurius-js/mercurius-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
36 lines (29 loc) · 762 Bytes
/
index.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
import fp from 'fastify-plugin'
import { processRequest, UploadOptions } from 'graphql-upload'
import type { FastifyPluginCallback } from 'fastify'
declare module 'fastify' {
interface FastifyRequest {
isMultipart?: true
}
}
const mercuriusGQLUpload: FastifyPluginCallback<UploadOptions> = (
fastify,
options,
done
) => {
fastify.addContentTypeParser('multipart', (req, _payload, done) => {
req.isMultipart = true
done(null)
})
fastify.addHook('preValidation', async function (request, reply) {
if (!request.isMultipart) {
return
}
request.body = await processRequest(request.raw, reply.raw, options)
})
done()
}
export default fp(mercuriusGQLUpload, {
fastify: '>= 3.x',
name: 'mercurius-upload',
})