0

I am issuing a request with node-fetch and receiving the following response (as it appears when I console.log it):

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 c0 bd 60 05 00 00 00 00 c0 bd 60 05 00 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 be ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _maxOutputLength: 4294967295,
      _level: -1,
      _strategy: 0,
      [Symbol(kCapture)]: false,
      [Symbol(kTransformState)]: [Object],
      [Symbol(kError)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://accounts.spotify.com/api/token',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

My question

How do I extract everything under [Symbol(Response internals)] as an object?

Clarification

I am aware that the individual properties listed under [Symbol(Response internals)] can be accessed as properties of my response. For instance,

const response = await fetch(..);
console.log(response.statusText)

will give me back that 'OK' string. I am also aware of this thread, which solves that problem (but not mine).

What I asking is how to reify [Symbol(Response internals)] as a whole and refer to it.

Additional comments

I (think I) know what Symbol does, and I can retrieve the symbols

[Symbol(Body internals), Symbol(Response internals)]

by running Object.getOwnPropertySymbols() on my response.

But how to extract the values (objects) attached to those symbols?

Object.values() doesn't do this, and Object.entries() doesn't either.

grobber
  • 781
  • 4
  • 13
  • 1
    A symbol is just a key, you need to do `obj[symbol]` and you'd get the value for it. However, it's a code smell. The symbols are harder to get for a reason - you normally shouldn't rely on them. If you're supposed to, then you'd have an exposed symbol to readily use as a key. – VLAZ Apr 08 '21 at 13:39
  • I understand (and agree on the smell): it was mostly curiosity, and my beeing needled by my inability to do this, initially, and the lack of info online (I tried..). I managed it in the end (see the answer below). – grobber Apr 08 '21 at 13:41
  • you can find the Symbol [here](https://github.com/node-fetch/node-fetch/blob/cbd4d895767e33491219e40013dd8daa5c7ac024/src/response.js#L11) – Jonas Wilms Apr 08 '21 at 14:03

1 Answers1

0

Ah! This does it:

const response = fetch(..)
console.log(response[Object.getOwnPropertySymbols(response)[1])

This will give me

{
  url: 'https://accounts.spotify.com/api/token',
  status: 200,
  statusText: 'OK',
  headers: Headers {
    [Symbol(map)]: [Object: null prototype] {
      date: [Array],
      'content-type': [Array],
      'set-cookie': [Array],
      'sp-trace-id': [Array],
      'strict-transport-security': [Array],
      'x-content-type-options': [Array],
      'content-encoding': [Array],
      vary: [Array],
      server: [Array],
      via: [Array],
      'alt-svc': [Array],
      connection: [Array],                                 
'transfer-encoding': [Array]
    }
  },                                                   
counter: 0
}

This is what was listed under the second symbol property, Symbol(Response internals).

Edit

I also found, in the end, a relevant prior discussion here on SO.

It also describes some more robust solutions, not relying on the order of the symbols (RE: the comment below touching on this).

grobber
  • 781
  • 4
  • 13