diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b9bd5eb4d..55d46c5e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,9 +13,8 @@ jobs: strategy: matrix: node-version: - - lts/-1 - - lts/* - - latest + - 18 + - 20 steps: - uses: actions/checkout@v4 diff --git a/.vscode/settings.json b/.vscode/settings.json index 90c7e0089..c256ef48a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,15 +1,10 @@ { - "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.codeActionsOnSave": { "source.fixAll": "explicit" }, - "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode", // 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, - - "search.exclude": { - "coverage": true - } + "files.insertFinalNewline": false } diff --git a/README.md b/README.md index 3bb04fcc0..4380f04d1 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,8 @@ console.log( }), ); -// generate Node.js: Axios output -console.log(snippet.convert('node', 'axios')); +// generate Node.js: Unirest output +console.log(snippet.convert('node', 'unirest')); ``` ### addTarget(target) diff --git a/integrations/node.Dockerfile b/integrations/node.Dockerfile index f599f3082..22b91b0ce 100755 --- a/integrations/node.Dockerfile +++ b/integrations/node.Dockerfile @@ -13,7 +13,10 @@ WORKDIR /src ADD package.json /src/ # https://www.npmjs.com/package/axios -RUN npm install 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 && \ npm install ADD . /src diff --git a/package.json b/package.json index 873b1cf51..8a167749c 100644 --- a/package.json +++ b/package.json @@ -53,12 +53,14 @@ "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 56c4029d4..98f15fbe8 100644 --- a/src/fixtures/customTarget.ts +++ b/src/fixtures/customTarget.ts @@ -1,15 +1,15 @@ import type { Target } from '../targets/index.js'; -import { axios } from '../targets/node/axios/client.js'; +import { request } from '../targets/node/request/client.js'; export const customTarget = { info: { - key: 'node-variant', - title: 'Node Variant', + key: 'js-variant', + title: 'JavaScript Variant', extname: '.js', - default: 'axios', + default: 'request', }, clientsById: { - axios, + request, }, } as unknown as Target; diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index 446baba49..cd0bf5934 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": "fetch", + "default": "xhr", "key": "javascript", "title": "JavaScript", }, @@ -192,23 +192,39 @@ 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": ".js", + "extname": ".cjs", "installation": "npm install axios --save", "key": "axios", "link": "https://github.com/axios/axios", "title": "Axios", }, { - "description": "Perform asynchronous HTTP requests with the Fetch API", - "extname": ".js", + "description": "Simplified HTTP node-fetch client", + "extname": ".cjs", + "installation": "npm install node-fetch@2 --save", "key": "fetch", - "link": "https://nodejs.org/docs/latest/api/globals.html#fetch", - "title": "fetch", + "link": "https://github.com/bitinn/node-fetch", + "title": "Fetch", }, ], - "default": "fetch", + "default": "native", "key": "node", "title": "Node.js", }, diff --git a/src/helpers/utils.test.ts b/src/helpers/utils.test.ts index dfe75158a..e90337932 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('.js'); + expect(extname('node', 'axios')).toBe('.cjs'); }); it('returns empty string if the extension is not found', () => { diff --git a/src/integration.test.ts b/src/integration.test.ts index 9bab58313..41f126236 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'], + node: ['axios', 'fetch', 'native', 'request'], 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: ['fetch'], + node: ['native'], php: ['curl'], python: ['requests'], shell: ['curl'], diff --git a/src/targets/index.test.ts b/src/targets/index.test.ts index 15c936d64..97eaade1f 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: - // 'axios', + // 'unirest', ]; /** 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 6dfbdc333..493c12104 100644 --- a/src/targets/javascript/axios/client.ts +++ b/src/targets/javascript/axios/client.ts @@ -99,8 +99,12 @@ export const axios: Client = { push('axios'); push('.request(options)', 1); - push('.then(res => console.log(res.data))', 1); - push('.catch(err => console.error(err));', 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); 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 1a83ea542..7a55fffc3 100644 --- a/src/targets/javascript/axios/fixtures/application-form-encoded.js +++ b/src/targets/javascript/axios/fixtures/application-form-encoded.js @@ -13,5 +13,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 98903a65a..999da16cb 100644 --- a/src/targets/javascript/axios/fixtures/application-json.js +++ b/src/targets/javascript/axios/fixtures/application-json.js @@ -16,5 +16,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 7e9cf7ae3..4d0e356da 100644 --- a/src/targets/javascript/axios/fixtures/cookies.js +++ b/src/targets/javascript/axios/fixtures/cookies.js @@ -8,5 +8,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 4142f5977..c5e3af26a 100644 --- a/src/targets/javascript/axios/fixtures/custom-method.js +++ b/src/targets/javascript/axios/fixtures/custom-method.js @@ -4,5 +4,9 @@ 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 + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 014bd7340..ae9dcb0d5 100644 --- a/src/targets/javascript/axios/fixtures/full.js +++ b/src/targets/javascript/axios/fixtures/full.js @@ -17,5 +17,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 8026a1ee9..cbdbcb4c8 100644 --- a/src/targets/javascript/axios/fixtures/headers.js +++ b/src/targets/javascript/axios/fixtures/headers.js @@ -13,5 +13,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 0512e2df7..cd424b513 100644 --- a/src/targets/javascript/axios/fixtures/http-insecure.js +++ b/src/targets/javascript/axios/fixtures/http-insecure.js @@ -4,5 +4,9 @@ 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 + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 7d41fbc52..867e8b396 100644 --- a/src/targets/javascript/axios/fixtures/jsonObj-multiline.js +++ b/src/targets/javascript/axios/fixtures/jsonObj-multiline.js @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 3731d644d..06d04de34 100644 --- a/src/targets/javascript/axios/fixtures/jsonObj-null-value.js +++ b/src/targets/javascript/axios/fixtures/jsonObj-null-value.js @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 bb36ba381..5c620c764 100644 --- a/src/targets/javascript/axios/fixtures/multipart-data.js +++ b/src/targets/javascript/axios/fixtures/multipart-data.js @@ -13,5 +13,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 6579bbe8c..30e222583 100644 --- a/src/targets/javascript/axios/fixtures/multipart-file.js +++ b/src/targets/javascript/axios/fixtures/multipart-file.js @@ -12,5 +12,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 57e424c87..28b915fec 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,5 +8,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 ec40b9e54..61e9870f1 100644 --- a/src/targets/javascript/axios/fixtures/multipart-form-data.js +++ b/src/targets/javascript/axios/fixtures/multipart-form-data.js @@ -12,5 +12,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 3fffb9755..e9d270e1c 100644 --- a/src/targets/javascript/axios/fixtures/nested.js +++ b/src/targets/javascript/axios/fixtures/nested.js @@ -8,5 +8,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 f40deb9ed..6e7eb1676 100644 --- a/src/targets/javascript/axios/fixtures/postdata-malformed.js +++ b/src/targets/javascript/axios/fixtures/postdata-malformed.js @@ -8,5 +8,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 489c9927e..1090af9c1 100644 --- a/src/targets/javascript/axios/fixtures/query-encoded.js +++ b/src/targets/javascript/axios/fixtures/query-encoded.js @@ -11,5 +11,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 39cca5992..e08494626 100644 --- a/src/targets/javascript/axios/fixtures/query.js +++ b/src/targets/javascript/axios/fixtures/query.js @@ -8,5 +8,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 ba835ded4..ec03ac010 100644 --- a/src/targets/javascript/axios/fixtures/short.js +++ b/src/targets/javascript/axios/fixtures/short.js @@ -4,5 +4,9 @@ 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 + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ 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 dbe78d903..c212a3157 100644 --- a/src/targets/javascript/axios/fixtures/text-plain.js +++ b/src/targets/javascript/axios/fixtures/text-plain.js @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .then(function (response) { + console.log(response.data); + }) + .catch(function (error) { + console.error(error); + }); \ No newline at end of file diff --git a/src/targets/javascript/fetch/client.ts b/src/targets/javascript/fetch/client.ts index 6c9287221..9db46a397 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(res => res.json())', 1); - push('.then(res => console.log(res))', 1); + push('.then(response => response.json())', 1); + push('.then(response => console.log(response))', 1); push('.catch(err => console.error(err));', 1); return join(); diff --git a/src/targets/javascript/fetch/fixtures/application-form-encoded.js b/src/targets/javascript/fetch/fixtures/application-form-encoded.js index 4e0d61445..fd256737b 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/application-json.js b/src/targets/javascript/fetch/fixtures/application-json.js index 49568ebfb..f79071c08 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/cookies.js b/src/targets/javascript/fetch/fixtures/cookies.js index ba1fb515c..a9ba5766d 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/custom-method.js b/src/targets/javascript/fetch/fixtures/custom-method.js index 102f9b51e..738405920 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/full.js b/src/targets/javascript/fetch/fixtures/full.js index b26902bb5..3aee96390 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/headers.js b/src/targets/javascript/fetch/fixtures/headers.js index 3ca72a640..6db2a5d5b 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/http-insecure.js b/src/targets/javascript/fetch/fixtures/http-insecure.js index 60ada7617..c2624597f 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js b/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js index fc681292a..f2e9c2795 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js b/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js index 305eed6b6..b6b9ea049 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-data.js b/src/targets/javascript/fetch/fixtures/multipart-data.js index ac8664bf9..5cc8ddf85 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-file.js b/src/targets/javascript/fetch/fixtures/multipart-file.js index 039019a1c..11b0a6b05 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js b/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js index 4b79e19a6..b1318179e 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/multipart-form-data.js b/src/targets/javascript/fetch/fixtures/multipart-form-data.js index 7a21714a4..90643fc61 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/nested.js b/src/targets/javascript/fetch/fixtures/nested.js index 97d251384..8bcc084d2 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/postdata-malformed.js b/src/targets/javascript/fetch/fixtures/postdata-malformed.js index 94f81b0a4..145b702c4 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/query-encoded.js b/src/targets/javascript/fetch/fixtures/query-encoded.js index 14b44cf6c..6da5448bb 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/query.js b/src/targets/javascript/fetch/fixtures/query.js index 22d68dcd3..fe792dbe1 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/short.js b/src/targets/javascript/fetch/fixtures/short.js index 68ab8947d..86cec7387 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/fetch/fixtures/text-plain.js b/src/targets/javascript/fetch/fixtures/text-plain.js index 5a83b05da..3ff4a6f81 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(res => res.json()) - .then(res => console.log(res)) + .then(response => response.json()) + .then(response => console.log(response)) .catch(err => console.error(err)); \ No newline at end of file diff --git a/src/targets/javascript/jquery/client.ts b/src/targets/javascript/jquery/client.ts index dbd9d3e7d..5eb246e00 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(res => {'); - push('console.log(res);', 1); + push('$.ajax(settings).done(function (response) {'); + push('console.log(response);', 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 fd6020417..c3084f07a 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 0599fa68a..740cf3fa0 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 18899ddd8..5ba967b46 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 5131ee1db..6f48d72c4 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 a92c8867f..32649fb75 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 f482429b7..7b2fc32bf 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 bcc14a5f3..63d557e39 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 361dfd3c7..c1fbeae46 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 682bf2726..bdb8f7b89 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 48c0ffc10..aab34a995 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 43aba5379..20fb8e2dc 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 da5946035..78c9ea809 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 68fde42fb..0c3c85697 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 4aac81436..74cb5dc94 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 a588c826c..7caf9328f 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 cf871a707..f65186c31 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 9d81c66db..fd9713a43 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 0aa030eef..a55f7bb88 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ 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 d99636080..15932a19c 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(res => { - console.log(res); +$.ajax(settings).done(function (response) { + console.log(response); }); \ No newline at end of file diff --git a/src/targets/javascript/target.ts b/src/targets/javascript/target.ts index 6c58572cb..116c26c4e 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: 'fetch', + default: 'xhr', }, clientsById: { diff --git a/src/targets/node/axios/client.ts b/src/targets/node/axios/client.ts index 193b528e1..fd71e74c3 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: '.js', + extname: '.cjs', installation: 'npm install axios --save', }, convert: ({ method, fullUrl, allHeaders, postData }, options) => { @@ -29,8 +29,7 @@ export const axios: Client = { }; const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent }); - push("import axios from 'axios';"); - blank(); + push("const axios = require('axios');"); const reqOpts: Record = { method, @@ -44,6 +43,9 @@ 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}');`); @@ -58,12 +60,14 @@ 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; } @@ -75,8 +79,12 @@ export const axios: Client = { push('axios'); push('.request(options)', 1); - push('.then(res => console.log(res.data))', 1); - push('.catch(err => console.error(err));', 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); return join(); }, diff --git a/src/targets/node/axios/fixtures/application-form-encoded.js b/src/targets/node/axios/fixtures/application-form-encoded.cjs similarity index 60% rename from src/targets/node/axios/fixtures/application-form-encoded.js rename to src/targets/node/axios/fixtures/application-form-encoded.cjs index 1a83ea542..2d3298955 100644 --- a/src/targets/node/axios/fixtures/application-form-encoded.js +++ b/src/targets/node/axios/fixtures/application-form-encoded.cjs @@ -1,4 +1,5 @@ -import axios from 'axios'; +const axios = require('axios'); +const { URLSearchParams } = require('url'); const encodedParams = new URLSearchParams(); encodedParams.set('foo', 'bar'); @@ -13,5 +14,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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/application-json.js b/src/targets/node/axios/fixtures/application-json.cjs similarity index 66% rename from src/targets/node/axios/fixtures/application-json.js rename to src/targets/node/axios/fixtures/application-json.cjs index 98903a65a..fc3d0b13a 100644 --- a/src/targets/node/axios/fixtures/application-json.js +++ b/src/targets/node/axios/fixtures/application-json.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -16,5 +16,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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.cjs b/src/targets/node/axios/fixtures/cookies.cjs new file mode 100644 index 000000000..e1a046321 --- /dev/null +++ b/src/targets/node/axios/fixtures/cookies.cjs @@ -0,0 +1,16 @@ +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 deleted file mode 100644 index 7e9cf7ae3..000000000 --- a/src/targets/node/axios/fixtures/cookies.js +++ /dev/null @@ -1,12 +0,0 @@ -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 new file mode 100644 index 000000000..795827399 --- /dev/null +++ b/src/targets/node/axios/fixtures/custom-method.cjs @@ -0,0 +1,12 @@ +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 deleted file mode 100644 index 4142f5977..000000000 --- a/src/targets/node/axios/fixtures/custom-method.js +++ /dev/null @@ -1,8 +0,0 @@ -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.js b/src/targets/node/axios/fixtures/full.cjs similarity index 65% rename from src/targets/node/axios/fixtures/full.js rename to src/targets/node/axios/fixtures/full.cjs index fce011d95..90c039479 100644 --- a/src/targets/node/axios/fixtures/full.js +++ b/src/targets/node/axios/fixtures/full.cjs @@ -1,4 +1,5 @@ -import axios from 'axios'; +const axios = require('axios'); +const { URLSearchParams } = require('url'); const encodedParams = new URLSearchParams(); encodedParams.set('foo', 'bar'); @@ -16,5 +17,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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/headers.js b/src/targets/node/axios/fixtures/headers.cjs similarity index 59% rename from src/targets/node/axios/fixtures/headers.js rename to src/targets/node/axios/fixtures/headers.cjs index 8026a1ee9..1f129cdd6 100644 --- a/src/targets/node/axios/fixtures/headers.js +++ b/src/targets/node/axios/fixtures/headers.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'GET', @@ -13,5 +13,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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.cjs b/src/targets/node/axios/fixtures/http-insecure.cjs new file mode 100644 index 000000000..66a197661 --- /dev/null +++ b/src/targets/node/axios/fixtures/http-insecure.cjs @@ -0,0 +1,12 @@ +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 deleted file mode 100644 index 0512e2df7..000000000 --- a/src/targets/node/axios/fixtures/http-insecure.js +++ /dev/null @@ -1,8 +0,0 @@ -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.js b/src/targets/node/axios/fixtures/jsonObj-multiline.cjs similarity index 52% rename from src/targets/node/axios/fixtures/jsonObj-multiline.js rename to src/targets/node/axios/fixtures/jsonObj-multiline.cjs index 7d41fbc52..6a02916c2 100644 --- a/src/targets/node/axios/fixtures/jsonObj-multiline.js +++ b/src/targets/node/axios/fixtures/jsonObj-multiline.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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/jsonObj-null-value.js b/src/targets/node/axios/fixtures/jsonObj-null-value.cjs similarity index 52% rename from src/targets/node/axios/fixtures/jsonObj-null-value.js rename to src/targets/node/axios/fixtures/jsonObj-null-value.cjs index 3731d644d..ba03201b7 100644 --- a/src/targets/node/axios/fixtures/jsonObj-null-value.js +++ b/src/targets/node/axios/fixtures/jsonObj-null-value.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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-data.js b/src/targets/node/axios/fixtures/multipart-data.cjs similarity index 76% rename from src/targets/node/axios/fixtures/multipart-data.js rename to src/targets/node/axios/fixtures/multipart-data.cjs index d8672a082..f2268d0e5 100644 --- a/src/targets/node/axios/fixtures/multipart-data.js +++ b/src/targets/node/axios/fixtures/multipart-data.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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-file.js b/src/targets/node/axios/fixtures/multipart-file.cjs similarity index 71% rename from src/targets/node/axios/fixtures/multipart-file.js rename to src/targets/node/axios/fixtures/multipart-file.cjs index 8f53fc1eb..48e0d0162 100644 --- a/src/targets/node/axios/fixtures/multipart-file.js +++ b/src/targets/node/axios/fixtures/multipart-file.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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.cjs b/src/targets/node/axios/fixtures/multipart-form-data-no-params.cjs new file mode 100644 index 000000000..6b8c2ec58 --- /dev/null +++ b/src/targets/node/axios/fixtures/multipart-form-data-no-params.cjs @@ -0,0 +1,16 @@ +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 deleted file mode 100644 index 57e424c87..000000000 --- a/src/targets/node/axios/fixtures/multipart-form-data-no-params.js +++ /dev/null @@ -1,12 +0,0 @@ -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.js b/src/targets/node/axios/fixtures/multipart-form-data.cjs similarity index 67% rename from src/targets/node/axios/fixtures/multipart-form-data.js rename to src/targets/node/axios/fixtures/multipart-form-data.cjs index 45f7a10fb..15a56e506 100644 --- a/src/targets/node/axios/fixtures/multipart-form-data.js +++ b/src/targets/node/axios/fixtures/multipart-form-data.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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.cjs b/src/targets/node/axios/fixtures/nested.cjs new file mode 100644 index 000000000..11e4065d2 --- /dev/null +++ b/src/targets/node/axios/fixtures/nested.cjs @@ -0,0 +1,15 @@ +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 deleted file mode 100644 index 62373db83..000000000 --- a/src/targets/node/axios/fixtures/nested.js +++ /dev/null @@ -1,11 +0,0 @@ -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 new file mode 100644 index 000000000..803140cec --- /dev/null +++ b/src/targets/node/axios/fixtures/postdata-malformed.cjs @@ -0,0 +1,16 @@ +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 deleted file mode 100644 index f40deb9ed..000000000 --- a/src/targets/node/axios/fixtures/postdata-malformed.js +++ /dev/null @@ -1,12 +0,0 @@ -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.js b/src/targets/node/axios/fixtures/query-encoded.cjs similarity index 53% rename from src/targets/node/axios/fixtures/query-encoded.js rename to src/targets/node/axios/fixtures/query-encoded.cjs index c53a743a0..7d0d03100 100644 --- a/src/targets/node/axios/fixtures/query-encoded.js +++ b/src/targets/node/axios/fixtures/query-encoded.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'GET', @@ -7,5 +7,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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.cjs b/src/targets/node/axios/fixtures/query.cjs new file mode 100644 index 000000000..eb7240ebd --- /dev/null +++ b/src/targets/node/axios/fixtures/query.cjs @@ -0,0 +1,15 @@ +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 deleted file mode 100644 index 7833a75b3..000000000 --- a/src/targets/node/axios/fixtures/query.js +++ /dev/null @@ -1,11 +0,0 @@ -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 new file mode 100644 index 000000000..3741d4555 --- /dev/null +++ b/src/targets/node/axios/fixtures/short.cjs @@ -0,0 +1,12 @@ +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 deleted file mode 100644 index ba835ded4..000000000 --- a/src/targets/node/axios/fixtures/short.js +++ /dev/null @@ -1,8 +0,0 @@ -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.js b/src/targets/node/axios/fixtures/text-plain.cjs similarity index 51% rename from src/targets/node/axios/fixtures/text-plain.js rename to src/targets/node/axios/fixtures/text-plain.cjs index dbe78d903..2ddf6191c 100644 --- a/src/targets/node/axios/fixtures/text-plain.js +++ b/src/targets/node/axios/fixtures/text-plain.cjs @@ -1,4 +1,4 @@ -import axios from 'axios'; +const axios = require('axios'); const options = { method: 'POST', @@ -9,5 +9,9 @@ const options = { axios .request(options) - .then(res => console.log(res.data)) - .catch(err => console.error(err)); \ No newline at end of file + .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/fetch/client.ts b/src/targets/node/fetch/client.ts index c3d52f456..c8f1b76c6 100644 --- a/src/targets/node/fetch/client.ts +++ b/src/targets/node/fetch/client.ts @@ -1,3 +1,12 @@ +/** + * @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'; @@ -8,10 +17,11 @@ import { getHeaderName } from '../../../helpers/headers.js'; export const fetch: Client = { info: { key: 'fetch', - title: 'fetch', - link: 'https://nodejs.org/docs/latest/api/globals.html#fetch', - description: 'Perform asynchronous HTTP requests with the Fetch API', - extname: '.js', + title: 'Fetch', + link: 'https://github.com/bitinn/node-fetch', + description: 'Simplified HTTP node-fetch client', + extname: '.cjs', + installation: 'npm install node-fetch@2 --save', }, convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => { const opts = { @@ -22,6 +32,7 @@ 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, @@ -33,14 +44,15 @@ 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': @@ -56,14 +68,16 @@ export const fetch: Client = { break; } - // The FormData API automatically adds a `Content-Type` header for `multipart/form-data` content and if we add our own here data won't be correctly transmitted. + // The `form-data` module automatically adds a `Content-Type` header for `multipart/form-data` content and if we add our own here data won't be correctly transmitted. // eslint-disable-next-line no-case-declarations -- We're only using `contentTypeHeader` within this block. const contentTypeHeader = getHeaderName(headersObj, 'content-type'); if (contentTypeHeader) { delete headersObj[contentTypeHeader]; } + unshift("const FormData = require('form-data');"); push('const formData = new FormData();'); + blank(); postData.params.forEach(param => { if (!param.fileName && !param.fileName && !param.contentType) { @@ -73,17 +87,9 @@ export const fetch: Client = { if (param.fileName) { includeFS = true; - - // 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());`, - ); + push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`); } }); - - reqOpts.body = 'formData'; - blank(); break; default: @@ -104,7 +110,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. @@ -131,16 +137,19 @@ export const fetch: Client = { blank(); if (includeFS) { - unshift("import fs from 'fs';\n"); + unshift("const fs = require('fs');"); + } + if (postData.params && postData.mimeType === 'multipart/form-data') { + push('options.body = formData;'); + blank(); } - push('fetch(url, options)'); push('.then(res => res.json())', 1); push('.then(json => console.log(json))', 1); - push('.catch(err => console.error(err));', 1); + push(".catch(err => console.error('error:' + err));", 1); return join() .replace(/'encodedParams'/, 'encodedParams') - .replace(/'formData'/, 'formData'); + .replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")'); }, }; diff --git a/src/targets/node/fetch/fixtures/application-form-encoded.js b/src/targets/node/fetch/fixtures/application-form-encoded.cjs similarity index 74% rename from src/targets/node/fetch/fixtures/application-form-encoded.js rename to src/targets/node/fetch/fixtures/application-form-encoded.cjs index 9c6404a2b..430528450 100644 --- a/src/targets/node/fetch/fixtures/application-form-encoded.js +++ b/src/targets/node/fetch/fixtures/application-form-encoded.cjs @@ -1,4 +1,7 @@ +const { URLSearchParams } = require('url'); +const fetch = require('node-fetch'); const encodedParams = new URLSearchParams(); + encodedParams.set('foo', 'bar'); encodedParams.set('hello', 'world'); @@ -12,4 +15,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/application-json.js b/src/targets/node/fetch/fixtures/application-json.cjs similarity index 81% rename from src/targets/node/fetch/fixtures/application-json.js rename to src/targets/node/fetch/fixtures/application-json.cjs index 73489d7b6..b6e1908f1 100644 --- a/src/targets/node/fetch/fixtures/application-json.js +++ b/src/targets/node/fetch/fixtures/application-json.cjs @@ -1,3 +1,5 @@ +const fetch = require('node-fetch'); + const url = 'https://httpbin.org/anything'; const options = { method: 'POST', @@ -15,4 +17,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/cookies.js b/src/targets/node/fetch/fixtures/cookies.cjs similarity index 69% rename from src/targets/node/fetch/fixtures/cookies.js rename to src/targets/node/fetch/fixtures/cookies.cjs index ab9629359..e61363e0a 100644 --- a/src/targets/node/fetch/fixtures/cookies.js +++ b/src/targets/node/fetch/fixtures/cookies.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/custom-method.js b/src/targets/node/fetch/fixtures/custom-method.cjs similarity index 66% rename from src/targets/node/fetch/fixtures/custom-method.js rename to src/targets/node/fetch/fixtures/custom-method.cjs index 781a8c46c..df3e72a79 100644 --- a/src/targets/node/fetch/fixtures/custom-method.js +++ b/src/targets/node/fetch/fixtures/custom-method.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/full.js b/src/targets/node/fetch/fixtures/full.cjs similarity index 77% rename from src/targets/node/fetch/fixtures/full.js rename to src/targets/node/fetch/fixtures/full.cjs index d33052c27..6777b1999 100644 --- a/src/targets/node/fetch/fixtures/full.js +++ b/src/targets/node/fetch/fixtures/full.cjs @@ -1,4 +1,7 @@ +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'; @@ -15,4 +18,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/headers.js b/src/targets/node/fetch/fixtures/headers.cjs similarity index 77% rename from src/targets/node/fetch/fixtures/headers.js rename to src/targets/node/fetch/fixtures/headers.cjs index edf72d140..59ee96acb 100644 --- a/src/targets/node/fetch/fixtures/headers.js +++ b/src/targets/node/fetch/fixtures/headers.cjs @@ -1,3 +1,5 @@ +const fetch = require('node-fetch'); + const url = 'https://httpbin.org/headers'; const options = { method: 'GET', @@ -12,4 +14,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/http-insecure.js b/src/targets/node/fetch/fixtures/http-insecure.cjs similarity index 65% rename from src/targets/node/fetch/fixtures/http-insecure.js rename to src/targets/node/fetch/fixtures/http-insecure.cjs index 5a9ed7362..37be04767 100644 --- a/src/targets/node/fetch/fixtures/http-insecure.js +++ b/src/targets/node/fetch/fixtures/http-insecure.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-multiline.js b/src/targets/node/fetch/fixtures/jsonObj-multiline.cjs similarity index 74% rename from src/targets/node/fetch/fixtures/jsonObj-multiline.js rename to src/targets/node/fetch/fixtures/jsonObj-multiline.cjs index a9b92d58e..3625b76e5 100644 --- a/src/targets/node/fetch/fixtures/jsonObj-multiline.js +++ b/src/targets/node/fetch/fixtures/jsonObj-multiline.cjs @@ -1,3 +1,5 @@ +const fetch = require('node-fetch'); + const url = 'https://httpbin.org/anything'; const options = { method: 'POST', @@ -8,4 +10,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/jsonObj-null-value.js b/src/targets/node/fetch/fixtures/jsonObj-null-value.cjs similarity index 74% rename from src/targets/node/fetch/fixtures/jsonObj-null-value.js rename to src/targets/node/fetch/fixtures/jsonObj-null-value.cjs index 4eb4892fb..c10da148a 100644 --- a/src/targets/node/fetch/fixtures/jsonObj-null-value.js +++ b/src/targets/node/fetch/fixtures/jsonObj-null-value.cjs @@ -1,3 +1,5 @@ +const fetch = require('node-fetch'); + const url = 'https://httpbin.org/anything'; const options = { method: 'POST', @@ -8,4 +10,4 @@ const options = { fetch(url, options) .then(res => res.json()) .then(json => console.log(json)) - .catch(err => console.error(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-data.cjs b/src/targets/node/fetch/fixtures/multipart-data.cjs new file mode 100644 index 000000000..2c3363e27 --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-data.cjs @@ -0,0 +1,17 @@ +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 deleted file mode 100644 index ca964a322..000000000 --- a/src/targets/node/fetch/fixtures/multipart-data.js +++ /dev/null @@ -1,13 +0,0 @@ -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 new file mode 100644 index 000000000..68f16405c --- /dev/null +++ b/src/targets/node/fetch/fixtures/multipart-file.cjs @@ -0,0 +1,16 @@ +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 deleted file mode 100644 index 02fc790b9..000000000 --- a/src/targets/node/fetch/fixtures/multipart-file.js +++ /dev/null @@ -1,12 +0,0 @@ -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.js b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs similarity index 71% rename from src/targets/node/fetch/fixtures/multipart-form-data-no-params.js rename to src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs index 053f56470..177943af9 100644 --- a/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js +++ b/src/targets/node/fetch/fixtures/multipart-form-data-no-params.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/multipart-form-data.js b/src/targets/node/fetch/fixtures/multipart-form-data.cjs similarity index 51% rename from src/targets/node/fetch/fixtures/multipart-form-data.js rename to src/targets/node/fetch/fixtures/multipart-form-data.cjs index 874d6b15a..f77d66774 100644 --- a/src/targets/node/fetch/fixtures/multipart-form-data.js +++ b/src/targets/node/fetch/fixtures/multipart-form-data.cjs @@ -1,10 +1,15 @@ +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', body: formData}; +const options = {method: 'POST'}; + +options.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 + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/nested.js b/src/targets/node/fetch/fixtures/nested.cjs similarity index 70% rename from src/targets/node/fetch/fixtures/nested.js rename to src/targets/node/fetch/fixtures/nested.cjs index af78c1dac..0fb008e35 100644 --- a/src/targets/node/fetch/fixtures/nested.js +++ b/src/targets/node/fetch/fixtures/nested.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/postdata-malformed.js b/src/targets/node/fetch/fixtures/postdata-malformed.cjs similarity index 70% rename from src/targets/node/fetch/fixtures/postdata-malformed.js rename to src/targets/node/fetch/fixtures/postdata-malformed.cjs index 76c3abf25..95af34d41 100644 --- a/src/targets/node/fetch/fixtures/postdata-malformed.js +++ b/src/targets/node/fetch/fixtures/postdata-malformed.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query-encoded.js b/src/targets/node/fetch/fixtures/query-encoded.cjs similarity index 73% rename from src/targets/node/fetch/fixtures/query-encoded.js rename to src/targets/node/fetch/fixtures/query-encoded.cjs index 5bb1a33ae..bd52227ef 100644 --- a/src/targets/node/fetch/fixtures/query-encoded.js +++ b/src/targets/node/fetch/fixtures/query-encoded.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/query.js b/src/targets/node/fetch/fixtures/query.cjs similarity index 69% rename from src/targets/node/fetch/fixtures/query.js rename to src/targets/node/fetch/fixtures/query.cjs index d18e8763b..d18f187c5 100644 --- a/src/targets/node/fetch/fixtures/query.js +++ b/src/targets/node/fetch/fixtures/query.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/short.js b/src/targets/node/fetch/fixtures/short.cjs similarity index 65% rename from src/targets/node/fetch/fixtures/short.js rename to src/targets/node/fetch/fixtures/short.cjs index 1deb47f08..bddc8acd7 100644 --- a/src/targets/node/fetch/fixtures/short.js +++ b/src/targets/node/fetch/fixtures/short.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/fetch/fixtures/text-plain.js b/src/targets/node/fetch/fixtures/text-plain.cjs similarity index 72% rename from src/targets/node/fetch/fixtures/text-plain.js rename to src/targets/node/fetch/fixtures/text-plain.cjs index aa55a9300..fc9aea5d7 100644 --- a/src/targets/node/fetch/fixtures/text-plain.js +++ b/src/targets/node/fetch/fixtures/text-plain.cjs @@ -1,7 +1,9 @@ +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(err)); \ No newline at end of file + .catch(err => console.error('error:' + err)); \ No newline at end of file diff --git a/src/targets/node/request/client.ts b/src/targets/node/request/client.ts new file mode 100644 index 000000000..284d13c0b --- /dev/null +++ b/src/targets/node/request/client.ts @@ -0,0 +1,132 @@ +/** + * @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 new file mode 100644 index 000000000..f49d8b71d --- /dev/null +++ b/src/targets/node/request/fixtures/application-form-encoded.cjs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..b7413c0ad --- /dev/null +++ b/src/targets/node/request/fixtures/application-json.cjs @@ -0,0 +1,22 @@ +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 new file mode 100644 index 000000000..66e4f4ed2 --- /dev/null +++ b/src/targets/node/request/fixtures/cookies.cjs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 000000000..c716db3e7 --- /dev/null +++ b/src/targets/node/request/fixtures/custom-method.cjs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..52e977901 --- /dev/null +++ b/src/targets/node/request/fixtures/full.cjs @@ -0,0 +1,22 @@ +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 new file mode 100644 index 000000000..88ade811b --- /dev/null +++ b/src/targets/node/request/fixtures/headers.cjs @@ -0,0 +1,18 @@ +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 new file mode 100644 index 000000000..8c04d4368 --- /dev/null +++ b/src/targets/node/request/fixtures/http-insecure.cjs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..240bad18d --- /dev/null +++ b/src/targets/node/request/fixtures/jsonObj-multiline.cjs @@ -0,0 +1,15 @@ +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 new file mode 100644 index 000000000..395c0c5ba --- /dev/null +++ b/src/targets/node/request/fixtures/jsonObj-null-value.cjs @@ -0,0 +1,15 @@ +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 new file mode 100644 index 000000000..52642fe8e --- /dev/null +++ b/src/targets/node/request/fixtures/multipart-data.cjs @@ -0,0 +1,21 @@ +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 new file mode 100644 index 000000000..6c04d1118 --- /dev/null +++ b/src/targets/node/request/fixtures/multipart-file.cjs @@ -0,0 +1,20 @@ +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 new file mode 100644 index 000000000..a2b3599f7 --- /dev/null +++ b/src/targets/node/request/fixtures/multipart-form-data-no-params.cjs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 000000000..dae439e67 --- /dev/null +++ b/src/targets/node/request/fixtures/multipart-form-data.cjs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..3bff0a48d --- /dev/null +++ b/src/targets/node/request/fixtures/nested.cjs @@ -0,0 +1,12 @@ +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 new file mode 100644 index 000000000..46ff13dc4 --- /dev/null +++ b/src/targets/node/request/fixtures/postdata-malformed.cjs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 000000000..5f3be8438 --- /dev/null +++ b/src/targets/node/request/fixtures/query-encoded.cjs @@ -0,0 +1,12 @@ +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 new file mode 100644 index 000000000..7f1cb5721 --- /dev/null +++ b/src/targets/node/request/fixtures/query.cjs @@ -0,0 +1,12 @@ +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 new file mode 100644 index 000000000..f02e48ca0 --- /dev/null +++ b/src/targets/node/request/fixtures/short.cjs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..6f52592a9 --- /dev/null +++ b/src/targets/node/request/fixtures/text-plain.cjs @@ -0,0 +1,14 @@ +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 77307bfd8..1def2a580 100644 --- a/src/targets/node/target.ts +++ b/src/targets/node/target.ts @@ -3,16 +3,20 @@ 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: 'fetch', + default: 'native', 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 new file mode 100644 index 000000000..94e01517f --- /dev/null +++ b/src/targets/node/unirest/client.ts @@ -0,0 +1,130 @@ +/** + * @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 new file mode 100644 index 000000000..305d6c63d --- /dev/null +++ b/src/targets/node/unirest/fixtures/application-form-encoded.cjs @@ -0,0 +1,18 @@ +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 new file mode 100644 index 000000000..32944b12f --- /dev/null +++ b/src/targets/node/unirest/fixtures/application-json.cjs @@ -0,0 +1,35 @@ +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 new file mode 100644 index 000000000..768565457 --- /dev/null +++ b/src/targets/node/unirest/fixtures/cookies.cjs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..7a4789dcb --- /dev/null +++ b/src/targets/node/unirest/fixtures/custom-method.cjs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..f5b0cacc2 --- /dev/null +++ b/src/targets/node/unirest/fixtures/full.cjs @@ -0,0 +1,32 @@ +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 new file mode 100644 index 000000000..1ae38ba36 --- /dev/null +++ b/src/targets/node/unirest/fixtures/headers.cjs @@ -0,0 +1,16 @@ +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 new file mode 100644 index 000000000..5e131256e --- /dev/null +++ b/src/targets/node/unirest/fixtures/http-insecure.cjs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..b3807fa41 --- /dev/null +++ b/src/targets/node/unirest/fixtures/jsonObj-multiline.cjs @@ -0,0 +1,18 @@ +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 new file mode 100644 index 000000000..2827f031a --- /dev/null +++ b/src/targets/node/unirest/fixtures/jsonObj-null-value.cjs @@ -0,0 +1,18 @@ +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 new file mode 100644 index 000000000..7d9a9cb6d --- /dev/null +++ b/src/targets/node/unirest/fixtures/multipart-data.cjs @@ -0,0 +1,23 @@ +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 new file mode 100644 index 000000000..83794833d --- /dev/null +++ b/src/targets/node/unirest/fixtures/multipart-file.cjs @@ -0,0 +1,21 @@ +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 new file mode 100644 index 000000000..16d9052b1 --- /dev/null +++ b/src/targets/node/unirest/fixtures/multipart-form-data-no-params.cjs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 000000000..ecd69034e --- /dev/null +++ b/src/targets/node/unirest/fixtures/multipart-form-data.cjs @@ -0,0 +1,19 @@ +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 new file mode 100644 index 000000000..c58635dd9 --- /dev/null +++ b/src/targets/node/unirest/fixtures/nested.cjs @@ -0,0 +1,15 @@ +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 new file mode 100644 index 000000000..502dba6c7 --- /dev/null +++ b/src/targets/node/unirest/fixtures/postdata-malformed.cjs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 000000000..d9d4b8468 --- /dev/null +++ b/src/targets/node/unirest/fixtures/query-encoded.cjs @@ -0,0 +1,14 @@ +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 new file mode 100644 index 000000000..88fdf4893 --- /dev/null +++ b/src/targets/node/unirest/fixtures/query.cjs @@ -0,0 +1,18 @@ +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 new file mode 100644 index 000000000..cbf99c49b --- /dev/null +++ b/src/targets/node/unirest/fixtures/short.cjs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..5d50100a1 --- /dev/null +++ b/src/targets/node/unirest/fixtures/text-plain.cjs @@ -0,0 +1,15 @@ +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