Skip to content

Commit 27f330c

Browse files
committed
Pretty-print JSON everywhere, to match httpbin and be generally readable
1 parent c923133 commit 27f330c

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

src/endpoints/http/basic-auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { serializeJson } from '../../util.js';
12
import { HttpEndpoint, HttpHandler } from '../http-index.js';
23

34
const matchPath = (path: string) =>
@@ -20,7 +21,7 @@ const handle: HttpHandler = (req, res, { path }) => {
2021
res.writeHead(200, {
2122
'content-type': 'application/json'
2223
});
23-
res.end(JSON.stringify({
24+
res.end(serializeJson({
2425
"authenticated": true,
2526
"user": username
2627
}));

src/endpoints/http/cookies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as Cookie from 'cookie';
22

33
import { HttpEndpoint } from '../http-index.js';
4+
import { serializeJson } from '../../util.js';
45

56
export const getCookies: HttpEndpoint = {
67
matchPath: (path) => path === '/cookies',
78
handle: (req, res) => {
89
const cookies = Cookie.parse(req.headers.cookie || '');
910
res.writeHead(200, { 'Content-Type': 'application/json' });
10-
res.end(JSON.stringify({ cookies }, null, 2));
11+
res.end(serializeJson({ cookies }));
1112
}
1213
}
1314

src/endpoints/http/json.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { serializeJson } from '../../util.js';
12
import { HttpEndpoint, HttpHandler } from '../http-index.js';
23

34
const matchPath = (path: string) => path === '/json';
@@ -8,7 +9,7 @@ const handle: HttpHandler = (req, res) => {
89
});
910

1011
// HTTPBin seems to just return this fixed example document:
11-
res.end(JSON.stringify({
12+
res.end(serializeJson({
1213
"slideshow": {
1314
"author": "Yours Truly",
1415
"date": "date of publication",
@@ -28,7 +29,7 @@ const handle: HttpHandler = (req, res) => {
2829
],
2930
"title": "Sample Slide Show"
3031
}
31-
}, null, 2));
32+
}));
3233
}
3334

3435
export const json: HttpEndpoint = {

src/endpoints/http/user-agent.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { serializeJson } from '../../util.js';
12
import { HttpEndpoint, HttpHandler } from '../http-index.js';
23

34
const matchPath = (path: string) => path === '/user-agent';
@@ -7,7 +8,9 @@ const handle: HttpHandler = (req, res) => {
78
res.writeHead(200, {
89
'content-type': 'application/json'
910
});
10-
res.end(JSON.stringify({ 'user-agent': userAgent }));
11+
res.end(serializeJson({
12+
'user-agent': userAgent
13+
}));
1114
}
1215

1316
export const userAgent: HttpEndpoint = {

src/httpbin-compat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as querystring from 'querystring';
66
import * as multipart from 'parse-multipart-data';
77

88
import { TLSSocket } from 'tls';
9+
import { serializeJson } from './util.js';
910

1011
const utf8Decoder = new TextDecoder('utf8', { fatal: true });
1112

@@ -99,5 +100,5 @@ export const buildHttpBinAnythingEndpoint = (options: {
99100
}
100101

101102
res.writeHead(200, { 'Content-Type': 'application/json' });
102-
res.end(JSON.stringify(result));
103+
res.end(serializeJson(result));
103104
}

src/util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export const clearArray = (array: Array<unknown> | undefined) => {
22
if (!array) return;
33
array.length = 0;
4-
}
4+
}
5+
6+
export const serializeJson = (json: any) =>
7+
JSON.stringify(json, null, 2) + '\n';

0 commit comments

Comments
 (0)