barkle-js v1.0.0
barkle.js
Strongly-typed official Barkle SDK for browsers/Node.js.
JavaScript(TypeScript) library for Barkle
The following is provided: -User authentication -API request -Friting -Utility function -Barkle various types definitions
The corresponding MissKey version is 12 or more.
Install
npm i barkle-js
Usage
Imports are convenient to be done together as follows.
import * as Barkle from 'barkle-js';
For convenience, the following code examples are based on the assumption that you are importing as * as Barkle
as above.
However, this import method does not allow Tree-Shaking, so we recommend the following individual imports for use cases where code size is important.
import { api as barkleAPI } from 'barkle-js';
Authenticate
todo
API request
When using the API, give the information of the server to be used and the access token, initialize the instance of the APIClient
class, and call therequest
method of that instance to make a request.
const cli = new Barkle.api.APIClient({
origin: 'https://app.barkle.xyz',
credential: 'TOKEN',
});
const meta = await cli.request('meta', { detail: true });
request
Pass the endpoint name to be called in the first argument of, and the parameter object in the second argument. The response is returned as a promise.
Streaming
barkle.js Streaming provides two classes.
One is the Stream
class, which controls the streaming connection itself, and the other is theChannel
class, which represents the concept of streaming channels.
When using streaming, first initialize the instance of the Stream
class, and then use the method of theStream
instance to get the instance of the Channel
class.
const stream = new Barkle.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
console.log('notification received', notification);
});
Even if the connection is lost, it will be automatically reconnected.
Connect to channel
To connect to a channel, use the useChannel
method of theStream
class.
No parameters
const stream = new Barkle.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
With parameters
const stream = new Barkle.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
Disconnect from channel
Channel
Call the class's dispose
method.
const stream = new Barkle.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.dispose();
Receive message
Channel
The class inherits from EventEmitter and emits the payload with the event name it receives when a message is received from the server.
const stream = new Barkle.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
console.log('notification received', notification);
});
Send message
Channel
You can send a message to the server using the class's send
method.
const stream = new Barkle.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
messagingChannel.send('read', {
id: 'xxxxxxxxxx'
});
Connection establishment event
Stream
The _connected_
event for the class is available.
const stream = new Misskey.Stream('https://app.barkle.xyz', { token: 'TOKEN' });
stream.on('_connected_', () => {
console.log('connected');
});
Connection disconnection event
Stream
The _disconnected_
event for the class is available.
const stream = new Misskey.Stream('https://app.barkle.xyz/', { token: 'TOKEN' });
stream.on('_disconnected_', () => {
console.log('disconnected');
});
Connection status
Stream
You can see it in the class's state
property.
initializing
: Before establishing a connectionconnected
: Connection completedreconnecting
: Reconnecting