diff --git a/lib/web/fetch/formdata.js b/lib/web/fetch/formdata.js index b8c4ccfc902..0e95a534f11 100644 --- a/lib/web/fetch/formdata.js +++ b/lib/web/fetch/formdata.js @@ -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 @@ -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') diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index dc9fac83570..82de276060b 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -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, @@ -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) diff --git a/lib/web/fetch/response.js b/lib/web/fetch/response.js index e31f619590f..bcb54237fcc 100644 --- a/lib/web/fetch/response.js +++ b/lib/web/fetch/response.js @@ -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, @@ -252,6 +253,26 @@ 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) { + if (options.depth === null) { + options.depth = 2 + } + + const properties = { + status: this.status, + statusText: this.statusText, + headers: this.headers, + 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) diff --git a/test/fetch/formdata-inspect-custom.js b/test/fetch/formdata-inspect-custom.js new file mode 100644 index 00000000000..e52ccdf3fdd --- /dev/null +++ b/test/fetch/formdata-inspect-custom.js @@ -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', 'john@example.com') + + const expectedOutput = 'FormData:\nusername: john_doe\nemail: john@example.com\n' + + assert.deepStrictEqual(inspect(formData), expectedOutput) +}) diff --git a/test/fetch/request-inspect-custom.js b/test/fetch/request-inspect-custom.js new file mode 100644 index 00000000000..7f4290d1d4e --- /dev/null +++ b/test/fetch/request-inspect-custom.js @@ -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) + }) +}) diff --git a/test/fetch/response-inspect-custom.js b/test/fetch/response-inspect-custom.js new file mode 100644 index 00000000000..bf72a053a6e --- /dev/null +++ b/test/fetch/response-inspect-custom.js @@ -0,0 +1,30 @@ +'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: Headers {}, + body: null, + bodyUsed: false, + ok: true, + redirected: false, + type: 'default', + url: '' +}` + + assert.strictEqual(inspectedOutput, expectedOutput) + }) +})