From fea4e186358b3e78411d79ab7857e038386a19c8 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:11:11 -0700 Subject: [PATCH 01/12] refactor: remove xhr target --- src/helpers/__snapshots__/utils.test.ts.snap | 9 +- src/targets/javascript/target.ts | 3 - src/targets/javascript/xhr/client.test.ts | 19 ---- src/targets/javascript/xhr/client.ts | 102 ------------------ .../xhr/fixtures/application-form-encoded.js | 15 --- .../xhr/fixtures/application-json.js | 34 ------ .../javascript/xhr/fixtures/cookies.js | 15 --- src/targets/javascript/xhr/fixtures/cors.js | 13 --- .../javascript/xhr/fixtures/custom-method.js | 14 --- src/targets/javascript/xhr/fixtures/full.js | 17 --- .../javascript/xhr/fixtures/headers.js | 18 ---- .../javascript/xhr/fixtures/http-insecure.js | 14 --- .../xhr/fixtures/jsonObj-multiline.js | 17 --- .../xhr/fixtures/jsonObj-null-value.js | 17 --- .../javascript/xhr/fixtures/multipart-data.js | 16 --- .../javascript/xhr/fixtures/multipart-file.js | 15 --- .../fixtures/multipart-form-data-no-params.js | 13 --- .../xhr/fixtures/multipart-form-data.js | 15 --- src/targets/javascript/xhr/fixtures/nested.js | 14 --- .../xhr/fixtures/postdata-malformed.js | 15 --- .../javascript/xhr/fixtures/query-encoded.js | 14 --- src/targets/javascript/xhr/fixtures/query.js | 14 --- src/targets/javascript/xhr/fixtures/short.js | 14 --- .../javascript/xhr/fixtures/text-plain.js | 15 --- 24 files changed, 1 insertion(+), 451 deletions(-) delete mode 100644 src/targets/javascript/xhr/client.test.ts delete mode 100644 src/targets/javascript/xhr/client.ts delete mode 100644 src/targets/javascript/xhr/fixtures/application-form-encoded.js delete mode 100644 src/targets/javascript/xhr/fixtures/application-json.js delete mode 100644 src/targets/javascript/xhr/fixtures/cookies.js delete mode 100644 src/targets/javascript/xhr/fixtures/cors.js delete mode 100644 src/targets/javascript/xhr/fixtures/custom-method.js delete mode 100644 src/targets/javascript/xhr/fixtures/full.js delete mode 100644 src/targets/javascript/xhr/fixtures/headers.js delete mode 100644 src/targets/javascript/xhr/fixtures/http-insecure.js delete mode 100644 src/targets/javascript/xhr/fixtures/jsonObj-multiline.js delete mode 100644 src/targets/javascript/xhr/fixtures/jsonObj-null-value.js delete mode 100644 src/targets/javascript/xhr/fixtures/multipart-data.js delete mode 100644 src/targets/javascript/xhr/fixtures/multipart-file.js delete mode 100644 src/targets/javascript/xhr/fixtures/multipart-form-data-no-params.js delete mode 100644 src/targets/javascript/xhr/fixtures/multipart-form-data.js delete mode 100644 src/targets/javascript/xhr/fixtures/nested.js delete mode 100644 src/targets/javascript/xhr/fixtures/postdata-malformed.js delete mode 100644 src/targets/javascript/xhr/fixtures/query-encoded.js delete mode 100644 src/targets/javascript/xhr/fixtures/query.js delete mode 100644 src/targets/javascript/xhr/fixtures/short.js delete mode 100644 src/targets/javascript/xhr/fixtures/text-plain.js diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index cd0bf5934..f6530b526 100644 --- a/src/helpers/__snapshots__/utils.test.ts.snap +++ b/src/helpers/__snapshots__/utils.test.ts.snap @@ -120,13 +120,6 @@ exports[`availableTargets > returns all available targets 1`] = ` }, { "clients": [ - { - "description": "W3C Standard API that provides scripted client functionality", - "extname": ".js", - "key": "xhr", - "link": "https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest", - "title": "XMLHttpRequest", - }, { "description": "Promise based HTTP client for the browser and node.js", "extname": ".js", @@ -150,7 +143,7 @@ exports[`availableTargets > returns all available targets 1`] = ` "title": "jQuery", }, ], - "default": "xhr", + "default": "fetch", "key": "javascript", "title": "JavaScript", }, diff --git a/src/targets/javascript/target.ts b/src/targets/javascript/target.ts index 116c26c4e..5f24434ed 100644 --- a/src/targets/javascript/target.ts +++ b/src/targets/javascript/target.ts @@ -3,17 +3,14 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; import { fetch } from './fetch/client.js'; import { jquery } from './jquery/client.js'; -import { xhr } from './xhr/client.js'; export const javascript: Target = { info: { key: 'javascript', title: 'JavaScript', - default: 'xhr', }, clientsById: { - xhr, axios, fetch, jquery, diff --git a/src/targets/javascript/xhr/client.test.ts b/src/targets/javascript/xhr/client.test.ts deleted file mode 100644 index bd66a9ea1..000000000 --- a/src/targets/javascript/xhr/client.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { Request } from '../../../index.js'; - -import request from '../../../fixtures/requests/short.cjs'; -import { runCustomFixtures } from '../../../fixtures/runCustomFixtures.js'; - -runCustomFixtures({ - targetId: 'javascript', - clientId: 'xhr', - tests: [ - { - it: 'should not use cors', - input: request.log.entries[0].request as Request, - options: { - cors: false, - }, - expected: 'cors.js', - }, - ], -}); diff --git a/src/targets/javascript/xhr/client.ts b/src/targets/javascript/xhr/client.ts deleted file mode 100644 index 33a2d1815..000000000 --- a/src/targets/javascript/xhr/client.ts +++ /dev/null @@ -1,102 +0,0 @@ -/** - * @description - * HTTP code snippet generator for native XMLHttpRequest - * - * @author - * @AhmadNassri - * - * 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'; - -import { CodeBuilder } from '../../../helpers/code-builder.js'; -import { escapeForSingleQuotes } from '../../../helpers/escape.js'; -import { getHeader, getHeaderName, hasHeader } from '../../../helpers/headers.js'; - -export interface XhrOptions { - cors?: boolean; -} - -export const xhr: Client = { - info: { - key: 'xhr', - title: 'XMLHttpRequest', - link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest', - description: 'W3C Standard API that provides scripted client functionality', - extname: '.js', - }, - convert: ({ postData, allHeaders, method, fullUrl }, options) => { - const opts = { - indent: ' ', - cors: true, - ...options, - }; - - const { blank, push, join } = new CodeBuilder({ indent: opts.indent }); - - switch (postData.mimeType) { - case 'application/json': - push( - `const data = JSON.stringify(${stringifyObject(postData.jsonObj, { - indent: opts.indent, - })});`, - ); - blank(); - break; - - case 'multipart/form-data': - if (!postData.params) { - break; - } - - push('const data = new FormData();'); - - postData.params.forEach(param => { - push(`data.append('${param.name}', '${param.value || param.fileName || ''}');`); - }); - - // remove the contentType header - if (hasHeader(allHeaders, 'content-type')) { - if (getHeader(allHeaders, 'content-type')?.includes('boundary')) { - const headerName = getHeaderName(allHeaders, 'content-type'); - if (headerName) { - delete allHeaders[headerName]; - } - } - } - - blank(); - break; - - default: - push(`const data = ${postData.text ? `'${postData.text}'` : 'null'};`); - blank(); - } - - push('const xhr = new XMLHttpRequest();'); - - if (opts.cors) { - push('xhr.withCredentials = true;'); - } - - blank(); - push("xhr.addEventListener('readystatechange', function () {"); - push('if (this.readyState === this.DONE) {', 1); - push('console.log(this.responseText);', 2); - push('}', 1); - push('});'); - blank(); - push(`xhr.open('${method}', '${fullUrl}');`); - - Object.keys(allHeaders).forEach(key => { - push(`xhr.setRequestHeader('${key}', '${escapeForSingleQuotes(allHeaders[key])}');`); - }); - - blank(); - push('xhr.send(data);'); - - return join(); - }, -}; diff --git a/src/targets/javascript/xhr/fixtures/application-form-encoded.js b/src/targets/javascript/xhr/fixtures/application-form-encoded.js deleted file mode 100644 index e745ca388..000000000 --- a/src/targets/javascript/xhr/fixtures/application-form-encoded.js +++ /dev/null @@ -1,15 +0,0 @@ -const data = 'foo=bar&hello=world'; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/application-json.js b/src/targets/javascript/xhr/fixtures/application-json.js deleted file mode 100644 index 05109f1f6..000000000 --- a/src/targets/javascript/xhr/fixtures/application-json.js +++ /dev/null @@ -1,34 +0,0 @@ -const data = JSON.stringify({ - number: 1, - string: 'f"oo', - arr: [ - 1, - 2, - 3 - ], - nested: { - a: 'b' - }, - arr_mix: [ - 1, - 'a', - { - arr_mix_nested: [] - } - ], - boolean: false -}); - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('content-type', 'application/json'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/cookies.js b/src/targets/javascript/xhr/fixtures/cookies.js deleted file mode 100644 index 303865891..000000000 --- a/src/targets/javascript/xhr/fixtures/cookies.js +++ /dev/null @@ -1,15 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/cookies'); -xhr.setRequestHeader('cookie', 'foo=bar; bar=baz'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/cors.js b/src/targets/javascript/xhr/fixtures/cors.js deleted file mode 100644 index 7be5b75a7..000000000 --- a/src/targets/javascript/xhr/fixtures/cors.js +++ /dev/null @@ -1,13 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/custom-method.js b/src/targets/javascript/xhr/fixtures/custom-method.js deleted file mode 100644 index b7249c740..000000000 --- a/src/targets/javascript/xhr/fixtures/custom-method.js +++ /dev/null @@ -1,14 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('PROPFIND', 'https://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/full.js b/src/targets/javascript/xhr/fixtures/full.js deleted file mode 100644 index e3190633e..000000000 --- a/src/targets/javascript/xhr/fixtures/full.js +++ /dev/null @@ -1,17 +0,0 @@ -const data = 'foo=bar'; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value'); -xhr.setRequestHeader('cookie', 'foo=bar; bar=baz'); -xhr.setRequestHeader('accept', 'application/json'); -xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/headers.js b/src/targets/javascript/xhr/fixtures/headers.js deleted file mode 100644 index 919deb13a..000000000 --- a/src/targets/javascript/xhr/fixtures/headers.js +++ /dev/null @@ -1,18 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/headers'); -xhr.setRequestHeader('accept', 'application/json'); -xhr.setRequestHeader('x-foo', 'Bar'); -xhr.setRequestHeader('x-bar', 'Foo'); -xhr.setRequestHeader('quoted-value', '"quoted" \'string\''); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/http-insecure.js b/src/targets/javascript/xhr/fixtures/http-insecure.js deleted file mode 100644 index 006f993bf..000000000 --- a/src/targets/javascript/xhr/fixtures/http-insecure.js +++ /dev/null @@ -1,14 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'http://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/jsonObj-multiline.js b/src/targets/javascript/xhr/fixtures/jsonObj-multiline.js deleted file mode 100644 index db126cbe6..000000000 --- a/src/targets/javascript/xhr/fixtures/jsonObj-multiline.js +++ /dev/null @@ -1,17 +0,0 @@ -const data = JSON.stringify({ - foo: 'bar' -}); - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('content-type', 'application/json'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/jsonObj-null-value.js b/src/targets/javascript/xhr/fixtures/jsonObj-null-value.js deleted file mode 100644 index 92befdc34..000000000 --- a/src/targets/javascript/xhr/fixtures/jsonObj-null-value.js +++ /dev/null @@ -1,17 +0,0 @@ -const data = JSON.stringify({ - foo: null -}); - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('content-type', 'application/json'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/multipart-data.js b/src/targets/javascript/xhr/fixtures/multipart-data.js deleted file mode 100644 index 0dabcfeaf..000000000 --- a/src/targets/javascript/xhr/fixtures/multipart-data.js +++ /dev/null @@ -1,16 +0,0 @@ -const data = new FormData(); -data.append('foo', 'Hello World'); -data.append('bar', 'Bonjour le monde'); - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/multipart-file.js b/src/targets/javascript/xhr/fixtures/multipart-file.js deleted file mode 100644 index aaca03d25..000000000 --- a/src/targets/javascript/xhr/fixtures/multipart-file.js +++ /dev/null @@ -1,15 +0,0 @@ -const data = new FormData(); -data.append('foo', 'src/fixtures/files/hello.txt'); - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/multipart-form-data-no-params.js b/src/targets/javascript/xhr/fixtures/multipart-form-data-no-params.js deleted file mode 100644 index 96179647d..000000000 --- a/src/targets/javascript/xhr/fixtures/multipart-form-data-no-params.js +++ /dev/null @@ -1,13 +0,0 @@ -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('Content-Type', 'multipart/form-data'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/multipart-form-data.js b/src/targets/javascript/xhr/fixtures/multipart-form-data.js deleted file mode 100644 index 2c4d372e3..000000000 --- a/src/targets/javascript/xhr/fixtures/multipart-form-data.js +++ /dev/null @@ -1,15 +0,0 @@ -const data = new FormData(); -data.append('foo', 'bar'); - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/nested.js b/src/targets/javascript/xhr/fixtures/nested.js deleted file mode 100644 index 37d2cdee4..000000000 --- a/src/targets/javascript/xhr/fixtures/nested.js +++ /dev/null @@ -1,14 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/postdata-malformed.js b/src/targets/javascript/xhr/fixtures/postdata-malformed.js deleted file mode 100644 index 51ea31877..000000000 --- a/src/targets/javascript/xhr/fixtures/postdata-malformed.js +++ /dev/null @@ -1,15 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('content-type', 'application/json'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/query-encoded.js b/src/targets/javascript/xhr/fixtures/query-encoded.js deleted file mode 100644 index 59626c98d..000000000 --- a/src/targets/javascript/xhr/fixtures/query-encoded.js +++ /dev/null @@ -1,14 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/query.js b/src/targets/javascript/xhr/fixtures/query.js deleted file mode 100644 index 5cc3bb5ce..000000000 --- a/src/targets/javascript/xhr/fixtures/query.js +++ /dev/null @@ -1,14 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/short.js b/src/targets/javascript/xhr/fixtures/short.js deleted file mode 100644 index d7184f2f4..000000000 --- a/src/targets/javascript/xhr/fixtures/short.js +++ /dev/null @@ -1,14 +0,0 @@ -const data = null; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('GET', 'https://httpbin.org/anything'); - -xhr.send(data); \ No newline at end of file diff --git a/src/targets/javascript/xhr/fixtures/text-plain.js b/src/targets/javascript/xhr/fixtures/text-plain.js deleted file mode 100644 index 6665974bc..000000000 --- a/src/targets/javascript/xhr/fixtures/text-plain.js +++ /dev/null @@ -1,15 +0,0 @@ -const data = 'Hello World'; - -const xhr = new XMLHttpRequest(); -xhr.withCredentials = true; - -xhr.addEventListener('readystatechange', function () { - if (this.readyState === this.DONE) { - console.log(this.responseText); - } -}); - -xhr.open('POST', 'https://httpbin.org/anything'); -xhr.setRequestHeader('content-type', 'text/plain'); - -xhr.send(data); \ No newline at end of file From 38752c5e7a44f37e68ef42248e312e32bffe1d58 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:12:07 -0700 Subject: [PATCH 02/12] refactor: remove node `request` target --- src/targets/node/request/client.ts | 132 ------------------ .../fixtures/application-form-encoded.cjs | 14 -- .../request/fixtures/application-json.cjs | 22 --- src/targets/node/request/fixtures/cookies.cjs | 13 -- .../node/request/fixtures/custom-method.cjs | 9 -- src/targets/node/request/fixtures/full.cjs | 22 --- src/targets/node/request/fixtures/headers.cjs | 18 --- .../node/request/fixtures/http-insecure.cjs | 9 -- .../request/fixtures/jsonObj-multiline.cjs | 15 -- .../request/fixtures/jsonObj-null-value.cjs | 15 -- .../node/request/fixtures/multipart-data.cjs | 21 --- .../node/request/fixtures/multipart-file.cjs | 20 --- .../multipart-form-data-no-params.cjs | 13 -- .../request/fixtures/multipart-form-data.cjs | 14 -- src/targets/node/request/fixtures/nested.cjs | 12 -- .../request/fixtures/postdata-malformed.cjs | 13 -- .../node/request/fixtures/query-encoded.cjs | 12 -- src/targets/node/request/fixtures/query.cjs | 12 -- src/targets/node/request/fixtures/short.cjs | 9 -- .../node/request/fixtures/text-plain.cjs | 14 -- src/targets/node/target.ts | 2 - 21 files changed, 411 deletions(-) delete mode 100644 src/targets/node/request/client.ts delete mode 100644 src/targets/node/request/fixtures/application-form-encoded.cjs delete mode 100644 src/targets/node/request/fixtures/application-json.cjs delete mode 100644 src/targets/node/request/fixtures/cookies.cjs delete mode 100644 src/targets/node/request/fixtures/custom-method.cjs delete mode 100644 src/targets/node/request/fixtures/full.cjs delete mode 100644 src/targets/node/request/fixtures/headers.cjs delete mode 100644 src/targets/node/request/fixtures/http-insecure.cjs delete mode 100644 src/targets/node/request/fixtures/jsonObj-multiline.cjs delete mode 100644 src/targets/node/request/fixtures/jsonObj-null-value.cjs delete mode 100644 src/targets/node/request/fixtures/multipart-data.cjs delete mode 100644 src/targets/node/request/fixtures/multipart-file.cjs delete mode 100644 src/targets/node/request/fixtures/multipart-form-data-no-params.cjs delete mode 100644 src/targets/node/request/fixtures/multipart-form-data.cjs delete mode 100644 src/targets/node/request/fixtures/nested.cjs delete mode 100644 src/targets/node/request/fixtures/postdata-malformed.cjs delete mode 100644 src/targets/node/request/fixtures/query-encoded.cjs delete mode 100644 src/targets/node/request/fixtures/query.cjs delete mode 100644 src/targets/node/request/fixtures/short.cjs delete mode 100644 src/targets/node/request/fixtures/text-plain.cjs diff --git a/src/targets/node/request/client.ts b/src/targets/node/request/client.ts deleted file mode 100644 index 284d13c0b..000000000 --- a/src/targets/node/request/client.ts +++ /dev/null @@ -1,132 +0,0 @@ -/** - * @description - * HTTP code snippet generator for Node.js using Request. - * - * @author - * @AhmadNassri - * - * 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'; - -import { CodeBuilder } from '../../../helpers/code-builder.js'; - -export const request: Client = { - info: { - key: 'request', - title: 'Request', - link: 'https://github.com/request/request', - description: 'Simplified HTTP request client', - extname: '.cjs', - installation: 'npm install request --save', - }, - convert: ({ method, url, fullUrl, postData, headersObj, cookies }, options) => { - const opts = { - indent: ' ', - ...options, - }; - - let includeFS = false; - const { push, blank, join, unshift, addPostProcessor } = new CodeBuilder({ indent: opts.indent }); - - push("const request = require('request');"); - blank(); - - const reqOpts: Record = { - method, - url: fullUrl, - }; - - if (Object.keys(headersObj).length) { - reqOpts.headers = headersObj; - } - - switch (postData.mimeType) { - case 'application/x-www-form-urlencoded': - reqOpts.form = postData.paramsObj; - break; - - case 'application/json': - if (postData.jsonObj) { - reqOpts.body = postData.jsonObj; - reqOpts.json = true; - } - break; - - case 'multipart/form-data': - if (!postData.params) { - break; - } - - reqOpts.formData = {}; - - postData.params.forEach(param => { - if (!param.fileName && !param.fileName && !param.contentType) { - reqOpts.formData[param.name] = param.value; - return; - } - - let attachment: { - options?: { - contentType: string | null; - filename: string; - }; - value?: string; - } = {}; - - if (param.fileName) { - includeFS = true; - attachment = { - value: `fs.createReadStream(${param.fileName})`, - options: { - filename: param.fileName, - contentType: param.contentType ? param.contentType : null, - }, - }; - } else if (param.value) { - attachment.value = param.value; - } - - reqOpts.formData[param.name] = attachment; - }); - - addPostProcessor(code => code.replace(/'fs\.createReadStream\((.*)\)'/, "fs.createReadStream('$1')")); - break; - - default: - if (postData.text) { - reqOpts.body = postData.text; - } - } - - // construct cookies argument - if (cookies.length) { - reqOpts.jar = 'JAR'; - - push('const jar = request.jar();'); - - cookies.forEach(({ name, value }) => { - push(`jar.setCookie(request.cookie('${encodeURIComponent(name)}=${encodeURIComponent(value)}'), '${url}');`); - }); - blank(); - addPostProcessor(code => code.replace(/'JAR'/, 'jar')); - } - - if (includeFS) { - unshift("const fs = require('fs');"); - } - - push(`const options = ${stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })};`); - blank(); - - push('request(options, function (error, response, body) {'); - push('if (error) throw new Error(error);', 1); - blank(); - push('console.log(body);', 1); - push('});'); - - return join(); - }, -}; diff --git a/src/targets/node/request/fixtures/application-form-encoded.cjs b/src/targets/node/request/fixtures/application-form-encoded.cjs deleted file mode 100644 index f49d8b71d..000000000 --- a/src/targets/node/request/fixtures/application-form-encoded.cjs +++ /dev/null @@ -1,14 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'application/x-www-form-urlencoded'}, - form: {foo: 'bar', hello: 'world'} -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/application-json.cjs b/src/targets/node/request/fixtures/application-json.cjs deleted file mode 100644 index b7413c0ad..000000000 --- a/src/targets/node/request/fixtures/application-json.cjs +++ /dev/null @@ -1,22 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'application/json'}, - body: { - number: 1, - string: 'f"oo', - arr: [1, 2, 3], - nested: {a: 'b'}, - arr_mix: [1, 'a', {arr_mix_nested: []}], - boolean: false - }, - json: true -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/cookies.cjs b/src/targets/node/request/fixtures/cookies.cjs deleted file mode 100644 index 66e4f4ed2..000000000 --- a/src/targets/node/request/fixtures/cookies.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const request = require('request'); - -const jar = request.jar(); -jar.setCookie(request.cookie('foo=bar'), 'https://httpbin.org/cookies'); -jar.setCookie(request.cookie('bar=baz'), 'https://httpbin.org/cookies'); - -const options = {method: 'GET', url: 'https://httpbin.org/cookies', jar: jar}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/custom-method.cjs b/src/targets/node/request/fixtures/custom-method.cjs deleted file mode 100644 index c716db3e7..000000000 --- a/src/targets/node/request/fixtures/custom-method.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const request = require('request'); - -const options = {method: 'PROPFIND', url: 'https://httpbin.org/anything'}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/full.cjs b/src/targets/node/request/fixtures/full.cjs deleted file mode 100644 index 52e977901..000000000 --- a/src/targets/node/request/fixtures/full.cjs +++ /dev/null @@ -1,22 +0,0 @@ -const request = require('request'); - -const jar = request.jar(); -jar.setCookie(request.cookie('foo=bar'), 'https://httpbin.org/anything'); -jar.setCookie(request.cookie('bar=baz'), 'https://httpbin.org/anything'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', - headers: { - accept: 'application/json', - 'content-type': 'application/x-www-form-urlencoded' - }, - form: {foo: 'bar'}, - jar: jar -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/headers.cjs b/src/targets/node/request/fixtures/headers.cjs deleted file mode 100644 index 88ade811b..000000000 --- a/src/targets/node/request/fixtures/headers.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const request = require('request'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/headers', - headers: { - accept: 'application/json', - 'x-foo': 'Bar', - 'x-bar': 'Foo', - 'quoted-value': '"quoted" \'string\'' - } -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/http-insecure.cjs b/src/targets/node/request/fixtures/http-insecure.cjs deleted file mode 100644 index 8c04d4368..000000000 --- a/src/targets/node/request/fixtures/http-insecure.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const request = require('request'); - -const options = {method: 'GET', url: 'http://httpbin.org/anything'}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/jsonObj-multiline.cjs b/src/targets/node/request/fixtures/jsonObj-multiline.cjs deleted file mode 100644 index 240bad18d..000000000 --- a/src/targets/node/request/fixtures/jsonObj-multiline.cjs +++ /dev/null @@ -1,15 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'application/json'}, - body: {foo: 'bar'}, - json: true -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/jsonObj-null-value.cjs b/src/targets/node/request/fixtures/jsonObj-null-value.cjs deleted file mode 100644 index 395c0c5ba..000000000 --- a/src/targets/node/request/fixtures/jsonObj-null-value.cjs +++ /dev/null @@ -1,15 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'application/json'}, - body: {foo: null}, - json: true -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/multipart-data.cjs b/src/targets/node/request/fixtures/multipart-data.cjs deleted file mode 100644 index 52642fe8e..000000000 --- a/src/targets/node/request/fixtures/multipart-data.cjs +++ /dev/null @@ -1,21 +0,0 @@ -const fs = require('fs'); -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}, - formData: { - foo: { - value: fs.createReadStream('src/fixtures/files/hello.txt'), - options: {filename: 'src/fixtures/files/hello.txt', contentType: 'text/plain'} - }, - bar: 'Bonjour le monde' - } -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/multipart-file.cjs b/src/targets/node/request/fixtures/multipart-file.cjs deleted file mode 100644 index 6c04d1118..000000000 --- a/src/targets/node/request/fixtures/multipart-file.cjs +++ /dev/null @@ -1,20 +0,0 @@ -const fs = require('fs'); -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}, - formData: { - foo: { - value: fs.createReadStream('src/fixtures/files/hello.txt'), - options: {filename: 'src/fixtures/files/hello.txt', contentType: 'text/plain'} - } - } -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/multipart-form-data-no-params.cjs b/src/targets/node/request/fixtures/multipart-form-data-no-params.cjs deleted file mode 100644 index a2b3599f7..000000000 --- a/src/targets/node/request/fixtures/multipart-form-data-no-params.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'Content-Type': 'multipart/form-data'} -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/multipart-form-data.cjs b/src/targets/node/request/fixtures/multipart-form-data.cjs deleted file mode 100644 index dae439e67..000000000 --- a/src/targets/node/request/fixtures/multipart-form-data.cjs +++ /dev/null @@ -1,14 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}, - formData: {foo: 'bar'} -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/nested.cjs b/src/targets/node/request/fixtures/nested.cjs deleted file mode 100644 index 3bff0a48d..000000000 --- a/src/targets/node/request/fixtures/nested.cjs +++ /dev/null @@ -1,12 +0,0 @@ -const request = require('request'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value' -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/postdata-malformed.cjs b/src/targets/node/request/fixtures/postdata-malformed.cjs deleted file mode 100644 index 46ff13dc4..000000000 --- a/src/targets/node/request/fixtures/postdata-malformed.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'application/json'} -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/query-encoded.cjs b/src/targets/node/request/fixtures/query-encoded.cjs deleted file mode 100644 index 5f3be8438..000000000 --- a/src/targets/node/request/fixtures/query-encoded.cjs +++ /dev/null @@ -1,12 +0,0 @@ -const request = require('request'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00' -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/query.cjs b/src/targets/node/request/fixtures/query.cjs deleted file mode 100644 index 7f1cb5721..000000000 --- a/src/targets/node/request/fixtures/query.cjs +++ /dev/null @@ -1,12 +0,0 @@ -const request = require('request'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value' -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/short.cjs b/src/targets/node/request/fixtures/short.cjs deleted file mode 100644 index f02e48ca0..000000000 --- a/src/targets/node/request/fixtures/short.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const request = require('request'); - -const options = {method: 'GET', url: 'https://httpbin.org/anything'}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/request/fixtures/text-plain.cjs b/src/targets/node/request/fixtures/text-plain.cjs deleted file mode 100644 index 6f52592a9..000000000 --- a/src/targets/node/request/fixtures/text-plain.cjs +++ /dev/null @@ -1,14 +0,0 @@ -const request = require('request'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'text/plain'}, - body: 'Hello World' -}; - -request(options, function (error, response, body) { - if (error) throw new Error(error); - - console.log(body); -}); \ No newline at end of file diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index 1def2a580..77ad49171 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -3,7 +3,6 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; import { fetch } from './fetch/client.js'; import { native } from './native/client.js'; -import { request } from './request/client.js'; import { unirest } from './unirest/client.js'; export const node: Target = { @@ -15,7 +14,6 @@ export const node: Target = { }, clientsById: { native, - request, unirest, axios, fetch, From f8d1227eec84ef385a263dab5e390604d3cdd307 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:12:54 -0700 Subject: [PATCH 03/12] refactor: remove node `node-fetch` target --- src/targets/node/fetch/client.ts | 155 ------------------ .../fixtures/application-form-encoded.cjs | 18 -- .../node/fetch/fixtures/application-json.cjs | 20 --- src/targets/node/fetch/fixtures/cookies.cjs | 9 - .../node/fetch/fixtures/custom-method.cjs | 9 - src/targets/node/fetch/fixtures/full.cjs | 21 --- src/targets/node/fetch/fixtures/headers.cjs | 17 -- .../node/fetch/fixtures/http-insecure.cjs | 9 - .../node/fetch/fixtures/jsonObj-multiline.cjs | 13 -- .../fetch/fixtures/jsonObj-null-value.cjs | 13 -- .../node/fetch/fixtures/multipart-data.cjs | 17 -- .../node/fetch/fixtures/multipart-file.cjs | 16 -- .../multipart-form-data-no-params.cjs | 9 - .../fetch/fixtures/multipart-form-data.cjs | 15 -- src/targets/node/fetch/fixtures/nested.cjs | 9 - .../fetch/fixtures/postdata-malformed.cjs | 9 - .../node/fetch/fixtures/query-encoded.cjs | 9 - src/targets/node/fetch/fixtures/query.cjs | 9 - src/targets/node/fetch/fixtures/short.cjs | 9 - .../node/fetch/fixtures/text-plain.cjs | 9 - src/targets/node/target.ts | 2 - 21 files changed, 397 deletions(-) delete mode 100644 src/targets/node/fetch/client.ts delete mode 100644 src/targets/node/fetch/fixtures/application-form-encoded.cjs delete mode 100644 src/targets/node/fetch/fixtures/application-json.cjs delete mode 100644 src/targets/node/fetch/fixtures/cookies.cjs delete mode 100644 src/targets/node/fetch/fixtures/custom-method.cjs delete mode 100644 src/targets/node/fetch/fixtures/full.cjs delete mode 100644 src/targets/node/fetch/fixtures/headers.cjs delete mode 100644 src/targets/node/fetch/fixtures/http-insecure.cjs delete mode 100644 src/targets/node/fetch/fixtures/jsonObj-multiline.cjs delete mode 100644 src/targets/node/fetch/fixtures/jsonObj-null-value.cjs delete mode 100644 src/targets/node/fetch/fixtures/multipart-data.cjs delete mode 100644 src/targets/node/fetch/fixtures/multipart-file.cjs delete mode 100644 src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs delete mode 100644 src/targets/node/fetch/fixtures/multipart-form-data.cjs delete mode 100644 src/targets/node/fetch/fixtures/nested.cjs delete mode 100644 src/targets/node/fetch/fixtures/postdata-malformed.cjs delete mode 100644 src/targets/node/fetch/fixtures/query-encoded.cjs delete mode 100644 src/targets/node/fetch/fixtures/query.cjs delete mode 100644 src/targets/node/fetch/fixtures/short.cjs delete mode 100644 src/targets/node/fetch/fixtures/text-plain.cjs diff --git a/src/targets/node/fetch/client.ts b/src/targets/node/fetch/client.ts deleted file mode 100644 index c8f1b76c6..000000000 --- a/src/targets/node/fetch/client.ts +++ /dev/null @@ -1,155 +0,0 @@ -/** - * @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'; - -import { CodeBuilder } from '../../../helpers/code-builder.js'; -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', - }, - convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => { - const opts = { - indent: ' ', - ...options, - }; - - 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 = { - method, - }; - - if (Object.keys(headersObj).length) { - reqOpts.headers = headersObj; - } - - 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'; - break; - - case 'application/json': - if (postData.jsonObj) { - // Though `fetch` doesn't accept JSON objects in the `body` option we're going to - // stringify it when we add this into the snippet further down. - reqOpts.body = postData.jsonObj; - } - break; - - case 'multipart/form-data': - if (!postData.params) { - 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. - // 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) { - push(`formData.append('${param.name}', '${param.value}');`); - return; - } - - if (param.fileName) { - includeFS = true; - push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`); - } - }); - break; - - default: - if (postData.text) { - reqOpts.body = postData.text; - } - } - - // construct cookies argument - if (cookies.length) { - const cookiesString = cookies - .map(({ name, value }) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`) - .join('; '); - if (reqOpts.headers) { - reqOpts.headers.cookie = cookiesString; - } else { - reqOpts.headers = {}; - 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. - if (reqOpts.headers && !Object.keys(reqOpts.headers).length) { - delete reqOpts.headers; - } - - const stringifiedOptions = stringifyObject(reqOpts, { - indent: ' ', - inlineCharacterLimit: 80, - - // The Fetch API body only accepts string parameters, but stringified JSON can be difficult to - // read, so we keep the object as a literal and use this transform function to wrap the literal - // in a `JSON.stringify` call. - transform: (object, property, originalResult) => { - if (property === 'body' && postData.mimeType === 'application/json') { - return `JSON.stringify(${originalResult})`; - } - - return originalResult; - }, - }); - push(`const options = ${stringifiedOptions};`); - blank(); - - if (includeFS) { - unshift("const fs = require('fs');"); - } - if (postData.params && postData.mimeType === 'multipart/form-data') { - push('options.body = formData;'); - blank(); - } - 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")'); - }, -}; diff --git a/src/targets/node/fetch/fixtures/application-form-encoded.cjs b/src/targets/node/fetch/fixtures/application-form-encoded.cjs deleted file mode 100644 index 430528450..000000000 --- a/src/targets/node/fetch/fixtures/application-form-encoded.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const { URLSearchParams } = require('url'); -const fetch = require('node-fetch'); -const encodedParams = new URLSearchParams(); - -encodedParams.set('foo', 'bar'); -encodedParams.set('hello', 'world'); - -const url = 'https://httpbin.org/anything'; -const options = { - method: 'POST', - headers: {'content-type': 'application/x-www-form-urlencoded'}, - body: encodedParams -}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/application-json.cjs b/src/targets/node/fetch/fixtures/application-json.cjs deleted file mode 100644 index b6e1908f1..000000000 --- a/src/targets/node/fetch/fixtures/application-json.cjs +++ /dev/null @@ -1,20 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = { - method: 'POST', - headers: {'content-type': 'application/json'}, - body: JSON.stringify({ - number: 1, - string: 'f"oo', - arr: [1, 2, 3], - nested: {a: 'b'}, - arr_mix: [1, 'a', {arr_mix_nested: []}], - boolean: false - }) -}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/cookies.cjs b/src/targets/node/fetch/fixtures/cookies.cjs deleted file mode 100644 index e61363e0a..000000000 --- a/src/targets/node/fetch/fixtures/cookies.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/cookies'; -const options = {method: 'GET', headers: {cookie: 'foo=bar; bar=baz'}}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/custom-method.cjs b/src/targets/node/fetch/fixtures/custom-method.cjs deleted file mode 100644 index df3e72a79..000000000 --- a/src/targets/node/fetch/fixtures/custom-method.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'PROPFIND'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/full.cjs b/src/targets/node/fetch/fixtures/full.cjs deleted file mode 100644 index 6777b1999..000000000 --- a/src/targets/node/fetch/fixtures/full.cjs +++ /dev/null @@ -1,21 +0,0 @@ -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'; -const options = { - method: 'POST', - headers: { - accept: 'application/json', - 'content-type': 'application/x-www-form-urlencoded', - cookie: 'foo=bar; bar=baz' - }, - body: encodedParams -}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/headers.cjs b/src/targets/node/fetch/fixtures/headers.cjs deleted file mode 100644 index 59ee96acb..000000000 --- a/src/targets/node/fetch/fixtures/headers.cjs +++ /dev/null @@ -1,17 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/headers'; -const options = { - method: 'GET', - headers: { - accept: 'application/json', - 'x-foo': 'Bar', - 'x-bar': 'Foo', - 'quoted-value': '"quoted" \'string\'' - } -}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/http-insecure.cjs b/src/targets/node/fetch/fixtures/http-insecure.cjs deleted file mode 100644 index 37be04767..000000000 --- a/src/targets/node/fetch/fixtures/http-insecure.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'http://httpbin.org/anything'; -const options = {method: 'GET'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-multiline.cjs b/src/targets/node/fetch/fixtures/jsonObj-multiline.cjs deleted file mode 100644 index 3625b76e5..000000000 --- a/src/targets/node/fetch/fixtures/jsonObj-multiline.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = { - method: 'POST', - headers: {'content-type': 'application/json'}, - body: JSON.stringify({foo: 'bar'}) -}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-null-value.cjs b/src/targets/node/fetch/fixtures/jsonObj-null-value.cjs deleted file mode 100644 index c10da148a..000000000 --- a/src/targets/node/fetch/fixtures/jsonObj-null-value.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = { - method: 'POST', - headers: {'content-type': 'application/json'}, - body: JSON.stringify({foo: null}) -}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-data.cjs b/src/targets/node/fetch/fixtures/multipart-data.cjs deleted file mode 100644 index 2c3363e27..000000000 --- a/src/targets/node/fetch/fixtures/multipart-data.cjs +++ /dev/null @@ -1,17 +0,0 @@ -const fs = require('fs'); -const FormData = require('form-data'); -const fetch = require('node-fetch'); -const formData = new FormData(); - -formData.append('foo', fs.createReadStream('src/fixtures/files/hello.txt')); -formData.append('bar', 'Bonjour le monde'); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'POST'}; - -options.body = formData; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-file.cjs b/src/targets/node/fetch/fixtures/multipart-file.cjs deleted file mode 100644 index 68f16405c..000000000 --- a/src/targets/node/fetch/fixtures/multipart-file.cjs +++ /dev/null @@ -1,16 +0,0 @@ -const fs = require('fs'); -const FormData = require('form-data'); -const fetch = require('node-fetch'); -const formData = new FormData(); - -formData.append('foo', fs.createReadStream('src/fixtures/files/hello.txt')); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'POST'}; - -options.body = formData; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs deleted file mode 100644 index 177943af9..000000000 --- a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'POST', headers: {'Content-Type': 'multipart/form-data'}}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data.cjs b/src/targets/node/fetch/fixtures/multipart-form-data.cjs deleted file mode 100644 index f77d66774..000000000 --- a/src/targets/node/fetch/fixtures/multipart-form-data.cjs +++ /dev/null @@ -1,15 +0,0 @@ -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; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/nested.cjs b/src/targets/node/fetch/fixtures/nested.cjs deleted file mode 100644 index 0fb008e35..000000000 --- a/src/targets/node/fetch/fixtures/nested.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value'; -const options = {method: 'GET'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/postdata-malformed.cjs b/src/targets/node/fetch/fixtures/postdata-malformed.cjs deleted file mode 100644 index 95af34d41..000000000 --- a/src/targets/node/fetch/fixtures/postdata-malformed.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'POST', headers: {'content-type': 'application/json'}}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query-encoded.cjs b/src/targets/node/fetch/fixtures/query-encoded.cjs deleted file mode 100644 index bd52227ef..000000000 --- a/src/targets/node/fetch/fixtures/query-encoded.cjs +++ /dev/null @@ -1,9 +0,0 @@ -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'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query.cjs b/src/targets/node/fetch/fixtures/query.cjs deleted file mode 100644 index d18f187c5..000000000 --- a/src/targets/node/fetch/fixtures/query.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value'; -const options = {method: 'GET'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/short.cjs b/src/targets/node/fetch/fixtures/short.cjs deleted file mode 100644 index bddc8acd7..000000000 --- a/src/targets/node/fetch/fixtures/short.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'GET'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/text-plain.cjs b/src/targets/node/fetch/fixtures/text-plain.cjs deleted file mode 100644 index fc9aea5d7..000000000 --- a/src/targets/node/fetch/fixtures/text-plain.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fetch = require('node-fetch'); - -const url = 'https://httpbin.org/anything'; -const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'}; - -fetch(url, options) - .then(res => res.json()) - .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index 77ad49171..49d470280 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -1,7 +1,6 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; -import { fetch } from './fetch/client.js'; import { native } from './native/client.js'; import { unirest } from './unirest/client.js'; @@ -16,6 +15,5 @@ export const node: Target = { native, unirest, axios, - fetch, }, }; From 5776564d9eb329617957f0770de9c01a5d7ff414 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:13:40 -0700 Subject: [PATCH 04/12] refactor: remove node `native` target --- src/targets/node/native/client.ts | 89 ------------------- .../fixtures/application-form-encoded.cjs | 28 ------ .../node/native/fixtures/application-json.cjs | 34 ------- src/targets/node/native/fixtures/cookies.cjs | 26 ------ .../node/native/fixtures/custom-method.cjs | 24 ----- src/targets/node/native/fixtures/full.cjs | 30 ------- src/targets/node/native/fixtures/headers.cjs | 29 ------ .../node/native/fixtures/http-insecure.cjs | 24 ----- .../native/fixtures/jsonObj-multiline.cjs | 27 ------ .../native/fixtures/jsonObj-null-value.cjs | 27 ------ .../node/native/fixtures/multipart-data.cjs | 27 ------ .../node/native/fixtures/multipart-file.cjs | 27 ------ .../multipart-form-data-no-params.cjs | 26 ------ .../native/fixtures/multipart-form-data.cjs | 27 ------ src/targets/node/native/fixtures/nested.cjs | 24 ----- .../native/fixtures/postdata-malformed.cjs | 26 ------ .../node/native/fixtures/query-encoded.cjs | 24 ----- src/targets/node/native/fixtures/query.cjs | 24 ----- src/targets/node/native/fixtures/short.cjs | 24 ----- .../node/native/fixtures/text-plain.cjs | 27 ------ src/targets/node/target.ts | 2 - 21 files changed, 596 deletions(-) delete mode 100644 src/targets/node/native/client.ts delete mode 100644 src/targets/node/native/fixtures/application-form-encoded.cjs delete mode 100644 src/targets/node/native/fixtures/application-json.cjs delete mode 100644 src/targets/node/native/fixtures/cookies.cjs delete mode 100644 src/targets/node/native/fixtures/custom-method.cjs delete mode 100644 src/targets/node/native/fixtures/full.cjs delete mode 100644 src/targets/node/native/fixtures/headers.cjs delete mode 100644 src/targets/node/native/fixtures/http-insecure.cjs delete mode 100644 src/targets/node/native/fixtures/jsonObj-multiline.cjs delete mode 100644 src/targets/node/native/fixtures/jsonObj-null-value.cjs delete mode 100644 src/targets/node/native/fixtures/multipart-data.cjs delete mode 100644 src/targets/node/native/fixtures/multipart-file.cjs delete mode 100644 src/targets/node/native/fixtures/multipart-form-data-no-params.cjs delete mode 100644 src/targets/node/native/fixtures/multipart-form-data.cjs delete mode 100644 src/targets/node/native/fixtures/nested.cjs delete mode 100644 src/targets/node/native/fixtures/postdata-malformed.cjs delete mode 100644 src/targets/node/native/fixtures/query-encoded.cjs delete mode 100644 src/targets/node/native/fixtures/query.cjs delete mode 100644 src/targets/node/native/fixtures/short.cjs delete mode 100644 src/targets/node/native/fixtures/text-plain.cjs diff --git a/src/targets/node/native/client.ts b/src/targets/node/native/client.ts deleted file mode 100644 index 498c5ffab..000000000 --- a/src/targets/node/native/client.ts +++ /dev/null @@ -1,89 +0,0 @@ -/** - * @description - * HTTP code snippet generator for native Node.js. - * - * @author - * @AhmadNassri - * - * 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'; - -import { CodeBuilder } from '../../../helpers/code-builder.js'; - -export const native: Client = { - info: { - key: 'native', - title: 'HTTP', - link: 'http://nodejs.org/api/http.html#http_http_request_options_callback', - description: 'Node.js native HTTP interface', - extname: '.cjs', - }, - convert: ({ uriObj, method, allHeaders, postData }, options = {}) => { - const { indent = ' ' } = options; - const { blank, join, push, unshift } = new CodeBuilder({ indent }); - - const reqOpts = { - method, - hostname: uriObj.hostname, - port: uriObj.port, - path: uriObj.path, - headers: allHeaders, - }; - - push(`const http = require('${uriObj.protocol?.replace(':', '')}');`); - - blank(); - push(`const options = ${stringifyObject(reqOpts, { indent })};`); - blank(); - push('const req = http.request(options, function (res) {'); - push('const chunks = [];', 1); - blank(); - push("res.on('data', function (chunk) {", 1); - push('chunks.push(chunk);', 2); - push('});', 1); - blank(); - push("res.on('end', function () {", 1); - push('const body = Buffer.concat(chunks);', 2); - push('console.log(body.toString());', 2); - push('});', 1); - push('});'); - blank(); - - switch (postData.mimeType) { - case 'application/x-www-form-urlencoded': - if (postData.paramsObj) { - unshift("const qs = require('querystring');"); - push( - `req.write(qs.stringify(${stringifyObject(postData.paramsObj, { - indent: ' ', - inlineCharacterLimit: 80, - })}));`, - ); - } - break; - - case 'application/json': - if (postData.jsonObj) { - push( - `req.write(JSON.stringify(${stringifyObject(postData.jsonObj, { - indent: ' ', - inlineCharacterLimit: 80, - })}));`, - ); - } - break; - - default: - if (postData.text) { - push(`req.write(${stringifyObject(postData.text, { indent })});`); - } - } - - push('req.end();'); - - return join(); - }, -}; diff --git a/src/targets/node/native/fixtures/application-form-encoded.cjs b/src/targets/node/native/fixtures/application-form-encoded.cjs deleted file mode 100644 index 057ff692f..000000000 --- a/src/targets/node/native/fixtures/application-form-encoded.cjs +++ /dev/null @@ -1,28 +0,0 @@ -const qs = require('querystring'); -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'application/x-www-form-urlencoded' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write(qs.stringify({foo: 'bar', hello: 'world'})); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/application-json.cjs b/src/targets/node/native/fixtures/application-json.cjs deleted file mode 100644 index 5129f834c..000000000 --- a/src/targets/node/native/fixtures/application-json.cjs +++ /dev/null @@ -1,34 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'application/json' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write(JSON.stringify({ - number: 1, - string: 'f"oo', - arr: [1, 2, 3], - nested: {a: 'b'}, - arr_mix: [1, 'a', {arr_mix_nested: []}], - boolean: false -})); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/cookies.cjs b/src/targets/node/native/fixtures/cookies.cjs deleted file mode 100644 index 76e950855..000000000 --- a/src/targets/node/native/fixtures/cookies.cjs +++ /dev/null @@ -1,26 +0,0 @@ -const http = require('https'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/cookies', - headers: { - cookie: 'foo=bar; bar=baz' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/custom-method.cjs b/src/targets/node/native/fixtures/custom-method.cjs deleted file mode 100644 index 0640c9991..000000000 --- a/src/targets/node/native/fixtures/custom-method.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const http = require('https'); - -const options = { - method: 'PROPFIND', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: {} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/full.cjs b/src/targets/node/native/fixtures/full.cjs deleted file mode 100644 index dee3821b2..000000000 --- a/src/targets/node/native/fixtures/full.cjs +++ /dev/null @@ -1,30 +0,0 @@ -const qs = require('querystring'); -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything?foo=bar&foo=baz&baz=abc&key=value', - headers: { - cookie: 'foo=bar; bar=baz', - accept: 'application/json', - 'content-type': 'application/x-www-form-urlencoded' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write(qs.stringify({foo: 'bar'})); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/headers.cjs b/src/targets/node/native/fixtures/headers.cjs deleted file mode 100644 index 80e694461..000000000 --- a/src/targets/node/native/fixtures/headers.cjs +++ /dev/null @@ -1,29 +0,0 @@ -const http = require('https'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/headers', - headers: { - accept: 'application/json', - 'x-foo': 'Bar', - 'x-bar': 'Foo', - 'quoted-value': '"quoted" \'string\'' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/http-insecure.cjs b/src/targets/node/native/fixtures/http-insecure.cjs deleted file mode 100644 index c39f9a68e..000000000 --- a/src/targets/node/native/fixtures/http-insecure.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const http = require('http'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: {} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/jsonObj-multiline.cjs b/src/targets/node/native/fixtures/jsonObj-multiline.cjs deleted file mode 100644 index ba61989d1..000000000 --- a/src/targets/node/native/fixtures/jsonObj-multiline.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'application/json' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write(JSON.stringify({foo: 'bar'})); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/jsonObj-null-value.cjs b/src/targets/node/native/fixtures/jsonObj-null-value.cjs deleted file mode 100644 index 5a5648288..000000000 --- a/src/targets/node/native/fixtures/jsonObj-null-value.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'application/json' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write(JSON.stringify({foo: null})); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/multipart-data.cjs b/src/targets/node/native/fixtures/multipart-data.cjs deleted file mode 100644 index 9470e8ae1..000000000 --- a/src/targets/node/native/fixtures/multipart-data.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'multipart/form-data; boundary=---011000010111000001101001' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write('-----011000010111000001101001\r\nContent-Disposition: form-data; name="foo"; filename="src/fixtures/files/hello.txt"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name="bar"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--'); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/multipart-file.cjs b/src/targets/node/native/fixtures/multipart-file.cjs deleted file mode 100644 index aefa51da4..000000000 --- a/src/targets/node/native/fixtures/multipart-file.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'multipart/form-data; boundary=---011000010111000001101001' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write('-----011000010111000001101001\r\nContent-Disposition: form-data; name="foo"; filename="src/fixtures/files/hello.txt"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--'); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/multipart-form-data-no-params.cjs b/src/targets/node/native/fixtures/multipart-form-data-no-params.cjs deleted file mode 100644 index 98bbf669a..000000000 --- a/src/targets/node/native/fixtures/multipart-form-data-no-params.cjs +++ /dev/null @@ -1,26 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'Content-Type': 'multipart/form-data' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/multipart-form-data.cjs b/src/targets/node/native/fixtures/multipart-form-data.cjs deleted file mode 100644 index e914bad77..000000000 --- a/src/targets/node/native/fixtures/multipart-form-data.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write('-----011000010111000001101001\r\nContent-Disposition: form-data; name="foo"\r\n\r\nbar\r\n-----011000010111000001101001--'); -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/nested.cjs b/src/targets/node/native/fixtures/nested.cjs deleted file mode 100644 index 9d0afd9e1..000000000 --- a/src/targets/node/native/fixtures/nested.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const http = require('https'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value', - headers: {} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/postdata-malformed.cjs b/src/targets/node/native/fixtures/postdata-malformed.cjs deleted file mode 100644 index 78f3ca704..000000000 --- a/src/targets/node/native/fixtures/postdata-malformed.cjs +++ /dev/null @@ -1,26 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'application/json' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/query-encoded.cjs b/src/targets/node/native/fixtures/query-encoded.cjs deleted file mode 100644 index d57697168..000000000 --- a/src/targets/node/native/fixtures/query-encoded.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const http = require('https'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00', - headers: {} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/query.cjs b/src/targets/node/native/fixtures/query.cjs deleted file mode 100644 index 74da137db..000000000 --- a/src/targets/node/native/fixtures/query.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const http = require('https'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/anything?foo=bar&foo=baz&baz=abc&key=value', - headers: {} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/short.cjs b/src/targets/node/native/fixtures/short.cjs deleted file mode 100644 index b726546d8..000000000 --- a/src/targets/node/native/fixtures/short.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const http = require('https'); - -const options = { - method: 'GET', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: {} -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.end(); \ No newline at end of file diff --git a/src/targets/node/native/fixtures/text-plain.cjs b/src/targets/node/native/fixtures/text-plain.cjs deleted file mode 100644 index 4fb117878..000000000 --- a/src/targets/node/native/fixtures/text-plain.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const http = require('https'); - -const options = { - method: 'POST', - hostname: 'httpbin.org', - port: null, - path: '/anything', - headers: { - 'content-type': 'text/plain' - } -}; - -const req = http.request(options, function (res) { - const chunks = []; - - res.on('data', function (chunk) { - chunks.push(chunk); - }); - - res.on('end', function () { - const body = Buffer.concat(chunks); - console.log(body.toString()); - }); -}); - -req.write('Hello World'); -req.end(); \ No newline at end of file diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index 49d470280..d8dba16dc 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -1,7 +1,6 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; -import { native } from './native/client.js'; import { unirest } from './unirest/client.js'; export const node: Target = { @@ -12,7 +11,6 @@ export const node: Target = { cli: 'node %s', }, clientsById: { - native, unirest, axios, }, From 972a9cb41f240964b89655a8377ad90d26442185 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:13:54 -0700 Subject: [PATCH 05/12] refactor: update target defaults --- src/targets/javascript/target.ts | 1 + src/targets/node/target.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/targets/javascript/target.ts b/src/targets/javascript/target.ts index 5f24434ed..475b61697 100644 --- a/src/targets/javascript/target.ts +++ b/src/targets/javascript/target.ts @@ -8,6 +8,7 @@ export const javascript: Target = { info: { key: 'javascript', title: 'JavaScript', + default: 'fetch', }, clientsById: { diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index d8dba16dc..bc352bb00 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -7,7 +7,7 @@ export const node: Target = { info: { key: 'node', title: 'Node.js', - default: 'native', + default: 'axios', cli: 'node %s', }, clientsById: { From 37ec78e408ee9520e5b462bb4aa00bfe1cf78c4b Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:14:02 -0700 Subject: [PATCH 06/12] test: update snapshot --- src/helpers/__snapshots__/utils.test.ts.snap | 25 +------------------- 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index f6530b526..2b278f799 100644 --- a/src/helpers/__snapshots__/utils.test.ts.snap +++ b/src/helpers/__snapshots__/utils.test.ts.snap @@ -178,21 +178,6 @@ exports[`availableTargets > returns all available targets 1`] = ` { "cli": "node %s", "clients": [ - { - "description": "Node.js native HTTP interface", - "extname": ".cjs", - "key": "native", - "link": "http://nodejs.org/api/http.html#http_http_request_options_callback", - "title": "HTTP", - }, - { - "description": "Simplified HTTP request client", - "extname": ".cjs", - "installation": "npm install request --save", - "key": "request", - "link": "https://github.com/request/request", - "title": "Request", - }, { "description": "Lightweight HTTP Request Client Library", "extname": ".cjs", @@ -208,16 +193,8 @@ exports[`availableTargets > returns all available targets 1`] = ` "link": "https://github.com/axios/axios", "title": "Axios", }, - { - "description": "Simplified HTTP node-fetch client", - "extname": ".cjs", - "installation": "npm install node-fetch@2 --save", - "key": "fetch", - "link": "https://github.com/bitinn/node-fetch", - "title": "Fetch", - }, ], - "default": "native", + "default": "axios", "key": "node", "title": "Node.js", }, From 1ac2dd7a756ea6c72c34649e2280fbb6bc3eec80 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Tue, 7 May 2024 12:20:43 -0700 Subject: [PATCH 07/12] test: update test to use existing client --- src/targets/index.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/targets/index.test.ts b/src/targets/index.test.ts index 97eaade1f..19843bcbe 100644 --- a/src/targets/index.test.ts +++ b/src/targets/index.test.ts @@ -266,7 +266,7 @@ describe('addTarget', () => { }); it('should add a new custom target', async () => { - const { fetch: fetchClient } = await import('./node/fetch/client'); + const { axios: axiosClient } = await import('./node/axios/client'); const deno: Target = { info: { @@ -274,10 +274,10 @@ describe('addTarget', () => { key: 'deno', title: 'Deno', extname: '.js', - default: 'fetch', + default: 'axios', }, clientsById: { - fetch: fetchClient, + axios: axiosClient }, }; From b8f492ce76b0b514eb9230f5a36675d2029779f7 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Mon, 13 May 2024 10:33:25 -0700 Subject: [PATCH 08/12] chore: update imports --- src/fixtures/customTarget.ts | 6 +++--- src/targets/index.test.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fixtures/customTarget.ts b/src/fixtures/customTarget.ts index 98f15fbe8..67c6278b2 100644 --- a/src/fixtures/customTarget.ts +++ b/src/fixtures/customTarget.ts @@ -1,15 +1,15 @@ import type { Target } from '../targets/index.js'; -import { request } from '../targets/node/request/client.js'; +import { axios } from '../targets/node/axios/client.js'; export const customTarget = { info: { key: 'js-variant', title: 'JavaScript Variant', extname: '.js', - default: 'request', + default: 'axios', }, clientsById: { - request, + axios, }, } as unknown as Target; diff --git a/src/targets/index.test.ts b/src/targets/index.test.ts index 19843bcbe..c42bf56f5 100644 --- a/src/targets/index.test.ts +++ b/src/targets/index.test.ts @@ -277,7 +277,7 @@ describe('addTarget', () => { default: 'axios', }, clientsById: { - axios: axiosClient + axios: axiosClient, }, }; From bf7badc8f0a1fa604170be54542f93a042ae395e Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Wed, 29 May 2024 10:33:09 -0700 Subject: [PATCH 09/12] feat: replace node-fetch with fetch --- src/helpers/__snapshots__/utils.test.ts.snap | 9 +- src/targets/node/fetch/client.ts | 130 ++++++++++++++++++ .../fixtures/application-form-encoded.js | 10 ++ .../node/fetch/fixtures/application-json.js | 17 +++ src/targets/node/fetch/fixtures/cookies.js | 6 + .../node/fetch/fixtures/custom-method.js | 6 + src/targets/node/fetch/fixtures/full.js | 14 ++ src/targets/node/fetch/fixtures/headers.js | 14 ++ .../node/fetch/fixtures/http-insecure.js | 6 + .../node/fetch/fixtures/jsonObj-multiline.js | 10 ++ .../node/fetch/fixtures/jsonObj-null-value.js | 10 ++ .../node/fetch/fixtures/multipart-data.js | 12 ++ .../node/fetch/fixtures/multipart-file.js | 11 ++ .../fixtures/multipart-form-data-no-params.js | 6 + .../fetch/fixtures/multipart-form-data.js | 11 ++ src/targets/node/fetch/fixtures/nested.js | 6 + .../node/fetch/fixtures/postdata-malformed.js | 6 + .../node/fetch/fixtures/query-encoded.js | 6 + src/targets/node/fetch/fixtures/query.js | 6 + src/targets/node/fetch/fixtures/short.js | 6 + src/targets/node/fetch/fixtures/text-plain.js | 6 + src/targets/node/target.ts | 4 +- 22 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 src/targets/node/fetch/client.ts create mode 100644 src/targets/node/fetch/fixtures/application-form-encoded.js create mode 100644 src/targets/node/fetch/fixtures/application-json.js create mode 100644 src/targets/node/fetch/fixtures/cookies.js create mode 100644 src/targets/node/fetch/fixtures/custom-method.js create mode 100644 src/targets/node/fetch/fixtures/full.js create mode 100644 src/targets/node/fetch/fixtures/headers.js create mode 100644 src/targets/node/fetch/fixtures/http-insecure.js create mode 100644 src/targets/node/fetch/fixtures/jsonObj-multiline.js create mode 100644 src/targets/node/fetch/fixtures/jsonObj-null-value.js create mode 100644 src/targets/node/fetch/fixtures/multipart-data.js create mode 100644 src/targets/node/fetch/fixtures/multipart-file.js create mode 100644 src/targets/node/fetch/fixtures/multipart-form-data-no-params.js create mode 100644 src/targets/node/fetch/fixtures/multipart-form-data.js create mode 100644 src/targets/node/fetch/fixtures/nested.js create mode 100644 src/targets/node/fetch/fixtures/postdata-malformed.js create mode 100644 src/targets/node/fetch/fixtures/query-encoded.js create mode 100644 src/targets/node/fetch/fixtures/query.js create mode 100644 src/targets/node/fetch/fixtures/short.js create mode 100644 src/targets/node/fetch/fixtures/text-plain.js diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index 2b278f799..6cf35fd48 100644 --- a/src/helpers/__snapshots__/utils.test.ts.snap +++ b/src/helpers/__snapshots__/utils.test.ts.snap @@ -178,6 +178,13 @@ exports[`availableTargets > returns all available targets 1`] = ` { "cli": "node %s", "clients": [ + { + "description": "Perform asynchronous HTTP requests with the Fetch API", + "extname": ".js", + "key": "fetch", + "link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch", + "title": "fetch", + }, { "description": "Lightweight HTTP Request Client Library", "extname": ".cjs", @@ -194,7 +201,7 @@ exports[`availableTargets > returns all available targets 1`] = ` "title": "Axios", }, ], - "default": "axios", + "default": "fetch", "key": "node", "title": "Node.js", }, diff --git a/src/targets/node/fetch/client.ts b/src/targets/node/fetch/client.ts new file mode 100644 index 000000000..9db46a397 --- /dev/null +++ b/src/targets/node/fetch/client.ts @@ -0,0 +1,130 @@ +/** + * @description + * HTTP code snippet generator for fetch + * + * @author + * @pmdroid + * + * 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'; + +import { CodeBuilder } from '../../../helpers/code-builder.js'; +import { getHeaderName } from '../../../helpers/headers.js'; + +interface FetchOptions { + credentials?: Record | null; +} + +export const fetch: Client = { + info: { + key: 'fetch', + 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, allHeaders, postData, fullUrl }, inputOpts) => { + const opts = { + indent: ' ', + credentials: null, + ...inputOpts, + }; + + const { blank, join, push } = new CodeBuilder({ indent: opts.indent }); + + const options: Record = { + method, + }; + + if (Object.keys(allHeaders).length) { + options.headers = allHeaders; + } + + if (opts.credentials !== null) { + options.credentials = opts.credentials; + } + + switch (postData.mimeType) { + case 'application/x-www-form-urlencoded': + options.body = postData.paramsObj ? postData.paramsObj : postData.text; + break; + + case 'application/json': + if (postData.jsonObj) { + // Though `fetch` doesn't accept JSON objects in the `body` option we're going to + // stringify it when we add this into the snippet further down. + options.body = postData.jsonObj; + } + break; + + case 'multipart/form-data': + if (!postData.params) { + break; + } + + // 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(allHeaders, 'content-type'); + if (contentTypeHeader) { + delete allHeaders[contentTypeHeader]; + } + + push('const form = new FormData();'); + + postData.params.forEach(param => { + push(`form.append('${param.name}', '${param.value || param.fileName || ''}');`); + }); + + blank(); + break; + + default: + if (postData.text) { + options.body = postData.text; + } + } + + // If we ultimately don't have any headers to send then we shouldn't add an empty object into the request options. + if (options.headers && !Object.keys(options.headers).length) { + delete options.headers; + } + + push( + `const options = ${stringifyObject(options, { + indent: opts.indent, + inlineCharacterLimit: 80, + + // The Fetch API body only accepts string parameters, but stringified JSON can be difficult + // to read, so we keep the object as a literal and use this transform function to wrap the + // literal in a `JSON.stringify` call. + transform: (object, property, originalResult) => { + if (property === 'body') { + if (postData.mimeType === 'application/x-www-form-urlencoded') { + return `new URLSearchParams(${originalResult})`; + } else if (postData.mimeType === 'application/json') { + return `JSON.stringify(${originalResult})`; + } + } + + return originalResult; + }, + })};`, + ); + blank(); + + if (postData.params && postData.mimeType === 'multipart/form-data') { + push('options.body = form;'); + blank(); + } + + push(`fetch('${fullUrl}', options)`); + push('.then(response => response.json())', 1); + push('.then(response => console.log(response))', 1); + push('.catch(err => console.error(err));', 1); + + return join(); + }, +}; diff --git a/src/targets/node/fetch/fixtures/application-form-encoded.js b/src/targets/node/fetch/fixtures/application-form-encoded.js new file mode 100644 index 000000000..fd256737b --- /dev/null +++ b/src/targets/node/fetch/fixtures/application-form-encoded.js @@ -0,0 +1,10 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({foo: 'bar', hello: 'world'}) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/application-json.js b/src/targets/node/fetch/fixtures/application-json.js new file mode 100644 index 000000000..f79071c08 --- /dev/null +++ b/src/targets/node/fetch/fixtures/application-json.js @@ -0,0 +1,17 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/json'}, + body: JSON.stringify({ + number: 1, + string: 'f"oo', + arr: [1, 2, 3], + nested: {a: 'b'}, + arr_mix: [1, 'a', {arr_mix_nested: []}], + boolean: false + }) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/cookies.js b/src/targets/node/fetch/fixtures/cookies.js new file mode 100644 index 000000000..a9ba5766d --- /dev/null +++ b/src/targets/node/fetch/fixtures/cookies.js @@ -0,0 +1,6 @@ +const options = {method: 'GET', headers: {cookie: 'foo=bar; bar=baz'}}; + +fetch('https://httpbin.org/cookies', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/custom-method.js b/src/targets/node/fetch/fixtures/custom-method.js new file mode 100644 index 000000000..738405920 --- /dev/null +++ b/src/targets/node/fetch/fixtures/custom-method.js @@ -0,0 +1,6 @@ +const options = {method: 'PROPFIND'}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/full.js b/src/targets/node/fetch/fixtures/full.js new file mode 100644 index 000000000..3aee96390 --- /dev/null +++ b/src/targets/node/fetch/fixtures/full.js @@ -0,0 +1,14 @@ +const options = { + method: 'POST', + headers: { + cookie: 'foo=bar; bar=baz', + accept: 'application/json', + 'content-type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({foo: 'bar'}) +}; + +fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/headers.js b/src/targets/node/fetch/fixtures/headers.js new file mode 100644 index 000000000..6db2a5d5b --- /dev/null +++ b/src/targets/node/fetch/fixtures/headers.js @@ -0,0 +1,14 @@ +const options = { + method: 'GET', + headers: { + accept: 'application/json', + 'x-foo': 'Bar', + 'x-bar': 'Foo', + 'quoted-value': '"quoted" \'string\'' + } +}; + +fetch('https://httpbin.org/headers', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/http-insecure.js b/src/targets/node/fetch/fixtures/http-insecure.js new file mode 100644 index 000000000..c2624597f --- /dev/null +++ b/src/targets/node/fetch/fixtures/http-insecure.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('http://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-multiline.js b/src/targets/node/fetch/fixtures/jsonObj-multiline.js new file mode 100644 index 000000000..f2e9c2795 --- /dev/null +++ b/src/targets/node/fetch/fixtures/jsonObj-multiline.js @@ -0,0 +1,10 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/json'}, + body: JSON.stringify({foo: 'bar'}) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-null-value.js b/src/targets/node/fetch/fixtures/jsonObj-null-value.js new file mode 100644 index 000000000..b6b9ea049 --- /dev/null +++ b/src/targets/node/fetch/fixtures/jsonObj-null-value.js @@ -0,0 +1,10 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/json'}, + body: JSON.stringify({foo: null}) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-data.js b/src/targets/node/fetch/fixtures/multipart-data.js new file mode 100644 index 000000000..5cc8ddf85 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-data.js @@ -0,0 +1,12 @@ +const form = new FormData(); +form.append('foo', 'Hello World'); +form.append('bar', 'Bonjour le monde'); + +const options = {method: 'POST'}; + +options.body = form; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-file.js b/src/targets/node/fetch/fixtures/multipart-file.js new file mode 100644 index 000000000..11b0a6b05 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-file.js @@ -0,0 +1,11 @@ +const form = new FormData(); +form.append('foo', 'src/fixtures/files/hello.txt'); + +const options = {method: 'POST'}; + +options.body = form; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js new file mode 100644 index 000000000..b1318179e --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js @@ -0,0 +1,6 @@ +const options = {method: 'POST', headers: {'Content-Type': 'multipart/form-data'}}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data.js b/src/targets/node/fetch/fixtures/multipart-form-data.js new file mode 100644 index 000000000..90643fc61 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-form-data.js @@ -0,0 +1,11 @@ +const form = new FormData(); +form.append('foo', 'bar'); + +const options = {method: 'POST'}; + +options.body = form; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/nested.js b/src/targets/node/fetch/fixtures/nested.js new file mode 100644 index 000000000..8bcc084d2 --- /dev/null +++ b/src/targets/node/fetch/fixtures/nested.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/postdata-malformed.js b/src/targets/node/fetch/fixtures/postdata-malformed.js new file mode 100644 index 000000000..145b702c4 --- /dev/null +++ b/src/targets/node/fetch/fixtures/postdata-malformed.js @@ -0,0 +1,6 @@ +const options = {method: 'POST', headers: {'content-type': 'application/json'}}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query-encoded.js b/src/targets/node/fetch/fixtures/query-encoded.js new file mode 100644 index 000000000..6da5448bb --- /dev/null +++ b/src/targets/node/fetch/fixtures/query-encoded.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query.js b/src/targets/node/fetch/fixtures/query.js new file mode 100644 index 000000000..fe792dbe1 --- /dev/null +++ b/src/targets/node/fetch/fixtures/query.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/short.js b/src/targets/node/fetch/fixtures/short.js new file mode 100644 index 000000000..86cec7387 --- /dev/null +++ b/src/targets/node/fetch/fixtures/short.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/text-plain.js b/src/targets/node/fetch/fixtures/text-plain.js new file mode 100644 index 000000000..3ff4a6f81 --- /dev/null +++ b/src/targets/node/fetch/fixtures/text-plain.js @@ -0,0 +1,6 @@ +const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index bc352bb00..4101479de 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -1,16 +1,18 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; +import { fetch } from './fetch/client.js'; import { unirest } from './unirest/client.js'; export const node: Target = { info: { key: 'node', title: 'Node.js', - default: 'axios', + default: 'fetch', cli: 'node %s', }, clientsById: { + fetch, unirest, axios, }, From e6ac3a0d5fd5a43ae9cfef6fc6c64be681508847 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Wed, 29 May 2024 11:12:09 -0700 Subject: [PATCH 10/12] refactor: oops --- src/helpers/__snapshots__/utils.test.ts.snap | 9 +- src/targets/node/fetch/client.ts | 130 ------------------ .../fixtures/application-form-encoded.js | 10 -- .../node/fetch/fixtures/application-json.js | 17 --- src/targets/node/fetch/fixtures/cookies.js | 6 - .../node/fetch/fixtures/custom-method.js | 6 - src/targets/node/fetch/fixtures/full.js | 14 -- src/targets/node/fetch/fixtures/headers.js | 14 -- .../node/fetch/fixtures/http-insecure.js | 6 - .../node/fetch/fixtures/jsonObj-multiline.js | 10 -- .../node/fetch/fixtures/jsonObj-null-value.js | 10 -- .../node/fetch/fixtures/multipart-data.js | 12 -- .../node/fetch/fixtures/multipart-file.js | 11 -- .../fixtures/multipart-form-data-no-params.js | 6 - .../fetch/fixtures/multipart-form-data.js | 11 -- src/targets/node/fetch/fixtures/nested.js | 6 - .../node/fetch/fixtures/postdata-malformed.js | 6 - .../node/fetch/fixtures/query-encoded.js | 6 - src/targets/node/fetch/fixtures/query.js | 6 - src/targets/node/fetch/fixtures/short.js | 6 - src/targets/node/fetch/fixtures/text-plain.js | 6 - src/targets/node/target.ts | 4 +- 22 files changed, 2 insertions(+), 310 deletions(-) delete mode 100644 src/targets/node/fetch/client.ts delete mode 100644 src/targets/node/fetch/fixtures/application-form-encoded.js delete mode 100644 src/targets/node/fetch/fixtures/application-json.js delete mode 100644 src/targets/node/fetch/fixtures/cookies.js delete mode 100644 src/targets/node/fetch/fixtures/custom-method.js delete mode 100644 src/targets/node/fetch/fixtures/full.js delete mode 100644 src/targets/node/fetch/fixtures/headers.js delete mode 100644 src/targets/node/fetch/fixtures/http-insecure.js delete mode 100644 src/targets/node/fetch/fixtures/jsonObj-multiline.js delete mode 100644 src/targets/node/fetch/fixtures/jsonObj-null-value.js delete mode 100644 src/targets/node/fetch/fixtures/multipart-data.js delete mode 100644 src/targets/node/fetch/fixtures/multipart-file.js delete mode 100644 src/targets/node/fetch/fixtures/multipart-form-data-no-params.js delete mode 100644 src/targets/node/fetch/fixtures/multipart-form-data.js delete mode 100644 src/targets/node/fetch/fixtures/nested.js delete mode 100644 src/targets/node/fetch/fixtures/postdata-malformed.js delete mode 100644 src/targets/node/fetch/fixtures/query-encoded.js delete mode 100644 src/targets/node/fetch/fixtures/query.js delete mode 100644 src/targets/node/fetch/fixtures/short.js delete mode 100644 src/targets/node/fetch/fixtures/text-plain.js diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index 6cf35fd48..2b278f799 100644 --- a/src/helpers/__snapshots__/utils.test.ts.snap +++ b/src/helpers/__snapshots__/utils.test.ts.snap @@ -178,13 +178,6 @@ exports[`availableTargets > returns all available targets 1`] = ` { "cli": "node %s", "clients": [ - { - "description": "Perform asynchronous HTTP requests with the Fetch API", - "extname": ".js", - "key": "fetch", - "link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch", - "title": "fetch", - }, { "description": "Lightweight HTTP Request Client Library", "extname": ".cjs", @@ -201,7 +194,7 @@ exports[`availableTargets > returns all available targets 1`] = ` "title": "Axios", }, ], - "default": "fetch", + "default": "axios", "key": "node", "title": "Node.js", }, diff --git a/src/targets/node/fetch/client.ts b/src/targets/node/fetch/client.ts deleted file mode 100644 index 9db46a397..000000000 --- a/src/targets/node/fetch/client.ts +++ /dev/null @@ -1,130 +0,0 @@ -/** - * @description - * HTTP code snippet generator for fetch - * - * @author - * @pmdroid - * - * 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'; - -import { CodeBuilder } from '../../../helpers/code-builder.js'; -import { getHeaderName } from '../../../helpers/headers.js'; - -interface FetchOptions { - credentials?: Record | null; -} - -export const fetch: Client = { - info: { - key: 'fetch', - 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, allHeaders, postData, fullUrl }, inputOpts) => { - const opts = { - indent: ' ', - credentials: null, - ...inputOpts, - }; - - const { blank, join, push } = new CodeBuilder({ indent: opts.indent }); - - const options: Record = { - method, - }; - - if (Object.keys(allHeaders).length) { - options.headers = allHeaders; - } - - if (opts.credentials !== null) { - options.credentials = opts.credentials; - } - - switch (postData.mimeType) { - case 'application/x-www-form-urlencoded': - options.body = postData.paramsObj ? postData.paramsObj : postData.text; - break; - - case 'application/json': - if (postData.jsonObj) { - // Though `fetch` doesn't accept JSON objects in the `body` option we're going to - // stringify it when we add this into the snippet further down. - options.body = postData.jsonObj; - } - break; - - case 'multipart/form-data': - if (!postData.params) { - break; - } - - // 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(allHeaders, 'content-type'); - if (contentTypeHeader) { - delete allHeaders[contentTypeHeader]; - } - - push('const form = new FormData();'); - - postData.params.forEach(param => { - push(`form.append('${param.name}', '${param.value || param.fileName || ''}');`); - }); - - blank(); - break; - - default: - if (postData.text) { - options.body = postData.text; - } - } - - // If we ultimately don't have any headers to send then we shouldn't add an empty object into the request options. - if (options.headers && !Object.keys(options.headers).length) { - delete options.headers; - } - - push( - `const options = ${stringifyObject(options, { - indent: opts.indent, - inlineCharacterLimit: 80, - - // The Fetch API body only accepts string parameters, but stringified JSON can be difficult - // to read, so we keep the object as a literal and use this transform function to wrap the - // literal in a `JSON.stringify` call. - transform: (object, property, originalResult) => { - if (property === 'body') { - if (postData.mimeType === 'application/x-www-form-urlencoded') { - return `new URLSearchParams(${originalResult})`; - } else if (postData.mimeType === 'application/json') { - return `JSON.stringify(${originalResult})`; - } - } - - return originalResult; - }, - })};`, - ); - blank(); - - if (postData.params && postData.mimeType === 'multipart/form-data') { - push('options.body = form;'); - blank(); - } - - push(`fetch('${fullUrl}', options)`); - push('.then(response => response.json())', 1); - push('.then(response => console.log(response))', 1); - push('.catch(err => console.error(err));', 1); - - return join(); - }, -}; diff --git a/src/targets/node/fetch/fixtures/application-form-encoded.js b/src/targets/node/fetch/fixtures/application-form-encoded.js deleted file mode 100644 index fd256737b..000000000 --- a/src/targets/node/fetch/fixtures/application-form-encoded.js +++ /dev/null @@ -1,10 +0,0 @@ -const options = { - method: 'POST', - headers: {'content-type': 'application/x-www-form-urlencoded'}, - body: new URLSearchParams({foo: 'bar', hello: 'world'}) -}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/application-json.js b/src/targets/node/fetch/fixtures/application-json.js deleted file mode 100644 index f79071c08..000000000 --- a/src/targets/node/fetch/fixtures/application-json.js +++ /dev/null @@ -1,17 +0,0 @@ -const options = { - method: 'POST', - headers: {'content-type': 'application/json'}, - body: JSON.stringify({ - number: 1, - string: 'f"oo', - arr: [1, 2, 3], - nested: {a: 'b'}, - arr_mix: [1, 'a', {arr_mix_nested: []}], - boolean: false - }) -}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/cookies.js b/src/targets/node/fetch/fixtures/cookies.js deleted file mode 100644 index a9ba5766d..000000000 --- a/src/targets/node/fetch/fixtures/cookies.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'GET', headers: {cookie: 'foo=bar; bar=baz'}}; - -fetch('https://httpbin.org/cookies', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/custom-method.js b/src/targets/node/fetch/fixtures/custom-method.js deleted file mode 100644 index 738405920..000000000 --- a/src/targets/node/fetch/fixtures/custom-method.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'PROPFIND'}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/full.js b/src/targets/node/fetch/fixtures/full.js deleted file mode 100644 index 3aee96390..000000000 --- a/src/targets/node/fetch/fixtures/full.js +++ /dev/null @@ -1,14 +0,0 @@ -const options = { - method: 'POST', - headers: { - cookie: 'foo=bar; bar=baz', - accept: 'application/json', - 'content-type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({foo: 'bar'}) -}; - -fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/headers.js b/src/targets/node/fetch/fixtures/headers.js deleted file mode 100644 index 6db2a5d5b..000000000 --- a/src/targets/node/fetch/fixtures/headers.js +++ /dev/null @@ -1,14 +0,0 @@ -const options = { - method: 'GET', - headers: { - accept: 'application/json', - 'x-foo': 'Bar', - 'x-bar': 'Foo', - 'quoted-value': '"quoted" \'string\'' - } -}; - -fetch('https://httpbin.org/headers', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/http-insecure.js b/src/targets/node/fetch/fixtures/http-insecure.js deleted file mode 100644 index c2624597f..000000000 --- a/src/targets/node/fetch/fixtures/http-insecure.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'GET'}; - -fetch('http://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-multiline.js b/src/targets/node/fetch/fixtures/jsonObj-multiline.js deleted file mode 100644 index f2e9c2795..000000000 --- a/src/targets/node/fetch/fixtures/jsonObj-multiline.js +++ /dev/null @@ -1,10 +0,0 @@ -const options = { - method: 'POST', - headers: {'content-type': 'application/json'}, - body: JSON.stringify({foo: 'bar'}) -}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-null-value.js b/src/targets/node/fetch/fixtures/jsonObj-null-value.js deleted file mode 100644 index b6b9ea049..000000000 --- a/src/targets/node/fetch/fixtures/jsonObj-null-value.js +++ /dev/null @@ -1,10 +0,0 @@ -const options = { - method: 'POST', - headers: {'content-type': 'application/json'}, - body: JSON.stringify({foo: null}) -}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-data.js b/src/targets/node/fetch/fixtures/multipart-data.js deleted file mode 100644 index 5cc8ddf85..000000000 --- a/src/targets/node/fetch/fixtures/multipart-data.js +++ /dev/null @@ -1,12 +0,0 @@ -const form = new FormData(); -form.append('foo', 'Hello World'); -form.append('bar', 'Bonjour le monde'); - -const options = {method: 'POST'}; - -options.body = form; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-file.js b/src/targets/node/fetch/fixtures/multipart-file.js deleted file mode 100644 index 11b0a6b05..000000000 --- a/src/targets/node/fetch/fixtures/multipart-file.js +++ /dev/null @@ -1,11 +0,0 @@ -const form = new FormData(); -form.append('foo', 'src/fixtures/files/hello.txt'); - -const options = {method: 'POST'}; - -options.body = form; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js deleted file mode 100644 index b1318179e..000000000 --- a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'POST', headers: {'Content-Type': 'multipart/form-data'}}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data.js b/src/targets/node/fetch/fixtures/multipart-form-data.js deleted file mode 100644 index 90643fc61..000000000 --- a/src/targets/node/fetch/fixtures/multipart-form-data.js +++ /dev/null @@ -1,11 +0,0 @@ -const form = new FormData(); -form.append('foo', 'bar'); - -const options = {method: 'POST'}; - -options.body = form; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/nested.js b/src/targets/node/fetch/fixtures/nested.js deleted file mode 100644 index 8bcc084d2..000000000 --- a/src/targets/node/fetch/fixtures/nested.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'GET'}; - -fetch('https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/postdata-malformed.js b/src/targets/node/fetch/fixtures/postdata-malformed.js deleted file mode 100644 index 145b702c4..000000000 --- a/src/targets/node/fetch/fixtures/postdata-malformed.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'POST', headers: {'content-type': 'application/json'}}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query-encoded.js b/src/targets/node/fetch/fixtures/query-encoded.js deleted file mode 100644 index 6da5448bb..000000000 --- a/src/targets/node/fetch/fixtures/query-encoded.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'GET'}; - -fetch('https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query.js b/src/targets/node/fetch/fixtures/query.js deleted file mode 100644 index fe792dbe1..000000000 --- a/src/targets/node/fetch/fixtures/query.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'GET'}; - -fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/short.js b/src/targets/node/fetch/fixtures/short.js deleted file mode 100644 index 86cec7387..000000000 --- a/src/targets/node/fetch/fixtures/short.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'GET'}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/text-plain.js b/src/targets/node/fetch/fixtures/text-plain.js deleted file mode 100644 index 3ff4a6f81..000000000 --- a/src/targets/node/fetch/fixtures/text-plain.js +++ /dev/null @@ -1,6 +0,0 @@ -const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'}; - -fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) - .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index 4101479de..bc352bb00 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -1,18 +1,16 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; -import { fetch } from './fetch/client.js'; import { unirest } from './unirest/client.js'; export const node: Target = { info: { key: 'node', title: 'Node.js', - default: 'fetch', + default: 'axios', cli: 'node %s', }, clientsById: { - fetch, unirest, axios, }, From ac35e4b670436635a0053a05ddd2d76a1c9f1c06 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Mon, 3 Jun 2024 12:45:23 -0700 Subject: [PATCH 11/12] refactor: add native `fetch` to node --- src/targets/node/fetch/client.ts | 130 ++++++++++++++++++ .../fixtures/application-form-encoded.js | 10 ++ .../node/fetch/fixtures/application-json.js | 17 +++ src/targets/node/fetch/fixtures/cookies.js | 6 + .../node/fetch/fixtures/custom-method.js | 6 + src/targets/node/fetch/fixtures/full.js | 14 ++ src/targets/node/fetch/fixtures/headers.js | 14 ++ .../node/fetch/fixtures/http-insecure.js | 6 + .../node/fetch/fixtures/jsonObj-multiline.js | 10 ++ .../node/fetch/fixtures/jsonObj-null-value.js | 10 ++ .../node/fetch/fixtures/multipart-data.js | 12 ++ .../node/fetch/fixtures/multipart-file.js | 11 ++ .../fixtures/multipart-form-data-no-params.js | 6 + .../fetch/fixtures/multipart-form-data.js | 11 ++ src/targets/node/fetch/fixtures/nested.js | 6 + .../node/fetch/fixtures/postdata-malformed.js | 6 + .../node/fetch/fixtures/query-encoded.js | 6 + src/targets/node/fetch/fixtures/query.js | 6 + src/targets/node/fetch/fixtures/short.js | 6 + src/targets/node/fetch/fixtures/text-plain.js | 6 + src/targets/node/target.ts | 4 +- 21 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 src/targets/node/fetch/client.ts create mode 100644 src/targets/node/fetch/fixtures/application-form-encoded.js create mode 100644 src/targets/node/fetch/fixtures/application-json.js create mode 100644 src/targets/node/fetch/fixtures/cookies.js create mode 100644 src/targets/node/fetch/fixtures/custom-method.js create mode 100644 src/targets/node/fetch/fixtures/full.js create mode 100644 src/targets/node/fetch/fixtures/headers.js create mode 100644 src/targets/node/fetch/fixtures/http-insecure.js create mode 100644 src/targets/node/fetch/fixtures/jsonObj-multiline.js create mode 100644 src/targets/node/fetch/fixtures/jsonObj-null-value.js create mode 100644 src/targets/node/fetch/fixtures/multipart-data.js create mode 100644 src/targets/node/fetch/fixtures/multipart-file.js create mode 100644 src/targets/node/fetch/fixtures/multipart-form-data-no-params.js create mode 100644 src/targets/node/fetch/fixtures/multipart-form-data.js create mode 100644 src/targets/node/fetch/fixtures/nested.js create mode 100644 src/targets/node/fetch/fixtures/postdata-malformed.js create mode 100644 src/targets/node/fetch/fixtures/query-encoded.js create mode 100644 src/targets/node/fetch/fixtures/query.js create mode 100644 src/targets/node/fetch/fixtures/short.js create mode 100644 src/targets/node/fetch/fixtures/text-plain.js diff --git a/src/targets/node/fetch/client.ts b/src/targets/node/fetch/client.ts new file mode 100644 index 000000000..9db46a397 --- /dev/null +++ b/src/targets/node/fetch/client.ts @@ -0,0 +1,130 @@ +/** + * @description + * HTTP code snippet generator for fetch + * + * @author + * @pmdroid + * + * 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'; + +import { CodeBuilder } from '../../../helpers/code-builder.js'; +import { getHeaderName } from '../../../helpers/headers.js'; + +interface FetchOptions { + credentials?: Record | null; +} + +export const fetch: Client = { + info: { + key: 'fetch', + 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, allHeaders, postData, fullUrl }, inputOpts) => { + const opts = { + indent: ' ', + credentials: null, + ...inputOpts, + }; + + const { blank, join, push } = new CodeBuilder({ indent: opts.indent }); + + const options: Record = { + method, + }; + + if (Object.keys(allHeaders).length) { + options.headers = allHeaders; + } + + if (opts.credentials !== null) { + options.credentials = opts.credentials; + } + + switch (postData.mimeType) { + case 'application/x-www-form-urlencoded': + options.body = postData.paramsObj ? postData.paramsObj : postData.text; + break; + + case 'application/json': + if (postData.jsonObj) { + // Though `fetch` doesn't accept JSON objects in the `body` option we're going to + // stringify it when we add this into the snippet further down. + options.body = postData.jsonObj; + } + break; + + case 'multipart/form-data': + if (!postData.params) { + break; + } + + // 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(allHeaders, 'content-type'); + if (contentTypeHeader) { + delete allHeaders[contentTypeHeader]; + } + + push('const form = new FormData();'); + + postData.params.forEach(param => { + push(`form.append('${param.name}', '${param.value || param.fileName || ''}');`); + }); + + blank(); + break; + + default: + if (postData.text) { + options.body = postData.text; + } + } + + // If we ultimately don't have any headers to send then we shouldn't add an empty object into the request options. + if (options.headers && !Object.keys(options.headers).length) { + delete options.headers; + } + + push( + `const options = ${stringifyObject(options, { + indent: opts.indent, + inlineCharacterLimit: 80, + + // The Fetch API body only accepts string parameters, but stringified JSON can be difficult + // to read, so we keep the object as a literal and use this transform function to wrap the + // literal in a `JSON.stringify` call. + transform: (object, property, originalResult) => { + if (property === 'body') { + if (postData.mimeType === 'application/x-www-form-urlencoded') { + return `new URLSearchParams(${originalResult})`; + } else if (postData.mimeType === 'application/json') { + return `JSON.stringify(${originalResult})`; + } + } + + return originalResult; + }, + })};`, + ); + blank(); + + if (postData.params && postData.mimeType === 'multipart/form-data') { + push('options.body = form;'); + blank(); + } + + push(`fetch('${fullUrl}', options)`); + push('.then(response => response.json())', 1); + push('.then(response => console.log(response))', 1); + push('.catch(err => console.error(err));', 1); + + return join(); + }, +}; diff --git a/src/targets/node/fetch/fixtures/application-form-encoded.js b/src/targets/node/fetch/fixtures/application-form-encoded.js new file mode 100644 index 000000000..fd256737b --- /dev/null +++ b/src/targets/node/fetch/fixtures/application-form-encoded.js @@ -0,0 +1,10 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/x-www-form-urlencoded'}, + body: new URLSearchParams({foo: 'bar', hello: 'world'}) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/application-json.js b/src/targets/node/fetch/fixtures/application-json.js new file mode 100644 index 000000000..f79071c08 --- /dev/null +++ b/src/targets/node/fetch/fixtures/application-json.js @@ -0,0 +1,17 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/json'}, + body: JSON.stringify({ + number: 1, + string: 'f"oo', + arr: [1, 2, 3], + nested: {a: 'b'}, + arr_mix: [1, 'a', {arr_mix_nested: []}], + boolean: false + }) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/cookies.js b/src/targets/node/fetch/fixtures/cookies.js new file mode 100644 index 000000000..a9ba5766d --- /dev/null +++ b/src/targets/node/fetch/fixtures/cookies.js @@ -0,0 +1,6 @@ +const options = {method: 'GET', headers: {cookie: 'foo=bar; bar=baz'}}; + +fetch('https://httpbin.org/cookies', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/custom-method.js b/src/targets/node/fetch/fixtures/custom-method.js new file mode 100644 index 000000000..738405920 --- /dev/null +++ b/src/targets/node/fetch/fixtures/custom-method.js @@ -0,0 +1,6 @@ +const options = {method: 'PROPFIND'}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/full.js b/src/targets/node/fetch/fixtures/full.js new file mode 100644 index 000000000..3aee96390 --- /dev/null +++ b/src/targets/node/fetch/fixtures/full.js @@ -0,0 +1,14 @@ +const options = { + method: 'POST', + headers: { + cookie: 'foo=bar; bar=baz', + accept: 'application/json', + 'content-type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({foo: 'bar'}) +}; + +fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/headers.js b/src/targets/node/fetch/fixtures/headers.js new file mode 100644 index 000000000..6db2a5d5b --- /dev/null +++ b/src/targets/node/fetch/fixtures/headers.js @@ -0,0 +1,14 @@ +const options = { + method: 'GET', + headers: { + accept: 'application/json', + 'x-foo': 'Bar', + 'x-bar': 'Foo', + 'quoted-value': '"quoted" \'string\'' + } +}; + +fetch('https://httpbin.org/headers', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/http-insecure.js b/src/targets/node/fetch/fixtures/http-insecure.js new file mode 100644 index 000000000..c2624597f --- /dev/null +++ b/src/targets/node/fetch/fixtures/http-insecure.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('http://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-multiline.js b/src/targets/node/fetch/fixtures/jsonObj-multiline.js new file mode 100644 index 000000000..f2e9c2795 --- /dev/null +++ b/src/targets/node/fetch/fixtures/jsonObj-multiline.js @@ -0,0 +1,10 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/json'}, + body: JSON.stringify({foo: 'bar'}) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-null-value.js b/src/targets/node/fetch/fixtures/jsonObj-null-value.js new file mode 100644 index 000000000..b6b9ea049 --- /dev/null +++ b/src/targets/node/fetch/fixtures/jsonObj-null-value.js @@ -0,0 +1,10 @@ +const options = { + method: 'POST', + headers: {'content-type': 'application/json'}, + body: JSON.stringify({foo: null}) +}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-data.js b/src/targets/node/fetch/fixtures/multipart-data.js new file mode 100644 index 000000000..5cc8ddf85 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-data.js @@ -0,0 +1,12 @@ +const form = new FormData(); +form.append('foo', 'Hello World'); +form.append('bar', 'Bonjour le monde'); + +const options = {method: 'POST'}; + +options.body = form; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-file.js b/src/targets/node/fetch/fixtures/multipart-file.js new file mode 100644 index 000000000..11b0a6b05 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-file.js @@ -0,0 +1,11 @@ +const form = new FormData(); +form.append('foo', 'src/fixtures/files/hello.txt'); + +const options = {method: 'POST'}; + +options.body = form; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js new file mode 100644 index 000000000..b1318179e --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js @@ -0,0 +1,6 @@ +const options = {method: 'POST', headers: {'Content-Type': 'multipart/form-data'}}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data.js b/src/targets/node/fetch/fixtures/multipart-form-data.js new file mode 100644 index 000000000..90643fc61 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-form-data.js @@ -0,0 +1,11 @@ +const form = new FormData(); +form.append('foo', 'bar'); + +const options = {method: 'POST'}; + +options.body = form; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/nested.js b/src/targets/node/fetch/fixtures/nested.js new file mode 100644 index 000000000..8bcc084d2 --- /dev/null +++ b/src/targets/node/fetch/fixtures/nested.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/postdata-malformed.js b/src/targets/node/fetch/fixtures/postdata-malformed.js new file mode 100644 index 000000000..145b702c4 --- /dev/null +++ b/src/targets/node/fetch/fixtures/postdata-malformed.js @@ -0,0 +1,6 @@ +const options = {method: 'POST', headers: {'content-type': 'application/json'}}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query-encoded.js b/src/targets/node/fetch/fixtures/query-encoded.js new file mode 100644 index 000000000..6da5448bb --- /dev/null +++ b/src/targets/node/fetch/fixtures/query-encoded.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query.js b/src/targets/node/fetch/fixtures/query.js new file mode 100644 index 000000000..fe792dbe1 --- /dev/null +++ b/src/targets/node/fetch/fixtures/query.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/short.js b/src/targets/node/fetch/fixtures/short.js new file mode 100644 index 000000000..86cec7387 --- /dev/null +++ b/src/targets/node/fetch/fixtures/short.js @@ -0,0 +1,6 @@ +const options = {method: 'GET'}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/text-plain.js b/src/targets/node/fetch/fixtures/text-plain.js new file mode 100644 index 000000000..3ff4a6f81 --- /dev/null +++ b/src/targets/node/fetch/fixtures/text-plain.js @@ -0,0 +1,6 @@ +const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'}; + +fetch('https://httpbin.org/anything', options) + .then(response => response.json()) + .then(response => console.log(response)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/target.ts b/src/targets/node/target.ts index bc352bb00..d54f3464d 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -1,17 +1,19 @@ import type { Target } from '../index.js'; import { axios } from './axios/client.js'; +import { fetch } from './fetch/client.js'; import { unirest } from './unirest/client.js'; export const node: Target = { info: { key: 'node', title: 'Node.js', - default: 'axios', + default: 'fetch', cli: 'node %s', }, clientsById: { unirest, axios, + fetch, }, }; From 5b8317c96ce0be74732dd8697197fb6137ba397b Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Mon, 3 Jun 2024 12:45:31 -0700 Subject: [PATCH 12/12] test: update test snapshot --- src/helpers/__snapshots__/utils.test.ts.snap | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index 2b278f799..cc3f79623 100644 --- a/src/helpers/__snapshots__/utils.test.ts.snap +++ b/src/helpers/__snapshots__/utils.test.ts.snap @@ -193,8 +193,15 @@ exports[`availableTargets > returns all available targets 1`] = ` "link": "https://github.com/axios/axios", "title": "Axios", }, + { + "description": "Perform asynchronous HTTP requests with the Fetch API", + "extname": ".js", + "key": "fetch", + "link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch", + "title": "fetch", + }, ], - "default": "axios", + "default": "fetch", "key": "node", "title": "Node.js", },