Convert received JSON objects to Masto.js interface #1329
-
|
Hello, Suppose I'm using the moderation webhook where the Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
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 Currently, we do not export import { mastodon } from "masto";
import { SerializerNativeImpl } from "masto/adapters/serializers/serializer-native-impl.js";
const serializer = new SerializerNativeImpl();
serializer.deserialize<mastodon.v1.Admin.Account>("json", `{"snake_cased_property": "value"}`)
// --> {"snakeCasedProperty":"value"}Please keep in mind that I think is reasonable to make it public for the Webhook developers. I will consider adding it in the next version. |
Beta Was this translation helpful? Give feedback.
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:
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_casetocamelCasein a module namedSerializer:https://github.com/neet/masto.js/blob/1ea987575960e1bf937999b0d8e062fe453bd0e4/src/adapters/seri…