graphql-upload 8.1.0 → 12.0.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/changelog.md CHANGED
@@ -1,5 +1,123 @@
1
1
  # graphql-upload changelog
2
2
 
3
+ ## 12.0.0
4
+
5
+ ### Major
6
+
7
+ - Updated Node.js support to `^12.20 || >= 14.13`.
8
+ - Updated dev dependencies, some of which require newer Node.js versions than previously supported.
9
+ - The tests are now ESM in `.mjs` files instead of CJS in `.js` files.
10
+ - Replaced the the `package.json` `exports` field public [subpath folder mapping](https://nodejs.org/api/packages.html#packages_subpath_folder_mappings) (deprecated by Node.js) with a [subpath pattern](https://nodejs.org/api/packages.html#packages_subpath_patterns). Deep `require` paths must now include the `.js` file extension.
11
+
12
+ ### Minor
13
+
14
+ - Added a package `sideEffects` field.
15
+
16
+ ### Patch
17
+
18
+ - Updated dependencies.
19
+ - Lint fixes for the updated Prettier version.
20
+ - Updated GitHub Actions CI config:
21
+ - Updated the tested Node.js versions to v12, v14, v16.
22
+ - Updated `actions/checkout` to v2.
23
+ - Updated `actions/setup-node` to v2.
24
+ - Simplify config with the [`npm install-test`](https://docs.npmjs.com/cli/v7/commands/npm-install-test) command.
25
+ - Don’t specify the `CI` environment variable as it’s set by default.
26
+ - Removed `npm-debug.log` from the `.gitignore` file as npm [v4.2.0](https://github.com/npm/npm/releases/tag/v4.2.0)+ doesn’t create it in the current working directory.
27
+ - Updated the EditorConfig URL.
28
+ - Updated the package `keywords` field.
29
+ - More specific package `main` field path.
30
+ - Simplified JSDoc related package scripts now that [`jsdoc-md`](https://npm.im/jsdoc-md) v10 automatically generates a Prettier formatted readme.
31
+ - Added a package `test:jsdoc` script that checks the readme API docs are up to date with the source JSDoc.
32
+ - Refactored private constants from exports of a single module to individual modules.
33
+ - Fixed test name and comment typos.
34
+ - Updated external URLs in docs.
35
+ - Prettier format for a JSDoc code example.
36
+ - Updated a `GraphQLUpload` code example to use [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema) instead of [`graphql-tools`](https://npm.im/graphql-tools).
37
+ - Removed from the readme the notice that the package was previously published as [`apollo-upload-server`](https://npm.im/apollo-upload-server).
38
+ - Simplified the readme “Setup” and “Support” sections.
39
+
40
+ ## 11.0.0
41
+
42
+ ### Major
43
+
44
+ - Updated Node.js support to `^10.13.0 || ^12.0.0 || >= 13.7.0`.
45
+ - Added a [package `exports` field](https://nodejs.org/api/esm.html#esm_package_entry_points) with [conditional exports](https://nodejs.org/api/esm.html#esm_conditional_exports) to support native ESM in Node.js and keep internal code private, [whilst avoiding the dual package hazard](https://nodejs.org/api/esm.html#esm_approach_1_use_an_es_module_wrapper). Published files have been reorganized, so previously undocumented deep imports will need to be rewritten according to the newly documented paths.
46
+ - Updated dev dependencies, some of which require newer Node.js versions than previously supported.
47
+
48
+ ### Patch
49
+
50
+ - Updated the [`graphql`](https://npm.im/graphql) peer dependency to `0.13.1 - 15`, fixing [#200](https://github.com/jaydenseric/graphql-upload/issues/200) via [#201](https://github.com/jaydenseric/graphql-upload/pull/201).
51
+ - Updated Prettier related package scripts.
52
+ - Configured Prettier option `semi` to the default, `true`.
53
+ - Ensure GitHub Actions run on pull request.
54
+ - Also run GitHub Actions with Node.js v14.
55
+ - Fixed the `ignoreStream` function tests for Node.js v14 with a new `CountReadableStream` test helper, fixing [#209](https://github.com/jaydenseric/graphql-upload/issues/209).
56
+ - Minor JSDoc wording tweak for consistency.
57
+ - Mention [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) in the readme “Tips” section.
58
+ - Updated MDN Web Docs links.
59
+
60
+ ## 10.0.0
61
+
62
+ ### Major
63
+
64
+ - Updated Node.js support from v8.10+ to v10+, as earlier versions have reached end-of-life.
65
+ - Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v6, which now requires Node.js v10+, via [#179](https://github.com/jaydenseric/graphql-upload/pull/179).
66
+ - Updated dev dependencies, some of which now require Node.js v10+.
67
+ - Replaced the [`tap`](https://npm.im/tap) dev dependency with [`test-director`](https://npm.im/test-director), [`coverage-node`](https://npm.im/coverage-node), and [`hard-rejection`](https://npm.im/hard-rejection) to improve the dev experience and reduce the dev install size by ~75.7 MB. These new dev dependencies require Node.js v10+.
68
+ - Reorganized files. This is only a breaking change for projects using undocumented deep imports.
69
+ - Removed now redundant Node.js version compatibility logic in the `processRequest` function.
70
+ - The `processRequest` function now places references to instances of the now exported and documented `Upload` class in the GraphQL operation for the `GraphQLUpload` scalar to derive its value, and the `GraphQLUpload` scalar now throws a `GraphQLError` when it parses an invalid value, fixing [#175](https://github.com/jaydenseric/graphql-upload/issues/175) via [#181](https://github.com/jaydenseric/graphql-upload/pull/181).
71
+ - The `GraphQLUpload` scalar `parseLiteral` and `serialize` methods now throw `GraphQLError` (instead of `Error`) instances, with tweaked messages.
72
+
73
+ ### Minor
74
+
75
+ - The `createReadStream` function in resolved file uploads now accepts options to configure the encoding and high water mark, fixing [#177](https://github.com/jaydenseric/graphql-upload/issues/177) via [#179](https://github.com/jaydenseric/graphql-upload/pull/179).
76
+
77
+ ### Patch
78
+
79
+ - Removed the now redundant [`eslint-plugin-import-order-alphabetical`](https://npm.im/eslint-plugin-import-order-alphabetical) and [`express-async-handler`](https://npm.im/express-async-handler) dev dependencies.
80
+ - Stop using [`husky`](https://npm.im/husky) and [`lint-staged`](https://npm.im/lint-staged).
81
+ - Use [`isobject`](https://npm.im/isobject) for checking if values are enumerable, non-array objects.
82
+ - Tests have been massively reorganized, refactored, and improved.
83
+ - Test the `GraphQLUpload` scalar.
84
+ - Test the `ignoreStream` function.
85
+ - Moved the `Upload` class to its own file.
86
+ - Added JSDoc for the `Upload` class instance property `file`.
87
+ - Test the `Upload` class.
88
+ - Improved JSDoc `FileUpload` typedef description.
89
+ - Removed now redundant `eslint-disable-next-line` comments.
90
+ - Use strict mode for scripts.
91
+
92
+ ## 9.0.0
93
+
94
+ ### Major
95
+
96
+ - Updated Node.js support from v8.5+ to v8.10+, to match what the [`eslint`](https://npm.im/eslint) dev dependency now supports. This is unlikely to be a breaking change for the published package.
97
+ - Removed the `Upload` scalar promise resolved `stream` property that has been deprecated since v7, along with associated tests.
98
+ - ESM is no longer published, due to CJS/ESM compatibility issues across recent Node.js versions, via [#169](https://github.com/jaydenseric/graphql-upload/pull/169).
99
+ - The file structure and non-index file exports have changed. This should only affect projects using undocumented deep imports.
100
+
101
+ ### Minor
102
+
103
+ - Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v4 to support Node.js v13, making required changes to the source and tests, via [#166](https://github.com/jaydenseric/graphql-upload/pull/166).
104
+ - JSDoc comments are now included in the published code.
105
+ - Several anonymous functions have been named, for better error stack traces.
106
+ - Setup [GitHub Sponsors funding](https://github.com/sponsors/jaydenseric):
107
+ - Added `.github/funding.yml` to display a sponsor button in GitHub.
108
+ - Added a `package.json` `funding` field to enable npm CLI funding features.
109
+
110
+ ### Patch
111
+
112
+ - Updated dev dependencies.
113
+ - Removed the `.nycrc.json` file:
114
+ - [`tap`](https://npm.im/tap) now ignores test files by default.
115
+ - The `lib/test-helpers` directory is now ignored using [`tap`](https://npm.im/tap) CLI arguments due to [tapjs/node-tap#612](https://github.com/tapjs/node-tap/issues/612).
116
+ - Removed the `esm` and `mjs` package tags; they will be added back once native ESM is properly supported.
117
+ - Updated JSDoc code examples to use CJS instead of ESM, as native ESM is not yet properly supported.
118
+ - No longer test [`fs-capacitor`](https://npm.im/fs-capacitor) implementation details such as temp file creation and cleanup.
119
+ - Commented the reasons for several `istanbul ignore next` comments.
120
+
3
121
  ## 8.1.0
4
122
 
5
123
  ### Minor
@@ -245,7 +363,7 @@ Big thanks to new collaborator [@mike-marcacci](https://github.com/mike-marcacci
245
363
  - Updated dependencies.
246
364
  - Simplified npm scripts.
247
365
  - Readme updates:
248
- - Documented [`Blob`](https://developer.mozilla.org/en/docs/Web/API/Blob) types, via [#39](https://github.com/jaydenseric/apollo-upload-server/pull/39).
366
+ - Documented [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) types, via [#39](https://github.com/jaydenseric/apollo-upload-server/pull/39).
249
367
  - Explained how to use `processRequest` for custom middleware.
250
368
  - Improved usage instructions.
251
369
  - Display oldest supported Node.js version.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-upload",
3
- "version": "8.1.0",
3
+ "version": "12.0.0",
4
4
  "description": "Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -11,12 +11,15 @@
11
11
  "repository": "github:jaydenseric/graphql-upload",
12
12
  "homepage": "https://github.com/jaydenseric/graphql-upload#readme",
13
13
  "bugs": "https://github.com/jaydenseric/graphql-upload/issues",
14
+ "funding": "https://github.com/sponsors/jaydenseric",
14
15
  "keywords": [
15
16
  "graphql",
16
17
  "upload",
17
18
  "file",
18
19
  "multipart",
20
+ "request",
19
21
  "server",
22
+ "middleware",
20
23
  "koa",
21
24
  "express",
22
25
  "apollo",
@@ -24,60 +27,59 @@
24
27
  "mjs"
25
28
  ],
26
29
  "files": [
27
- "lib",
28
- "!*.test.*",
29
- "!test-helpers"
30
+ "private",
31
+ "public"
30
32
  ],
31
- "main": "lib",
33
+ "sideEffects": false,
34
+ "main": "./public/index.js",
35
+ "exports": {
36
+ ".": {
37
+ "import": "./public/index.mjs",
38
+ "require": "./public/index.js"
39
+ },
40
+ "./public/*": "./public/*",
41
+ "./package": "./package.json",
42
+ "./package.json": "./package.json"
43
+ },
32
44
  "engines": {
33
- "node": ">=8.5"
45
+ "node": "^12.20 || >= 14.13"
34
46
  },
35
- "browserslist": "node >= 8.5",
36
47
  "peerDependencies": {
37
- "graphql": "0.13.1 - 14"
48
+ "graphql": "0.13.1 - 15"
38
49
  },
39
50
  "dependencies": {
40
51
  "busboy": "^0.3.1",
41
- "fs-capacitor": "^2.0.4",
42
- "http-errors": "^1.7.3",
43
- "object-path": "^0.11.4"
52
+ "fs-capacitor": "^6.2.0",
53
+ "http-errors": "^1.8.0",
54
+ "isobject": "^4.0.0",
55
+ "object-path": "^0.11.5"
44
56
  },
45
57
  "devDependencies": {
46
- "@babel/cli": "^7.6.3",
47
- "@babel/core": "^7.6.3",
48
- "@babel/preset-env": "^7.6.3",
49
- "babel-eslint": "^10.0.3",
50
- "eslint": "^6.5.1",
51
- "eslint-config-env": "^9.1.0",
52
- "eslint-config-prettier": "^6.4.0",
53
- "eslint-plugin-import": "^2.18.2",
54
- "eslint-plugin-import-order-alphabetical": "^1.0.0",
55
- "eslint-plugin-jsdoc": "^15.9.10",
56
- "eslint-plugin-node": "^10.0.0",
57
- "eslint-plugin-prettier": "^3.1.1",
58
+ "coverage-node": "^5.0.1",
59
+ "eslint": "^7.25.0",
60
+ "eslint-config-env": "^19.0.2",
61
+ "eslint-config-prettier": "^8.3.0",
62
+ "eslint-plugin-import": "^2.22.1",
63
+ "eslint-plugin-jsdoc": "^33.1.0",
64
+ "eslint-plugin-node": "^11.1.0",
65
+ "eslint-plugin-prettier": "^3.4.0",
58
66
  "express": "^4.17.1",
59
- "express-async-handler": "^1.1.4",
60
- "form-data": "^2.5.1",
61
- "graphql": "^14.5.8",
62
- "husky": "^3.0.8",
63
- "jsdoc-md": "^4.0.1",
64
- "koa": "^2.8.2",
65
- "lint-staged": "^9.4.2",
66
- "node-fetch": "^2.6.0",
67
- "prettier": "^1.18.2",
68
- "tap": "^14.6.9"
67
+ "form-data": "^4.0.0",
68
+ "graphql": "^15.5.0",
69
+ "hard-rejection": "^2.1.0",
70
+ "jsdoc-md": "^10.1.0",
71
+ "koa": "^2.13.1",
72
+ "node-fetch": "^2.6.1",
73
+ "prettier": "^2.2.1",
74
+ "test-director": "^6.0.0"
69
75
  },
70
76
  "scripts": {
71
- "prepare": "npm run prepare:clean && npm run prepare:mjs && npm run prepare:js && npm run prepare:jsdoc && npm run prepare:prettier",
72
- "prepare:clean": "rm -rf lib",
73
- "prepare:mjs": "BABEL_ESM=1 babel src -d lib --keep-file-extension",
74
- "prepare:js": "babel src -d lib",
75
- "prepare:jsdoc": "jsdoc-md",
76
- "prepare:prettier": "prettier 'lib/**/*.{mjs,js}' readme.md --write",
77
- "test": "npm run test:eslint && npm run test:prettier && npm run test:tap",
78
- "test:eslint": "eslint . --ext mjs,js",
79
- "test:prettier": "prettier '**/*.{json,yml,md}' -l",
80
- "test:tap": "tap --test-ignore=src --100",
77
+ "jsdoc": "jsdoc-md",
78
+ "test": "npm run test:eslint && npm run test:prettier && npm run test:jsdoc && npm run test:api",
79
+ "test:eslint": "eslint .",
80
+ "test:prettier": "prettier -c .",
81
+ "test:jsdoc": "jsdoc-md -c",
82
+ "test:api": "coverage-node -r hard-rejection/register test/index.mjs",
81
83
  "prepublishOnly": "npm test"
82
84
  }
83
85
  }
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
5
+ * URL. Useful for error messages, etc.
6
+ * @kind constant
7
+ * @name GRAPHQL_MULTIPART_REQUEST_SPEC_URL
8
+ * @type {string}
9
+ * @ignore
10
+ */
11
+ module.exports =
12
+ 'https://github.com/jaydenseric/graphql-multipart-request-spec';
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Safely ignores a Node.js readable stream.
5
+ * @kind function
6
+ * @name ignoreStream
7
+ * @param {ReadableStream} stream Node.js readable stream.
8
+ * @ignore
9
+ */
10
+ module.exports = function ignoreStream(stream) {
11
+ // Prevent an unhandled error from crashing the process.
12
+ stream.on('error', () => {});
13
+
14
+ // Waste the stream.
15
+ stream.resume();
16
+ };
@@ -0,0 +1,90 @@
1
+ // One of the JSDoc code examples contains `*/` escaped using `*\/`, see:
2
+ // https://github.com/gajus/eslint-plugin-jsdoc/issues/710
3
+ /* eslint-disable jsdoc/check-examples */
4
+
5
+ 'use strict';
6
+
7
+ const { GraphQLScalarType, GraphQLError } = require('graphql');
8
+ const Upload = require('./Upload');
9
+
10
+ /**
11
+ * A GraphQL `Upload` scalar that can be used in a
12
+ * [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema).
13
+ * It’s value in resolvers is a promise that resolves
14
+ * [file upload details]{@link FileUpload} for processing and storage.
15
+ * @kind class
16
+ * @name GraphQLUpload
17
+ * @example <caption>Ways to `import`.</caption>
18
+ * ```js
19
+ * import { GraphQLUpload } from 'graphql-upload';
20
+ * ```
21
+ *
22
+ * ```js
23
+ * import GraphQLUpload from 'graphql-upload/public/GraphQLUpload.js';
24
+ * ```
25
+ * @example <caption>Ways to `require`.</caption>
26
+ * ```js
27
+ * const { GraphQLUpload } = require('graphql-upload');
28
+ * ```
29
+ *
30
+ * ```js
31
+ * const GraphQLUpload = require('graphql-upload/public/GraphQLUpload.js');
32
+ * ```
33
+ * @example <caption>A schema built using [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema#makeexecutableschema) from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema).</caption>
34
+ * ```js
35
+ * const { makeExecutableSchema } = require('@graphql-tools/schema');
36
+ * const { GraphQLUpload } = require('graphql-upload');
37
+ *
38
+ * const schema = makeExecutableSchema({
39
+ * typeDefs: /* GraphQL *\/ `
40
+ * scalar Upload
41
+ * `,
42
+ * resolvers: {
43
+ * Upload: GraphQLUpload,
44
+ * },
45
+ * });
46
+ * ```
47
+ * @example <caption>A manually constructed schema with an image upload mutation.</caption>
48
+ * ```js
49
+ * const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require('graphql');
50
+ * const { GraphQLUpload } = require('graphql-upload');
51
+ *
52
+ * const schema = new GraphQLSchema({
53
+ * mutation: new GraphQLObjectType({
54
+ * name: 'Mutation',
55
+ * fields: {
56
+ * uploadImage: {
57
+ * description: 'Uploads an image.',
58
+ * type: GraphQLBoolean,
59
+ * args: {
60
+ * image: {
61
+ * description: 'Image file.',
62
+ * type: GraphQLUpload,
63
+ * },
64
+ * },
65
+ * async resolve(parent, { image }) {
66
+ * const { filename, mimetype, createReadStream } = await image;
67
+ * const stream = createReadStream();
68
+ * // Promisify the stream and store the file, then…
69
+ * return true;
70
+ * },
71
+ * },
72
+ * },
73
+ * }),
74
+ * });
75
+ * ```
76
+ */
77
+ module.exports = new GraphQLScalarType({
78
+ name: 'Upload',
79
+ description: 'The `Upload` scalar type represents a file upload.',
80
+ parseValue(value) {
81
+ if (value instanceof Upload) return value.promise;
82
+ throw new GraphQLError('Upload value invalid.');
83
+ },
84
+ parseLiteral(ast) {
85
+ throw new GraphQLError('Upload literal unsupported.', ast);
86
+ },
87
+ serialize() {
88
+ throw new GraphQLError('Upload serialization unsupported.');
89
+ },
90
+ });
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * A file expected to be uploaded as it has been declared in the `map` field of
5
+ * a [GraphQL multipart request](https://github.com/jaydenseric/graphql-multipart-request-spec).
6
+ * The [`processRequest`]{@link processRequest} function places references to an
7
+ * instance of this class wherever the file is expected in the
8
+ * [GraphQL operation]{@link GraphQLOperation}. The
9
+ * [`Upload`]{@link GraphQLUpload} scalar derives it’s value from the
10
+ * [`promise`]{@link Upload#promise} property.
11
+ * @kind class
12
+ * @name Upload
13
+ * @example <caption>Ways to `import`.</caption>
14
+ * ```js
15
+ * import { Upload } from 'graphql-upload';
16
+ * ```
17
+ *
18
+ * ```js
19
+ * import Upload from 'graphql-upload/public/Upload.js';
20
+ * ```
21
+ * @example <caption>Ways to `require`.</caption>
22
+ * ```js
23
+ * const { Upload } = require('graphql-upload');
24
+ * ```
25
+ *
26
+ * ```js
27
+ * const Upload = require('graphql-upload/public/Upload.js');
28
+ * ```
29
+ */
30
+ module.exports = class Upload {
31
+ constructor() {
32
+ /**
33
+ * Promise that resolves file upload details. This should only be utilized
34
+ * by [`GraphQLUpload`]{@link GraphQLUpload}.
35
+ * @kind member
36
+ * @name Upload#promise
37
+ * @type {Promise<FileUpload>}
38
+ */
39
+ this.promise = new Promise((resolve, reject) => {
40
+ /**
41
+ * Resolves the upload promise with the file upload details. This should
42
+ * only be utilized by [`processRequest`]{@link processRequest}.
43
+ * @kind function
44
+ * @name Upload#resolve
45
+ * @param {FileUpload} file File upload details.
46
+ */
47
+ this.resolve = (file) => {
48
+ /**
49
+ * The file upload details, available when the
50
+ * [upload promise]{@link Upload#promise} resolves. This should only be
51
+ * utilized by [`processRequest`]{@link processRequest}.
52
+ * @kind member
53
+ * @name Upload#file
54
+ * @type {undefined|FileUpload}
55
+ */
56
+ this.file = file;
57
+
58
+ resolve(file);
59
+ };
60
+
61
+ /**
62
+ * Rejects the upload promise with an error. This should only be
63
+ * utilized by [`processRequest`]{@link processRequest}.
64
+ * @kind function
65
+ * @name Upload#reject
66
+ * @param {object} error Error instance.
67
+ */
68
+ this.reject = reject;
69
+ });
70
+
71
+ // Prevent errors crashing Node.js, see:
72
+ // https://github.com/nodejs/node/issues/20392
73
+ this.promise.catch(() => {});
74
+ }
75
+ };
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ const defaultProcessRequest = require('./processRequest');
4
+
5
+ /**
6
+ * Creates [Express](https://expressjs.com) middleware that processes
7
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
8
+ * using [`processRequest`]{@link processRequest}, ignoring non-multipart
9
+ * requests. It sets the request body to be
10
+ * [similar to a conventional GraphQL POST request]{@link GraphQLOperation} for
11
+ * following GraphQL middleware to consume.
12
+ * @kind function
13
+ * @name graphqlUploadExpress
14
+ * @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
15
+ * @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
16
+ * @returns {Function} Express middleware.
17
+ * @example <caption>Ways to `import`.</caption>
18
+ * ```js
19
+ * import { graphqlUploadExpress } from 'graphql-upload';
20
+ * ```
21
+ *
22
+ * ```js
23
+ * import graphqlUploadExpress from 'graphql-upload/public/graphqlUploadExpress.js';
24
+ * ```
25
+ * @example <caption>Ways to `require`.</caption>
26
+ * ```js
27
+ * const { graphqlUploadExpress } = require('graphql-upload');
28
+ * ```
29
+ *
30
+ * ```js
31
+ * const graphqlUploadExpress = require('graphql-upload/public/graphqlUploadExpress.js');
32
+ * ```
33
+ * @example <caption>Basic [`express-graphql`](https://npm.im/express-graphql) setup.</caption>
34
+ * ```js
35
+ * const express = require('express');
36
+ * const graphqlHTTP = require('express-graphql');
37
+ * const { graphqlUploadExpress } = require('graphql-upload');
38
+ * const schema = require('./schema');
39
+ *
40
+ * express()
41
+ * .use(
42
+ * '/graphql',
43
+ * graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
44
+ * graphqlHTTP({ schema })
45
+ * )
46
+ * .listen(3000);
47
+ * ```
48
+ */
49
+ module.exports = function graphqlUploadExpress({
50
+ processRequest = defaultProcessRequest,
51
+ ...processRequestOptions
52
+ } = {}) {
53
+ return function graphqlUploadExpressMiddleware(request, response, next) {
54
+ if (!request.is('multipart/form-data')) return next();
55
+
56
+ const finished = new Promise((resolve) => request.on('end', resolve));
57
+ const { send } = response;
58
+
59
+ response.send = (...args) => {
60
+ finished.then(() => {
61
+ response.send = send;
62
+ response.send(...args);
63
+ });
64
+ };
65
+
66
+ processRequest(request, response, processRequestOptions)
67
+ .then((body) => {
68
+ request.body = body;
69
+ next();
70
+ })
71
+ .catch((error) => {
72
+ if (error.status && error.expose) response.status(error.status);
73
+ next(error);
74
+ });
75
+ };
76
+ };
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+
3
+ const defaultProcessRequest = require('./processRequest');
4
+
5
+ /**
6
+ * Creates [Koa](https://koajs.com) middleware that processes
7
+ * [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
8
+ * using [`processRequest`]{@link processRequest}, ignoring non-multipart
9
+ * requests. It sets the request body to be
10
+ * [similar to a conventional GraphQL POST request]{@link GraphQLOperation} for
11
+ * following GraphQL middleware to consume.
12
+ * @kind function
13
+ * @name graphqlUploadKoa
14
+ * @param {ProcessRequestOptions} options Middleware options. Any [`ProcessRequestOptions`]{@link ProcessRequestOptions} can be used.
15
+ * @param {ProcessRequestFunction} [options.processRequest=processRequest] Used to process [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec).
16
+ * @returns {Function} Koa middleware.
17
+ * @example <caption>Ways to `import`.</caption>
18
+ * ```js
19
+ * import { graphqlUploadKoa } from 'graphql-upload';
20
+ * ```
21
+ *
22
+ * ```js
23
+ * import graphqlUploadKoa from 'graphql-upload/public/graphqlUploadKoa.js';
24
+ * ```
25
+ * @example <caption>Ways to `require`.</caption>
26
+ * ```js
27
+ * const { graphqlUploadKoa } = require('graphql-upload');
28
+ * ```
29
+ *
30
+ * ```js
31
+ * const graphqlUploadKoa = require('graphql-upload/public/graphqlUploadKoa.js');
32
+ * ```
33
+ * @example <caption>Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup.</caption>
34
+ * ```js
35
+ * const Koa = require('koa');
36
+ * const bodyParser = require('koa-bodyparser');
37
+ * const { errorHandler, execute } = require('graphql-api-koa');
38
+ * const { graphqlUploadKoa } = require('graphql-upload');
39
+ * const schema = require('./schema');
40
+ *
41
+ * new Koa()
42
+ * .use(errorHandler())
43
+ * .use(bodyParser())
44
+ * .use(graphqlUploadKoa({ maxFileSize: 10000000, maxFiles: 10 }))
45
+ * .use(execute({ schema }))
46
+ * .listen(3000);
47
+ * ```
48
+ */
49
+ module.exports = function graphqlUploadKoa({
50
+ processRequest = defaultProcessRequest,
51
+ ...processRequestOptions
52
+ } = {}) {
53
+ return async function graphqlUploadKoaMiddleware(ctx, next) {
54
+ if (!ctx.request.is('multipart/form-data')) return next();
55
+
56
+ const finished = new Promise((resolve) => ctx.req.on('end', resolve));
57
+
58
+ try {
59
+ ctx.request.body = await processRequest(
60
+ ctx.req,
61
+ ctx.res,
62
+ processRequestOptions
63
+ );
64
+ await next();
65
+ } finally {
66
+ await finished;
67
+ }
68
+ };
69
+ };