diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 55d46c5e9..b9bd5eb4d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,8 +13,9 @@ jobs: strategy: matrix: node-version: - - 18 - - 20 + - lts/-1 + - lts/* + - latest steps: - uses: actions/checkout@v4 diff --git a/.vscode/settings.json b/.vscode/settings.json index c256ef48a..90c7e0089 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,15 @@ { + "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.codeActionsOnSave": { "source.fixAll": "explicit" }, - "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.formatOnSave": true, // controlled by the .editorconfig at root since we can't map vscode settings directly to files // https://github.com/microsoft/vscode/issues/35350 - "files.insertFinalNewline": false + "files.insertFinalNewline": false, + + "search.exclude": { + "coverage": true + } } diff --git a/README.md b/README.md index 4380f04d1..3bb04fcc0 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,8 @@ console.log( }), ); -// generate Node.js: Unirest output -console.log(snippet.convert('node', 'unirest')); +// generate Node.js: Axios output +console.log(snippet.convert('node', 'axios')); ``` ### addTarget(target) diff --git a/integrations/node.Dockerfile b/integrations/node.Dockerfile index 22b91b0ce..f599f3082 100755 --- a/integrations/node.Dockerfile +++ b/integrations/node.Dockerfile @@ -13,10 +13,7 @@ WORKDIR /src ADD package.json /src/ # https://www.npmjs.com/package/axios -# https://www.npmjs.com/package/request -# Installing node-fetch@2 because as of 3.0 is't now an ESM-only package. -# https://www.npmjs.com/package/node-fetch -RUN npm install axios request node-fetch@2 && \ +RUN npm install axios && \ npm install ADD . /src diff --git a/package.json b/package.json index c76add9d0..e01bc45a5 100644 --- a/package.json +++ b/package.json @@ -53,14 +53,12 @@ "ocaml", "php", "python", - "request", "requests", "ruby", "shell", "snippet", "swift", "swift", - "unirest", "xhr", "xmlhttprequest" ], diff --git a/src/fixtures/customTarget.ts b/src/fixtures/customTarget.ts index 98f15fbe8..56c4029d4 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', + key: 'node-variant', + title: 'Node Variant', extname: '.js', - default: 'request', + default: 'axios', }, clientsById: { - request, + axios, }, } as unknown as Target; diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index cd0bf5934..446baba49 100644 --- a/src/helpers/__snapshots__/utils.test.ts.snap +++ b/src/helpers/__snapshots__/utils.test.ts.snap @@ -150,7 +150,7 @@ exports[`availableTargets > returns all available targets 1`] = ` "title": "jQuery", }, ], - "default": "xhr", + "default": "fetch", "key": "javascript", "title": "JavaScript", }, @@ -192,39 +192,23 @@ exports[`availableTargets > returns all available targets 1`] = ` "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", - "key": "unirest", - "link": "http://unirest.io/nodejs.html", - "title": "Unirest", - }, { "description": "Promise based HTTP client for the browser and node.js", - "extname": ".cjs", + "extname": ".js", "installation": "npm install axios --save", "key": "axios", "link": "https://github.com/axios/axios", "title": "Axios", }, { - "description": "Simplified HTTP node-fetch client", - "extname": ".cjs", - "installation": "npm install node-fetch@2 --save", + "description": "Perform asynchronous HTTP requests with the Fetch API", + "extname": ".js", "key": "fetch", - "link": "https://github.com/bitinn/node-fetch", - "title": "Fetch", + "link": "https://nodejs.org/docs/latest/api/globals.html#fetch", + "title": "fetch", }, ], - "default": "native", + "default": "fetch", "key": "node", "title": "Node.js", }, diff --git a/src/helpers/utils.test.ts b/src/helpers/utils.test.ts index e90337932..dfe75158a 100644 --- a/src/helpers/utils.test.ts +++ b/src/helpers/utils.test.ts @@ -22,7 +22,7 @@ describe('extname', () => { expect(extname('c', 'libcurl')).toBe('.c'); expect(extname('clojure', 'clj_http')).toBe('.clj'); expect(extname('javascript', 'axios')).toBe('.js'); - expect(extname('node', 'axios')).toBe('.cjs'); + expect(extname('node', 'axios')).toBe('.js'); }); it('returns empty string if the extension is not found', () => { diff --git a/src/integration.test.ts b/src/integration.test.ts index 41f126236..9bab58313 100644 --- a/src/integration.test.ts +++ b/src/integration.test.ts @@ -21,7 +21,7 @@ const ENVIRONMENT_CONFIG = { c: ['libcurl'], csharp: ['httpclient', 'restsharp'], go: ['native'], - node: ['axios', 'fetch', 'native', 'request'], + node: ['axios', 'fetch'], php: ['curl', 'guzzle'], python: ['requests'], shell: ['curl'], @@ -30,7 +30,7 @@ const ENVIRONMENT_CONFIG = { // When running tests locally, or within a CI environment, we shold limit the targets that // we're testing so as to not require a mess of dependency requirements that would be better // served within a container. - node: ['native'], + node: ['fetch'], php: ['curl'], python: ['requests'], shell: ['curl'], diff --git a/src/targets/index.test.ts b/src/targets/index.test.ts index 97eaade1f..15c936d64 100644 --- a/src/targets/index.test.ts +++ b/src/targets/index.test.ts @@ -31,7 +31,7 @@ const targetFilter: TargetId[] = [ /** useful for debuggin, only run a particular set of targets */ const clientFilter: ClientId[] = [ // put your clientId here: - // 'unirest', + // 'axios', ]; /** useful for debuggin, only run a particular set of fixtures */ diff --git a/src/targets/javascript/axios/client.ts b/src/targets/javascript/axios/client.ts index 493c12104..6dfbdc333 100644 --- a/src/targets/javascript/axios/client.ts +++ b/src/targets/javascript/axios/client.ts @@ -99,12 +99,8 @@ export const axios: Client = { push('axios'); push('.request(options)', 1); - push('.then(function (response) {', 1); - push('console.log(response.data);', 2); - push('})', 1); - push('.catch(function (error) {', 1); - push('console.error(error);', 2); - push('});', 1); + push('.then(res => console.log(res.data))', 1); + push('.catch(err => console.error(err));', 1); return join(); }, diff --git a/src/targets/javascript/axios/fixtures/application-form-encoded.js b/src/targets/javascript/axios/fixtures/application-form-encoded.js index 7a55fffc3..1a83ea542 100644 --- a/src/targets/javascript/axios/fixtures/application-form-encoded.js +++ b/src/targets/javascript/axios/fixtures/application-form-encoded.js @@ -13,9 +13,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/application-json.js b/src/targets/javascript/axios/fixtures/application-json.js index 999da16cb..98903a65a 100644 --- a/src/targets/javascript/axios/fixtures/application-json.js +++ b/src/targets/javascript/axios/fixtures/application-json.js @@ -16,9 +16,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/cookies.js b/src/targets/javascript/axios/fixtures/cookies.js index 4d0e356da..7e9cf7ae3 100644 --- a/src/targets/javascript/axios/fixtures/cookies.js +++ b/src/targets/javascript/axios/fixtures/cookies.js @@ -8,9 +8,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/custom-method.js b/src/targets/javascript/axios/fixtures/custom-method.js index c5e3af26a..4142f5977 100644 --- a/src/targets/javascript/axios/fixtures/custom-method.js +++ b/src/targets/javascript/axios/fixtures/custom-method.js @@ -4,9 +4,5 @@ const options = {method: 'PROPFIND', url: 'https://httpbin.org/anything'}; axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/full.js b/src/targets/javascript/axios/fixtures/full.js index ae9dcb0d5..014bd7340 100644 --- a/src/targets/javascript/axios/fixtures/full.js +++ b/src/targets/javascript/axios/fixtures/full.js @@ -17,9 +17,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/headers.js b/src/targets/javascript/axios/fixtures/headers.js index cbdbcb4c8..8026a1ee9 100644 --- a/src/targets/javascript/axios/fixtures/headers.js +++ b/src/targets/javascript/axios/fixtures/headers.js @@ -13,9 +13,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/http-insecure.js b/src/targets/javascript/axios/fixtures/http-insecure.js index cd424b513..0512e2df7 100644 --- a/src/targets/javascript/axios/fixtures/http-insecure.js +++ b/src/targets/javascript/axios/fixtures/http-insecure.js @@ -4,9 +4,5 @@ const options = {method: 'GET', url: 'http://httpbin.org/anything'}; axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/jsonObj-multiline.js b/src/targets/javascript/axios/fixtures/jsonObj-multiline.js index 867e8b396..7d41fbc52 100644 --- a/src/targets/javascript/axios/fixtures/jsonObj-multiline.js +++ b/src/targets/javascript/axios/fixtures/jsonObj-multiline.js @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/jsonObj-null-value.js b/src/targets/javascript/axios/fixtures/jsonObj-null-value.js index 06d04de34..3731d644d 100644 --- a/src/targets/javascript/axios/fixtures/jsonObj-null-value.js +++ b/src/targets/javascript/axios/fixtures/jsonObj-null-value.js @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/multipart-data.js b/src/targets/javascript/axios/fixtures/multipart-data.js index 5c620c764..bb36ba381 100644 --- a/src/targets/javascript/axios/fixtures/multipart-data.js +++ b/src/targets/javascript/axios/fixtures/multipart-data.js @@ -13,9 +13,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/multipart-file.js b/src/targets/javascript/axios/fixtures/multipart-file.js index 30e222583..6579bbe8c 100644 --- a/src/targets/javascript/axios/fixtures/multipart-file.js +++ b/src/targets/javascript/axios/fixtures/multipart-file.js @@ -12,9 +12,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/multipart-form-data-no-params.js b/src/targets/javascript/axios/fixtures/multipart-form-data-no-params.js index 28b915fec..57e424c87 100644 --- a/src/targets/javascript/axios/fixtures/multipart-form-data-no-params.js +++ b/src/targets/javascript/axios/fixtures/multipart-form-data-no-params.js @@ -8,9 +8,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/multipart-form-data.js b/src/targets/javascript/axios/fixtures/multipart-form-data.js index 61e9870f1..ec40b9e54 100644 --- a/src/targets/javascript/axios/fixtures/multipart-form-data.js +++ b/src/targets/javascript/axios/fixtures/multipart-form-data.js @@ -12,9 +12,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/nested.js b/src/targets/javascript/axios/fixtures/nested.js index e9d270e1c..3fffb9755 100644 --- a/src/targets/javascript/axios/fixtures/nested.js +++ b/src/targets/javascript/axios/fixtures/nested.js @@ -8,9 +8,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/postdata-malformed.js b/src/targets/javascript/axios/fixtures/postdata-malformed.js index 6e7eb1676..f40deb9ed 100644 --- a/src/targets/javascript/axios/fixtures/postdata-malformed.js +++ b/src/targets/javascript/axios/fixtures/postdata-malformed.js @@ -8,9 +8,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/query-encoded.js b/src/targets/javascript/axios/fixtures/query-encoded.js index 1090af9c1..489c9927e 100644 --- a/src/targets/javascript/axios/fixtures/query-encoded.js +++ b/src/targets/javascript/axios/fixtures/query-encoded.js @@ -11,9 +11,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/query.js b/src/targets/javascript/axios/fixtures/query.js index e08494626..39cca5992 100644 --- a/src/targets/javascript/axios/fixtures/query.js +++ b/src/targets/javascript/axios/fixtures/query.js @@ -8,9 +8,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/short.js b/src/targets/javascript/axios/fixtures/short.js index ec03ac010..ba835ded4 100644 --- a/src/targets/javascript/axios/fixtures/short.js +++ b/src/targets/javascript/axios/fixtures/short.js @@ -4,9 +4,5 @@ const options = {method: 'GET', url: 'https://httpbin.org/anything'}; axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/axios/fixtures/text-plain.js b/src/targets/javascript/axios/fixtures/text-plain.js index c212a3157..dbe78d903 100644 --- a/src/targets/javascript/axios/fixtures/text-plain.js +++ b/src/targets/javascript/axios/fixtures/text-plain.js @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/client.ts b/src/targets/javascript/fetch/client.ts index 9db46a397..6c9287221 100644 --- a/src/targets/javascript/fetch/client.ts +++ b/src/targets/javascript/fetch/client.ts @@ -121,8 +121,8 @@ export const fetch: Client = { } push(`fetch('${fullUrl}', options)`); - push('.then(response => response.json())', 1); - push('.then(response => console.log(response))', 1); + push('.then(res => res.json())', 1); + push('.then(res => console.log(res))', 1); push('.catch(err => console.error(err));', 1); return join(); diff --git a/src/targets/javascript/fetch/fixtures/application-form-encoded.js b/src/targets/javascript/fetch/fixtures/application-form-encoded.js index fd256737b..4e0d61445 100644 --- a/src/targets/javascript/fetch/fixtures/application-form-encoded.js +++ b/src/targets/javascript/fetch/fixtures/application-form-encoded.js @@ -5,6 +5,6 @@ const options = { }; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/application-json.js b/src/targets/javascript/fetch/fixtures/application-json.js index f79071c08..49568ebfb 100644 --- a/src/targets/javascript/fetch/fixtures/application-json.js +++ b/src/targets/javascript/fetch/fixtures/application-json.js @@ -12,6 +12,6 @@ const options = { }; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/cookies.js b/src/targets/javascript/fetch/fixtures/cookies.js index a9ba5766d..ba1fb515c 100644 --- a/src/targets/javascript/fetch/fixtures/cookies.js +++ b/src/targets/javascript/fetch/fixtures/cookies.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/custom-method.js b/src/targets/javascript/fetch/fixtures/custom-method.js index 738405920..102f9b51e 100644 --- a/src/targets/javascript/fetch/fixtures/custom-method.js +++ b/src/targets/javascript/fetch/fixtures/custom-method.js @@ -1,6 +1,6 @@ const options = {method: 'PROPFIND'}; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/full.js b/src/targets/javascript/fetch/fixtures/full.js index 3aee96390..b26902bb5 100644 --- a/src/targets/javascript/fetch/fixtures/full.js +++ b/src/targets/javascript/fetch/fixtures/full.js @@ -9,6 +9,6 @@ const options = { }; fetch('https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/headers.js b/src/targets/javascript/fetch/fixtures/headers.js index 6db2a5d5b..3ca72a640 100644 --- a/src/targets/javascript/fetch/fixtures/headers.js +++ b/src/targets/javascript/fetch/fixtures/headers.js @@ -9,6 +9,6 @@ const options = { }; fetch('https://httpbin.org/headers', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/http-insecure.js b/src/targets/javascript/fetch/fixtures/http-insecure.js index c2624597f..60ada7617 100644 --- a/src/targets/javascript/fetch/fixtures/http-insecure.js +++ b/src/targets/javascript/fetch/fixtures/http-insecure.js @@ -1,6 +1,6 @@ const options = {method: 'GET'}; fetch('http://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js b/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js index f2e9c2795..fc681292a 100644 --- a/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js +++ b/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js @@ -5,6 +5,6 @@ const options = { }; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js b/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js index b6b9ea049..305eed6b6 100644 --- a/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js +++ b/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js @@ -5,6 +5,6 @@ const options = { }; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-data.js b/src/targets/javascript/fetch/fixtures/multipart-data.js index 5cc8ddf85..ac8664bf9 100644 --- a/src/targets/javascript/fetch/fixtures/multipart-data.js +++ b/src/targets/javascript/fetch/fixtures/multipart-data.js @@ -7,6 +7,6 @@ const options = {method: 'POST'}; options.body = form; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-file.js b/src/targets/javascript/fetch/fixtures/multipart-file.js index 11b0a6b05..039019a1c 100644 --- a/src/targets/javascript/fetch/fixtures/multipart-file.js +++ b/src/targets/javascript/fetch/fixtures/multipart-file.js @@ -6,6 +6,6 @@ const options = {method: 'POST'}; options.body = form; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js b/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js index b1318179e..4b79e19a6 100644 --- a/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js +++ b/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-form-data.js b/src/targets/javascript/fetch/fixtures/multipart-form-data.js index 90643fc61..7a21714a4 100644 --- a/src/targets/javascript/fetch/fixtures/multipart-form-data.js +++ b/src/targets/javascript/fetch/fixtures/multipart-form-data.js @@ -6,6 +6,6 @@ const options = {method: 'POST'}; options.body = form; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/nested.js b/src/targets/javascript/fetch/fixtures/nested.js index 8bcc084d2..97d251384 100644 --- a/src/targets/javascript/fetch/fixtures/nested.js +++ b/src/targets/javascript/fetch/fixtures/nested.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/postdata-malformed.js b/src/targets/javascript/fetch/fixtures/postdata-malformed.js index 145b702c4..94f81b0a4 100644 --- a/src/targets/javascript/fetch/fixtures/postdata-malformed.js +++ b/src/targets/javascript/fetch/fixtures/postdata-malformed.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/query-encoded.js b/src/targets/javascript/fetch/fixtures/query-encoded.js index 6da5448bb..14b44cf6c 100644 --- a/src/targets/javascript/fetch/fixtures/query-encoded.js +++ b/src/targets/javascript/fetch/fixtures/query-encoded.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/query.js b/src/targets/javascript/fetch/fixtures/query.js index fe792dbe1..22d68dcd3 100644 --- a/src/targets/javascript/fetch/fixtures/query.js +++ b/src/targets/javascript/fetch/fixtures/query.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/short.js b/src/targets/javascript/fetch/fixtures/short.js index 86cec7387..68ab8947d 100644 --- a/src/targets/javascript/fetch/fixtures/short.js +++ b/src/targets/javascript/fetch/fixtures/short.js @@ -1,6 +1,6 @@ const options = {method: 'GET'}; fetch('https://httpbin.org/anything', options) - .then(response => response.json()) - .then(response => console.log(response)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/text-plain.js b/src/targets/javascript/fetch/fixtures/text-plain.js index 3ff4a6f81..5a83b05da 100644 --- a/src/targets/javascript/fetch/fixtures/text-plain.js +++ b/src/targets/javascript/fetch/fixtures/text-plain.js @@ -1,6 +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)) + .then(res => res.json()) + .then(res => console.log(res)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/jquery/client.ts b/src/targets/javascript/jquery/client.ts index 5eb246e00..dbd9d3e7d 100644 --- a/src/targets/javascript/jquery/client.ts +++ b/src/targets/javascript/jquery/client.ts @@ -87,8 +87,8 @@ export const jquery: Client = { push(`const settings = ${stringifiedSettings};`); blank(); - push('$.ajax(settings).done(function (response) {'); - push('console.log(response);', 1); + push('$.ajax(settings).done(res => {'); + push('console.log(res);', 1); push('});'); return join(); diff --git a/src/targets/javascript/jquery/fixtures/application-form-encoded.js b/src/targets/javascript/jquery/fixtures/application-form-encoded.js index c3084f07a..fd6020417 100644 --- a/src/targets/javascript/jquery/fixtures/application-form-encoded.js +++ b/src/targets/javascript/jquery/fixtures/application-form-encoded.js @@ -12,6 +12,6 @@ const settings = { } }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/application-json.js b/src/targets/javascript/jquery/fixtures/application-json.js index 740cf3fa0..0599fa68a 100644 --- a/src/targets/javascript/jquery/fixtures/application-json.js +++ b/src/targets/javascript/jquery/fixtures/application-json.js @@ -10,6 +10,6 @@ const settings = { data: '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":[]}],"boolean":false}' }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/cookies.js b/src/targets/javascript/jquery/fixtures/cookies.js index 5ba967b46..18899ddd8 100644 --- a/src/targets/javascript/jquery/fixtures/cookies.js +++ b/src/targets/javascript/jquery/fixtures/cookies.js @@ -8,6 +8,6 @@ const settings = { } }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/custom-method.js b/src/targets/javascript/jquery/fixtures/custom-method.js index 6f48d72c4..5131ee1db 100644 --- a/src/targets/javascript/jquery/fixtures/custom-method.js +++ b/src/targets/javascript/jquery/fixtures/custom-method.js @@ -6,6 +6,6 @@ const settings = { headers: {} }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/full.js b/src/targets/javascript/jquery/fixtures/full.js index 32649fb75..a92c8867f 100644 --- a/src/targets/javascript/jquery/fixtures/full.js +++ b/src/targets/javascript/jquery/fixtures/full.js @@ -13,6 +13,6 @@ const settings = { } }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/headers.js b/src/targets/javascript/jquery/fixtures/headers.js index 7b2fc32bf..f482429b7 100644 --- a/src/targets/javascript/jquery/fixtures/headers.js +++ b/src/targets/javascript/jquery/fixtures/headers.js @@ -11,6 +11,6 @@ const settings = { } }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/http-insecure.js b/src/targets/javascript/jquery/fixtures/http-insecure.js index 63d557e39..bcc14a5f3 100644 --- a/src/targets/javascript/jquery/fixtures/http-insecure.js +++ b/src/targets/javascript/jquery/fixtures/http-insecure.js @@ -6,6 +6,6 @@ const settings = { headers: {} }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/jsonObj-multiline.js b/src/targets/javascript/jquery/fixtures/jsonObj-multiline.js index c1fbeae46..361dfd3c7 100644 --- a/src/targets/javascript/jquery/fixtures/jsonObj-multiline.js +++ b/src/targets/javascript/jquery/fixtures/jsonObj-multiline.js @@ -10,6 +10,6 @@ const settings = { data: '{\n "foo": "bar"\n}' }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/jsonObj-null-value.js b/src/targets/javascript/jquery/fixtures/jsonObj-null-value.js index bdb8f7b89..682bf2726 100644 --- a/src/targets/javascript/jquery/fixtures/jsonObj-null-value.js +++ b/src/targets/javascript/jquery/fixtures/jsonObj-null-value.js @@ -10,6 +10,6 @@ const settings = { data: '{"foo":null}' }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/multipart-data.js b/src/targets/javascript/jquery/fixtures/multipart-data.js index aab34a995..48c0ffc10 100644 --- a/src/targets/javascript/jquery/fixtures/multipart-data.js +++ b/src/targets/javascript/jquery/fixtures/multipart-data.js @@ -14,6 +14,6 @@ const settings = { data: form }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/multipart-file.js b/src/targets/javascript/jquery/fixtures/multipart-file.js index 20fb8e2dc..43aba5379 100644 --- a/src/targets/javascript/jquery/fixtures/multipart-file.js +++ b/src/targets/javascript/jquery/fixtures/multipart-file.js @@ -13,6 +13,6 @@ const settings = { data: form }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/multipart-form-data-no-params.js b/src/targets/javascript/jquery/fixtures/multipart-form-data-no-params.js index 78c9ea809..da5946035 100644 --- a/src/targets/javascript/jquery/fixtures/multipart-form-data-no-params.js +++ b/src/targets/javascript/jquery/fixtures/multipart-form-data-no-params.js @@ -8,6 +8,6 @@ const settings = { } }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/multipart-form-data.js b/src/targets/javascript/jquery/fixtures/multipart-form-data.js index 0c3c85697..68fde42fb 100644 --- a/src/targets/javascript/jquery/fixtures/multipart-form-data.js +++ b/src/targets/javascript/jquery/fixtures/multipart-form-data.js @@ -13,6 +13,6 @@ const settings = { data: form }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/nested.js b/src/targets/javascript/jquery/fixtures/nested.js index 74cb5dc94..4aac81436 100644 --- a/src/targets/javascript/jquery/fixtures/nested.js +++ b/src/targets/javascript/jquery/fixtures/nested.js @@ -6,6 +6,6 @@ const settings = { headers: {} }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/postdata-malformed.js b/src/targets/javascript/jquery/fixtures/postdata-malformed.js index 7caf9328f..a588c826c 100644 --- a/src/targets/javascript/jquery/fixtures/postdata-malformed.js +++ b/src/targets/javascript/jquery/fixtures/postdata-malformed.js @@ -8,6 +8,6 @@ const settings = { } }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/query-encoded.js b/src/targets/javascript/jquery/fixtures/query-encoded.js index f65186c31..cf871a707 100644 --- a/src/targets/javascript/jquery/fixtures/query-encoded.js +++ b/src/targets/javascript/jquery/fixtures/query-encoded.js @@ -6,6 +6,6 @@ const settings = { headers: {} }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/query.js b/src/targets/javascript/jquery/fixtures/query.js index fd9713a43..9d81c66db 100644 --- a/src/targets/javascript/jquery/fixtures/query.js +++ b/src/targets/javascript/jquery/fixtures/query.js @@ -6,6 +6,6 @@ const settings = { headers: {} }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/short.js b/src/targets/javascript/jquery/fixtures/short.js index a55f7bb88..0aa030eef 100644 --- a/src/targets/javascript/jquery/fixtures/short.js +++ b/src/targets/javascript/jquery/fixtures/short.js @@ -6,6 +6,6 @@ const settings = { headers: {} }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/jquery/fixtures/text-plain.js b/src/targets/javascript/jquery/fixtures/text-plain.js index 15932a19c..d99636080 100644 --- a/src/targets/javascript/jquery/fixtures/text-plain.js +++ b/src/targets/javascript/jquery/fixtures/text-plain.js @@ -9,6 +9,6 @@ const settings = { data: 'Hello World' }; -$.ajax(settings).done(function (response) { - console.log(response); +$.ajax(settings).done(res => { + console.log(res); }); \ No newline at end of file diff --git a/src/targets/javascript/target.ts b/src/targets/javascript/target.ts index 116c26c4e..6c58572cb 100644 --- a/src/targets/javascript/target.ts +++ b/src/targets/javascript/target.ts @@ -9,7 +9,7 @@ export const javascript: Target = { info: { key: 'javascript', title: 'JavaScript', - default: 'xhr', + default: 'fetch', }, clientsById: { diff --git a/src/targets/node/axios/client.ts b/src/targets/node/axios/client.ts index fd71e74c3..193b528e1 100644 --- a/src/targets/node/axios/client.ts +++ b/src/targets/node/axios/client.ts @@ -19,7 +19,7 @@ export const axios: Client = { title: 'Axios', link: 'https://github.com/axios/axios', description: 'Promise based HTTP client for the browser and node.js', - extname: '.cjs', + extname: '.js', installation: 'npm install axios --save', }, convert: ({ method, fullUrl, allHeaders, postData }, options) => { @@ -29,7 +29,8 @@ export const axios: Client = { }; const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent }); - push("const axios = require('axios');"); + push("import axios from 'axios';"); + blank(); const reqOpts: Record = { method, @@ -43,9 +44,6 @@ export const axios: Client = { switch (postData.mimeType) { case 'application/x-www-form-urlencoded': if (postData.params) { - push("const { URLSearchParams } = require('url');"); - blank(); - push('const encodedParams = new URLSearchParams();'); postData.params.forEach(param => { push(`encodedParams.set('${param.name}', '${param.value}');`); @@ -60,14 +58,12 @@ export const axios: Client = { break; case 'application/json': - blank(); if (postData.jsonObj) { reqOpts.data = postData.jsonObj; } break; default: - blank(); if (postData.text) { reqOpts.data = postData.text; } @@ -79,12 +75,8 @@ export const axios: Client = { push('axios'); push('.request(options)', 1); - push('.then(function (response) {', 1); - push('console.log(response.data);', 2); - push('})', 1); - push('.catch(function (error) {', 1); - push('console.error(error);', 2); - push('});', 1); + push('.then(res => console.log(res.data))', 1); + push('.catch(err => console.error(err));', 1); return join(); }, diff --git a/src/targets/node/axios/fixtures/application-form-encoded.cjs b/src/targets/node/axios/fixtures/application-form-encoded.js similarity index 60% rename from src/targets/node/axios/fixtures/application-form-encoded.cjs rename to src/targets/node/axios/fixtures/application-form-encoded.js index 2d3298955..1a83ea542 100644 --- a/src/targets/node/axios/fixtures/application-form-encoded.cjs +++ b/src/targets/node/axios/fixtures/application-form-encoded.js @@ -1,5 +1,4 @@ -const axios = require('axios'); -const { URLSearchParams } = require('url'); +import axios from 'axios'; const encodedParams = new URLSearchParams(); encodedParams.set('foo', 'bar'); @@ -14,9 +13,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/application-json.cjs b/src/targets/node/axios/fixtures/application-json.js similarity index 66% rename from src/targets/node/axios/fixtures/application-json.cjs rename to src/targets/node/axios/fixtures/application-json.js index fc3d0b13a..98903a65a 100644 --- a/src/targets/node/axios/fixtures/application-json.cjs +++ b/src/targets/node/axios/fixtures/application-json.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -16,9 +16,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/cookies.cjs b/src/targets/node/axios/fixtures/cookies.cjs deleted file mode 100644 index e1a046321..000000000 --- a/src/targets/node/axios/fixtures/cookies.cjs +++ /dev/null @@ -1,16 +0,0 @@ -const axios = require('axios'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/cookies', - headers: {cookie: 'foo=bar; bar=baz'} -}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/cookies.js b/src/targets/node/axios/fixtures/cookies.js new file mode 100644 index 000000000..7e9cf7ae3 --- /dev/null +++ b/src/targets/node/axios/fixtures/cookies.js @@ -0,0 +1,12 @@ +import axios from 'axios'; + +const options = { + method: 'GET', + url: 'https://httpbin.org/cookies', + headers: {cookie: 'foo=bar; bar=baz'} +}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/custom-method.cjs b/src/targets/node/axios/fixtures/custom-method.cjs deleted file mode 100644 index 795827399..000000000 --- a/src/targets/node/axios/fixtures/custom-method.cjs +++ /dev/null @@ -1,12 +0,0 @@ -const axios = require('axios'); - -const options = {method: 'PROPFIND', url: 'https://httpbin.org/anything'}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/custom-method.js b/src/targets/node/axios/fixtures/custom-method.js new file mode 100644 index 000000000..4142f5977 --- /dev/null +++ b/src/targets/node/axios/fixtures/custom-method.js @@ -0,0 +1,8 @@ +import axios from 'axios'; + +const options = {method: 'PROPFIND', url: 'https://httpbin.org/anything'}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/full.cjs b/src/targets/node/axios/fixtures/full.js similarity index 65% rename from src/targets/node/axios/fixtures/full.cjs rename to src/targets/node/axios/fixtures/full.js index 90c039479..fce011d95 100644 --- a/src/targets/node/axios/fixtures/full.cjs +++ b/src/targets/node/axios/fixtures/full.js @@ -1,5 +1,4 @@ -const axios = require('axios'); -const { URLSearchParams } = require('url'); +import axios from 'axios'; const encodedParams = new URLSearchParams(); encodedParams.set('foo', 'bar'); @@ -17,9 +16,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/headers.cjs b/src/targets/node/axios/fixtures/headers.js similarity index 59% rename from src/targets/node/axios/fixtures/headers.cjs rename to src/targets/node/axios/fixtures/headers.js index 1f129cdd6..8026a1ee9 100644 --- a/src/targets/node/axios/fixtures/headers.cjs +++ b/src/targets/node/axios/fixtures/headers.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'GET', @@ -13,9 +13,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/http-insecure.cjs b/src/targets/node/axios/fixtures/http-insecure.cjs deleted file mode 100644 index 66a197661..000000000 --- a/src/targets/node/axios/fixtures/http-insecure.cjs +++ /dev/null @@ -1,12 +0,0 @@ -const axios = require('axios'); - -const options = {method: 'GET', url: 'http://httpbin.org/anything'}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/http-insecure.js b/src/targets/node/axios/fixtures/http-insecure.js new file mode 100644 index 000000000..0512e2df7 --- /dev/null +++ b/src/targets/node/axios/fixtures/http-insecure.js @@ -0,0 +1,8 @@ +import axios from 'axios'; + +const options = {method: 'GET', url: 'http://httpbin.org/anything'}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/jsonObj-multiline.cjs b/src/targets/node/axios/fixtures/jsonObj-multiline.js similarity index 52% rename from src/targets/node/axios/fixtures/jsonObj-multiline.cjs rename to src/targets/node/axios/fixtures/jsonObj-multiline.js index 6a02916c2..7d41fbc52 100644 --- a/src/targets/node/axios/fixtures/jsonObj-multiline.cjs +++ b/src/targets/node/axios/fixtures/jsonObj-multiline.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/jsonObj-null-value.cjs b/src/targets/node/axios/fixtures/jsonObj-null-value.js similarity index 52% rename from src/targets/node/axios/fixtures/jsonObj-null-value.cjs rename to src/targets/node/axios/fixtures/jsonObj-null-value.js index ba03201b7..3731d644d 100644 --- a/src/targets/node/axios/fixtures/jsonObj-null-value.cjs +++ b/src/targets/node/axios/fixtures/jsonObj-null-value.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/multipart-data.cjs b/src/targets/node/axios/fixtures/multipart-data.js similarity index 76% rename from src/targets/node/axios/fixtures/multipart-data.cjs rename to src/targets/node/axios/fixtures/multipart-data.js index f2268d0e5..d8672a082 100644 --- a/src/targets/node/axios/fixtures/multipart-data.cjs +++ b/src/targets/node/axios/fixtures/multipart-data.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/multipart-file.cjs b/src/targets/node/axios/fixtures/multipart-file.js similarity index 71% rename from src/targets/node/axios/fixtures/multipart-file.cjs rename to src/targets/node/axios/fixtures/multipart-file.js index 48e0d0162..8f53fc1eb 100644 --- a/src/targets/node/axios/fixtures/multipart-file.cjs +++ b/src/targets/node/axios/fixtures/multipart-file.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/multipart-form-data-no-params.cjs b/src/targets/node/axios/fixtures/multipart-form-data-no-params.cjs deleted file mode 100644 index 6b8c2ec58..000000000 --- a/src/targets/node/axios/fixtures/multipart-form-data-no-params.cjs +++ /dev/null @@ -1,16 +0,0 @@ -const axios = require('axios'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'Content-Type': 'multipart/form-data'} -}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/multipart-form-data-no-params.js b/src/targets/node/axios/fixtures/multipart-form-data-no-params.js new file mode 100644 index 000000000..57e424c87 --- /dev/null +++ b/src/targets/node/axios/fixtures/multipart-form-data-no-params.js @@ -0,0 +1,12 @@ +import axios from 'axios'; + +const options = { + method: 'POST', + url: 'https://httpbin.org/anything', + headers: {'Content-Type': 'multipart/form-data'} +}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/multipart-form-data.cjs b/src/targets/node/axios/fixtures/multipart-form-data.js similarity index 67% rename from src/targets/node/axios/fixtures/multipart-form-data.cjs rename to src/targets/node/axios/fixtures/multipart-form-data.js index 15a56e506..45f7a10fb 100644 --- a/src/targets/node/axios/fixtures/multipart-form-data.cjs +++ b/src/targets/node/axios/fixtures/multipart-form-data.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/nested.cjs b/src/targets/node/axios/fixtures/nested.cjs deleted file mode 100644 index 11e4065d2..000000000 --- a/src/targets/node/axios/fixtures/nested.cjs +++ /dev/null @@ -1,15 +0,0 @@ -const axios = require('axios'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value' -}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/nested.js b/src/targets/node/axios/fixtures/nested.js new file mode 100644 index 000000000..62373db83 --- /dev/null +++ b/src/targets/node/axios/fixtures/nested.js @@ -0,0 +1,11 @@ +import axios from 'axios'; + +const options = { + method: 'GET', + url: 'https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value' +}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/postdata-malformed.cjs b/src/targets/node/axios/fixtures/postdata-malformed.cjs deleted file mode 100644 index 803140cec..000000000 --- a/src/targets/node/axios/fixtures/postdata-malformed.cjs +++ /dev/null @@ -1,16 +0,0 @@ -const axios = require('axios'); - -const options = { - method: 'POST', - url: 'https://httpbin.org/anything', - headers: {'content-type': 'application/json'} -}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/postdata-malformed.js b/src/targets/node/axios/fixtures/postdata-malformed.js new file mode 100644 index 000000000..f40deb9ed --- /dev/null +++ b/src/targets/node/axios/fixtures/postdata-malformed.js @@ -0,0 +1,12 @@ +import axios from 'axios'; + +const options = { + method: 'POST', + url: 'https://httpbin.org/anything', + headers: {'content-type': 'application/json'} +}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/query-encoded.cjs b/src/targets/node/axios/fixtures/query-encoded.js similarity index 53% rename from src/targets/node/axios/fixtures/query-encoded.cjs rename to src/targets/node/axios/fixtures/query-encoded.js index 7d0d03100..c53a743a0 100644 --- a/src/targets/node/axios/fixtures/query-encoded.cjs +++ b/src/targets/node/axios/fixtures/query-encoded.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'GET', @@ -7,9 +7,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/query.cjs b/src/targets/node/axios/fixtures/query.cjs deleted file mode 100644 index eb7240ebd..000000000 --- a/src/targets/node/axios/fixtures/query.cjs +++ /dev/null @@ -1,15 +0,0 @@ -const axios = require('axios'); - -const options = { - method: 'GET', - url: 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value' -}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/query.js b/src/targets/node/axios/fixtures/query.js new file mode 100644 index 000000000..7833a75b3 --- /dev/null +++ b/src/targets/node/axios/fixtures/query.js @@ -0,0 +1,11 @@ +import axios from 'axios'; + +const options = { + method: 'GET', + url: 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value' +}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/short.cjs b/src/targets/node/axios/fixtures/short.cjs deleted file mode 100644 index 3741d4555..000000000 --- a/src/targets/node/axios/fixtures/short.cjs +++ /dev/null @@ -1,12 +0,0 @@ -const axios = require('axios'); - -const options = {method: 'GET', url: 'https://httpbin.org/anything'}; - -axios - .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/short.js b/src/targets/node/axios/fixtures/short.js new file mode 100644 index 000000000..ba835ded4 --- /dev/null +++ b/src/targets/node/axios/fixtures/short.js @@ -0,0 +1,8 @@ +import axios from 'axios'; + +const options = {method: 'GET', url: 'https://httpbin.org/anything'}; + +axios + .request(options) + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/axios/fixtures/text-plain.cjs b/src/targets/node/axios/fixtures/text-plain.js similarity index 51% rename from src/targets/node/axios/fixtures/text-plain.cjs rename to src/targets/node/axios/fixtures/text-plain.js index 2ddf6191c..dbe78d903 100644 --- a/src/targets/node/axios/fixtures/text-plain.cjs +++ b/src/targets/node/axios/fixtures/text-plain.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +import axios from 'axios'; const options = { method: 'POST', @@ -9,9 +9,5 @@ const options = { axios .request(options) - .then(function (response) { - console.log(response.data); - }) - .catch(function (error) { - console.error(error); - }); \ No newline at end of file + .then(res => console.log(res.data)) + .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/node/fetch/client.ts b/src/targets/node/fetch/client.ts index c8f1b76c6..c3d52f456 100644 --- a/src/targets/node/fetch/client.ts +++ b/src/targets/node/fetch/client.ts @@ -1,12 +1,3 @@ -/** - * @description - * HTTP code snippet generator for Node.js using node-fetch. - * - * @author - * @hirenoble - * - * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. - */ import type { Client } from '../../index.js'; import stringifyObject from 'stringify-object'; @@ -17,11 +8,10 @@ import { getHeaderName } from '../../../helpers/headers.js'; export const fetch: Client = { info: { key: 'fetch', - title: 'Fetch', - link: 'https://github.com/bitinn/node-fetch', - description: 'Simplified HTTP node-fetch client', - extname: '.cjs', - installation: 'npm install node-fetch@2 --save', + title: 'fetch', + link: 'https://nodejs.org/docs/latest/api/globals.html#fetch', + description: 'Perform asynchronous HTTP requests with the Fetch API', + extname: '.js', }, convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => { const opts = { @@ -32,7 +22,6 @@ export const fetch: Client = { let includeFS = false; const { blank, push, join, unshift } = new CodeBuilder({ indent: opts.indent }); - push("const fetch = require('node-fetch');"); const url = fullUrl; const reqOpts: Record = { method, @@ -44,15 +33,14 @@ export const fetch: Client = { switch (postData.mimeType) { case 'application/x-www-form-urlencoded': - unshift("const { URLSearchParams } = require('url');"); push('const encodedParams = new URLSearchParams();'); - blank(); postData.params?.forEach(param => { push(`encodedParams.set('${param.name}', '${param.value}');`); }); reqOpts.body = 'encodedParams'; + blank(); break; case 'application/json': @@ -68,16 +56,14 @@ export const fetch: Client = { break; } - // The `form-data` module automatically adds a `Content-Type` header for `multipart/form-data` content and if we add our own here data won't be correctly transmitted. + // The FormData API automatically adds a `Content-Type` header for `multipart/form-data` content and if we add our own here data won't be correctly transmitted. // eslint-disable-next-line no-case-declarations -- We're only using `contentTypeHeader` within this block. const contentTypeHeader = getHeaderName(headersObj, 'content-type'); if (contentTypeHeader) { delete headersObj[contentTypeHeader]; } - unshift("const FormData = require('form-data');"); push('const formData = new FormData();'); - blank(); postData.params.forEach(param => { if (!param.fileName && !param.fileName && !param.contentType) { @@ -87,9 +73,17 @@ export const fetch: Client = { if (param.fileName) { includeFS = true; - push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`); + + // Whenever we drop support for Node 18 we can change this blob work to use + // `fs.openAsBlob('filename')`. + push( + `formData.append('${param.name}', await new Response(fs.createReadStream('${param.fileName}')).blob());`, + ); } }); + + reqOpts.body = 'formData'; + blank(); break; default: @@ -110,7 +104,7 @@ export const fetch: Client = { reqOpts.headers.cookie = cookiesString; } } - blank(); + push(`const url = '${url}';`); // If we ultimately don't have any headers to send then we shouldn't add an empty object into the request options. @@ -137,19 +131,16 @@ export const fetch: Client = { blank(); if (includeFS) { - unshift("const fs = require('fs');"); - } - if (postData.params && postData.mimeType === 'multipart/form-data') { - push('options.body = formData;'); - blank(); + unshift("import fs from 'fs';\n"); } + push('fetch(url, options)'); push('.then(res => res.json())', 1); push('.then(json => console.log(json))', 1); - push(".catch(err => console.error('error:' + err));", 1); + push('.catch(err => console.error(err));', 1); return join() .replace(/'encodedParams'/, 'encodedParams') - .replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")'); + .replace(/'formData'/, 'formData'); }, }; diff --git a/src/targets/node/fetch/fixtures/application-form-encoded.cjs b/src/targets/node/fetch/fixtures/application-form-encoded.js similarity index 74% rename from src/targets/node/fetch/fixtures/application-form-encoded.cjs rename to src/targets/node/fetch/fixtures/application-form-encoded.js index 430528450..9c6404a2b 100644 --- a/src/targets/node/fetch/fixtures/application-form-encoded.cjs +++ b/src/targets/node/fetch/fixtures/application-form-encoded.js @@ -1,7 +1,4 @@ -const { URLSearchParams } = require('url'); -const fetch = require('node-fetch'); const encodedParams = new URLSearchParams(); - encodedParams.set('foo', 'bar'); encodedParams.set('hello', 'world'); @@ -15,4 +12,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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.js similarity index 81% rename from src/targets/node/fetch/fixtures/application-json.cjs rename to src/targets/node/fetch/fixtures/application-json.js index b6e1908f1..73489d7b6 100644 --- a/src/targets/node/fetch/fixtures/application-json.cjs +++ b/src/targets/node/fetch/fixtures/application-json.js @@ -1,5 +1,3 @@ -const fetch = require('node-fetch'); - const url = 'https://httpbin.org/anything'; const options = { method: 'POST', @@ -17,4 +15,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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.js similarity index 69% rename from src/targets/node/fetch/fixtures/cookies.cjs rename to src/targets/node/fetch/fixtures/cookies.js index e61363e0a..ab9629359 100644 --- a/src/targets/node/fetch/fixtures/cookies.cjs +++ b/src/targets/node/fetch/fixtures/cookies.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 66% rename from src/targets/node/fetch/fixtures/custom-method.cjs rename to src/targets/node/fetch/fixtures/custom-method.js index df3e72a79..781a8c46c 100644 --- a/src/targets/node/fetch/fixtures/custom-method.cjs +++ b/src/targets/node/fetch/fixtures/custom-method.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 77% rename from src/targets/node/fetch/fixtures/full.cjs rename to src/targets/node/fetch/fixtures/full.js index 6777b1999..d33052c27 100644 --- a/src/targets/node/fetch/fixtures/full.cjs +++ b/src/targets/node/fetch/fixtures/full.js @@ -1,7 +1,4 @@ -const { URLSearchParams } = require('url'); -const fetch = require('node-fetch'); const encodedParams = new URLSearchParams(); - encodedParams.set('foo', 'bar'); const url = 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value'; @@ -18,4 +15,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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.js similarity index 77% rename from src/targets/node/fetch/fixtures/headers.cjs rename to src/targets/node/fetch/fixtures/headers.js index 59ee96acb..edf72d140 100644 --- a/src/targets/node/fetch/fixtures/headers.cjs +++ b/src/targets/node/fetch/fixtures/headers.js @@ -1,5 +1,3 @@ -const fetch = require('node-fetch'); - const url = 'https://httpbin.org/headers'; const options = { method: 'GET', @@ -14,4 +12,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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.js similarity index 65% rename from src/targets/node/fetch/fixtures/http-insecure.cjs rename to src/targets/node/fetch/fixtures/http-insecure.js index 37be04767..5a9ed7362 100644 --- a/src/targets/node/fetch/fixtures/http-insecure.cjs +++ b/src/targets/node/fetch/fixtures/http-insecure.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 74% rename from src/targets/node/fetch/fixtures/jsonObj-multiline.cjs rename to src/targets/node/fetch/fixtures/jsonObj-multiline.js index 3625b76e5..a9b92d58e 100644 --- a/src/targets/node/fetch/fixtures/jsonObj-multiline.cjs +++ b/src/targets/node/fetch/fixtures/jsonObj-multiline.js @@ -1,5 +1,3 @@ -const fetch = require('node-fetch'); - const url = 'https://httpbin.org/anything'; const options = { method: 'POST', @@ -10,4 +8,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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.js similarity index 74% rename from src/targets/node/fetch/fixtures/jsonObj-null-value.cjs rename to src/targets/node/fetch/fixtures/jsonObj-null-value.js index c10da148a..4eb4892fb 100644 --- a/src/targets/node/fetch/fixtures/jsonObj-null-value.cjs +++ b/src/targets/node/fetch/fixtures/jsonObj-null-value.js @@ -1,5 +1,3 @@ -const fetch = require('node-fetch'); - const url = 'https://httpbin.org/anything'; const options = { method: 'POST', @@ -10,4 +8,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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-data.js b/src/targets/node/fetch/fixtures/multipart-data.js new file mode 100644 index 000000000..ca964a322 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-data.js @@ -0,0 +1,13 @@ +import fs from 'fs'; + +const formData = new FormData(); +formData.append('foo', await new Response(fs.createReadStream('src/fixtures/files/hello.txt')).blob()); +formData.append('bar', 'Bonjour le monde'); + +const url = 'https://httpbin.org/anything'; +const options = {method: 'POST', body: formData}; + +fetch(url, options) + .then(res => res.json()) + .then(json => console.log(json)) + .catch(err => console.error(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-file.js b/src/targets/node/fetch/fixtures/multipart-file.js new file mode 100644 index 000000000..02fc790b9 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-file.js @@ -0,0 +1,12 @@ +import fs from 'fs'; + +const formData = new FormData(); +formData.append('foo', await new Response(fs.createReadStream('src/fixtures/files/hello.txt')).blob()); + +const url = 'https://httpbin.org/anything'; +const options = {method: 'POST', body: formData}; + +fetch(url, options) + .then(res => res.json()) + .then(json => console.log(json)) + .catch(err => console.error(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.js similarity index 71% rename from src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs rename to src/targets/node/fetch/fixtures/multipart-form-data-no-params.js index 177943af9..053f56470 100644 --- a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs +++ b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 51% rename from src/targets/node/fetch/fixtures/multipart-form-data.cjs rename to src/targets/node/fetch/fixtures/multipart-form-data.js index f77d66774..874d6b15a 100644 --- a/src/targets/node/fetch/fixtures/multipart-form-data.cjs +++ b/src/targets/node/fetch/fixtures/multipart-form-data.js @@ -1,15 +1,10 @@ -const FormData = require('form-data'); -const fetch = require('node-fetch'); const formData = new FormData(); - formData.append('foo', 'bar'); const url = 'https://httpbin.org/anything'; -const options = {method: 'POST'}; - -options.body = formData; +const options = {method: 'POST', body: formData}; fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error('error:' + err)); \ No newline at end of file + .catch(err => console.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.js similarity index 70% rename from src/targets/node/fetch/fixtures/nested.cjs rename to src/targets/node/fetch/fixtures/nested.js index 0fb008e35..af78c1dac 100644 --- a/src/targets/node/fetch/fixtures/nested.cjs +++ b/src/targets/node/fetch/fixtures/nested.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 70% rename from src/targets/node/fetch/fixtures/postdata-malformed.cjs rename to src/targets/node/fetch/fixtures/postdata-malformed.js index 95af34d41..76c3abf25 100644 --- a/src/targets/node/fetch/fixtures/postdata-malformed.cjs +++ b/src/targets/node/fetch/fixtures/postdata-malformed.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 73% rename from src/targets/node/fetch/fixtures/query-encoded.cjs rename to src/targets/node/fetch/fixtures/query-encoded.js index bd52227ef..5bb1a33ae 100644 --- a/src/targets/node/fetch/fixtures/query-encoded.cjs +++ b/src/targets/node/fetch/fixtures/query-encoded.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 69% rename from src/targets/node/fetch/fixtures/query.cjs rename to src/targets/node/fetch/fixtures/query.js index d18f187c5..d18e8763b 100644 --- a/src/targets/node/fetch/fixtures/query.cjs +++ b/src/targets/node/fetch/fixtures/query.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 65% rename from src/targets/node/fetch/fixtures/short.cjs rename to src/targets/node/fetch/fixtures/short.js index bddc8acd7..1deb47f08 100644 --- a/src/targets/node/fetch/fixtures/short.cjs +++ b/src/targets/node/fetch/fixtures/short.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.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.js similarity index 72% rename from src/targets/node/fetch/fixtures/text-plain.cjs rename to src/targets/node/fetch/fixtures/text-plain.js index fc9aea5d7..aa55a9300 100644 --- a/src/targets/node/fetch/fixtures/text-plain.cjs +++ b/src/targets/node/fetch/fixtures/text-plain.js @@ -1,9 +1,7 @@ -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 + .catch(err => console.error(err)); \ No newline at end of file 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..77307bfd8 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -3,20 +3,16 @@ 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 = { info: { key: 'node', title: 'Node.js', - default: 'native', + default: 'fetch', cli: 'node %s', }, clientsById: { native, - request, - unirest, axios, fetch, }, diff --git a/src/targets/node/unirest/client.ts b/src/targets/node/unirest/client.ts deleted file mode 100644 index 94e01517f..000000000 --- a/src/targets/node/unirest/client.ts +++ /dev/null @@ -1,130 +0,0 @@ -/** - * @description - * HTTP code snippet generator for Node.js using Unirest. - * - * @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 unirest: Client = { - info: { - key: 'unirest', - title: 'Unirest', - link: 'http://unirest.io/nodejs.html', - description: 'Lightweight HTTP Request Client Library', - extname: '.cjs', - }, - convert: ({ method, url, cookies, queryObj, postData, headersObj }, options) => { - const opts = { - indent: ' ', - ...options, - }; - - let includeFS = false; - const { addPostProcessor, blank, join, push, unshift } = new CodeBuilder({ - indent: opts.indent, - }); - - push("const unirest = require('unirest');"); - blank(); - push(`const req = unirest('${method}', '${url}');`); - blank(); - - if (cookies.length) { - push('const CookieJar = unirest.jar();'); - - cookies.forEach(cookie => { - push(`CookieJar.add('${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}', '${url}');`); - }); - - push('req.jar(CookieJar);'); - blank(); - } - - if (Object.keys(queryObj).length) { - push(`req.query(${stringifyObject(queryObj, { indent: opts.indent })});`); - blank(); - } - - if (Object.keys(headersObj).length) { - push(`req.headers(${stringifyObject(headersObj, { indent: opts.indent })});`); - blank(); - } - - switch (postData.mimeType) { - case 'application/x-www-form-urlencoded': - if (postData.paramsObj) { - push(`req.form(${stringifyObject(postData.paramsObj, { indent: opts.indent })});`); - blank(); - } - break; - - case 'application/json': - if (postData.jsonObj) { - push("req.type('json');"); - push(`req.send(${stringifyObject(postData.jsonObj, { indent: opts.indent })});`); - blank(); - } - break; - - case 'multipart/form-data': { - if (!postData.params) { - break; - } - - const multipart: Record[] = []; - - postData.params.forEach(param => { - const part: Record = {}; - - if (param.fileName && !param.value) { - includeFS = true; - - part.body = `fs.createReadStream('${param.fileName}')`; - addPostProcessor(code => code.replace(/'fs\.createReadStream\(\\'(.+)\\'\)'/, "fs.createReadStream('$1')")); - } else if (param.value) { - part.body = param.value; - } - - if (part.body) { - if (param.contentType) { - part['content-type'] = param.contentType; - } - - multipart.push(part); - } - }); - - push(`req.multipart(${stringifyObject(multipart, { indent: opts.indent })});`); - blank(); - break; - } - - default: - if (postData.text) { - push(`req.send(${stringifyObject(postData.text, { indent: opts.indent })});`); - blank(); - } - } - - if (includeFS) { - unshift("const fs = require('fs');"); - } - - push('req.end(function (res) {'); - push('if (res.error) throw new Error(res.error);', 1); - blank(); - - push('console.log(res.body);', 1); - push('});'); - - return join(); - }, -}; diff --git a/src/targets/node/unirest/fixtures/application-form-encoded.cjs b/src/targets/node/unirest/fixtures/application-form-encoded.cjs deleted file mode 100644 index 305d6c63d..000000000 --- a/src/targets/node/unirest/fixtures/application-form-encoded.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'application/x-www-form-urlencoded' -}); - -req.form({ - foo: 'bar', - hello: 'world' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/application-json.cjs b/src/targets/node/unirest/fixtures/application-json.cjs deleted file mode 100644 index 32944b12f..000000000 --- a/src/targets/node/unirest/fixtures/application-json.cjs +++ /dev/null @@ -1,35 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'application/json' -}); - -req.type('json'); -req.send({ - number: 1, - string: 'f"oo', - arr: [ - 1, - 2, - 3 - ], - nested: { - a: 'b' - }, - arr_mix: [ - 1, - 'a', - { - arr_mix_nested: [] - } - ], - boolean: false -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/cookies.cjs b/src/targets/node/unirest/fixtures/cookies.cjs deleted file mode 100644 index 768565457..000000000 --- a/src/targets/node/unirest/fixtures/cookies.cjs +++ /dev/null @@ -1,14 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'https://httpbin.org/cookies'); - -const CookieJar = unirest.jar(); -CookieJar.add('foo=bar', 'https://httpbin.org/cookies'); -CookieJar.add('bar=baz', 'https://httpbin.org/cookies'); -req.jar(CookieJar); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/custom-method.cjs b/src/targets/node/unirest/fixtures/custom-method.cjs deleted file mode 100644 index 7a4789dcb..000000000 --- a/src/targets/node/unirest/fixtures/custom-method.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('PROPFIND', 'https://httpbin.org/anything'); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/full.cjs b/src/targets/node/unirest/fixtures/full.cjs deleted file mode 100644 index f5b0cacc2..000000000 --- a/src/targets/node/unirest/fixtures/full.cjs +++ /dev/null @@ -1,32 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -const CookieJar = unirest.jar(); -CookieJar.add('foo=bar', 'https://httpbin.org/anything'); -CookieJar.add('bar=baz', 'https://httpbin.org/anything'); -req.jar(CookieJar); - -req.query({ - foo: [ - 'bar', - 'baz' - ], - baz: 'abc', - key: 'value' -}); - -req.headers({ - accept: 'application/json', - 'content-type': 'application/x-www-form-urlencoded' -}); - -req.form({ - foo: 'bar' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/headers.cjs b/src/targets/node/unirest/fixtures/headers.cjs deleted file mode 100644 index 1ae38ba36..000000000 --- a/src/targets/node/unirest/fixtures/headers.cjs +++ /dev/null @@ -1,16 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'https://httpbin.org/headers'); - -req.headers({ - accept: 'application/json', - 'x-foo': 'Bar', - 'x-bar': 'Foo', - 'quoted-value': '"quoted" \'string\'' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/http-insecure.cjs b/src/targets/node/unirest/fixtures/http-insecure.cjs deleted file mode 100644 index 5e131256e..000000000 --- a/src/targets/node/unirest/fixtures/http-insecure.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'http://httpbin.org/anything'); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/jsonObj-multiline.cjs b/src/targets/node/unirest/fixtures/jsonObj-multiline.cjs deleted file mode 100644 index b3807fa41..000000000 --- a/src/targets/node/unirest/fixtures/jsonObj-multiline.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'application/json' -}); - -req.type('json'); -req.send({ - foo: 'bar' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/jsonObj-null-value.cjs b/src/targets/node/unirest/fixtures/jsonObj-null-value.cjs deleted file mode 100644 index 2827f031a..000000000 --- a/src/targets/node/unirest/fixtures/jsonObj-null-value.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'application/json' -}); - -req.type('json'); -req.send({ - foo: null -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/multipart-data.cjs b/src/targets/node/unirest/fixtures/multipart-data.cjs deleted file mode 100644 index 7d9a9cb6d..000000000 --- a/src/targets/node/unirest/fixtures/multipart-data.cjs +++ /dev/null @@ -1,23 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'multipart/form-data; boundary=---011000010111000001101001' -}); - -req.multipart([ - { - body: 'Hello World', - 'content-type': 'text/plain' - }, - { - body: 'Bonjour le monde' - } -]); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/multipart-file.cjs b/src/targets/node/unirest/fixtures/multipart-file.cjs deleted file mode 100644 index 83794833d..000000000 --- a/src/targets/node/unirest/fixtures/multipart-file.cjs +++ /dev/null @@ -1,21 +0,0 @@ -const fs = require('fs'); -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'multipart/form-data; boundary=---011000010111000001101001' -}); - -req.multipart([ - { - body: fs.createReadStream('src/fixtures/files/hello.txt'), - 'content-type': 'text/plain' - } -]); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/multipart-form-data-no-params.cjs b/src/targets/node/unirest/fixtures/multipart-form-data-no-params.cjs deleted file mode 100644 index 16d9052b1..000000000 --- a/src/targets/node/unirest/fixtures/multipart-form-data-no-params.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'Content-Type': 'multipart/form-data' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/multipart-form-data.cjs b/src/targets/node/unirest/fixtures/multipart-form-data.cjs deleted file mode 100644 index ecd69034e..000000000 --- a/src/targets/node/unirest/fixtures/multipart-form-data.cjs +++ /dev/null @@ -1,19 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001' -}); - -req.multipart([ - { - body: 'bar' - } -]); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/nested.cjs b/src/targets/node/unirest/fixtures/nested.cjs deleted file mode 100644 index c58635dd9..000000000 --- a/src/targets/node/unirest/fixtures/nested.cjs +++ /dev/null @@ -1,15 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'https://httpbin.org/anything'); - -req.query({ - 'foo[bar]': 'baz,zap', - fiz: 'buz', - key: 'value' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/postdata-malformed.cjs b/src/targets/node/unirest/fixtures/postdata-malformed.cjs deleted file mode 100644 index 502dba6c7..000000000 --- a/src/targets/node/unirest/fixtures/postdata-malformed.cjs +++ /dev/null @@ -1,13 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'application/json' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/query-encoded.cjs b/src/targets/node/unirest/fixtures/query-encoded.cjs deleted file mode 100644 index d9d4b8468..000000000 --- a/src/targets/node/unirest/fixtures/query-encoded.cjs +++ /dev/null @@ -1,14 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'https://httpbin.org/anything'); - -req.query({ - startTime: '2019-06-13T19%3A08%3A25.455Z', - endTime: '2015-09-15T14%3A00%3A12-04%3A00' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/query.cjs b/src/targets/node/unirest/fixtures/query.cjs deleted file mode 100644 index 88fdf4893..000000000 --- a/src/targets/node/unirest/fixtures/query.cjs +++ /dev/null @@ -1,18 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'https://httpbin.org/anything'); - -req.query({ - foo: [ - 'bar', - 'baz' - ], - baz: 'abc', - key: 'value' -}); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/short.cjs b/src/targets/node/unirest/fixtures/short.cjs deleted file mode 100644 index cbf99c49b..000000000 --- a/src/targets/node/unirest/fixtures/short.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('GET', 'https://httpbin.org/anything'); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file diff --git a/src/targets/node/unirest/fixtures/text-plain.cjs b/src/targets/node/unirest/fixtures/text-plain.cjs deleted file mode 100644 index 5d50100a1..000000000 --- a/src/targets/node/unirest/fixtures/text-plain.cjs +++ /dev/null @@ -1,15 +0,0 @@ -const unirest = require('unirest'); - -const req = unirest('POST', 'https://httpbin.org/anything'); - -req.headers({ - 'content-type': 'text/plain' -}); - -req.send('Hello World'); - -req.end(function (res) { - if (res.error) throw new Error(res.error); - - console.log(res.body); -}); \ No newline at end of file