-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
index.js
58 lines (50 loc) · 1.71 KB
/
index.js
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
import mkdirp from 'mkdirp'
import formidable from 'formidable'
import objectPath from 'object-path'
export function processRequest (request, {uploadDir} = {}) {
// Ensure provided upload directory exists
if (uploadDir) mkdirp.sync(uploadDir)
const form = formidable.IncomingForm({
// Defaults to the OS temp directory
uploadDir
})
// Parse the multipart request
return new Promise((resolve, reject) => {
form.parse(request, (error, fields, files) => {
if (error) reject(new Error(error))
// Decode the GraphQL variables
fields.variables = JSON.parse(fields.variables)
// File field names contain the original path to
// the File object in the GraphQL input variables.
// Relevent data for each uploaded file now gets
// placed back in the variables.
const variables = objectPath(fields.variables)
Object.keys(files).forEach(variablesPath => {
const {name, type, size, path} = files[variablesPath]
variables.set(variablesPath, {name, type, size, path})
})
// Provide fields for new request body
resolve(fields)
})
})
}
export function apolloUploadExpress (options) {
return (request, response, next) => {
// Skip if there are no uploads
if (!request.is('multipart/form-data')) return next()
// Process the request
processRequest(request, options).then(body => {
request.body = body
next()
})
}
}
export function apolloUploadKoa (options) {
return async function (ctx, next) {
// Skip if there are no uploads
if (!ctx.request.is('multipart/form-data')) return await next()
// Process the request
ctx.request.body = await processRequest(ctx.req, options)
await next()
}
}