Skip to content

Convert received JSON objects to Masto.js interface #1329

Answered by neet
austinhuang0131 asked this question in Q&A
Discussion options

You must be logged in to vote

The API entities exported from Masto.js are just TypeScript annotations which does not include any runtime code. Therefore, if you cast an arbitrary object as a type from Masto.js, you can make TypeScript recognise it as a Mastodon entity:

import { mastodon } from "masto";

let myEntity = {...};
let myEntityAnnotated = myEntity as mastodon.v1.Admin.Account;

However, there are subtle differences between the original data structure returned from Mastodon API and type annotations from Masto.js. Masto.js internally converts a response body from snake_case to camelCase in a module named Serializer:
https://github.com/neet/masto.js/blob/1ea987575960e1bf937999b0d8e062fe453bd0e4/src/adapters/seri…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by austinhuang0131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants