Skip to content

Commit e31c053

Browse files
committed
Revert "refactor: removing js xhr"
This reverts commit fd170ff.
1 parent cca8b0d commit e31c053

24 files changed

+452
-3
lines changed

src/helpers/__snapshots__/utils.test.ts.snap

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ exports[`availableTargets > returns all available targets 1`] = `
120120
},
121121
{
122122
"clients": [
123+
{
124+
"description": "W3C Standard API that provides scripted client functionality",
125+
"extname": ".js",
126+
"key": "xhr",
127+
"link": "https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest",
128+
"title": "XMLHttpRequest",
129+
},
123130
{
124131
"description": "Promise based HTTP client for the browser and node.js",
125132
"extname": ".js",
@@ -143,7 +150,7 @@ exports[`availableTargets > returns all available targets 1`] = `
143150
"title": "jQuery",
144151
},
145152
],
146-
"default": "fetch",
153+
"default": "xhr",
147154
"key": "javascript",
148155
"title": "JavaScript",
149156
},
@@ -201,7 +208,7 @@ exports[`availableTargets > returns all available targets 1`] = `
201208
"title": "fetch",
202209
},
203210
],
204-
"default": "native",
211+
"default": "fetch",
205212
"key": "node",
206213
"title": "Node.js",
207214
},

src/targets/javascript/target.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import type { Target } from '../index.js';
33
import { axios } from './axios/client.js';
44
import { fetch } from './fetch/client.js';
55
import { jquery } from './jquery/client.js';
6+
import { xhr } from './xhr/client.js';
67

78
export const javascript: Target = {
89
info: {
910
key: 'javascript',
1011
title: 'JavaScript',
11-
default: 'fetch',
12+
default: 'xhr',
1213
},
1314

1415
clientsById: {
16+
xhr,
1517
axios,
1618
fetch,
1719
jquery,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { Request } from '../../../index.js';
2+
3+
import request from '../../../fixtures/requests/short.cjs';
4+
import { runCustomFixtures } from '../../../fixtures/runCustomFixtures.js';
5+
6+
runCustomFixtures({
7+
targetId: 'javascript',
8+
clientId: 'xhr',
9+
tests: [
10+
{
11+
it: 'should not use cors',
12+
input: request.log.entries[0].request as Request,
13+
options: {
14+
cors: false,
15+
},
16+
expected: 'cors.js',
17+
},
18+
],
19+
});

src/targets/javascript/xhr/client.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for native XMLHttpRequest
4+
*
5+
* @author
6+
* @AhmadNassri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
import type { Client } from '../../index.js';
11+
12+
import stringifyObject from 'stringify-object';
13+
14+
import { CodeBuilder } from '../../../helpers/code-builder.js';
15+
import { escapeForSingleQuotes } from '../../../helpers/escape.js';
16+
import { getHeader, getHeaderName, hasHeader } from '../../../helpers/headers.js';
17+
18+
export interface XhrOptions {
19+
cors?: boolean;
20+
}
21+
22+
export const xhr: Client = {
23+
info: {
24+
key: 'xhr',
25+
title: 'XMLHttpRequest',
26+
link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
27+
description: 'W3C Standard API that provides scripted client functionality',
28+
extname: '.js',
29+
},
30+
convert: ({ postData, allHeaders, method, fullUrl }, options) => {
31+
const opts = {
32+
indent: ' ',
33+
cors: true,
34+
...options,
35+
};
36+
37+
const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
38+
39+
switch (postData.mimeType) {
40+
case 'application/json':
41+
push(
42+
`const data = JSON.stringify(${stringifyObject(postData.jsonObj, {
43+
indent: opts.indent,
44+
})});`,
45+
);
46+
blank();
47+
break;
48+
49+
case 'multipart/form-data':
50+
if (!postData.params) {
51+
break;
52+
}
53+
54+
push('const data = new FormData();');
55+
56+
postData.params.forEach(param => {
57+
push(`data.append('${param.name}', '${param.value || param.fileName || ''}');`);
58+
});
59+
60+
// remove the contentType header
61+
if (hasHeader(allHeaders, 'content-type')) {
62+
if (getHeader(allHeaders, 'content-type')?.includes('boundary')) {
63+
const headerName = getHeaderName(allHeaders, 'content-type');
64+
if (headerName) {
65+
delete allHeaders[headerName];
66+
}
67+
}
68+
}
69+
70+
blank();
71+
break;
72+
73+
default:
74+
push(`const data = ${postData.text ? `'${postData.text}'` : 'null'};`);
75+
blank();
76+
}
77+
78+
push('const xhr = new XMLHttpRequest();');
79+
80+
if (opts.cors) {
81+
push('xhr.withCredentials = true;');
82+
}
83+
84+
blank();
85+
push("xhr.addEventListener('readystatechange', function () {");
86+
push('if (this.readyState === this.DONE) {', 1);
87+
push('console.log(this.responseText);', 2);
88+
push('}', 1);
89+
push('});');
90+
blank();
91+
push(`xhr.open('${method}', '${fullUrl}');`);
92+
93+
Object.keys(allHeaders).forEach(key => {
94+
push(`xhr.setRequestHeader('${key}', '${escapeForSingleQuotes(allHeaders[key])}');`);
95+
});
96+
97+
blank();
98+
push('xhr.send(data);');
99+
100+
return join();
101+
},
102+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const data = 'foo=bar&hello=world';
2+
3+
const xhr = new XMLHttpRequest();
4+
xhr.withCredentials = true;
5+
6+
xhr.addEventListener('readystatechange', function () {
7+
if (this.readyState === this.DONE) {
8+
console.log(this.responseText);
9+
}
10+
});
11+
12+
xhr.open('POST', 'https://httpbin.org/anything');
13+
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
14+
15+
xhr.send(data);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const data = JSON.stringify({
2+
number: 1,
3+
string: 'f"oo',
4+
arr: [
5+
1,
6+
2,
7+
3
8+
],
9+
nested: {
10+
a: 'b'
11+
},
12+
arr_mix: [
13+
1,
14+
'a',
15+
{
16+
arr_mix_nested: []
17+
}
18+
],
19+
boolean: false
20+
});
21+
22+
const xhr = new XMLHttpRequest();
23+
xhr.withCredentials = true;
24+
25+
xhr.addEventListener('readystatechange', function () {
26+
if (this.readyState === this.DONE) {
27+
console.log(this.responseText);
28+
}
29+
});
30+
31+
xhr.open('POST', 'https://httpbin.org/anything');
32+
xhr.setRequestHeader('content-type', 'application/json');
33+
34+
xhr.send(data);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const data = null;
2+
3+
const xhr = new XMLHttpRequest();
4+
xhr.withCredentials = true;
5+
6+
xhr.addEventListener('readystatechange', function () {
7+
if (this.readyState === this.DONE) {
8+
console.log(this.responseText);
9+
}
10+
});
11+
12+
xhr.open('GET', 'https://httpbin.org/cookies');
13+
xhr.setRequestHeader('cookie', 'foo=bar; bar=baz');
14+
15+
xhr.send(data);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const data = null;
2+
3+
const xhr = new XMLHttpRequest();
4+
5+
xhr.addEventListener('readystatechange', function () {
6+
if (this.readyState === this.DONE) {
7+
console.log(this.responseText);
8+
}
9+
});
10+
11+
xhr.open('GET', 'https://httpbin.org/anything');
12+
13+
xhr.send(data);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const data = null;
2+
3+
const xhr = new XMLHttpRequest();
4+
xhr.withCredentials = true;
5+
6+
xhr.addEventListener('readystatechange', function () {
7+
if (this.readyState === this.DONE) {
8+
console.log(this.responseText);
9+
}
10+
});
11+
12+
xhr.open('PROPFIND', 'https://httpbin.org/anything');
13+
14+
xhr.send(data);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const data = 'foo=bar';
2+
3+
const xhr = new XMLHttpRequest();
4+
xhr.withCredentials = true;
5+
6+
xhr.addEventListener('readystatechange', function () {
7+
if (this.readyState === this.DONE) {
8+
console.log(this.responseText);
9+
}
10+
});
11+
12+
xhr.open('POST', 'https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value');
13+
xhr.setRequestHeader('cookie', 'foo=bar; bar=baz');
14+
xhr.setRequestHeader('accept', 'application/json');
15+
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
16+
17+
xhr.send(data);

0 commit comments

Comments
 (0)