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
log(n)
log(n)OP4d ago
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 is
ssalbdivad
ssalbdivad4d ago
yeah 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
log(n)
log(n)OP4d ago
i still have no idea how i made that work
log(n)
log(n)OP4d ago
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.
ssalbdivad
ssalbdivad4d ago
pretty perfect just missing the & unknown at the end of renameKeys so the type displays a little cleaner
log(n)
log(n)OP4d ago
oh interesting does that force it to evaluate or something?
ssalbdivad
ssalbdivad4d ago
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 again
log(n)
log(n)OP4d ago
yeah i actually used show the first time around and when i removed it it looked fine
ssalbdivad
ssalbdivad4d ago
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}>
log(n)
log(n)OP4d ago
I forgot that doing type-level stuff can actually be pretty fun :) *when it works
ssalbdivad
ssalbdivad4d ago
it always works
log(n)
log(n)OP4d ago
it works when you're smart like you this took me a little bit to make lmao
ssalbdivad
ssalbdivad4d ago
smart === wasted prime of life writing ts types
log(n)
log(n)OP4d ago
just know how much time you've saved people with it
ssalbdivad
ssalbdivad4d ago
just need a way to quantify that
log(n)
log(n)OP4d ago
seven units

Did you find this page helpful?