Skip to content

Suggestion to auto convert date value from type string to type Date #1429

@soapwong703

Description

@soapwong703

What is the problem this feature would solve?

Currently in the @kubb/plugin-ts, it supports to generate Date value type to Date with the config dateType: true, but the actual value returned is still string type, as the result we need to manually convert the value to match its type with a custom client.

External documents/projects?

No response

What is the feature you are proposing to solve the problem?

I suggest adding an config to handle the value conversion.

What alternatives have you considered?

Currently we need to handle it with a custom client with manual checking on key and content-type.

const dateKeyRegex = /date$/i;

export const axiosInstance = axios.create({
  transformResponse: (data, headers) => {
    if (headers["content-type"] === "application/json") {
      return JSON.parse(data, (key, value) =>
        dateKeyRegex.test(key) ? new Date(value) : value,
      );
    }
    return data;
  },
});

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions