Skip to content

Respondable Json instance should return json blob instead of a string #55

Closed
@cdepillabout

Description

@cdepillabout

The Respondable instance for Json looks like this:

responseTypeToString :: forall a. (ResponseType a) -> String
responseTypeToString ArrayBufferResponse = "arraybuffer"
responseTypeToString BlobResponse = "blob"
responseTypeToString DocumentResponse = "document"
responseTypeToString JSONResponse = "text" -- IE doesn't support "json" responseType
responseTypeToString StringResponse = "text"

type ResponseContent = Foreign

type F = Either ForeignError

class Respondable a where
  responseType :: Tuple (Maybe MimeType) (ResponseType a)
  fromResponse :: ResponseContent -> F a

instance responsableJson :: Respondable Json where
  responseType = Tuple (Just applicationJSON) JSONResponse
  fromResponse = Right <<< unsafeCoerce

Since responseTypeToString JSONResponse returns "text" instead of a "json", the fromResponse function is basically just wrapping a JavaScript string instead of a JSON blob.

This causes functions from Data.Argonaut.Decode (like toObject) to fail when they really should be succeeding.

I think the Respondable Json instance should look something like this:

import Data.Argonaut.Parser (jsonParser)

instance responsableJson :: Respondable Json where
  responseType = Tuple (Just applicationJSON) JSONResponse
  fromResponse = jsonParser <=< readString

This will give you a correct blob of JSON data and things like toObject will succeed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions