Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: improve output for FormData, Response, Request #2955

Merged
merged 11 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { kEnumerableProperty } = require('../../core/util')
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
const { webidl } = require('./webidl')
const { File: NativeFile } = require('node:buffer')
const nodeUtil = require('node:util')

/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile
Expand Down Expand Up @@ -154,6 +155,15 @@ class FormData {
this[kState].push(entry)
}
}

[nodeUtil.inspect.custom] (depth, options) {
let output = 'FormData:\n'
this[kState].forEach(entry => {
output += `${entry.name}: ${entry.value}\n`
})

return output
}
}

iteratorMixin('FormData', FormData, kState, 'name', 'value')
Expand Down
27 changes: 27 additions & 0 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { extractBody, mixinBody, cloneBody } = require('./body')
const { Headers, fill: fillHeaders, HeadersList } = require('./headers')
const { FinalizationRegistry } = require('./dispatcher-weakref')()
const util = require('../../core/util')
const nodeUtil = require('node:util')
const {
isValidHTTPToken,
sameOrigin,
Expand Down Expand Up @@ -771,6 +772,32 @@ class Request {
// 4. Return clonedRequestObject.
return fromInnerRequest(clonedRequest, ac.signal, this[kHeaders][kGuard], this[kRealm])
}

[nodeUtil.inspect.custom] (depth, options) {
if (options.depth === null) {
options.depth = 2
}

const properties = {
method: this.method,
url: this.url,
headers: this.headers,
destination: this.destination,
referrer: this.referrer,
referrerPolicy: this.referrerPolicy,
mode: this.mode,
credentials: this.credentials,
cache: this.cache,
redirect: this.redirect,
integrity: this.integrity,
keepalive: this.keepalive,
isReloadNavigation: this.isReloadNavigation,
isHistoryNavigation: this.isHistoryNavigation,
signal: this.signal
}

return nodeUtil.formatWithOptions(options, { ...properties })
}
}

mixinBody(Request)
Expand Down
22 changes: 22 additions & 0 deletions lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { Headers, HeadersList, fill } = require('./headers')
const { extractBody, cloneBody, mixinBody } = require('./body')
const util = require('../../core/util')
const nodeUtil = require('node:util')
const { kEnumerableProperty } = util
const {
isValidReasonPhrase,
Expand Down Expand Up @@ -252,6 +253,27 @@ class Response {
// clonedResponse, this’s headers’s guard, and this’s relevant Realm.
return fromInnerResponse(clonedResponse, this[kHeaders][kGuard], this[kRealm])
}

[nodeUtil.inspect.custom] (depth, options) {
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
if (options.depth === null) {
options.depth = 2
}

const { status, statusText, headersList } = this[kState]
const properties = {
status,
statusText,
headers: headersList,
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
body: this.body,
bodyUsed: this.bodyUsed,
ok: this.ok,
redirected: this.redirected,
type: this.type,
url: this.url
}

return nodeUtil.formatWithOptions(options, `Response ${nodeUtil.inspect(properties)}`)
}
}

mixinBody(Response)
Expand Down
16 changes: 16 additions & 0 deletions test/fetch/formdata-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const { FormData } = require('../../')
const { inspect } = require('node:util')
const { test } = require('node:test')
const assert = require('node:assert')

test('FormData class custom inspection', () => {
const formData = new FormData()
formData.append('username', 'john_doe')
formData.append('email', '[email protected]')

const expectedOutput = 'FormData:\nusername: john_doe\nemail: [email protected]\n'

assert.deepStrictEqual(inspect(formData), expectedOutput)
})
22 changes: 22 additions & 0 deletions test/fetch/request-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const util = require('util')
const { Request } = require('../../')

describe('Request custom inspection', () => {
it('should return a custom inspect output', () => {
const request = new Request('https://example.com/api', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})

const inspectedOutput = util.inspect(request)

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}'
assert.strictEqual(inspectedOutput, expectedOutput)
})
})
34 changes: 34 additions & 0 deletions test/fetch/response-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const { describe, it } = require('node:test')
const assert = require('assert')
const util = require('util')
const { Response } = require('../../')

describe('Response custom inspection', () => {
it('should return a custom inspect output', () => {
const response = new Response(null)
const inspectedOutput = util.inspect(response, {
depth: null,
getters: true
})

const expectedOutput = `Response {
status: 200,
statusText: '',
headers: HeadersList {
cookies: null,
[Symbol(headers map)]: Map(0) {},
[Symbol(headers map sorted)]: null
},
body: null,
bodyUsed: false,
ok: true,
redirected: false,
type: 'default',
url: ''
}`

assert.strictEqual(inspectedOutput, expectedOutput)
})
})
Loading