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