2022-08-31 23:18:56 +00:00
# foundkey-js
`foundkey-js` is a fork of [misskey-js ](https://github.com/misskey-dev/misskey.js ) that is more up to date.
2022-08-26 01:39:55 +00:00
2022-08-31 23:18:56 +00:00
The following is provided:
- User authentication
- API requests
- Streaming
- Utility functions
2022-09-12 08:21:29 +00:00
- Various foundkey type definitions
2022-08-26 01:39:55 +00:00
2022-08-31 23:18:56 +00:00
This library is designed to work with FoundKey. It should also work with Misskey 12+ but compatibility is not guaranteed.
2022-08-26 01:39:55 +00:00
## Install
2022-08-31 23:18:56 +00:00
This package is not currently published to npmjs.
2022-08-26 01:39:55 +00:00
# Usage
2022-08-31 23:18:56 +00:00
To use `foundkey-js` in your code, use the following import:
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
import * as foundkey from 'foundkey-js';
2022-08-26 01:39:55 +00:00
```
2022-09-12 08:21:29 +00:00
For convenience, the following code examples are based on the assumption that the code is imported as `* as foundkey` as shown above.
2022-08-26 01:39:55 +00:00
2022-08-31 23:18:56 +00:00
However, since tree-shaking is not possible with this import method, we recommend the following individual import for use cases where code size is important.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
import { api as foundkeyApi } from 'foundkey-js';
2022-08-26 01:39:55 +00:00
```
## Authenticate
todo
## API request
2022-08-31 23:18:56 +00:00
When using the API, initialize an instance of the `APIClient` class by providing information on the server to be used and an access token, and make a request by calling the `request` method of the instance.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const cli = new foundkey.api.APIClient({
origin: 'https://foundkey.example',
2022-08-26 01:39:55 +00:00
credential: 'TOKEN',
});
const meta = await cli.request('meta', { detail: true });
```
2022-08-31 23:18:56 +00:00
The first argument of `request` is the name of the endpoint to call, and the second argument is a parameter object. The response is returned as a Promise.
2022-08-26 01:39:55 +00:00
## Streaming
2022-08-31 23:18:56 +00:00
Two classes are provided for streaming in `foundkey-js` .
One is the `Stream` class, which handles the streaming connection itself, and the other is the `Channel` class, which represents the concept of a channel on the streaming.
When using streaming, you first initialize an instance of the `Stream` class, and then use the methods of the `Stream` instance to get an instance of the `Channel` class.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
console.log('notification received', notification);
});
```
2022-08-31 23:18:56 +00:00
If a connection is lost, it is automatically reconnected.
2022-08-26 01:39:55 +00:00
2022-08-31 23:18:56 +00:00
### Connecting to a channel
Connection to a channel is made using the `useChannel` method of the `Stream` class.
2022-08-26 01:39:55 +00:00
2022-08-31 23:18:56 +00:00
No parameters
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
const mainChannel = stream.useChannel('main');
```
2022-08-31 23:18:56 +00:00
With parameters
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
```
2022-08-31 23:18:56 +00:00
### Disconnect from a channel
Call the `dispose` method of the `Channel` class.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
const mainChannel = stream.useChannel('main');
mainChannel.dispose();
```
2022-08-31 23:18:56 +00:00
### Receiving messages
The `Channel` class inherits from EventEmitter and when a message is received from the server, it emits a payload with the name of the event received.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
console.log('notification received', notification);
});
```
2022-08-31 23:18:56 +00:00
### Sending messages
Messages can be sent to the server using the `send` method of the `Channel` class.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
messagingChannel.send('read', {
id: 'xxxxxxxxxx'
});
```
2022-08-31 23:18:56 +00:00
### `_connected_` event
The `_connected_` event of the `Stream` class is available.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
stream.on('_connected_', () => {
console.log('connected');
});
```
2022-08-31 23:18:56 +00:00
### `_disconnected_` event
The `_disconnected_` event of the `Stream` class is available.
2022-08-26 01:39:55 +00:00
``` ts
2022-09-12 08:21:29 +00:00
const stream = new foundkey.Stream('https://foundkey.example', { token: 'TOKEN' });
2022-08-26 01:39:55 +00:00
stream.on('_disconnected_', () => {
console.log('disconnected');
});
```
2022-08-31 23:18:56 +00:00
### Connection state
You can check the `state` property of the `Stream` class.
2022-08-26 01:39:55 +00:00
2022-08-31 23:18:56 +00:00
- `initializing` : before connection is established
- `connected` : connected.
- `reconnecting` : reconnecting.