Skip to content

Commit 9cbd22f

Browse files
committed
refactor: moving the node fetch client from node-fetch to native
1 parent 244502a commit 9cbd22f

24 files changed

+43
-103
lines changed

.vscode/settings.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
23
"editor.codeActionsOnSave": {
34
"source.fixAll": "explicit"
45
},
5-
"editor.defaultFormatter": "esbenp.prettier-vscode",
6+
"editor.formatOnSave": true,
67

78
// controlled by the .editorconfig at root since we can't map vscode settings directly to files
89
// https://github.com/microsoft/vscode/issues/35350
9-
"files.insertFinalNewline": false
10+
"files.insertFinalNewline": false,
11+
12+
"search.exclude": {
13+
"coverage": true,
14+
}
1015
}

integrations/node.Dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ WORKDIR /src
1313
ADD package.json /src/
1414

1515
# https://www.npmjs.com/package/axios
16-
# https://www.npmjs.com/package/request
17-
# Installing node-fetch@2 because as of 3.0 is't now an ESM-only package.
18-
# https://www.npmjs.com/package/node-fetch
19-
RUN npm install axios request node-fetch@2 && \
16+
RUN npm install axios && \
2017
npm install
2118

2219
ADD . /src

src/helpers/__snapshots__/utils.test.ts.snap

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,11 @@ exports[`availableTargets > returns all available targets 1`] = `
187187
"title": "Axios",
188188
},
189189
{
190-
"description": "Simplified HTTP node-fetch client",
191-
"extname": ".cjs",
192-
"installation": "npm install node-fetch@2 --save",
190+
"description": "Perform asynchronous HTTP requests with the Fetch API",
191+
"extname": ".js",
193192
"key": "fetch",
194-
"link": "https://github.com/bitinn/node-fetch",
195-
"title": "Fetch",
193+
"link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch",
194+
"title": "fetch",
196195
},
197196
],
198197
"default": "fetch",

src/targets/node/fetch/client.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
/**
2-
* @description
3-
* HTTP code snippet generator for Node.js using node-fetch.
4-
*
5-
* @author
6-
* @hirenoble
7-
*
8-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9-
*/
101
import type { Client } from '../../index.js';
112

123
import stringifyObject from 'stringify-object';
@@ -17,11 +8,10 @@ import { getHeaderName } from '../../../helpers/headers.js';
178
export const fetch: Client = {
189
info: {
1910
key: 'fetch',
20-
title: 'Fetch',
21-
link: 'https://github.com/bitinn/node-fetch',
22-
description: 'Simplified HTTP node-fetch client',
23-
extname: '.cjs',
24-
installation: 'npm install node-fetch@2 --save',
11+
title: 'fetch',
12+
link: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch',
13+
description: 'Perform asynchronous HTTP requests with the Fetch API',
14+
extname: '.js',
2515
},
2616
convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => {
2717
const opts = {
@@ -32,7 +22,6 @@ export const fetch: Client = {
3222
let includeFS = false;
3323
const { blank, push, join, unshift } = new CodeBuilder({ indent: opts.indent });
3424

35-
push("const fetch = require('node-fetch');");
3625
const url = fullUrl;
3726
const reqOpts: Record<string, any> = {
3827
method,
@@ -44,15 +33,14 @@ export const fetch: Client = {
4433

4534
switch (postData.mimeType) {
4635
case 'application/x-www-form-urlencoded':
47-
unshift("const { URLSearchParams } = require('url');");
4836
push('const encodedParams = new URLSearchParams();');
49-
blank();
5037

5138
postData.params?.forEach(param => {
5239
push(`encodedParams.set('${param.name}', '${param.value}');`);
5340
});
5441

5542
reqOpts.body = 'encodedParams';
43+
blank();
5644
break;
5745

5846
case 'application/json':
@@ -68,16 +56,14 @@ export const fetch: Client = {
6856
break;
6957
}
7058

71-
// The `form-data` module automatically adds a `Content-Type` header for `multipart/form-data` content and if we add our own here data won't be correctly transmitted.
59+
// The FormData API automatically adds a `Content-Type` header for `multipart/form-data` content and if we add our own here data won't be correctly transmitted.
7260
// eslint-disable-next-line no-case-declarations -- We're only using `contentTypeHeader` within this block.
7361
const contentTypeHeader = getHeaderName(headersObj, 'content-type');
7462
if (contentTypeHeader) {
7563
delete headersObj[contentTypeHeader];
7664
}
7765

78-
unshift("const FormData = require('form-data');");
7966
push('const formData = new FormData();');
80-
blank();
8167

8268
postData.params.forEach(param => {
8369
if (!param.fileName && !param.fileName && !param.contentType) {
@@ -87,9 +73,12 @@ export const fetch: Client = {
8773

8874
if (param.fileName) {
8975
includeFS = true;
90-
push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
76+
push(`formData.append('${param.name}', await fs.openAsBlob('${param.fileName}'));`);
9177
}
9278
});
79+
80+
reqOpts.body = 'formData';
81+
blank();
9382
break;
9483

9584
default:
@@ -110,7 +99,7 @@ export const fetch: Client = {
11099
reqOpts.headers.cookie = cookiesString;
111100
}
112101
}
113-
blank();
102+
114103
push(`const url = '${url}';`);
115104

116105
// If we ultimately don't have any headers to send then we shouldn't add an empty object into the request options.
@@ -137,19 +126,16 @@ export const fetch: Client = {
137126
blank();
138127

139128
if (includeFS) {
140-
unshift("const fs = require('fs');");
141-
}
142-
if (postData.params && postData.mimeType === 'multipart/form-data') {
143-
push('options.body = formData;');
144-
blank();
129+
unshift("import fs from 'fs';\n");
145130
}
131+
146132
push('fetch(url, options)');
147133
push('.then(res => res.json())', 1);
148134
push('.then(json => console.log(json))', 1);
149135
push(".catch(err => console.error('error:' + err));", 1);
150136

151137
return join()
152138
.replace(/'encodedParams'/, 'encodedParams')
153-
.replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
139+
.replace(/'formData'/, 'formData');
154140
},
155141
};

src/targets/node/fetch/fixtures/application-form-encoded.cjs renamed to src/targets/node/fetch/fixtures/application-form-encoded.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
const { URLSearchParams } = require('url');
2-
const fetch = require('node-fetch');
31
const encodedParams = new URLSearchParams();
4-
52
encodedParams.set('foo', 'bar');
63
encodedParams.set('hello', 'world');
74

src/targets/node/fetch/fixtures/application-json.cjs renamed to src/targets/node/fetch/fixtures/application-json.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {
53
method: 'POST',

src/targets/node/fetch/fixtures/cookies.cjs renamed to src/targets/node/fetch/fixtures/cookies.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/cookies';
42
const options = {method: 'GET', headers: {cookie: 'foo=bar; bar=baz'}};
53

src/targets/node/fetch/fixtures/custom-method.cjs renamed to src/targets/node/fetch/fixtures/custom-method.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {method: 'PROPFIND'};
53

src/targets/node/fetch/fixtures/full.cjs renamed to src/targets/node/fetch/fixtures/full.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
const { URLSearchParams } = require('url');
2-
const fetch = require('node-fetch');
31
const encodedParams = new URLSearchParams();
4-
52
encodedParams.set('foo', 'bar');
63

74
const url = 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value';

src/targets/node/fetch/fixtures/headers.cjs renamed to src/targets/node/fetch/fixtures/headers.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/headers';
42
const options = {
53
method: 'GET',

src/targets/node/fetch/fixtures/http-insecure.cjs renamed to src/targets/node/fetch/fixtures/http-insecure.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'http://httpbin.org/anything';
42
const options = {method: 'GET'};
53

src/targets/node/fetch/fixtures/jsonObj-multiline.cjs renamed to src/targets/node/fetch/fixtures/jsonObj-multiline.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {
53
method: 'POST',

src/targets/node/fetch/fixtures/jsonObj-null-value.cjs renamed to src/targets/node/fetch/fixtures/jsonObj-null-value.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {
53
method: 'POST',

src/targets/node/fetch/fixtures/multipart-data.cjs renamed to src/targets/node/fetch/fixtures/multipart-data.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
const fs = require('fs');
2-
const FormData = require('form-data');
3-
const fetch = require('node-fetch');
4-
const formData = new FormData();
1+
import fs from 'fs';
52

6-
formData.append('foo', fs.createReadStream('src/fixtures/files/hello.txt'));
3+
const formData = new FormData();
4+
formData.append('foo', await fs.openAsBlob('src/fixtures/files/hello.txt'));
75
formData.append('bar', 'Bonjour le monde');
86

97
const url = 'https://httpbin.org/anything';
10-
const options = {method: 'POST'};
11-
12-
options.body = formData;
8+
const options = {method: 'POST', body: formData};
139

1410
fetch(url, options)
1511
.then(res => res.json())

src/targets/node/fetch/fixtures/multipart-file.cjs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'fs';
2+
3+
const formData = new FormData();
4+
formData.append('foo', await fs.openAsBlob('src/fixtures/files/hello.txt'));
5+
6+
const url = 'https://httpbin.org/anything';
7+
const options = {method: 'POST', body: formData};
8+
9+
fetch(url, options)
10+
.then(res => res.json())
11+
.then(json => console.log(json))
12+
.catch(err => console.error('error:' + err));

src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs renamed to src/targets/node/fetch/fixtures/multipart-form-data-no-params.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {method: 'POST', headers: {'Content-Type': 'multipart/form-data'}};
53

src/targets/node/fetch/fixtures/multipart-form-data.cjs renamed to src/targets/node/fetch/fixtures/multipart-form-data.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
const FormData = require('form-data');
2-
const fetch = require('node-fetch');
31
const formData = new FormData();
4-
52
formData.append('foo', 'bar');
63

74
const url = 'https://httpbin.org/anything';
8-
const options = {method: 'POST'};
9-
10-
options.body = formData;
5+
const options = {method: 'POST', body: formData};
116

127
fetch(url, options)
138
.then(res => res.json())

src/targets/node/fetch/fixtures/nested.cjs renamed to src/targets/node/fetch/fixtures/nested.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value';
42
const options = {method: 'GET'};
53

src/targets/node/fetch/fixtures/postdata-malformed.cjs renamed to src/targets/node/fetch/fixtures/postdata-malformed.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {method: 'POST', headers: {'content-type': 'application/json'}};
53

src/targets/node/fetch/fixtures/query-encoded.cjs renamed to src/targets/node/fetch/fixtures/query-encoded.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00';
42
const options = {method: 'GET'};
53

src/targets/node/fetch/fixtures/query.cjs renamed to src/targets/node/fetch/fixtures/query.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value';
42
const options = {method: 'GET'};
53

src/targets/node/fetch/fixtures/short.cjs renamed to src/targets/node/fetch/fixtures/short.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {method: 'GET'};
53

src/targets/node/fetch/fixtures/text-plain.cjs renamed to src/targets/node/fetch/fixtures/text-plain.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
const url = 'https://httpbin.org/anything';
42
const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'};
53

0 commit comments

Comments
 (0)