FoundKey/packages/foundkey-js
Francis Dinh a16d7d9dc2
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
foundkey-js: fix no-param-reassign lint in acct.ts
2022-09-06 11:36:21 -04:00
..
etc Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
src foundkey-js: fix no-param-reassign lint in acct.ts 2022-09-06 11:36:21 -04:00
test Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
test-d Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
.editorconfig Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
.eslintignore migrate to Yarn 3.2.3 2022-08-26 15:24:29 -04:00
.eslintrc.cjs migrate to Yarn 3.2.3 2022-08-26 15:24:29 -04:00
.gitignore Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
api-extractor.json Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
CHANGELOG.md foundkey-js: make DriveFile.comment nullable 2022-08-31 06:55:31 -04:00
jest.config.ts Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
LICENSE Add 'packages/foundkey-js/' from commit '637c6f1411c9c03f204f21da12e1f8e390d05cb8' 2022-08-25 21:39:55 -04:00
package.json update eslint plugins 2022-09-06 11:19:34 -04:00
README.md foundkey-js: update and translate README 2022-08-31 19:18:56 -04:00
tsconfig.json client: Use foundkey-js instead of misskey-js 2022-08-26 01:25:56 -04:00

foundkey-js

foundkey-js is a fork of misskey-js that is more up to date.

The following is provided:

  • User authentication
  • API requests
  • Streaming
  • Utility functions
  • Various Misskey type definitions

This library is designed to work with FoundKey. It should also work with Misskey 12+ but compatibility is not guaranteed.

Install

This package is not currently published to npmjs.

Usage

To use foundkey-js in your code, use the following import:

import * as Misskey from 'foundkey-js';

For convenience, the following code examples are based on the assumption that the code is imported as * as Misskey as shown above.

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.

import { api as misskeyApi } from 'foundkey-js';

Authenticate

todo

API request

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.

const cli = new Misskey.api.APIClient({
	origin: 'https://misskey.test',
	credential: 'TOKEN',
});

const meta = await cli.request('meta', { detail: true });

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.

Streaming

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.

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
	console.log('notification received', notification);
});

If a connection is lost, it is automatically reconnected.

Connecting to a channel

Connection to a channel is made using the useChannel method of the Stream class.

No parameters

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });

const mainChannel = stream.useChannel('main');

With parameters

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });

const messagingChannel = stream.useChannel('messaging', {
	otherparty: 'xxxxxxxxxx',
});

Disconnect from a channel

Call the dispose method of the Channel class.

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });

const mainChannel = stream.useChannel('main');

mainChannel.dispose();

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.

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
	console.log('notification received', notification);
});

Sending messages

Messages can be sent to the server using the send method of the Channel class.

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const messagingChannel = stream.useChannel('messaging', {
	otherparty: 'xxxxxxxxxx',
});

messagingChannel.send('read', {
	id: 'xxxxxxxxxx'
});

_connected_ event

The _connected_ event of the Stream class is available.

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
stream.on('_connected_', () => {
	console.log('connected');
});

_disconnected_ event

The _disconnected_ event of the Stream class is available.

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
stream.on('_disconnected_', () => {
	console.log('disconnected');
});

Connection state

You can check the state property of the Stream class.

  • initializing: before connection is established
  • connected: connected.
  • reconnecting: reconnecting.