diff --git a/src/helpers/__snapshots__/utils.test.ts.snap b/src/helpers/__snapshots__/utils.test.ts.snap index da6d47024..84a0e6a50 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", }, @@ -194,11 +187,10 @@ exports[`availableTargets > returns all available targets 1`] = ` "title": "Axios", }, { - "description": "Simplified HTTP node-fetch client", + "description": "Perform asynchronous HTTP requests with the Fetch API", "extname": ".cjs", - "installation": "npm install node-fetch@2 --save", "key": "fetch", - "link": "https://github.com/bitinn/node-fetch", + "link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch", "title": "Fetch", }, ], 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