-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Upload.mjs
52 lines (47 loc) · 1.7 KB
/
Upload.mjs
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
// @ts-check
/**
* @import GraphQLUpload from "./GraphQLUpload.mjs"
* @import processRequest, { FileUpload } from "./processRequest.mjs"
*/
/**
* A file expected to be uploaded as it was declared in the `map` field of a
* [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
* The {@linkcode processRequest} function places references to an instance of
* this class wherever the file is expected in the GraphQL operation. The scalar
* {@linkcode GraphQLUpload} derives it’s value from {@linkcode Upload.promise}.
*/
export default class Upload {
constructor() {
/**
* Promise that resolves file upload details. This should only be utilized
* by {@linkcode GraphQLUpload}.
* @type {Promise<FileUpload>}
*/
this.promise = new Promise((resolve, reject) => {
/**
* Resolves the upload promise with the file upload details. This should
* only be utilized by {@linkcode processRequest}.
* @param {FileUpload} file File upload details.
*/
this.resolve = (file) => {
/**
* The file upload details, available when the
* {@linkcode Upload.promise} resolves. This should only be utilized by
* {@linkcode processRequest}.
* @type {FileUpload | undefined}
*/
this.file = file;
resolve(file);
};
/**
* Rejects the upload promise with an error. This should only be
* utilized by {@linkcode processRequest}.
* @param {Error} error Error instance.
*/
this.reject = reject;
});
// Prevent errors crashing Node.js, see:
// https://github.com/nodejs/node/issues/20392
this.promise.catch(() => {});
}
}