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 2 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
9 changes: 9 additions & 0 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ 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
}
}

iteratorMixin('FormData', FormData, kState, 'name', 'value')
Expand Down
8 changes: 8 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('util')
const { kEnumerableProperty } = util
const {
isValidReasonPhrase,
Expand Down Expand Up @@ -252,6 +253,13 @@ 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
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)
})
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