Skip to content

Response format - global default or override ? #251

Closed
@Liwoj

Description

@Liwoj

Hi

First thanks for great generator - really like it! Great work!

In release 8.0.0 there was this commit which introduced feature "Allow to set global response type format through HttpClient constructor"

It was implemented both in Axios and Fetch clients as const responseFormat = format && requestParams.format; which works more like a global override (format passed into the constructor takes precedence over format passed into request)

It was later changed to const responseFormat = format || requestParams.format; with this commit so it works more like a global default. But the change was made only in Fetch template - in Axios it is still implemented as global override...

As the current Fetch template behavior makes more sense, I guess it is actually a bug in the Axios template.

And that brings me to other part of the question.
I do not want the Axios (or underlying XmlHttpRequest) to run a JSON.parse on my responses. Reason is I'm using custom JSON.parse reviver function to parse all strings in the correct format directly to Date. So code generated with current version of swagger-typescript-api when used with current version of Axios (0.21.1) and passing format: 'text' into Api constructor works just fine. But if the problem mentioned above will be fixed and format: 'json' generated for my API routes starts overriding global default, it will break because format is passed into Axios param responseType and that is passed into responseType of XmlHttpRequest - as a result Axios starts to return JS objets parsed from JSON instead of just text...

This might not be an issue in the end thanks to this recent change in Axios (not yet released) but I'm not sure (Axios development is a bit chaotic for me to follow) what is plan for the future (see this change in 1.0.0 branch)

So the question is, what is the easiest way to fix this ? Override the generation of format: 'json' for my routes ? Or something else ?

Thanks!

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