@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.
Files changed (49) hide show
  1. package/CHANGELOG.md +193 -227
  2. package/LICENSE +1 -1
  3. package/README.md +2 -2
  4. package/client.d.ts +1 -1
  5. package/lib/channels/channel/base.d.ts +1 -3
  6. package/lib/channels/channel/base.js +2 -2
  7. package/lib/channels/channel/base.js.map +1 -1
  8. package/lib/channels/channel/combined.d.ts +2 -1
  9. package/lib/channels/channel/combined.js +2 -2
  10. package/lib/channels/channel/combined.js.map +1 -1
  11. package/lib/channels/index.d.ts +14 -12
  12. package/lib/channels/index.js +15 -10
  13. package/lib/channels/index.js.map +1 -1
  14. package/lib/channels/mixins.d.ts +3 -3
  15. package/lib/channels/mixins.js +3 -3
  16. package/lib/channels/mixins.js.map +1 -1
  17. package/lib/client.d.ts +2 -2
  18. package/lib/client.js +10 -12
  19. package/lib/client.js.map +1 -1
  20. package/lib/http.d.ts +14 -9
  21. package/lib/http.js +30 -20
  22. package/lib/http.js.map +1 -1
  23. package/lib/index.d.ts +3 -2
  24. package/lib/index.js +8 -2
  25. package/lib/index.js.map +1 -1
  26. package/lib/routing/index.d.ts +9 -4
  27. package/lib/routing/index.js +26 -16
  28. package/lib/routing/index.js.map +1 -1
  29. package/lib/routing/router.d.ts +4 -1
  30. package/lib/routing/router.js +37 -4
  31. package/lib/routing/router.js.map +1 -1
  32. package/lib/socket/index.d.ts +1 -2
  33. package/lib/socket/index.js +10 -10
  34. package/lib/socket/index.js.map +1 -1
  35. package/lib/socket/utils.d.ts +1 -2
  36. package/lib/socket/utils.js +43 -53
  37. package/lib/socket/utils.js.map +1 -1
  38. package/package.json +19 -14
  39. package/src/channels/channel/base.ts +28 -31
  40. package/src/channels/channel/combined.ts +32 -31
  41. package/src/channels/index.ts +67 -61
  42. package/src/channels/mixins.ts +49 -46
  43. package/src/client.ts +70 -70
  44. package/src/http.ts +55 -43
  45. package/src/index.ts +6 -5
  46. package/src/routing/index.ts +45 -28
  47. package/src/routing/router.ts +80 -42
  48. package/src/socket/index.ts +43 -44
  49. package/src/socket/utils.ts +71 -59
@@ -1,114 +1,126 @@
1
- import { HookContext, Application, createContext, getServiceOptions } from '@feathersjs/feathers';
2
- import { NotFound, MethodNotAllowed, BadRequest } from '@feathersjs/errors';
3
- import { createDebug } from '@feathersjs/commons';
4
- import isEqual from 'lodash/isEqual';
5
- import { CombinedChannel } from '../channels/channel/combined';
6
- import { RealTimeConnection } from '../channels/channel/base';
7
-
8
- const debug = createDebug('@feathersjs/transport-commons');
9
-
10
- export const DEFAULT_PARAMS_POSITION = 1;
1
+ import {
2
+ HookContext,
3
+ Application,
4
+ RealTimeConnection,
5
+ createContext,
6
+ getServiceOptions
7
+ } from '@feathersjs/feathers'
8
+ import { NotFound, MethodNotAllowed, BadRequest } from '@feathersjs/errors'
9
+ import { createDebug } from '@feathersjs/commons'
10
+ import isEqual from 'lodash/isEqual'
11
+ import { CombinedChannel } from '../channels/channel/combined'
12
+
13
+ const debug = createDebug('@feathersjs/transport-commons')
14
+
15
+ export const DEFAULT_PARAMS_POSITION = 1
11
16
 
12
17
  export const paramsPositions: { [key: string]: number } = {
13
18
  find: 0,
14
19
  update: 2,
15
20
  patch: 2
16
- };
21
+ }
17
22
 
