Comparing version 1.1.0 to 2.0.0
import { CentMethods } from './cent-methods.enum'; | ||
import { StreamPosition } from './interfaces'; | ||
declare type UserParams = { | ||
@@ -29,6 +30,2 @@ user: string; | ||
declare type InfoParams = {}; | ||
export interface StreamPosition { | ||
offset: number; | ||
epoch: string; | ||
} | ||
export interface CentParams { | ||
@@ -35,0 +32,0 @@ [CentMethods.Publish]: PublishParams; |
@@ -1,6 +0,5 @@ | ||
export interface PublishResult { | ||
offset?: number; | ||
epoch?: string; | ||
} | ||
export interface OverrideResult { | ||
import { CentMethods } from './cent-methods.enum'; | ||
import { ClientInfo, Node, StreamPosition } from './interfaces'; | ||
export declare type PublishResponse = Partial<StreamPosition>; | ||
export interface OverrideResponse { | ||
presence?: boolean; | ||
@@ -11,33 +10,13 @@ join_leave?: boolean; | ||
} | ||
export interface ClientInfo { | ||
client: string; | ||
user: string; | ||
conn_info?: Record<string, any>; | ||
chan_info?: Record<string, any>; | ||
} | ||
export declare type PresenceResult = { | ||
export declare type PresenceResponse = { | ||
presence: Record<string, ClientInfo>; | ||
}; | ||
export declare type PresenceStatsResult = { | ||
export declare type PresenceStatsResponse = { | ||
num_clients: number; | ||
num_users: number; | ||
}; | ||
export declare type ChannelsResult = { | ||
channels: Record<string, Omit<PresenceStatsResult, 'num_users'>>; | ||
export declare type ChannelsResponse = { | ||
channels: Record<string, Omit<PresenceStatsResponse, 'num_users'>>; | ||
}; | ||
interface Node { | ||
name: string; | ||
num_channels: number; | ||
num_clients: number; | ||
num_users: number; | ||
uid: string; | ||
uptime: number; | ||
version: string; | ||
metrics: NodeMetrics; | ||
} | ||
interface NodeMetrics { | ||
interval: number; | ||
items: Record<string, number>; | ||
} | ||
export interface HistoryResult { | ||
export interface HistoryResponse { | ||
epoch: string; | ||
@@ -50,6 +29,19 @@ offset: string; | ||
} | ||
export declare type EmptyResult = {}; | ||
export declare type InfoResult = { | ||
export declare type EmptyResponse = {}; | ||
export declare type InfoResponse = { | ||
nodes: Node[]; | ||
}; | ||
export {}; | ||
export interface CentResponses { | ||
[CentMethods.Publish]: PublishResponse; | ||
[CentMethods.Broadcast]: PublishResponse[]; | ||
[CentMethods.Subscribe]: OverrideResponse; | ||
[CentMethods.Unsubscribe]: EmptyResponse; | ||
[CentMethods.Disconnect]: EmptyResponse; | ||
[CentMethods.Refresh]: EmptyResponse; | ||
[CentMethods.Presence]: PresenceResponse; | ||
[CentMethods.PresenceStats]: PresenceStatsResponse; | ||
[CentMethods.History]: HistoryResponse; | ||
[CentMethods.HistoryRemove]: EmptyResponse; | ||
[CentMethods.Channels]: ChannelsResponse; | ||
[CentMethods.Info]: InfoResponse; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const cent_methods_enum_1 = require("./cent-methods.enum"); |
import { CentOptions } from './cent-options.interface'; | ||
import { StreamPosition } from './cent-params.interface'; | ||
import { ChannelsResult, EmptyResult, HistoryResult, InfoResult, OverrideResult, PresenceResult, PresenceStatsResult, PublishResult } from './cent-responses.interface'; | ||
export declare class CentClient { | ||
private readonly options; | ||
constructor(options: CentOptions); | ||
private send; | ||
publish(channel: string, data: Record<string, any>, skip?: boolean): Promise<PublishResult>; | ||
broadcast(channels: string[], data: Record<string, any>, skip?: boolean): Promise<PublishResult[]>; | ||
subscribe(user: string, channel: string, client?: string): Promise<OverrideResult>; | ||
unsubscribe(user: string, channel: string, client?: string): Promise<EmptyResult>; | ||
disconnect(user: string, client?: string): Promise<EmptyResult>; | ||
refresh(user: string, client?: string): Promise<EmptyResult>; | ||
getPresence(channel: string): Promise<PresenceResult>; | ||
getPresenceStats(channel: string): Promise<PresenceStatsResult>; | ||
getHistory(channel: string, limit: number, since: StreamPosition, reverse?: boolean): Promise<HistoryResult>; | ||
removeHistory(channel: string): Promise<EmptyResult>; | ||
getChannels(pattern?: string): Promise<ChannelsResult>; | ||
getInfo(): Promise<InfoResult>; | ||
private methodFactory; | ||
publish: (params?: { | ||
channel: string; | ||
} & { | ||
data: Record<string, any>; | ||
} & { | ||
skip_history: boolean; | ||
}) => Promise<Partial<import("./interfaces").StreamPosition>>; | ||
broadcast: (params?: { | ||
data: Record<string, any>; | ||
} & { | ||
skip_history: boolean; | ||
} & { | ||
channels: string[]; | ||
}) => Promise<Partial<import("./interfaces").StreamPosition>[]>; | ||
subscribe: (params?: { | ||
user: string; | ||
} & { | ||
channel: string; | ||
} & { | ||
client: string; | ||
}) => Promise<import("./cent-responses.interface").OverrideResponse>; | ||
unsubscribe: (params?: { | ||
user: string; | ||
} & { | ||
channel: string; | ||
} & { | ||
client: string; | ||
}) => Promise<import("./cent-responses.interface").EmptyResponse>; | ||
disconnect: (params?: { | ||
user: string; | ||
} & { | ||
client: string; | ||
}) => Promise<import("./cent-responses.interface").EmptyResponse>; | ||
refresh: (params?: { | ||
user: string; | ||
} & { | ||
client: string; | ||
}) => Promise<import("./cent-responses.interface").EmptyResponse>; | ||
getPresence: (params?: { | ||
channel: string; | ||
}) => Promise<import("./cent-responses.interface").PresenceResponse>; | ||
getPresenceStats: (params?: { | ||
channel: string; | ||
}) => Promise<import("./cent-responses.interface").PresenceStatsResponse>; | ||
getHistory: (params?: { | ||
channel: string; | ||
} & { | ||
since?: import("./interfaces").StreamPosition; | ||
limit?: number; | ||
reverse?: boolean; | ||
}) => Promise<import("./cent-responses.interface").HistoryResponse>; | ||
removeHistory: (params?: { | ||
channel: string; | ||
}) => Promise<import("./cent-responses.interface").EmptyResponse>; | ||
getChannels: (params?: { | ||
pattern: string; | ||
}) => Promise<import("./cent-responses.interface").ChannelsResponse>; | ||
getInfo: (params?: {}) => Promise<import("./cent-responses.interface").InfoResponse>; | ||
} |
@@ -9,6 +9,18 @@ "use strict"; | ||
constructor(options) { | ||
this.publish = this.methodFactory(cent_methods_enum_1.CentMethods.Publish); | ||
this.broadcast = this.methodFactory(cent_methods_enum_1.CentMethods.Broadcast); | ||
this.subscribe = this.methodFactory(cent_methods_enum_1.CentMethods.Subscribe); | ||
this.unsubscribe = this.methodFactory(cent_methods_enum_1.CentMethods.Unsubscribe); | ||
this.disconnect = this.methodFactory(cent_methods_enum_1.CentMethods.Disconnect); | ||
this.refresh = this.methodFactory(cent_methods_enum_1.CentMethods.Refresh); | ||
this.getPresence = this.methodFactory(cent_methods_enum_1.CentMethods.Presence); | ||
this.getPresenceStats = this.methodFactory(cent_methods_enum_1.CentMethods.PresenceStats); | ||
this.getHistory = this.methodFactory(cent_methods_enum_1.CentMethods.History); | ||
this.removeHistory = this.methodFactory(cent_methods_enum_1.CentMethods.HistoryRemove); | ||
this.getChannels = this.methodFactory(cent_methods_enum_1.CentMethods.Channels); | ||
this.getInfo = this.methodFactory(cent_methods_enum_1.CentMethods.Info); | ||
this.options = options; | ||
} | ||
send(method, params) { | ||
return (0, axios_1.default)({ | ||
methodFactory(method) { | ||
return (params) => (0, axios_1.default)({ | ||
method: 'POST', | ||
@@ -28,66 +40,3 @@ url: this.options.host, | ||
} | ||
publish(channel, data, skip = false) { | ||
return this.send(cent_methods_enum_1.CentMethods.Publish, { | ||
channel, | ||
data, | ||
skip_history: skip | ||
}); | ||
} | ||
broadcast(channels, data, skip = false) { | ||
return this.send(cent_methods_enum_1.CentMethods.Broadcast, { | ||
channels, | ||
data, | ||
skip_history: skip | ||
}); | ||
} | ||
subscribe(user, channel, client) { | ||
return this.send(cent_methods_enum_1.CentMethods.Subscribe, { | ||
user, | ||
channel, | ||
client | ||
}); | ||
} | ||
unsubscribe(user, channel, client) { | ||
return this.send(cent_methods_enum_1.CentMethods.Unsubscribe, { | ||
user, | ||
channel, | ||
client | ||
}); | ||
} | ||
disconnect(user, client) { | ||
return this.send(cent_methods_enum_1.CentMethods.Disconnect, { | ||
user, | ||
client | ||
}); | ||
} | ||
refresh(user, client) { | ||
return this.send(cent_methods_enum_1.CentMethods.Refresh, { | ||
user, | ||
client | ||
}); | ||
} | ||
getPresence(channel) { | ||
return this.send(cent_methods_enum_1.CentMethods.Presence, { channel }); | ||
} | ||
getPresenceStats(channel) { | ||
return this.send(cent_methods_enum_1.CentMethods.PresenceStats, { channel }); | ||
} | ||
getHistory(channel, limit = 0, since, reverse = false) { | ||
return this.send(cent_methods_enum_1.CentMethods.History, { | ||
channel, | ||
limit, | ||
since, | ||
reverse | ||
}); | ||
} | ||
removeHistory(channel) { | ||
return this.send(cent_methods_enum_1.CentMethods.HistoryRemove, { channel }); | ||
} | ||
getChannels(pattern = '') { | ||
return this.send(cent_methods_enum_1.CentMethods.Channels, { pattern }); | ||
} | ||
getInfo() { | ||
return this.send(cent_methods_enum_1.CentMethods.Info, {}); | ||
} | ||
} | ||
exports.CentClient = CentClient; |
{ | ||
"name": "cent.js", | ||
"description": "Javascript library to communicate with Centrifugo HTTP API", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"scripts": { | ||
@@ -20,2 +20,8 @@ "build": "rimraf -rf dist && tsc -p tsconfig.json", | ||
"license": "MIT", | ||
"keywords": [ | ||
"centrifugo", | ||
"ws", | ||
"websocket", | ||
"websockets" | ||
], | ||
"dependencies": { | ||
@@ -22,0 +28,0 @@ "axios": "0.25.0" |
# cent.js | ||
Javascript library to communicate with Centrifugo HTTP API | ||
___ | ||
Javascript library to communicate with Centrifugo HTTP API. | ||
## Installation | ||
```bash | ||
$ npm i cent.js | ||
``` | ||
## Usage | ||
First see [available API methods in documentation](https://centrifugal.dev/docs/server/server_api). | ||
This library contains `CentClient` class to send messages to Centrifugo from your node-powered backend: | ||
```javascript | ||
const { CentClient } = require('cent.js'); | ||
// Initialize client instance. | ||
const client = new CentClient({ | ||
host: 'http://localhost:8000/api', | ||
token: 'XXX' | ||
}) | ||
// Publish data into channel | ||
client.publish({ | ||
channel: 'public:chat', | ||
data: { input: "test" } | ||
}) | ||
// Other available methods | ||
client.unsubscribe({ user: 'user_id', channel: 'channel' }); | ||
client.disconnect({ user: 'user_id' }) | ||
client.getHistory({ channel: 'channel' }) | ||
client.getPresence({ channel: 'channel' }) | ||
client.getChannels() | ||
client.getInfo() | ||
client.removeHistory({ channel: 'channel' }) | ||
``` | ||
## Stay in touch | ||
* Author - [Alexey Filippov](https://t.me/socketsomeone) | ||
* Twitter - [@SocketSomeone](https://twitter.com/SocketSomeone) | ||
## License | ||
[MIT](https://github.com/SocketSomeone/necord/blob/master/LICENSE) © [Alexey Filippov](https://github.com/SocketSomeone) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16000
28
338
52
0