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