From e6ac3a0d5fd5a43ae9cfef6fc6c64be681508847 Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Wed, 29 May 2024 11:12:09 -0700 Subject: [PATCH] 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, },