@feathersjs/transport-commons 5.0.0-pre.9 → 5.0.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/CHANGELOG.md +193 -227
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/client.d.ts +1 -1
- package/lib/channels/channel/base.d.ts +1 -3
- package/lib/channels/channel/base.js +2 -2
- package/lib/channels/channel/base.js.map +1 -1
- package/lib/channels/channel/combined.d.ts +2 -1
- package/lib/channels/channel/combined.js +2 -2
- package/lib/channels/channel/combined.js.map +1 -1
- package/lib/channels/index.d.ts +14 -12
- package/lib/channels/index.js +15 -10
- package/lib/channels/index.js.map +1 -1
- package/lib/channels/mixins.d.ts +3 -3
- package/lib/channels/mixins.js +3 -3
- package/lib/channels/mixins.js.map +1 -1
- package/lib/client.d.ts +2 -2
- package/lib/client.js +10 -12
- package/lib/client.js.map +1 -1
- package/lib/http.d.ts +14 -9
- package/lib/http.js +30 -20
- package/lib/http.js.map +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.js +8 -2
- package/lib/index.js.map +1 -1
- package/lib/routing/index.d.ts +9 -4
- package/lib/routing/index.js +26 -16
- package/lib/routing/index.js.map +1 -1
- package/lib/routing/router.d.ts +4 -1
- package/lib/routing/router.js +37 -4
- package/lib/routing/router.js.map +1 -1
- package/lib/socket/index.d.ts +1 -2
- package/lib/socket/index.js +10 -10
- package/lib/socket/index.js.map +1 -1
- package/lib/socket/utils.d.ts +1 -2
- package/lib/socket/utils.js +43 -53
- package/lib/socket/utils.js.map +1 -1
- package/package.json +19 -14
- package/src/channels/channel/base.ts +28 -31
- package/src/channels/channel/combined.ts +32 -31
- package/src/channels/index.ts +67 -61
- package/src/channels/mixins.ts +49 -46
- package/src/client.ts +70 -70
- package/src/http.ts +55 -43
- package/src/index.ts +6 -5
- package/src/routing/index.ts +45 -28
- package/src/routing/router.ts +80 -42
- package/src/socket/index.ts +43 -44
- package/src/socket/utils.ts +71 -59
|
@@ -1,56 +1,57 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RealTimeConnection } from '@feathersjs/feathers'
|
|
2
|
+
import { Channel } from './base'
|
|
2
3
|
|
|
3
|
-
function collectConnections
|
|
4
|
-
const mappings = new WeakMap<RealTimeConnection, any>()
|
|
5
|
-
const connections: RealTimeConnection[] = []
|
|
4
|
+
function collectConnections(children: Channel[]) {
|
|
5
|
+
const mappings = new WeakMap<RealTimeConnection, any>()
|
|
6
|
+
const connections: RealTimeConnection[] = []
|
|
6
7
|
|
|
7
|
-
children.forEach(channel => {
|
|
8
|
-
channel.connections.forEach(connection => {
|
|
8
|
+
children.forEach((channel) => {
|
|
9
|
+
channel.connections.forEach((connection) => {
|
|
9
10
|
if (!mappings.has(connection)) {
|
|
10
|
-
connections.push(connection)
|
|
11
|
-
mappings.set(connection, channel.data)
|
|
11
|
+
connections.push(connection)
|
|
12
|
+
mappings.set(connection, channel.data)
|
|
12
13
|
}
|
|
13
|
-
})
|
|
14
|
-
})
|
|
14
|
+
})
|
|
15
|
+
})
|
|
15
16
|
|
|
16
|
-
return { connections, mappings }
|
|
17
|
+
return { connections, mappings }
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export class CombinedChannel extends Channel {
|
|
20
|
-
children: Channel[]
|
|
21
|
-
mappings: WeakMap<RealTimeConnection, any
|
|
21
|
+
children: Channel[]
|
|
22
|
+
mappings: WeakMap<RealTimeConnection, any>
|
|
22
23
|
|
|
23
|
-
constructor
|
|
24
|
-
const { mappings, connections } = collectConnections(children)
|
|
24
|
+
constructor(children: Channel[], data: any = null) {
|
|
25
|
+
const { mappings, connections } = collectConnections(children)
|
|
25
26
|
|
|
26
|
-
super(connections, data)
|
|
27
|
+
super(connections, data)
|
|
27
28
|
|
|
28
|
-
this.children = children
|
|
29
|
-
this.mappings = mappings
|
|
29
|
+
this.children = children
|
|
30
|
+
this.mappings = mappings
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
refresh
|
|
33
|
-
const collected = collectConnections(this.children)
|
|
33
|
+
refresh() {
|
|
34
|
+
const collected = collectConnections(this.children)
|
|
34
35
|
|
|
35
|
-
return Object.assign(this, collected)
|
|
36
|
+
return Object.assign(this, collected)
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
leave
|
|
39
|
-
return this.callChildren('leave', connections)
|
|
39
|
+
leave(...connections: RealTimeConnection[]) {
|
|
40
|
+
return this.callChildren('leave', connections)
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
join
|
|
43
|
-
return this.callChildren('join', connections)
|
|
43
|
+
join(...connections: RealTimeConnection[]) {
|
|
44
|
+
return this.callChildren('join', connections)
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
dataFor
|
|
47
|
-
return this.mappings.get(connection)
|
|
47
|
+
dataFor(connection: RealTimeConnection) {
|
|
48
|
+
return this.mappings.get(connection)
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
private callChildren
|
|
51
|
-
this.children.forEach((child: any) => child[method](...connections))
|
|
52
|
-
this.refresh()
|
|
51
|
+
private callChildren(method: string, connections: RealTimeConnection[]) {
|
|
52
|
+
this.children.forEach((child: any) => child[method](...connections))
|
|
53
|
+
this.refresh()
|
|
53
54
|
|
|
54
|
-
return this
|
|
55
|
+
return this
|
|
55
56
|
}
|
|
56
57
|
}
|
package/src/channels/index.ts
CHANGED
|
@@ -1,80 +1,83 @@
|
|
|
1
|
-
import { Application, FeathersService, getServiceOptions } from '@feathersjs/feathers'
|
|
2
|
-
import { createDebug } from '@feathersjs/commons'
|
|
3
|
-
import { compact, flattenDeep, noop } from 'lodash'
|
|
4
|
-
import { Channel
|
|
5
|
-
import { CombinedChannel } from './channel/combined'
|
|
6
|
-
import { channelMixin, publishMixin, keys, PublishMixin, Event, Publisher } from './mixins'
|
|
7
|
-
import EventEmitter from 'events'
|
|
1
|
+
import { Application, FeathersService, RealTimeConnection, getServiceOptions } from '@feathersjs/feathers'
|
|
2
|
+
import { createDebug } from '@feathersjs/commons'
|
|
3
|
+
import { compact, flattenDeep, noop } from 'lodash'
|
|
4
|
+
import { Channel } from './channel/base'
|
|
5
|
+
import { CombinedChannel } from './channel/combined'
|
|
6
|
+
import { channelMixin, publishMixin, keys, PublishMixin, Event, Publisher } from './mixins'
|
|
7
|
+
import EventEmitter from 'events'
|
|
8
8
|
|
|
9
|
-
const debug = createDebug('@feathersjs/transport-commons/channels')
|
|
10
|
-
const { CHANNELS } = keys
|
|
9
|
+
const debug = createDebug('@feathersjs/transport-commons/channels')
|
|
10
|
+
const { CHANNELS } = keys
|
|
11
11
|
|
|
12
12
|
declare module '@feathersjs/feathers/lib/declarations' {
|
|
13
|
-
interface ServiceAddons<A, S> extends EventEmitter {
|
|
14
|
-
|
|
15
|
-
publish
|
|
13
|
+
interface ServiceAddons<A, S> extends EventEmitter {
|
|
14
|
+
// eslint-disable-line
|
|
15
|
+
publish(publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
16
|
+
publish(event: Event, publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
16
17
|
|
|
17
|
-
registerPublisher
|
|
18
|
-
registerPublisher
|
|
18
|
+
registerPublisher(publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
19
|
+
registerPublisher(event: Event, publisher: Publisher<ServiceGenericType<S>, A, this>): this
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
interface Application<Services, Settings> {
|
|
24
|
+
// eslint-disable-line
|
|
25
|
+
channels: string[]
|
|
23
26
|
|
|
24
|
-
channel
|
|
25
|
-
channel
|
|
27
|
+
channel(name: string | string[]): Channel
|
|
28
|
+
channel(...names: string[]): Channel
|
|
26
29
|
|
|
27
|
-
publish<T>
|
|
28
|
-
publish<T>
|
|
30
|
+
publish<T>(publisher: Publisher<T, this>): this
|
|
31
|
+
publish<T>(event: Event, publisher: Publisher<T, this>): this
|
|
29
32
|
|
|
30
|
-
registerPublisher<T>
|
|
31
|
-
registerPublisher<T>
|
|
33
|
+
registerPublisher<T>(publisher: Publisher<T, this>): this
|
|
34
|
+
registerPublisher<T>(event: Event, publisher: Publisher<T, this>): this
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
interface Params {
|
|
35
|
-
connection?: RealTimeConnection
|
|
38
|
+
connection?: RealTimeConnection
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
export { keys }
|
|
42
|
+
export { keys }
|
|
40
43
|
|
|
41
|
-
export function channels
|
|
44
|
+
export function channels() {
|
|
42
45
|
return (app: Application) => {
|
|
43
46
|
if (typeof app.channel === 'function' && typeof app.publish === 'function') {
|
|
44
|
-
return
|
|
47
|
+
return
|
|
45
48
|
}
|
|
46
49
|
|
|
47
|
-
Object.assign(app, channelMixin(), publishMixin())
|
|
50
|
+
Object.assign(app, channelMixin(), publishMixin())
|
|
48
51
|
Object.defineProperty(app, 'channels', {
|
|
49
|
-
get
|
|
50
|
-
return Object.keys(this[CHANNELS])
|
|
52
|
+
get() {
|
|
53
|
+
return Object.keys(this[CHANNELS])
|
|
51
54
|
}
|
|
52
|
-
})
|
|
55
|
+
})
|
|
53
56
|
|
|
54
|
-
app.mixins.push((service: FeathersService
|
|
55
|
-
const { serviceEvents } = getServiceOptions(service)
|
|
57
|
+
app.mixins.push((service: FeathersService, path: string) => {
|
|
58
|
+
const { serviceEvents } = getServiceOptions(service)
|
|
56
59
|
|
|
57
60
|
if (typeof service.publish === 'function') {
|
|
58
|
-
return
|
|
61
|
+
return
|
|
59
62
|
}
|
|
60
63
|
|
|
61
|
-
Object.assign(service, publishMixin())
|
|
64
|
+
Object.assign(service, publishMixin())
|
|
62
65
|
|
|
63
66
|
serviceEvents.forEach((event: string) => {
|
|
64
67
|
service.on(event, function (data, hook) {
|
|
65
68
|
if (!hook) {
|
|
66
69
|
// Fake hook for custom events
|
|
67
|
-
hook = { path, service, app, result: data }
|
|
70
|
+
hook = { path, service, app, result: data }
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
debug('Publishing event', event, hook.path)
|
|
73
|
+
debug('Publishing event', event, hook.path)
|
|
71
74
|
|
|
72
|
-
const logError = (error: any) => debug(`Error in '${hook.path} ${event}' publisher`, error)
|
|
73
|
-
const servicePublishers = (service as unknown as PublishMixin)[keys.PUBLISHERS]
|
|
74
|
-
const appPublishers = (app as unknown as PublishMixin)[keys.PUBLISHERS]
|
|
75
|
+
const logError = (error: any) => debug(`Error in '${hook.path} ${event}' publisher`, error)
|
|
76
|
+
const servicePublishers = (service as unknown as PublishMixin)[keys.PUBLISHERS]
|
|
77
|
+
const appPublishers = (app as unknown as PublishMixin)[keys.PUBLISHERS]
|
|
75
78
|
// This will return the first publisher list that is not empty
|
|
76
79
|
// In the following precedence
|
|
77
|
-
const publisher =
|
|
80
|
+
const publisher =
|
|
78
81
|
// 1. Service publisher for a specific event
|
|
79
82
|
servicePublishers[event] ||
|
|
80
83
|
// 2. Service publisher for all events
|
|
@@ -85,28 +88,31 @@ export function channels () {
|
|
|
85
88
|
appPublishers[keys.ALL_EVENTS] ||
|
|
86
89
|
// 5. No publisher
|
|
87
90
|
noop
|
|
88
|
-
);
|
|
89
91
|
|
|
90
92
|
try {
|
|
91
|
-
Promise.resolve(publisher(data, hook))
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
93
|
+
Promise.resolve(publisher(data, hook))
|
|
94
|
+
.then((result: any) => {
|
|
95
|
+
if (!result) {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const results = Array.isArray(result) ? compact(flattenDeep(result)) : ([result] as Channel[])
|
|
100
|
+
const channel = new CombinedChannel(results)
|
|
101
|
+
|
|
102
|
+
if (channel && channel.length > 0) {
|
|
103
|
+
app.emit('publish', event, channel, hook, data)
|
|
104
|
+
} else {
|
|
105
|
+
debug('No connections to publish to')
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
.catch(logError)
|
|
109
|
+
} catch (error: any) {
|
|
110
|
+
logError(error)
|
|
107
111
|
}
|
|
108
|
-
})
|
|
109
|
-
})
|
|
110
|
-
})
|
|
111
|
-
}
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
}
|
|
112
116
|
}
|
|
117
|
+
|
|
118
|
+
export { Channel, CombinedChannel, RealTimeConnection }
|
package/src/channels/mixins.ts
CHANGED
|
@@ -1,105 +1,108 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
|
|
2
|
-
import { HookContext, getServiceOptions } from '@feathersjs/feathers'
|
|
3
|
-
import { createDebug } from '@feathersjs/commons'
|
|
4
|
-
import { Channel } from './channel/base'
|
|
5
|
-
import { CombinedChannel } from './channel/combined'
|
|
2
|
+
import { Application, HookContext, getServiceOptions, defaultServiceEvents } from '@feathersjs/feathers'
|
|
3
|
+
import { createDebug } from '@feathersjs/commons'
|
|
4
|
+
import { Channel } from './channel/base'
|
|
5
|
+
import { CombinedChannel } from './channel/combined'
|
|
6
6
|
|
|
7
|
-
const debug = createDebug('@feathersjs/transport-commons
|
|
8
|
-
const PUBLISHERS = Symbol('@feathersjs/transport-commons/publishers')
|
|
9
|
-
const CHANNELS = Symbol('@feathersjs/transport-commons/channels')
|
|
10
|
-
const ALL_EVENTS = Symbol('@feathersjs/transport-commons/all-events')
|
|
7
|
+
const debug = createDebug('@feathersjs/transport-commons/channels/mixins')
|
|
8
|
+
const PUBLISHERS = Symbol('@feathersjs/transport-commons/publishers')
|
|
9
|
+
const CHANNELS = Symbol('@feathersjs/transport-commons/channels')
|
|
10
|
+
const ALL_EVENTS = Symbol('@feathersjs/transport-commons/all-events')
|
|
11
11
|
|
|
12
12
|
export const keys = {
|
|
13
13
|
PUBLISHERS: PUBLISHERS as typeof PUBLISHERS,
|
|
14
14
|
CHANNELS: CHANNELS as typeof CHANNELS,
|
|
15
15
|
ALL_EVENTS: ALL_EVENTS as typeof ALL_EVENTS
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
|
|
18
18
|
export interface ChannelMixin {
|
|
19
|
-
[CHANNELS]: { [key: string]: Channel }
|
|
20
|
-
channel
|
|
19
|
+
[CHANNELS]: { [key: string]: Channel }
|
|
20
|
+
channel(...names: string[]): Channel
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export function channelMixin
|
|
23
|
+
export function channelMixin() {
|
|
24
24
|
const mixin: ChannelMixin = {
|
|
25
25
|
[CHANNELS]: {},
|
|
26
26
|
|
|
27
|
-
channel
|
|
28
|
-
debug('Returning channels', names)
|
|
27
|
+
channel(...names: string[]): Channel {
|
|
28
|
+
debug('Returning channels', names)
|
|
29
29
|
|
|
30
30
|
if (names.length === 0) {
|
|
31
|
-
throw new Error('app.channel needs at least one channel name')
|
|
31
|
+
throw new Error('app.channel needs at least one channel name')
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
if (names.length === 1) {
|
|
35
|
-
const [
|
|
35
|
+
const [name] = names
|
|
36
36
|
|
|
37
37
|
if (Array.isArray(name)) {
|
|
38
|
-
return this.channel(...name)
|
|
38
|
+
return this.channel(...name)
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
if (!this[CHANNELS][name]) {
|
|
42
|
-
const channel = new Channel()
|
|
42
|
+
const channel = new Channel()
|
|
43
43
|
|
|
44
44
|
channel.once('empty', () => {
|
|
45
|
-
channel.removeAllListeners()
|
|
46
|
-
delete this[CHANNELS][name]
|
|
47
|
-
})
|
|
45
|
+
channel.removeAllListeners()
|
|
46
|
+
delete this[CHANNELS][name]
|
|
47
|
+
})
|
|
48
48
|
|
|
49
|
-
this[CHANNELS][name] = channel
|
|
49
|
+
this[CHANNELS][name] = channel
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
return this[CHANNELS][name]
|
|
52
|
+
return this[CHANNELS][name]
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
const channels = names.map(name => this.channel(name))
|
|
55
|
+
const channels = names.map((name) => this.channel(name))
|
|
56
56
|
|
|
57
|
-
return new CombinedChannel(channels)
|
|
57
|
+
return new CombinedChannel(channels)
|
|
58
58
|
}
|
|
59
|
-
}
|
|
59
|
+
}
|
|
60
60
|
|
|
61
|
-
return mixin
|
|
61
|
+
return mixin
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
export type Event = string|
|
|
64
|
+
export type Event = string | typeof ALL_EVENTS
|
|
65
65
|
|
|
66
|
-
export type Publisher<T = any
|
|
66
|
+
export type Publisher<T = any, A = Application, S = any> = (
|
|
67
|
+
data: T,
|
|
68
|
+
context: HookContext<A, S>
|
|
69
|
+
) => Channel | Channel[] | void | Promise<Channel | Channel[] | void>
|
|
67
70
|
|
|
68
71
|
export interface PublishMixin<T = any> {
|
|
69
|
-
[PUBLISHERS]: { [ALL_EVENTS]?: Publisher<T
|
|
70
|
-
publish
|
|
71
|
-
registerPublisher
|
|
72
|
+
[PUBLISHERS]: { [ALL_EVENTS]?: Publisher<T>; [key: string]: Publisher<T> }
|
|
73
|
+
publish(event: Event, publisher: Publisher<T>): this
|
|
74
|
+
registerPublisher(event: Event, publisher: Publisher<T>): this
|
|
72
75
|
}
|
|
73
76
|
|
|
74
|
-
export function publishMixin
|
|
77
|
+
export function publishMixin() {
|
|
75
78
|
const result: PublishMixin = {
|
|
76
79
|
[PUBLISHERS]: {},
|
|
77
80
|
|
|
78
|
-
publish
|
|
79
|
-
return this.registerPublisher(...args)
|
|
81
|
+
publish(...args) {
|
|
82
|
+
return this.registerPublisher(...args)
|
|
80
83
|
},
|
|
81
84
|
|
|
82
|
-
registerPublisher
|
|
83
|
-
debug('Registering publisher', event)
|
|
85
|
+
registerPublisher(event, publisher) {
|
|
86
|
+
debug('Registering publisher', event)
|
|
84
87
|
|
|
85
88
|
if (!publisher && typeof event === 'function') {
|
|
86
|
-
publisher = event
|
|
87
|
-
event = ALL_EVENTS
|
|
89
|
+
publisher = event
|
|
90
|
+
event = ALL_EVENTS
|
|
88
91
|
}
|
|
89
92
|
|
|
90
|
-
const { serviceEvents } = getServiceOptions(this)
|
|
93
|
+
const { serviceEvents = defaultServiceEvents } = getServiceOptions(this) || {}
|
|
91
94
|
|
|
92
95
|
if (event !== ALL_EVENTS && !serviceEvents.includes(event)) {
|
|
93
|
-
throw new Error(`'${event.toString()}' is not a valid service event`)
|
|
96
|
+
throw new Error(`'${event.toString()}' is not a valid service event`)
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
const publishers = this[PUBLISHERS]
|
|
99
|
+
const publishers = this[PUBLISHERS]
|
|
97
100
|
|
|
98
|
-
publishers[event] = publisher
|
|
101
|
+
publishers[event] = publisher
|
|
99
102
|
|
|
100
|
-
return this
|
|
103
|
+
return this
|
|
101
104
|
}
|
|
102
|
-
}
|
|
105
|
+
}
|
|
103
106
|
|
|
104
|
-
return result
|
|
107
|
+
return result
|
|
105
108
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import { convert } from '@feathersjs/errors'
|
|
3
|
+
import { createDebug } from '@feathersjs/commons'
|
|
4
|
+
import { Id, NullableId, Params, ServiceInterface } from '@feathersjs/feathers'
|
|
4
5
|
|
|
5
|
-
const debug = createDebug('@feathersjs/transport-commons/client')
|
|
6
|
+
const debug = createDebug('@feathersjs/transport-commons/client')
|
|
6
7
|
|
|
7
8
|
const namespacedEmitterMethods = [
|
|
8
9
|
'addListener',
|
|
10
|
+
'addEventListener',
|
|
9
11
|
'emit',
|
|
10
12
|
'listenerCount',
|
|
11
13
|
'listeners',
|
|
@@ -14,128 +16,126 @@ const namespacedEmitterMethods = [
|
|
|
14
16
|
'prependListener',
|
|
15
17
|
'prependOnceListener',
|
|
16
18
|
'removeAllListeners',
|
|
19
|
+
'removeEventListener',
|
|
17
20
|
'removeListener'
|
|
18
|
-
]
|
|
19
|
-
const otherEmitterMethods = [
|
|
20
|
-
'eventNames',
|
|
21
|
-
'getMaxListeners',
|
|
22
|
-
'setMaxListeners'
|
|
23
|
-
];
|
|
21
|
+
]
|
|
22
|
+
const otherEmitterMethods = ['eventNames', 'getMaxListeners', 'setMaxListeners']
|
|
24
23
|
|
|
25
24
|
const addEmitterMethods = (service: any) => {
|
|
26
|
-
otherEmitterMethods.forEach(method => {
|
|
25
|
+
otherEmitterMethods.forEach((method) => {
|
|
27
26
|
service[method] = function (...args: any[]) {
|
|
28
27
|
if (typeof this.connection[method] !== 'function') {
|
|
29
|
-
throw new Error(`Can not call '${method}' on the client service connection`)
|
|
28
|
+
throw new Error(`Can not call '${method}' on the client service connection`)
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
return this.connection[method](...args)
|
|
33
|
-
}
|
|
34
|
-
})
|
|
31
|
+
return this.connection[method](...args)
|
|
32
|
+
}
|
|
33
|
+
})
|
|
35
34
|
|
|
36
35
|
// Methods that should add the namespace (service path)
|
|
37
|
-
namespacedEmitterMethods.forEach(method => {
|
|
36
|
+
namespacedEmitterMethods.forEach((method) => {
|
|
38
37
|
service[method] = function (name: string, ...args: any[]) {
|
|
39
38
|
if (typeof this.connection[method] !== 'function') {
|
|
40
|
-
throw new Error(`Can not call '${method}' on the client service connection`)
|
|
39
|
+
throw new Error(`Can not call '${method}' on the client service connection`)
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
const eventName = `${this.path} ${name}
|
|
42
|
+
const eventName = `${this.path} ${name}`
|
|
44
43
|
|
|
45
|
-
debug(`Calling emitter method ${method} with ` +
|
|
46
|
-
`namespaced event '${eventName}'`);
|
|
44
|
+
debug(`Calling emitter method ${method} with ` + `namespaced event '${eventName}'`)
|
|
47
45
|
|
|
48
|
-
const result = this.connection[method](eventName, ...args)
|
|
46
|
+
const result = this.connection[method](eventName, ...args)
|
|
49
47
|
|
|
50
|
-
return result === this.connection ? this : result
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
}
|
|
48
|
+
return result === this.connection ? this : result
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
}
|
|
54
52
|
|
|
55
53
|
interface ServiceOptions {
|
|
56
|
-
name: string
|
|
57
|
-
connection: any
|
|
58
|
-
method: string
|
|
59
|
-
events?: string[]
|
|
54
|
+
name: string
|
|
55
|
+
connection: any
|
|
56
|
+
method: string
|
|
57
|
+
events?: string[]
|
|
60
58
|
}
|
|
61
59
|
|
|
62
|
-
export type SocketService<T = any, D = Partial<any
|
|
60
|
+
export type SocketService<T = any, D = Partial<any>, P extends Params = Params> = Service<T, D, P>
|
|
63
61
|
|
|
64
|
-
export class Service<T = any, D = Partial<T
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
export class Service<T = any, D = Partial<T>, P extends Params = Params>
|
|
63
|
+
implements ServiceInterface<T, D, P>
|
|
64
|
+
{
|
|
65
|
+
events: string[]
|
|
66
|
+
path: string
|
|
67
|
+
connection: any
|
|
68
|
+
method: string
|
|
69
69
|
|
|
70
|
-
constructor
|
|
71
|
-
this.events = options.events
|
|
72
|
-
this.path = options.name
|
|
73
|
-
this.connection = options.connection
|
|
74
|
-
this.method = options.method
|
|
70
|
+
constructor(options: ServiceOptions) {
|
|
71
|
+
this.events = options.events
|
|
72
|
+
this.path = options.name
|
|
73
|
+
this.connection = options.connection
|
|
74
|
+
this.method = options.method
|
|
75
75
|
|
|
76
|
-
addEmitterMethods(this)
|
|
76
|
+
addEmitterMethods(this)
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
send<X = any>
|
|
79
|
+
send<X = any>(method: string, ...args: any[]) {
|
|
80
80
|
return new Promise<X>((resolve, reject) => {
|
|
81
|
-
args.unshift(method, this.path)
|
|
81
|
+
args.unshift(method, this.path)
|
|
82
82
|
args.push(function (error: any, data: any) {
|
|
83
|
-
return error ? reject(convert(error)) : resolve(data)
|
|
84
|
-
})
|
|
83
|
+
return error ? reject(convert(error)) : resolve(data)
|
|
84
|
+
})
|
|
85
85
|
|
|
86
|
-
debug(`Sending socket.${this.method}`, args)
|
|
86
|
+
debug(`Sending socket.${this.method}`, args)
|
|
87
87
|
|
|
88
|
-
this.connection[this.method](...args)
|
|
89
|
-
})
|
|
88
|
+
this.connection[this.method](...args)
|
|
89
|
+
})
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
methods
|
|
93
|
-
names.forEach(name => {
|
|
92
|
+
methods(this: any, ...names: string[]) {
|
|
93
|
+
names.forEach((name) => {
|
|
94
94
|
this[name] = function (data: any, params: Params = {}) {
|
|
95
|
-
return this.send(name, data, params.query || {})
|
|
95
|
+
return this.send(name, data, params.query || {})
|
|
96
96
|
}
|
|
97
|
-
})
|
|
98
|
-
return this
|
|
97
|
+
})
|
|
98
|
+
return this
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
find
|
|
102
|
-
return this.send<T|T[]>('find', params.query || {})
|
|
101
|
+
find(params: Params = {}) {
|
|
102
|
+
return this.send<T | T[]>('find', params.query || {})
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
get
|
|
106
|
-
return this.send<T>('get', id, params.query || {})
|
|
105
|
+
get(id: Id, params: Params = {}) {
|
|
106
|
+
return this.send<T>('get', id, params.query || {})
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
create
|
|
110
|
-
return this.send<T>('create', data, params.query || {})
|
|
109
|
+
create(data: any, params: Params = {}) {
|
|
110
|
+
return this.send<T>('create', data, params.query || {})
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
update
|
|
114
|
-
return this.send<T>
|
|
113
|
+
update(id: Id, data: any, params: Params = {}) {
|
|
114
|
+
return this.send<T>('update', id, data, params.query || {})
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
patch
|
|
118
|
-
return this.send<T|T[]>
|
|
117
|
+
patch(id: NullableId, data: any, params: Params = {}) {
|
|
118
|
+
return this.send<T | T[]>('patch', id, data, params.query || {})
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
remove
|
|
122
|
-
return this.send<T|T[]>
|
|
121
|
+
remove(id: NullableId, params: Params = {}) {
|
|
122
|
+
return this.send<T | T[]>('remove', id, params.query || {})
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
// `off` is actually not part of the Node event emitter spec
|
|
126
126
|
// but we are adding it since everybody is expecting it because
|
|
127
127
|
// of the emitter-component Socket.io is using
|
|
128
|
-
off
|
|
128
|
+
off(name: string, ...args: any[]) {
|
|
129
129
|
if (typeof this.connection.off === 'function') {
|
|
130
|
-
const result = this.connection.off(`${this.path} ${name}`, ...args)
|
|
130
|
+
const result = this.connection.off(`${this.path} ${name}`, ...args)
|
|
131
131
|
|
|
132
|
-
return result === this.connection ? this : result
|
|
132
|
+
return result === this.connection ? this : result
|
|
133
133
|
} else if (args.length === 0) {
|
|
134
134
|
// @ts-ignore
|
|
135
|
-
return this.removeAllListeners(name)
|
|
135
|
+
return this.removeAllListeners(name)
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
// @ts-ignore
|
|
139
|
-
return this.removeListener(name, ...args)
|
|
139
|
+
return this.removeListener(name, ...args)
|
|
140
140
|
}
|
|
141
141
|
}
|