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 6 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
14 changes: 14 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,19 @@ class FormData {
this[kState].push(entry)
}
}

toString () {
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
let output = 'FormData:\n'
this[kState].forEach(entry => {
output += `${entry.name}: ${entry.value}\n`
})

return output
}

[nodeUtil.inspect.custom] (depth, options) {
return this.toString()
}
}

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
12 changes: 12 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,17 @@ 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 headersString = nodeUtil.inspect(headersList)

return `Response { status: ${status}, statusText: "${statusText}", headers: ${headersString} }`
}
}

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

const { FormData } = require('../../')
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', 'john@example.com')

const expectedOutput = 'FormData:\nusername: john_doe\nemail: john@example.com\n'

assert.strictEqual(formData.toString(), expectedOutput)
})
23 changes: 23 additions & 0 deletions test/fetch/request-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'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)
})
})
19 changes: 19 additions & 0 deletions test/fetch/response-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'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)
const expectedOutput = `Response { status: 200, statusText: "", headers: HeadersList {
cookies: null,
[Symbol(headers map)]: Map(0) {},
[Symbol(headers map sorted)]: null
} }`
assert.strictEqual(inspectedOutput, expectedOutput)
})
})
Loading