Skip to content

Commit

Permalink
feat: use FormData, File and Blob from fetch implementation (#3139)
Browse files Browse the repository at this point in the history
Refs #3137
  • Loading branch information
char0n authored Sep 13, 2023
1 parent 1a45622 commit f341994
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 41 deletions.
2 changes: 1 addition & 1 deletion config/jest/jest.unit.coverage.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
collectCoverageFrom: ['src/**/*.js'],
coverageThreshold: {
'./src/': {
branches: 85,
branches: 84,
functions: 91,
lines: 89,
statements: 89,
Expand Down
21 changes: 0 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
"cookie": "~0.5.0",
"deepmerge": "~4.3.0",
"fast-json-patch": "^3.0.0-1",
"formdata-node": "^4.0.0",
"is-plain-object": "^5.0.0",
"js-yaml": "^4.1.0",
"node-fetch-commonjs": "^3.3.1",
Expand Down
19 changes: 18 additions & 1 deletion src/helpers/fetch-polyfill.browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { fetch, Headers, Request, Response } from './fetch-ponyfill.browser.js';
import {
fetch,
Headers,
Request,
Response,
FormData,
File,
Blob,
} from './fetch-ponyfill.browser.js';

if (typeof globalThis.fetch === 'undefined') {
globalThis.fetch = fetch;
Expand All @@ -12,3 +20,12 @@ if (typeof globalThis.Request === 'undefined') {
if (typeof globalThis.Response === 'undefined') {
globalThis.Response = Response;
}
if (typeof globalThis.FormData === 'undefined') {
globalThis.FormData = FormData;
}
if (typeof globalThis.File === 'undefined') {
globalThis.File = File;
}
if (typeof globalThis.Blob === 'undefined') {
globalThis.Blob = Blob;
}
15 changes: 15 additions & 0 deletions src/helpers/fetch-polyfill.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import {
Headers as HeaderU,
Request as RequestU,
Response as ResponseU,
FormData as FormDataU,
File as FileU,
Blob as BlobU,
} from './fetch-ponyfill-undici.node.js';
import {
fetch as fetchNF,
Headers as HeadersNF,
Request as RequestNF,
Response as ResponseNF,
FormData as FormDataNF,
File as FileNF,
Blob as BlobNF,
} from './fetch-ponyfill-node-fetch.node.js';

if (typeof globalThis.fetch === 'undefined') {
Expand All @@ -23,3 +29,12 @@ if (typeof globalThis.Request === 'undefined') {
if (typeof globalThis.Response === 'undefined') {
globalThis.Response = ResponseU || ResponseNF;
}
if (typeof globalThis.FormData === 'undefined') {
globalThis.FormData = FormDataU || FormDataNF;
}
if (typeof globalThis.File === 'undefined') {
globalThis.File = FileU || FileNF;
}
if (typeof globalThis.Blob === 'undefined') {
globalThis.Blob = BlobU || BlobNF;
}
4 changes: 2 additions & 2 deletions src/helpers/fetch-ponyfill-node-fetch.node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// we cannot use `node-fetch@3` as it's pure ESM package not compatible with CommonJS
import fetch, { Response, Headers, Request } from 'node-fetch-commonjs';
import fetch, { Response, Headers, Request, FormData, File, Blob } from 'node-fetch-commonjs';

export { fetch, Response, Headers, Request };
export { fetch, Response, Headers, Request, FormData, File, Blob };
7 changes: 5 additions & 2 deletions src/helpers/fetch-ponyfill-undici.node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { fetch, Response, Headers, Request } from 'undici';
import { Blob } from 'buffer';
import { fetch, Response, Headers, Request, FormData, File } from 'undici';

export { fetch, Response, Headers, Request };
const BlobU = typeof fetch === 'undefined' ? undefined : Blob;

export { fetch, Response, Headers, Request, FormData, File, BlobU as Blob };
4 changes: 2 additions & 2 deletions src/helpers/fetch-ponyfill.browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// we're targeting browsers that already support fetch API
const { fetch, Response, Headers, Request } = globalThis;
const { fetch, Response, Headers, Request, FormData, File, Blob } = globalThis;

export { fetch, Response, Headers, Request };
export { fetch, Response, Headers, Request, FormData, File, Blob };
1 change: 0 additions & 1 deletion src/http/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import qs from 'qs';
import jsYaml from 'js-yaml';
import { FormData, File, Blob } from 'formdata-node';

import '../helpers/fetch-polyfill.node.js';
import { encodeDisallowedCharacters } from '../execute/oas3/style-serializer.js';
Expand Down
1 change: 0 additions & 1 deletion test/execute/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FormData } from 'formdata-node';
import AbortController from 'abort-controller';

import { execute, buildRequest, self as stubs } from '../../src/execute/index.js';
Expand Down
15 changes: 11 additions & 4 deletions test/http/http-multipart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import * as undici from 'undici';
import { FormData, File, Blob } from 'formdata-node';
import { fileFromPathSync } from 'formdata-node/lib/cjs/fileFromPath.js';

import { buildRequest } from '../../src/execute/index.js';
import sampleMultipartOpenApi2 from '../data/sample-multipart-oas2.js';
Expand Down Expand Up @@ -263,8 +262,16 @@ describe('buildRequest - openapi 3.0', () => {
* `fileFromPathSync` helper should be used to load files from Node.js env
* that are further used as values for FormData.
*/
const file1 = fileFromPathSync(path.join(__dirname, 'data', 'file1.txt'));
const file2 = fileFromPathSync(path.join(__dirname, 'data', 'file2.txt'));
const file1 = new File(
fs.readFileSync(path.join(__dirname, 'data', 'file1.txt')),
'file1.txt',
{ type: 'text/plain' }
);
const file2 = new File(
fs.readFileSync(path.join(__dirname, 'data', 'file1.txt')),
'file1.txt',
{ type: 'text/plain' }
);

const req = buildRequest({
spec: sampleMultipartOpenApi3,
Expand Down
1 change: 0 additions & 1 deletion test/http/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { File, Blob } from 'formdata-node';
import * as undici from 'undici';

import http, {
Expand Down
13 changes: 12 additions & 1 deletion test/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import path from 'node:path';
import fs from 'node:fs';
import AbortController from 'abort-controller';

import { fetch, Headers, Request, Response } from '../src/helpers/fetch-ponyfill-undici.node.js';
import {
fetch,
Headers,
Request,
Response,
FormData,
File,
Blob,
} from '../src/helpers/fetch-ponyfill-undici.node.js';

// force using undici for testing
globalThis.fetch = fetch;
globalThis.Headers = Headers;
globalThis.Request = Request;
globalThis.Response = Response;
globalThis.FormData = FormData;
globalThis.File = File;
globalThis.Blob = Blob;

// provide AbortController for older Node.js versions
globalThis.AbortController = AbortController;
Expand Down
4 changes: 1 addition & 3 deletions test/oas3/execute/build-request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { File } from 'formdata-node';

// https://github.com/swagger-api/swagger-js/issues/1116
import { buildRequest } from '../../../src/execute/index.js';

Expand Down Expand Up @@ -145,7 +143,7 @@ describe('buildRequest - OAS 3.0.x', () => {
expect(json).toStrictEqual(JSON.stringify(options));
expect(file).toBeInstanceOf(File);
expect(file.valueOf()).toStrictEqual(JSON.stringify(options));
expect(file.type).toStrictEqual('application/json; charset=utf-8');
expect(file.type).toStrictEqual('application/json;charset=utf-8');
});
});

Expand Down

0 comments on commit f341994

Please sign in to comment.