Skip to content

Commit 8fb8ac5

Browse files
authored
Merge pull request #1309 from neet/add-extensions-docs
chore: Add documentations for declaration merging
2 parents 8f5240d + 82ef9ef commit 8fb8ac5

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

EXTENSIONS.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Extensions
2+
3+
Masto.js allows you to extend its functionality to enhance support for various Mastodon forks. This document outlines how to extend Masto.js to support additional features or endpoints.
4+
5+
## Extending REST API Entities
6+
7+
Masto.js uses [Proxy API](./CONTRIBUTING.md) to build a network request, which does not include the actual implementation of the API. Instead, each access to properties and methods will be converted to a `Request` object. This allows you to easily enhance the functionality of Masto.js without adding a runtime code.
8+
9+
We support TypeScript’s [declaration merging](https://www.typescriptlang.org/docs/handbook/2/modules.html#declaration-merging) to extend existing entities. This allows you to add properties to existing entities without modifying the original code.
10+
11+
For example, if you want to add an `extraProperty` to the `mastodon.v2.Filter` entity, you can create a `global.d.ts` file in your project with the following content:
12+
13+
```ts
14+
// global.d.ts
15+
// make sure this is included in your tsconfig.json
16+
declare module "masto/mastodon/entities/v2/index.js" {
17+
interface Filter {
18+
extraProperty: string;
19+
}
20+
}
21+
```
22+
23+
You can even extend [Union Types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) like `FilterContext`. This case we adopt a _registry_ pattern. Try adding a property `"extra"` with `never` type to the `FilterContextRegistry` interface:
24+
25+
```ts
26+
declare module "masto/mastodon/entities/v2/index.js" {
27+
interface FilterContextRegistry {
28+
extra: never;
29+
}
30+
}
31+
```
32+
33+
With these changes, you can now use the following code
34+
35+
```ts
36+
const filter = await rest.v2.filters.create({
37+
title: "test",
38+
context: ["home", "extra"],
39+
});
40+
41+
console.log(filter.extraProperty);
42+
```
43+
44+
If you would like to identify which module you need to extend, you can see `./src/mastodon/entities` directory to find the original implementation.
45+
46+
## Extending REST API Endpoints
47+
48+
You can also extend the REST API endpoints to add new methods or properties. This is useful if you want to add support for a new Mastodon fork that has additional endpoints or features.
49+
50+
```ts
51+
declare module "masto/mastodon/rest/v2/index.js" {
52+
interface CreateFilterParams {
53+
extraProperty: string;
54+
}
55+
56+
interface Filters$SelectKeywordsResource {
57+
someExtraMethod(): void;
58+
}
59+
}
60+
```
61+
62+
With this change, you can now use the following code to create a filter with an extra property and call a method on the `keywords` resource:
63+
64+
```ts
65+
const filter = await rest.v2.filters.create({
66+
title: "test",
67+
context: ["home", "extra"],
68+
extraProperty: "extraValue",
69+
});
70+
71+
// POST /api/v2/filters/123/keywords/some_extra_method
72+
await rest.v2.filters.$select("123").keywords.someExtraMethod();
73+
```
74+
75+
If you would like to identify which module you need to extend, you can see `./src/mastodon/rest` directory to find the original implementation.
76+
77+
## Extending WebSocket API
78+
79+
You can also extend the WebSocket API endpoints to add new methods or properties. This is useful if you want to add support for a new Mastodon fork that has additional endpoints or features.
80+
81+
```ts
82+
declare module "masto/mastodon/streaming/index.js" {
83+
interface PublicResource {
84+
extra: {
85+
subscribe(): Subscription;
86+
};
87+
}
88+
}
89+
```
90+
91+
With this change, you can now use the following code to subscribe to the `extra` stream:
92+
93+
```ts
94+
// { "type": "subscribe", "stream": "extra", "params": {} }
95+
streaming.public.extra.subscribe();
96+
```
97+
98+
If you would like to identify which module you need to extend, you can see `./src/mastodon/streaming` directory to find the original implementation.
99+
100+
If you have any questions or need help extending Masto.js, feel free to open an issue or ask in the [Discussions](https://github.com/neet/masto.js/discussions). We are always happy to help!

0 commit comments

Comments
 (0)