glynnbird commented on code in PR #314:
URL: https://github.com/apache/couchdb-nano/pull/314#discussion_r1842057816


##########
lib/nano.js:
##########
@@ -168,29 +163,48 @@ module.exports = exports = function dbScope (cfg) {
     delete responseHeaders.server
     delete responseHeaders['content-length']
 
-    /* if (opts.dontParse) {
-      parsed = body
-    } else {
-      try { parsed = JSON.parse(body) } catch (err) { parsed = body }
-    } */
-
     if (statusCode >= 200 && statusCode < 400) {
-      log({ err: null, body, headers: responseHeaders })
-      if (resolve) {
-        resolve(body)
+      // collect response
+      const ct = response.headers.get('content-type')
+      let retval = ''
+      if (ct === 'application/json') {
+        try {
+          retval = await response.json()
+        } catch {
+          // do nothing
+        }
+      } else if (ct && (ct.startsWith('text/') || 
ct.startsWith('multipart/related'))) {
+        retval = await response.text()
+      } else {
+        const ab = await response.arrayBuffer()
+        retval = Buffer.from(ab)
       }
-      if (callback) {
-        callback(null, body, responseHeaders)
+
+      // log
+      log({ err: null, retval, headers: responseHeaders })
+
+      // promisey
+      if (resolve) {
+        resolve(retval)
+      } else if (callback) {
+        // callbacky
+        callback(null, retval, Object.fromEntries(response.headers))
       }
       return
     }
 
     // cloudant stacktrace
+    try {
+      body = await response.json()
+    } catch (e) {
+      body = ''
+    }
+
     if (typeof body === 'string') {
       body = { message: body }
     }
 
-    if (!body.message && (body.reason || body.error)) {
+    if (body && !body.message && (body.reason || body.error)) {

Review Comment:
   yeah. the `if (body...` bit isn't really doing anything in the post fetch 
world :(



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to