@enonic-types/lib-websocket 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -0
- package/package.json +36 -0
- package/websocket.d.ts +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Enonic XP lib-websocket TS types
|
|
2
|
+
|
|
3
|
+
> TypeScript definitions for `lib-websocket` library of Enonic XP
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i --save-dev @enonic/lib-websocket
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
Add the corresponding types to your `tsconfig.json` file that is used for application's server-side TypeScript code.
|
|
14
|
+
|
|
15
|
+
`tsconfig.json`
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"compilerOptions": {
|
|
20
|
+
"types": [
|
|
21
|
+
"@enonic-types/lib-websocket"
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Require and custom imports
|
|
28
|
+
|
|
29
|
+
To make `require` work out of box, you must install and add the `@enonic-types/global` types. Aside from providing definitions for XP global
|
|
30
|
+
objects, e.g. `log`, `app`, `__`, etc, requiring library by the default path will return typed object.
|
|
31
|
+
|
|
32
|
+
`tsconfig.json`
|
|
33
|
+
|
|
34
|
+
```diff
|
|
35
|
+
{
|
|
36
|
+
"compilerOptions": {
|
|
37
|
+
"types": [
|
|
38
|
+
+ "@enonic-types/global"
|
|
39
|
+
"@enonic-types/lib-websocket"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`example.ts`
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const {addToGroup, removeFromGroup, send, sendToGroup, getGroupSize} = require('/lib/xp/websocket');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
More detailed explanation on how it works and how to type custom import function can be
|
|
52
|
+
found [here](https://github.com/enonic/xp/tree/master/modules/lib/typescript/README.md).
|
|
53
|
+
|
|
54
|
+
### ES6-style import
|
|
55
|
+
|
|
56
|
+
If you are planning to use `import` in your code and transpile it with the default `tsc` TypeScript compiler, you'll need to add proper
|
|
57
|
+
types mapping to your configuration.
|
|
58
|
+
|
|
59
|
+
`tsconfig.json`
|
|
60
|
+
|
|
61
|
+
```diff
|
|
62
|
+
{
|
|
63
|
+
"compilerOptions": {
|
|
64
|
+
"types": [
|
|
65
|
+
"@enonic-types/lib-websocket"
|
|
66
|
+
]
|
|
67
|
+
+ "baseUrl": "./",
|
|
68
|
+
+ "paths": {
|
|
69
|
+
+ "/lib/xp/websocket": ["node_modules/@enonic-types/lib-websocket"],
|
|
70
|
+
+ }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`example.ts`
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import {addToGroup, removeFromGroup, send, sendToGroup, getGroupSize} from '/lib/xp/websocket';
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Setting `baseUrl` and `paths` will allow the `tsc` to keep the valid paths in the resulting JavaScript files.
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@enonic-types/lib-websocket",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Type definitions for lib-websocket.",
|
|
5
|
+
"types": "websocket.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"websocket.d.ts"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"pre": "npm run --prefix ../ pre",
|
|
11
|
+
"packlist": "npx npm-packlist"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/enonic/xp.git#master"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"enonic",
|
|
19
|
+
"enonic-xp",
|
|
20
|
+
"lib-websocket",
|
|
21
|
+
"websocket",
|
|
22
|
+
"types",
|
|
23
|
+
"typescript"
|
|
24
|
+
],
|
|
25
|
+
"contributors": [
|
|
26
|
+
"Mikita Taukachou <edloidas@gmail.com> (https://edloidas.github.io/)"
|
|
27
|
+
],
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/enonic/xp/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/enonic/xp/tree/master#readme",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/websocket.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Websocket functions.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* var webSocketLib = require('/lib/xp/websocket');
|
|
6
|
+
*
|
|
7
|
+
* @module websocket
|
|
8
|
+
*/
|
|
9
|
+
declare global {
|
|
10
|
+
interface XpLibraries {
|
|
11
|
+
'/lib/xp/websocket': typeof import('./websocket');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Add an id to a socket group.
|
|
16
|
+
*
|
|
17
|
+
* @example-ref examples/websocket/addToGroup.js
|
|
18
|
+
*
|
|
19
|
+
* @param {string} group Group name.
|
|
20
|
+
* @param {string} id Socket id.
|
|
21
|
+
*/
|
|
22
|
+
export declare function addToGroup(group: string, id: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Remove an id from a socket group.
|
|
25
|
+
*
|
|
26
|
+
* @example-ref examples/websocket/removeFromGroup.js
|
|
27
|
+
*
|
|
28
|
+
* @param {string} group Group name.
|
|
29
|
+
* @param {string} id Socket id.
|
|
30
|
+
*/
|
|
31
|
+
export declare function removeFromGroup(group: string, id: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Send message directly to a socket id.
|
|
34
|
+
*
|
|
35
|
+
* @example-ref examples/websocket/send.js
|
|
36
|
+
*
|
|
37
|
+
* @param {string} id Socket id.
|
|
38
|
+
* @param {string} message Message as text.
|
|
39
|
+
*/
|
|
40
|
+
export declare function send(id: string, message: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Send message to all sockets in group.
|
|
43
|
+
*
|
|
44
|
+
* @example-ref examples/websocket/sendToGroup.js
|
|
45
|
+
*
|
|
46
|
+
* @param {string} group Group name.
|
|
47
|
+
* @param {string} message Message as text.
|
|
48
|
+
*/
|
|
49
|
+
export declare function sendToGroup(group: string, message: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* Get number of all sockets in group.
|
|
52
|
+
*
|
|
53
|
+
* @example-ref examples/websocket/addToGroup.js
|
|
54
|
+
*
|
|
55
|
+
* @param {string} group Group name.
|
|
56
|
+
*/
|
|
57
|
+
export declare function getGroupSize(group: string): number;
|