acacode / acacode/swagger-typescript-api

Mapping schema iso Dates to js Date

未關閉
#656 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
TypeScript
星號
4.1k
分支
436
PR 合併指標
30 天內沒有已合併 PR

描述

# Context

I understand I can use this

```
primitiveTypeConstructs: (struct) => ({
string: {
date: 'Date',
},
})
```

To make sure Schema date is mapped to js Date.
But this will just change TS type in the api response interfaces,

```
interface ResponseType {
someDate: string; // becomes Date
}
```

It will not actually make the generated api transform those date strings into a data like `const mappedDate = new Date(isoString)`

# My Question

Is there a way to make sure the actual api created already maps the dates it finds in the response to js Date.

I can think of one way of doing this which would be to write my own axios interceptor to do this. Something like this https://weblog.west-wind.com/posts/2014/jan/06/javascript-json-date-parsing-and-real-dates and then the interceptor could do
```
var date = JSON.parse(JSON.stringify(data),JSON.dateParser);
```

I am thinking about his coming from GraphQL where I can use an Apollo client transformer to auto cast all iso strings to dates.

Please let me know if there is another/better way to do this.

Thanks

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。