18
- export function normalizeError (e: any) {
19
- const hasToJSON = typeof e.toJSON === 'function';
20
- const result = hasToJSON ? e.toJSON() : {};
23
+ export function normalizeError(e: any) {
24
+ const hasToJSON = typeof e.toJSON === 'function'
25
+ const result = hasToJSON ? e.toJSON() : {}
21
26
 
22
27
  if (!hasToJSON) {
23
- Object.getOwnPropertyNames(e).forEach(key => {
24
- result[key] = e[key];
25
- });
28
+ Object.getOwnPropertyNames(e).forEach((key) => {
29
+ result[key] = e[key]
30
+ })
26
31
  }
27
32
 
28
33
  if (process.env.NODE_ENV === 'production') {
29
- delete result.stack;
34
+ delete result.stack
30
35
  }
31
36
 
32
- delete result.hook;
37
+ delete result.hook
33
38
 
34
- return result;
39
+ return result
35
40
  }
36
41
 
37
- export function getDispatcher (emit: string, socketMap: WeakMap<RealTimeConnection, any>, socketKey?: any) {
42
+ export function getDispatcher(emit: string, socketMap: WeakMap<RealTimeConnection, any>, socketKey?: any) {
38
43
  return function (event: string, channel: CombinedChannel, context: HookContext, data?: any) {
39
- debug(`Dispatching '${event}' to ${channel.length} connections`);
44
+ debug(`Dispatching '${event}' to ${channel.length} connections`)
40
45
 
41
- channel.connections.forEach(connection => {
46
+ channel.connections.forEach((connection) => {
42
47
  // The reference between connection and socket is set in `app.setup`
43
- const socket = socketKey ? connection[socketKey] : socketMap.get(connection);
48
+ const socket = socketKey ? connection[socketKey] : socketMap.get(connection)
44
49
 
45
50
  if (socket) {
46
- const eventName = `${context.path || ''} ${event}`.trim();
51
+ const eventName = `${context.path || ''} ${event}`.trim()
47
52
 
48
- let result = channel.dataFor(connection) || context.dispatch || context.result;
53
+ let result = channel.dataFor(connection) || context.dispatch || context.result
49
54
 
50
55
  // If we are getting events from an array but try to dispatch individual data
51
56
  // try to get the individual item to dispatch from the correct index.
52
57
  if (!Array.isArray(data) && Array.isArray(context.result) && Array.isArray(result)) {
53
- result = context.result.find(resultData => isEqual(resultData, data));
58
+ result = context.result.find((resultData) => isEqual(resultData, data))
54
59
  }
55
60
 
56
- debug(`Dispatching '${eventName}' to Socket ${socket.id} with`, result);
61
+ debug(`Dispatching '${eventName}' to Socket ${socket.id} with`, result)
57
62
 
58
- socket[emit](eventName, result);
63
+ socket[emit](eventName, result)
59
64
  }
60
- });
61
- };
65
+ })
66
+ }
62
67
  }
63
68
 
64
- export async function runMethod (app: Application, connection: RealTimeConnection, path: string, method: string, args: any[]) {
65
- const trace = `method '${method}' on service '${path}'`;
66
- const methodArgs = args.slice(0);
67
- const callback = typeof methodArgs[methodArgs.length - 1] === 'function'
68
- ? methodArgs.pop() : function () {};
69
-
70
- debug(`Running ${trace}`, connection, args);
69
+ export async function runMethod(
70
+ app: Application,
71
+ connection: RealTimeConnection,
72
+ path: string,
73
+ method: string,
74
+ args: any[]
75
+ ) {
76
+ const trace = `method '${method}' on service '${path}'`
77
+ const methodArgs = args.slice(0)
78
+ const callback =
79
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
80
+ typeof methodArgs[methodArgs.length - 1] === 'function' ? methodArgs.pop() : function () {}
81
+
82
+ debug(`Running ${trace}`, connection, args)
71
83
 
72
84
  const handleError = (error: any) => {
73
- debug(`Error in ${trace}`, error);
74
- callback(normalizeError(error));
75
- };
85
+ debug(`Error in ${trace}`, error)
86
+ callback(normalizeError(error))
87
+ }
76
88
 
77
89
  try {
78
- const lookup = app.lookup(path);
90
+ const lookup = app.lookup(path)
79
91
 
80
92
  // No valid service was found throw a NotFound error
81
93
  if (lookup === null) {
82
- throw new NotFound(`Service '${path}' not found`);
94
+ throw new NotFound(`Service '${path}' not found`)
83
95
  }
84
96
 
85
- const { service, params: route = {} } = lookup;
86
- const { methods } = getServiceOptions(service);
97
+ const { service, params: route = {} } = lookup
98
+ const { methods } = getServiceOptions(service)
87
99
 
88
100
  // Only service methods are allowed
89
101
  if (!methods.includes(method)) {
90
- throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`);
102
+ throw new MethodNotAllowed(`Method '${method}' not allowed on service '${path}'`)
91
103
  }
92
104
 
93
- const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION;
94
- const query = methodArgs[position] || {};
105
+ const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION
106
+ const query = Object.assign({}, methodArgs[position])
95
107
  // `params` have to be re-mapped to the query and added with the route
96
- const params = Object.assign({ query, route, connection }, connection);
108
+ const params = Object.assign({ query, route, connection }, connection)
97
109
 
98
110
  // `params` is always the last parameter. Error if we got more arguments.
99
- if (methodArgs.length > (position + 1)) {
100
- throw new BadRequest(`Too many arguments for '${method}' method`);
111
+ if (methodArgs.length > position + 1) {
112
+ throw new BadRequest(`Too many arguments for '${method}' method`)
101
113
  }
102
114
 
103
- methodArgs[position] = params;
115
+ methodArgs[position] = params
104
116
 
105
- const ctx = createContext(service, method);
106
- const returnedCtx: HookContext = await (service as any)[method](...methodArgs, ctx);
107
- const result = returnedCtx.dispatch || returnedCtx.result;
117
+ const ctx = createContext(service, method)
118
+ const returnedCtx: HookContext = await (service as any)[method](...methodArgs, ctx)
119
+ const result = returnedCtx.dispatch || returnedCtx.result
108
120
 
109
- debug(`Returned successfully ${trace}`, result);
110
- callback(null, result);
111
- } catch (error) {
112
- handleError(error);
121
+ debug(`Returned successfully ${trace}`, result)
122
+ callback(null, result)
123
+ } catch (error: any) {
124
+ handleError(error)
113
125
  }
114
126
  }