Skip to content

Commit 07b8b80

Browse files
committed
Use ESM instead of CJS and update the fs-capacitor dependency.
Fixes #318 . Closes #290 .
1 parent 8914950 commit 07b8b80

16 files changed

+139
-124
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// @ts-check
22

3-
"use strict";
4-
53
/**
64
* [GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec)
75
* URL. Useful for error messages, etc.
86
*/
97
const GRAPHQL_MULTIPART_REQUEST_SPEC_URL =
108
"https://github.com/jaydenseric/graphql-multipart-request-spec";
119

12-
module.exports = GRAPHQL_MULTIPART_REQUEST_SPEC_URL;
10+
export default GRAPHQL_MULTIPART_REQUEST_SPEC_URL;

GraphQLUpload.js renamed to GraphQLUpload.mjs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// @ts-check
22

3-
"use strict";
3+
import { GraphQLError, GraphQLScalarType } from "graphql";
44

5-
const { GraphQLScalarType, GraphQLError } = require("graphql");
6-
const Upload = require("./Upload.js");
5+
import Upload from "./Upload.mjs";
76

8-
/** @typedef {import("./processRequest").FileUpload} FileUpload */
7+
/** @typedef {import("./processRequest.mjs").FileUpload} FileUpload */
98

109
/**
1110
* A GraphQL `Upload` scalar that can be used in a
@@ -18,8 +17,8 @@ const Upload = require("./Upload.js");
1817
* from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema):
1918
*
2019
* ```js
21-
* const { makeExecutableSchema } = require("@graphql-tools/schema");
22-
* const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
20+
* import { makeExecutableSchema } from "@graphql-tools/schema/makeExecutableSchema";
21+
* import GraphQLUpload from "graphql-upload/GraphQLUpload.mjs";
2322
*
2423
* const schema = makeExecutableSchema({
2524
* typeDefs: `
@@ -34,8 +33,8 @@ const Upload = require("./Upload.js");
3433
* A manually constructed schema with an image upload mutation:
3534
*
3635
* ```js
37-
* const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require("graphql");
38-
* const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
36+
* import { GraphQLBoolean, GraphQLObjectType, GraphQLSchema } from "graphql";
37+
* import GraphQLUpload from "graphql-upload/GraphQLUpload.mjs";
3938
*
4039
* const schema = new GraphQLSchema({
4140
* mutation: new GraphQLObjectType({
@@ -77,4 +76,4 @@ const GraphQLUpload = new GraphQLScalarType({
7776
},
7877
});
7978

80-
module.exports = GraphQLUpload;
79+
export default GraphQLUpload;

GraphQLUpload.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import { doesNotThrow, throws } from "assert";
44
import { parseValue } from "graphql";
55

6-
import GraphQLUpload from "./GraphQLUpload.js";
7-
import Upload from "./Upload.js";
6+
import GraphQLUpload from "./GraphQLUpload.mjs";
7+
import Upload from "./Upload.mjs";
88

99
/**
1010
* Adds `GraphQLUpload` tests.

Upload.js renamed to Upload.mjs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
/** @typedef {import("./GraphQLUpload.js")} GraphQLUpload */
6-
/** @typedef {import("./processRequest.js")} processRequest */
3+
/** @typedef {import("./GraphQLUpload.mjs").default} GraphQLUpload */
4+
/** @typedef {import("./processRequest.mjs").default} processRequest */
75

