Renaming data sourced from an API
Finally getting a chance to play around wth AT again! I'm working with an annoying snake_cased REST API, wondering the cleanest way to rename keys? Just a manual morph I'd assume?
16 Replies
The Rust version which I am trying to 1:1 has a sweet
#[serde(rename_all)], so wondering if there's anything similar or at least what the recommended solution isyeah you'd have to use a morph but you could write a wrapper type that would be pretty straightforward to apply the morph automatically
there is an open issue for morph keys where if you specify a morph as the key itself it would automatically be renamed, but it doesn't even help a ton here necessarily where you want to apply a deterministic transformation to all key names
but it should be a write once use neatly everywhere situation
especially with the generic prowess you demonstrated implementing pattern matching
i still have no idea how i made that work
https://tsplay.dev/WJglkw
pretty easy :p
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
pretty perfect just missing the
& unknown at the end of renameKeys so the type displays a little cleaneroh interesting does that force it to evaluate or something?
yeah it's like the
show utility from @ark/util but if you already have a mapped type you can just do that instead of wrapping it againyeah i actually used show the first time around and when i removed it it looked fine
it's fine after you
infer because it has that built in, but when you hover the Type without inferring it it just shows renameKeys<{fldhs_dofihsd: fodsiha}>I forgot that doing type-level stuff can actually be pretty fun :)
*when it works
it always works
it works when you're smart like you
this took me a little bit to make lmao
smart === wasted prime of life writing ts types
just know how much time you've saved people with it
just need a way to quantify that
seven units