valkyriecoms-js v0.0.17
valk.js
JavaScript(TypeScript) library for Gcoms
The following is provided: -User authentication -API request -Friting -Utility function -Gcoms various types definitions
The corresponding MissKey version is 12 or more.
Install
npm i valk-js
Usage
Imports are convenient to be done together as follows.
import * as Valkyrie from 'valkyriecoms-js';
For convenience, the following code examples are based on the assumption that you are importing as * as Valkyrie
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 valkAPI } from 'valk-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 Valkyrie.api.APIClient({
origin: 'https://app.valkyriecoms.com',
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
valk.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 Gcoms.Stream('https://app.valkyriecoms.com', { 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 Gcoms.Stream('https://app.valkyriecoms.com', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
With parameters
const stream = new Gcoms.Stream('https://app.valkyriecoms.com', { token: 'TOKEN' });
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
Disconnect from channel
Channel
Call the class's dispose
method.
const stream = new Gcoms.Stream('https://app.valkyriecoms.com', { 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 Gcoms.Stream('https://app.valkyriecoms.com', { 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 Gcoms.Stream('https://app.valkyriecoms.com', { 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.valkyriecoms.com', { 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.valkyriecoms.com/', { 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
1 year ago