86
/**
97
* A file expected to be uploaded as it was declared in the `map` field of a
@@ -12,26 +10,26 @@
1210
* this class wherever the file is expected in the GraphQL operation. The scalar
1311
* {@linkcode GraphQLUpload} derives it’s value from {@linkcode Upload.promise}.
1412
*/
15-
class Upload {
13+
export default class Upload {
1614
constructor() {
1715
/**
1816
* Promise that resolves file upload details. This should only be utilized
1917
* by {@linkcode GraphQLUpload}.
20-
* @type {Promise<import("./processRequest.js").FileUpload>}
18+
* @type {Promise<import("./processRequest.mjs").FileUpload>}
2119
*/
2220
this.promise = new Promise((resolve, reject) => {
2321
/**
2422
* Resolves the upload promise with the file upload details. This should
2523
* only be utilized by {@linkcode processRequest}.
26-
* @param {import("./processRequest.js").FileUpload} file File upload
24+
* @param {import("./processRequest.mjs").FileUpload} file File upload
2725
* details.
2826
*/
2927
this.resolve = (file) => {
3028
/**
3129
* The file upload details, available when the
3230
* {@linkcode Upload.promise} resolves. This should only be utilized by
3331
* {@linkcode processRequest}.
34-
* @type {import("./processRequest.js").FileUpload | undefined}
32+
* @type {import("./processRequest.mjs").FileUpload | undefined}
3533
*/
3634
this.file = file;
3735

@@ -51,5 +49,3 @@ class Upload {
5149
this.promise.catch(() => {});
5250
}
5351
}
54-
55-
module.exports = Upload;

Upload.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { ok, rejects, strictEqual } from "assert";
44

5-
import Upload from "./Upload.js";
5+
import Upload from "./Upload.mjs";
66

77
/**
88
* Adds `Upload` tests.

changelog.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,41 @@
22

33
## Next
44

5+
### Major
6+
7+
- Updated the [`fs-capacitor`](https://npm.im/fs-capacitor) dependency to v8, fixing [#318](https://github.com/jaydenseric/graphql-upload/issues/318).
8+
- The type `FileUploadCreateReadStreamOptions` from the `processRequest.mjs` module now uses types from [`fs-capacitor`](https://npm.im/fs-capacitor) that are slightly more specific.
9+
- The API is now ESM in `.mjs` files instead of CJS in `.js` files, [accessible via `import` but not `require`](https://nodejs.org/dist/latest/docs/api/esm.html#require). To migrate imports:
10+
11+
```diff
12+
- import GraphQLUpload from "graphql-upload/GraphQLUpload.js";
13+
+ import GraphQLUpload from "graphql-upload/GraphQLUpload.mjs";
14+
```
15+
16+
```diff
17+
- import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.js";
18+
+ import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.mjs";
19+
```
20+
21+
```diff
22+
- import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.js";
23+
+ import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.mjs";
24+
```
25+
26+
```diff
27+
- import processRequest from "graphql-upload/processRequest.js";
28+
+ import processRequest from "graphql-upload/processRequest.mjs";
29+
```
30+
31+
```diff
32+
- import Upload from "graphql-upload/Upload.js";
33+
+ import Upload from "graphql-upload/Upload.mjs";
34+
```
35+
536
### Patch
637

738
- Updated dev dependencies.
39+
- Updated examples in JSDoc comments.
840
- Updated the changelog entry for v14.0.0 to show how to migrate imports.
941

1042
## 15.0.2
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const defaultProcessRequest = require("./processRequest.js");
3+
import defaultProcessRequest from "./processRequest.mjs";
64

75
/**
86
* Creates [Express](https://expressjs.com) middleware that processes incoming
97
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
108
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
119
* the request `body` to be similar to a conventional GraphQL POST request for
1210
* following GraphQL middleware to consume.
13-
* @param {import("./processRequest.js").ProcessRequestOptions & {
14-
* processRequest?: import("./processRequest.js").ProcessRequestFunction
11+
* @param {import("./processRequest.mjs").ProcessRequestOptions & {
12+
* processRequest?: import("./processRequest.mjs").ProcessRequestFunction
1513
* }} options Options.
1614
* @returns Express middleware.
1715
* @example
1816
* Basic [`express-graphql`](https://npm.im/express-graphql) setup:
1917
*
2018
* ```js
21-
* const express = require("express");
22-
* const graphqlHTTP = require("express-graphql");
23-
* const graphqlUploadExpress = require("graphql-upload/graphqlUploadExpress.js");
24-
* const schema = require("./schema.js");
19+
* import express from "express";
20+
* import expressGraphQL from "express-graphql";
21+
* import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.mjs";
22+
*
23+
* import schema from "./schema.mjs";
2524
*
2625
* express()
2726
* .use(
2827
* "/graphql",
2928
* graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
30-
* graphqlHTTP({ schema })
29+
* expressGraphQL.graphqlHTTP({ schema })
3130
* )
3231
* .listen(3000);
3332
* ```
3433
*/
35-
function graphqlUploadExpress({
34+
export default function graphqlUploadExpress({
3635
processRequest = defaultProcessRequest,
3736
...processRequestOptions
3837
} = {}) {
@@ -76,5 +75,3 @@ function graphqlUploadExpress({
7675

7776
return graphqlUploadExpressMiddleware;
7877
}
79-
80-
module.exports = graphqlUploadExpress;

graphqlUploadExpress.test.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { createServer } from "http";
66
import createError from "http-errors";
77
import fetch, { File, FormData } from "node-fetch";
88

9-
import graphqlUploadExpress from "./graphqlUploadExpress.js";
10-
import processRequest from "./processRequest.js";
9+
import graphqlUploadExpress from "./graphqlUploadExpress.mjs";
10+
import processRequest from "./processRequest.mjs";
1111
import listen from "./test/listen.mjs";
1212

1313
/**
@@ -44,7 +44,7 @@ export default (tests) => {
4444
/**
4545
* @type {{
4646
* variables: {
47-
* file: import("./Upload.js"),
47+
* file: import("./Upload.mjs").default,
4848
* },
4949
* } | undefined}
5050
*/
@@ -84,7 +84,7 @@ export default (tests) => {
8484
/**
8585
* @type {{
8686
* variables: {
87-
* file: import("./Upload.js"),
87+
* file: import("./Upload.mjs").default,
8888
* },
8989
* } | undefined}
9090
*/
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
// @ts-check
22

3-
"use strict";
4-
5-
const defaultProcessRequest = require("./processRequest.js");
3+
import defaultProcessRequest from "./processRequest.mjs";
64

75
/**
86
* Creates [Koa](https://koajs.com) middleware that processes incoming
97
* [GraphQL multipart requests](https://github.com/jaydenseric/graphql-multipart-request-spec)
108
* using {@linkcode processRequest}, ignoring non multipart requests. It sets
119
* the request `body` to be similar to a conventional GraphQL POST request for
1210
* following GraphQL middleware to consume.
13-
* @param {import("./processRequest.js").ProcessRequestOptions & {
14-
* processRequest?: import("./processRequest.js").ProcessRequestFunction
11+
* @param {import("./processRequest.mjs").ProcessRequestOptions & {
12+
* processRequest?: import("./processRequest.mjs").ProcessRequestFunction
1513
* }} options Options.
1614
* @returns Koa middleware.
1715
* @example
1816
* Basic [`graphql-api-koa`](https://npm.im/graphql-api-koa) setup:
1917
*
2018
* ```js
21-
* const Koa = require("koa");
22-
* const bodyParser = require("koa-bodyparser");
23-
* const { errorHandler, execute } = require("graphql-api-koa");
24-
* const graphqlUploadKoa = require("graphql-upload/graphqlUploadKoa.js");
25-
* const schema = require("./schema.js");
19+
* import errorHandler from "graphql-api-koa/errorHandler.mjs";
20+
* import execute from "graphql-api-koa/execute.mjs";
21+
* import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.mjs";
22+
* import Koa from "koa";
23+
* import bodyParser from "koa-bodyparser";
24+
*
25+
* import schema from "./schema.mjs";
2626
*
2727
* new Koa()
2828
* .use(errorHandler())
@@ -32,7 +32,7 @@ const defaultProcessRequest = require("./processRequest.js");
3232
* .listen(3000);
3333
* ```
3434
*/
35-
function graphqlUploadKoa({
35+
export default function graphqlUploadKoa({
3636
processRequest = defaultProcessRequest,
3737
...processRequestOptions
3838
} = {}) {
@@ -65,5 +65,3 @@ function graphqlUploadKoa({
6565

6666
return graphqlUploadKoaMiddleware;
6767
}
68-
69-
module.exports = graphqlUploadKoa;

graphqlUploadKoa.test.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { createServer } from "http";
55
import Koa from "koa";
66
import fetch, { File, FormData } from "node-fetch";
77

8-
import graphqlUploadKoa from "./graphqlUploadKoa.js";
9-
import processRequest from "./processRequest.js";
8+
import graphqlUploadKoa from "./graphqlUploadKoa.mjs";
9+
import processRequest from "./processRequest.mjs";
1010
import listen from "./test/listen.mjs";
1111

1212
/**
@@ -40,7 +40,7 @@ export default (tests) => {
4040
/**
4141
* @type {{
4242
* variables: {
43-
* file: import("./Upload.js"),
43+
* file: import("./Upload.mjs").default,
4444
* },
4545
* } | undefined}
4646
*/
@@ -80,7 +80,7 @@ export default (tests) => {
8080
/**
8181
* @type {{
8282
* variables: {
83-
* file: import("./Upload.js"),
83+
* file: import("./Upload.mjs").default,
8484
* },
8585
* } | undefined}
8686
*/

0 commit comments

Comments
 (0)