@feathersjs/transport-commons 5.0.0-pre.3 → 5.0.0-pre.30

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