npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

misskey-js

v2024.3.1

Published

Misskey SDK for JavaScript

Downloads

2,004

Readme

misskey.js

Strongly-typed official Misskey SDK for browsers/Node.js.

Test codecov

NPM

JavaScript(TypeScript)用の公式MisskeySDKです。ブラウザ/Node.js上で動作します。

以下が提供されています:

  • ユーザー認証
  • APIリクエスト
  • ストリーミング
  • ユーティリティ関数
  • Misskeyの各種型定義

対応するMisskeyのバージョンは12以上です。

Install

npm i misskey-js

Usage

インポートは以下のようにまとめて行うと便利です。

import * as Misskey from 'misskey-js';

便宜上、以後のコード例は上記のように* as Misskeyとしてインポートしている前提のものになります。

ただし、このインポート方法だとTree-Shakingできなくなるので、コードサイズが重要なユースケースでは以下のような個別インポートをお勧めします。

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

Authenticate

todo

API request

APIを利用する際は、利用するサーバーの情報とアクセストークンを与えてAPIClientクラスのインスタンスを初期化し、そのインスタンスのrequestメソッドを呼び出してリクエストを行います。

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

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

requestの第一引数には呼び出すエンドポイント名、第二引数にはパラメータオブジェクトを渡します。レスポンスはPromiseとして返ります。

Streaming

misskey.jsのストリーミングでは、二つのクラスが提供されます。 ひとつは、ストリーミングのコネクション自体を司るStreamクラスと、もうひとつはストリーミング上のチャンネルの概念を表すChannelクラスです。 ストリーミングを利用する際は、まずStreamクラスのインスタンスを初期化し、その後でStreamインスタンスのメソッドを利用してChannelクラスのインスタンスを取得する形になります。

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

コネクションが途切れても自動で再接続されます。

チャンネルへの接続

チャンネルへの接続はStreamクラスのuseChannelメソッドを使用します。

パラメータなし

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

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

パラメータあり

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

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

チャンネルから切断

Channelクラスのdisposeメソッドを呼び出します。

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

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

mainChannel.dispose();

メッセージの受信

ChannelクラスはEventEmitterを継承しており、メッセージがサーバーから受信されると受け取ったイベント名でペイロードをemitします。

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

メッセージの送信

Channelクラスのsendメソッドを使用してメッセージをサーバーに送信することができます。

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

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

コネクション確立イベント

Streamクラスの_connected_イベントが利用可能です。

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

コネクション切断イベント

Streamクラスの_disconnected_イベントが利用可能です。

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

コネクションの状態

Streamクラスのstateプロパティで確認できます。

  • initializing: 接続確立前
  • connected: 接続完了
  • reconnecting: 再接続中