Skip to content

Commit

Permalink
refactor: moving the node fetch client from node-fetch to native
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Sep 5, 2024
1 parent 244502a commit 9cbd22f
Show file tree
Hide file tree
Showing 24 changed files with 43 additions and 103 deletions.
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,

// controlled by the .editorconfig at root since we can't map vscode settings directly to files
// https://github.com/microsoft/vscode/issues/35350
"files.insertFinalNewline": false
"files.insertFinalNewline": false,

"search.exclude": {
"coverage": true,
}
}
5 changes: 1 addition & 4 deletions integrations/node.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ WORKDIR /src
ADD package.json /src/

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

ADD . /src
9 changes: 4 additions & 5 deletions src/helpers/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,11 @@ exports[`availableTargets > returns all available targets 1`] = `
"title": "Axios",
},
{
"description": "Simplified HTTP node-fetch client",
"extname": ".cjs",
"installation": "npm install node-fetch@2 --save",
"description": "Perform asynchronous HTTP requests with the Fetch API",
"extname": ".js",
"key": "fetch",
"link": "https://github.com/bitinn/node-fetch",
"title": "Fetch",
"link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch",
"title": "fetch",
},
],
"default": "fetch",
Expand Down
42 changes: 14 additions & 28 deletions src/targets/node/fetch/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/**
* @description
* HTTP code snippet generator for Node.js using node-fetch.
*
* @author
* @hirenoble
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
import type { Client } from '../../index.js';

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

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

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

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

reqOpts.body = 'encodedParams';
blank();
break;

case 'application/json':
Expand All @@ -68,16 +56,14 @@ export const fetch: Client = {
break;
}

// 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.
// 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.
// eslint-disable-next-line no-case-declarations -- We're only using `contentTypeHeader` within this block.
const contentTypeHeader = getHeaderName(headersObj, 'content-type');
if (contentTypeHeader) {
delete headersObj[contentTypeHeader];
}

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

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

if (param.fileName) {
includeFS = true;
push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
push(`formData.append('${param.name}', await fs.openAsBlob('${param.fileName}'));`);
}
});

reqOpts.body = 'formData';
blank();
break;

default:
Expand All @@ -110,7 +99,7 @@ export const fetch: Client = {
reqOpts.headers.cookie = cookiesString;
}
}
blank();

push(`const url = '${url}';`);

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

if (includeFS) {
unshift("const fs = require('fs');");
}
if (postData.params && postData.mimeType === 'multipart/form-data') {
push('options.body = formData;');
blank();
unshift("import fs from 'fs';\n");
}

push('fetch(url, options)');
push('.then(res => res.json())', 1);
push('.then(json => console.log(json))', 1);
push(".catch(err => console.error('error:' + err));", 1);

return join()
.replace(/'encodedParams'/, 'encodedParams')
.replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
.replace(/'formData'/, 'formData');
},
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const { URLSearchParams } = require('url');
const fetch = require('node-fetch');
const encodedParams = new URLSearchParams();

encodedParams.set('foo', 'bar');
encodedParams.set('hello', 'world');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/cookies';
const options = {method: 'GET', headers: {cookie: 'foo=bar; bar=baz'}};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {method: 'PROPFIND'};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const { URLSearchParams } = require('url');
const fetch = require('node-fetch');
const encodedParams = new URLSearchParams();

encodedParams.set('foo', 'bar');

const url = 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/headers';
const options = {
method: 'GET',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'http://httpbin.org/anything';
const options = {method: 'GET'};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
const fs = require('fs');
const FormData = require('form-data');
const fetch = require('node-fetch');
const formData = new FormData();
import fs from 'fs';

formData.append('foo', fs.createReadStream('src/fixtures/files/hello.txt'));
const formData = new FormData();
formData.append('foo', await fs.openAsBlob('src/fixtures/files/hello.txt'));
formData.append('bar', 'Bonjour le monde');

const url = 'https://httpbin.org/anything';
const options = {method: 'POST'};

options.body = formData;
const options = {method: 'POST', body: formData};

fetch(url, options)
.then(res => res.json())
Expand Down
16 changes: 0 additions & 16 deletions src/targets/node/fetch/fixtures/multipart-file.cjs

This file was deleted.

12 changes: 12 additions & 0 deletions src/targets/node/fetch/fixtures/multipart-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs';

const formData = new FormData();
formData.append('foo', await fs.openAsBlob('src/fixtures/files/hello.txt'));

const url = 'https://httpbin.org/anything';
const options = {method: 'POST', body: formData};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {method: 'POST', headers: {'Content-Type': 'multipart/form-data'}};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const FormData = require('form-data');
const fetch = require('node-fetch');
const formData = new FormData();

formData.append('foo', 'bar');

const url = 'https://httpbin.org/anything';
const options = {method: 'POST'};

options.body = formData;
const options = {method: 'POST', body: formData};

fetch(url, options)
.then(res => res.json())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value';
const options = {method: 'GET'};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {method: 'POST', headers: {'content-type': 'application/json'}};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00';
const options = {method: 'GET'};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value';
const options = {method: 'GET'};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {method: 'GET'};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require('node-fetch');

const url = 'https://httpbin.org/anything';
const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'};

Expand Down

0 comments on commit 9cbd22f

Please sign in to comment.