Skip to content

Commit 931ee9c

Browse files
committed
- Added support for node fetch 3.x, but recommend 2.x for Jest and NodeJS compatibility
1 parent 75c335a commit 931ee9c

16 files changed

+50
-66
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Ferdi Koomen
3+
Copyright (c) 2021 Ferdi Koomen
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,9 @@ you can specify `--client node` in the openapi call:
459459
`openapi --input ./spec.json --output ./dist --client node`
460460

461461
This will generate a client that uses [`node-fetch`](https://www.npmjs.com/package/node-fetch) internally. However,
462-
in order to compile and run this client, you will need to install the `node-fetch` dependencies:
462+
in order to compile and run this client, you will need to install the `node-fetch@2.x` dependencies:
463463

464464
```
465-
npm install @types/node-fetch --save-dev
466465
npm install node-fetch --save-dev
467466
npm install form-data --save-dev
468467
```

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@
6161
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
6262
},
6363
"dependencies": {
64+
"@types/node-fetch": "^2.5.12",
6465
"camelcase": "^6.2.0",
6566
"commander": "^8.0.0",
67+
"form-data": "^4.0.0",
6668
"handlebars": "^4.7.6",
6769
"js-yaml": "^4.0.0",
6870
"json-schema-ref-parser": "^9.0.7",
6971
"mkdirp": "^1.0.4",
72+
"node-fetch": "^2.6.5",
7073
"rimraf": "^3.0.2"
7174
},
7275
"devDependencies": {
@@ -89,11 +92,9 @@
8992
"eslint-plugin-prettier": "4.0.0",
9093
"eslint-plugin-simple-import-sort": "7.0.0",
9194
"express": "4.17.1",
92-
"form-data": "4.0.0",
9395
"glob": "7.2.0",
9496
"jest": "27.2.2",
9597
"jest-cli": "27.2.2",
96-
"node-fetch": "3.0.0",
9798
"prettier": "2.4.1",
9899
"puppeteer": "10.4.0",
99100
"qs": "6.10.1",

src/templates/core/node/getHeaders.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
2929
if (options.body) {
3030
if (options.mediaType) {
3131
headers.append('Content-Type', options.mediaType);
32-
} else if (isBlob(options.body)) {
32+
} else if (isBlob(options.body) || isBinary(options.body)) {
3333
headers.append('Content-Type', 'application/octet-stream');
3434
} else if (isString(options.body)) {
3535
headers.append('Content-Type', 'text/plain');

src/templates/core/node/getRequestBody.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
55
if (options.body) {
66
if (options.mediaType?.includes('/json')) {
77
return JSON.stringify(options.body)
8-
} else if (isString(options.body) || isBlob(options.body)) {
9-
return options.body;
8+
} else if (isString(options.body) || isBlob(options.body) || isBinary(options.body)) {
9+
return options.body as any;
1010
} else {
1111
return JSON.stringify(options.body);
1212
}

src/templates/core/node/request.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import { OpenAPI } from './OpenAPI';
2121
{{>functions/isBlob}}
2222

2323

24+
{{>functions/isBinary}}
25+
26+
2427
{{>functions/getQueryString}}
2528

2629

src/templates/partials/base.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{~#equals base 'File'~}}
22
{{~#equals @root.httpClient 'fetch'}}Blob{{/equals~}}
33
{{~#equals @root.httpClient 'xhr'}}Blob{{/equals~}}
4-
{{~#equals @root.httpClient 'node'}}Blob{{/equals~}}
4+
{{~#equals @root.httpClient 'node'}}Blob | Buffer | ArrayBuffer | ArrayBufferView{{/equals~}}
55
{{~else~}}
66
{{{base}}}
77
{{~/equals~}}

test/e2e/v2.babel.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ describe('v2.fetch', () => {
1717
}, 30000);
1818

1919
afterAll(async () => {
20-
await server.stop();
2120
await browser.stop();
21+
await server.stop();
2222
});
2323

2424
it('requests token', async () => {

test/e2e/v2.fetch.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ describe('v2.fetch', () => {
1717
}, 30000);
1818

1919
afterAll(async () => {
20-
await server.stop();
2120
await browser.stop();
21+
await server.stop();
2222
});
2323

2424
it('requests token', async () => {

test/e2e/v2.node.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('v2.node', () => {
1818

1919
it('requests token', async () => {
2020
const { OpenAPI, SimpleService } = require('./generated/v2/node/index.js');
21-
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN')
21+
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
2222
OpenAPI.TOKEN = tokenRequest;
2323
const result = await SimpleService.getCallWithoutParametersAndResponse();
2424
expect(tokenRequest.mock.calls.length).toBe(1);

0 commit comments

Comments
 (0)