Skip to content

Commit 2e11fc8

Browse files
mertcanaltinMert Can AltinKhafraDev
authored
fetch: improve output for FormData, Response, Request (#2955)
* fetch: improve output for FormData * fetch: improve output for Response * fetch: improve output for Request * fetch: improve output for Request * feat: repair code * feat: repair code * feat: repair code * fix: repair codes & tests * fix: repair tests & response & request class * Update response.js Co-authored-by: Khafra <maitken033380023@gmail.com> * fix: test repair --------- Co-authored-by: Mert Can Altin <mert.altin@trendyol.com> Co-authored-by: Khafra <maitken033380023@gmail.com>
1 parent 321763e commit 2e11fc8

6 files changed

Lines changed: 126 additions & 0 deletions

File tree

lib/web/fetch/formdata.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { kEnumerableProperty } = require('../../core/util')
66
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
77
const { webidl } = require('./webidl')
88
const { File: NativeFile } = require('node:buffer')
9+
const nodeUtil = require('node:util')
910

1011
/** @type {globalThis['File']} */
1112
const File = NativeFile ?? UndiciFile
@@ -154,6 +155,15 @@ class FormData {
154155
this[kState].push(entry)
155156
}
156157
}
158+
159+
[nodeUtil.inspect.custom] (depth, options) {
160+
let output = 'FormData:\n'
161+
this[kState].forEach(entry => {
162+
output += `${entry.name}: ${entry.value}\n`
163+
})
164+
165+
return output
166+
}
157167
}
158168

159169
iteratorMixin('FormData', FormData, kState, 'name', 'value')

lib/web/fetch/request.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { extractBody, mixinBody, cloneBody } = require('./body')
66
const { Headers, fill: fillHeaders, HeadersList } = require('./headers')
77
const { FinalizationRegistry } = require('./dispatcher-weakref')()
88
const util = require('../../core/util')
9+
const nodeUtil = require('node:util')
910
const {
1011
isValidHTTPToken,
1112
sameOrigin,
@@ -771,6 +772,32 @@ class Request {
771772
// 4. Return clonedRequestObject.
772773
return fromInnerRequest(clonedRequest, ac.signal, this[kHeaders][kGuard], this[kRealm])
773774
}
775+
776+
[nodeUtil.inspect.custom] (depth, options) {
777+
if (options.depth === null) {
778+
options.depth = 2
779+
}
780+
781+
const properties = {
782+
method: this.method,
783+
url: this.url,
784+
headers: this.headers,
785+
destination: this.destination,
786+
referrer: this.referrer,
787+
referrerPolicy: this.referrerPolicy,
788+
mode: this.mode,
789+
credentials: this.credentials,
790+
cache: this.cache,
791+
redirect: this.redirect,
792+
integrity: this.integrity,
793+
keepalive: this.keepalive,
794+
isReloadNavigation: this.isReloadNavigation,
795+
isHistoryNavigation: this.isHistoryNavigation,
796+
signal: this.signal
797+
}
798+
799+
return nodeUtil.formatWithOptions(options, { ...properties })
800+
}
774801
}
775802

776803
mixinBody(Request)

lib/web/fetch/response.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { Headers, HeadersList, fill } = require('./headers')
44
const { extractBody, cloneBody, mixinBody } = require('./body')
55
const util = require('../../core/util')
6+
const nodeUtil = require('node:util')
67
const { kEnumerableProperty } = util
78
const {
89
isValidReasonPhrase,
@@ -252,6 +253,26 @@ class Response {
252253
// clonedResponse, this’s headers’s guard, and this’s relevant Realm.
253254
return fromInnerResponse(clonedResponse, this[kHeaders][kGuard], this[kRealm])
254255
}
256+
257+
[nodeUtil.inspect.custom] (depth, options) {
258+
if (options.depth === null) {
259+
options.depth = 2
260+
}
261+
262+
const properties = {
263+
status: this.status,
264+
statusText: this.statusText,
265+
headers: this.headers,
266+
body: this.body,
267+
bodyUsed: this.bodyUsed,
268+
ok: this.ok,
269+
redirected: this.redirected,
270+
type: this.type,
271+
url: this.url
272+
}
273+
274+
return nodeUtil.formatWithOptions(options, `Response ${nodeUtil.inspect(properties)}`)
275+
}
255276
}
256277

257278
mixinBody(Response)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const { FormData } = require('../../')
4+
const { inspect } = require('node:util')
5+
const { test } = require('node:test')
6+
const assert = require('node:assert')
7+
8+
test('FormData class custom inspection', () => {
9+
const formData = new FormData()
10+
formData.append('username', 'john_doe')
11+
formData.append('email', 'john@example.com')
12+
13+
const expectedOutput = 'FormData:\nusername: john_doe\nemail: john@example.com\n'
14+
15+
assert.deepStrictEqual(inspect(formData), expectedOutput)
16+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const { describe, it } = require('node:test')
4+
const assert = require('assert')
5+
const util = require('util')
6+
const { Request } = require('../../')
7+
8+
describe('Request custom inspection', () => {
9+
it('should return a custom inspect output', () => {
10+
const request = new Request('https://example.com/api', {
11+
method: 'POST',
12+
headers: {
13+
'Content-Type': 'application/json'
14+
}
15+
})
16+
17+
const inspectedOutput = util.inspect(request)
18+
19+
const expectedOutput = '{\n method: \'POST\',\n url: \'https://example.com/api\',\n headers: Headers { \'Content-Type\': \'application/json\' },\n destination: \'\',\n referrer: \'about:client\',\n referrerPolicy: \'\',\n mode: \'cors\',\n credentials: \'same-origin\',\n cache: \'default\',\n redirect: \'follow\',\n integrity: \'\',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}'
20+
assert.strictEqual(inspectedOutput, expectedOutput)
21+
})
22+
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const { describe, it } = require('node:test')
4+
const assert = require('assert')
5+
const util = require('util')
6+
const { Response } = require('../../')
7+
8+
describe('Response custom inspection', () => {
9+
it('should return a custom inspect output', () => {
10+
const response = new Response(null)
11+
const inspectedOutput = util.inspect(response, {
12+
depth: null,
13+
getters: true
14+
})
15+
16+
const expectedOutput = `Response {
17+
status: 200,
18+
statusText: '',
19+
headers: Headers {},
20+
body: null,
21+
bodyUsed: false,
22+
ok: true,
23+
redirected: false,
24+
type: 'default',
25+
url: ''
26+
}`
27+
28+
assert.strictEqual(inspectedOutput, expectedOutput)
29+
})
30+
})

0 commit comments

Comments
 (0)