Skip to content

Commit 8f1594a

Browse files
authored
feat: creation of a new JSON target (readmeio#191)
1 parent 40e1886 commit 8f1594a

23 files changed

Lines changed: 138 additions & 0 deletions

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ exports[`availableTargets returns all available targets 1`] = `
145145
"key": "javascript",
146146
"title": "JavaScript",
147147
},
148+
{
149+
"clients": [
150+
{
151+
"description": "A JSON represetation of any HAR payload.",
152+
"key": "native",
153+
"link": "https://www.json.org/json-en.html",
154+
"title": "Native JSON",
155+
},
156+
],
157+
"default": "native",
158+
"extname": ".json",
159+
"key": "json",
160+
"title": "JSON",
161+
},
148162
{
149163
"clients": [
150164
{

src/targets/json/native/client.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator to generate raw JSON payload objects.
4+
*
5+
* @author
6+
* @erunion
7+
*
8+
* For any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
import type { ReducedHelperObject } from '../../../helpers/reducer';
11+
import type { Client } from '../../targets';
12+
13+
export const native: Client = {
14+
info: {
15+
key: 'native',
16+
title: 'Native JSON',
17+
link: 'https://www.json.org/json-en.html',
18+
description: 'A JSON represetation of any HAR payload.',
19+
},
20+
convert: ({ postData }, inputOpts) => {
21+
const opts = {
22+
indent: ' ',
23+
...inputOpts,
24+
};
25+
26+
let payload: string | ReducedHelperObject | undefined = '';
27+
28+
switch (postData.mimeType) {
29+
case 'application/x-www-form-urlencoded':
30+
payload = postData.paramsObj ? postData.paramsObj : postData.text;
31+
break;
32+
33+
case 'application/json':
34+
if (postData.jsonObj) {
35+
payload = postData.jsonObj;
36+
}
37+
break;
38+
39+
case 'multipart/form-data':
40+
if (!postData.params) {
41+
break;
42+
}
43+
44+
// eslint-disable-next-line no-case-declarations
45+
const multipartPayload: Record<string, any> = {};
46+
postData.params.forEach(param => {
47+
multipartPayload[param.name] = param.value;
48+
});
49+
50+
payload = multipartPayload;
51+
break;
52+
53+
default:
54+
if (postData.text) {
55+
payload = postData.text;
56+
}
57+
}
58+
59+
if (typeof payload === undefined || payload === '') {
60+
return '';
61+
}
62+
63+
return JSON.stringify(payload, null, opts.indent);
64+
},
65+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"foo": "bar",
3+
"hello": "world"
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
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+
}

src/targets/json/native/fixtures/cookies.json

Whitespace-only changes.

src/targets/json/native/fixtures/custom-method.json

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}

src/targets/json/native/fixtures/headers.json

Whitespace-only changes.

src/targets/json/native/fixtures/http-insecure.json

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}

0 commit comments

Comments
 (0)