@livequery/nestjs 1.0.24 → 1.0.28

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/build/index.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  export * from './src/LivequeryRequest';
2
- export * from './src/LivequeryResponse';
3
2
  export * from './src/LivequeryInterceptor';
4
3
  export * from './src/LivequeryWebsocketSync';
5
- export * from './src/ControllerMetadata';
6
- export * from './src/UseDataSource';
4
+ export * from './src/pathResolver';
package/build/index.js CHANGED
@@ -11,8 +11,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./src/LivequeryRequest"), exports);
14
- __exportStar(require("./src/LivequeryResponse"), exports);
15
14
  __exportStar(require("./src/LivequeryInterceptor"), exports);
16
15
  __exportStar(require("./src/LivequeryWebsocketSync"), exports);
17
- __exportStar(require("./src/ControllerMetadata"), exports);
18
- __exportStar(require("./src/UseDataSource"), exports);
16
+ __exportStar(require("./src/pathResolver"), exports);
@@ -29,14 +29,15 @@ const LivequeryWebsocketSync_1 = require("./LivequeryWebsocketSync");
29
29
  const const_1 = require("./const");
30
30
  const rxjs_1 = require("rxjs");
31
31
  const operators_1 = require("rxjs/operators");
32
+ const index_1 = require("index");
32
33
  let LivequeryInterceptor = class LivequeryInterceptor {
33
34
  constructor(LivequeryWebsocketSync = null) {
34
35
  this.LivequeryWebsocketSync = LivequeryWebsocketSync;
35
36
  }
36
37
  intercept(context, next) {
37
- var _a;
38
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
38
39
  const req = context.switchToHttp().getRequest();
39
- const _b = req.query, { _limit = 20, _cursor, _order_by, _sort, _select } = _b, rest = __rest(_b, ["_limit", "_cursor", "_order_by", "_sort", "_select"]);
40
+ const _l = req.query, { _limit = 20, _cursor, _order_by, _sort, _select } = _l, rest = __rest(_l, ["_limit", "_cursor", "_order_by", "_sort", "_select"]);
40
41
  const filters = Object
41
42
  .keys(rest)
42
43
  .map(key => {
@@ -48,18 +49,17 @@ let LivequeryInterceptor = class LivequeryInterceptor {
48
49
  return [name, expression || 'eq', rest[key]];
49
50
  }
50
51
  });
51
- const refs = req._parsedUrl.pathname.split('/').slice(const_1.COLLECTION_REF_SLICE_INDEX + 1);
52
+ const refs = (_c = (_b = (_a = req._parsedUrl.pathname) === null || _a === void 0 ? void 0 : _a.split(const_1.LIVEQUERY_MAGIC_KEY)) === null || _b === void 0 ? void 0 : _b[1]) === null || _c === void 0 ? void 0 : _c.split('/');
53
+ if (!refs)
54
+ throw new common_1.HttpException('CAN_NOT_DETECT_LIVEQUERY_KEY', 400);
52
55
  const ref = refs.join('/');
53
56
  const is_collection = refs.length % 2 == 1;
54
57
  const collection_ref = refs.slice(0, refs.length - (is_collection ? 0 : 1)).join('/');
55
58
  const doc_id = !is_collection && refs[refs.length - 1];
56
- const schema_collection_ref = req.route.path
57
- .split('/')
58
- .slice(const_1.COLLECTION_REF_SLICE_INDEX + 1)
59
- .filter((_, i) => i % 2 == 0)
60
- .join('/')
61
- .replaceAll(':', '');
62
- req.__livequery_request = {
59
+ const schema_collection_ref = (_j = (_h = (_g = (_f = (_e = (_d = req.route.path) === null || _d === void 0 ? void 0 : _d.split(const_1.LIVEQUERY_MAGIC_KEY)) === null || _e === void 0 ? void 0 : _e[1]) === null || _f === void 0 ? void 0 : _f.split('/')) === null || _g === void 0 ? void 0 : _g.filter((_, i) => i % 2 == 0)) === null || _h === void 0 ? void 0 : _h.join('/')) === null || _j === void 0 ? void 0 : _j.replaceAll(':', '');
60
+ if (!schema_collection_ref)
61
+ throw new common_1.HttpException('CAN_NOT_DETECT_LIVEQUERY_KEY', 400);
62
+ req[index_1.LivequeryRequestKey] = {
63
63
  ref,
64
64
  collection_ref,
65
65
  schema_collection_ref,
@@ -72,15 +72,15 @@ let LivequeryInterceptor = class LivequeryInterceptor {
72
72
  keys: req.params
73
73
  };
74
74
  const socket_id = req.headers.socket_id;
75
- socket_id && ((_a = this.LivequeryWebsocketSync) === null || _a === void 0 ? void 0 : _a.listen(socket_id, collection_ref));
76
- return next.handle().pipe(operators_1.map(data => ({ data })), operators_1.catchError(error => rxjs_1.of({ error })));
75
+ socket_id && ((_k = this.LivequeryWebsocketSync) === null || _k === void 0 ? void 0 : _k.listen(socket_id, collection_ref, doc_id));
76
+ return next.handle().pipe((0, operators_1.map)(data => ({ data })), (0, operators_1.catchError)(error => (0, rxjs_1.of)({ error })));
77
77
  }
78
78
  };
79
79
  LivequeryInterceptor = __decorate([
80
- common_1.Injectable(),
81
- __param(0, common_1.Optional()),
80
+ (0, common_1.Injectable)(),
81
+ __param(0, (0, common_1.Optional)()),
82
82
  __metadata("design:paramtypes", [LivequeryWebsocketSync_1.LivequeryWebsocketSync])
83
83
  ], LivequeryInterceptor);
84
84
  exports.LivequeryInterceptor = LivequeryInterceptor;
85
- const UseLivequeryInterceptor = () => common_1.UseInterceptors(LivequeryInterceptor);
85
+ const UseLivequeryInterceptor = () => (0, common_1.UseInterceptors)(LivequeryInterceptor);
86
86
  exports.UseLivequeryInterceptor = UseLivequeryInterceptor;
@@ -1 +1,2 @@
1
+ export declare const LivequeryRequestKey: unique symbol;
1
2
  export declare const LivequeryRequest: (...dataOrPipes: unknown[]) => ParameterDecorator;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LivequeryRequest = void 0;
3
+ exports.LivequeryRequest = exports.LivequeryRequestKey = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
- exports.LivequeryRequest = common_1.createParamDecorator((data, ctx) => {
5
+ exports.LivequeryRequestKey = Symbol();
6
+ exports.LivequeryRequest = (0, common_1.createParamDecorator)((data, ctx) => {
6
7
  const request = ctx.switchToHttp().getRequest();
7
- return request.__livequery_request;
8
+ return request[exports.LivequeryRequestKey];
8
9
  });
@@ -16,5 +16,5 @@ export declare class LivequeryWebsocketSync {
16
16
  }, socket: WebSocket & {
17
17
  id: string;
18
18
  }): void;
19
- listen(connection_id: string, ref: string): void;
19
+ listen(connection_id: string, collection_ref: string, doc_id?: string): void;
20
20
  }
@@ -21,7 +21,7 @@ let LivequeryWebsocketSync = class LivequeryWebsocketSync {
21
21
  this.connections = new Map();
22
22
  this.refs = new Map();
23
23
  this.changes = new rxjs_1.Subject();
24
- this.changes.pipe(operators_1.mergeMap(change => {
24
+ this.changes.pipe((0, operators_1.mergeMap)(change => {
25
25
  const map = new Map();
26
26
  if (!map.has(change.ref))
27
27
  map.set(change.ref, []);
@@ -39,9 +39,9 @@ let LivequeryWebsocketSync = class LivequeryWebsocketSync {
39
39
  });
40
40
  }
41
41
  async handleDisconnect(socket) {
42
- var _a;
42
+ var _a, _b;
43
43
  for (const ref of ((_a = this.connections.get(socket.id)) === null || _a === void 0 ? void 0 : _a.refs) || []) {
44
- this.refs.get(ref).delete(socket.id);
44
+ (_b = this.refs.get(ref)) === null || _b === void 0 ? void 0 : _b.delete(socket.id);
45
45
  }
46
46
  this.connections.delete(socket.id);
47
47
  }
@@ -50,41 +50,41 @@ let LivequeryWebsocketSync = class LivequeryWebsocketSync {
50
50
  socket.id = id;
51
51
  }
52
52
  unsubscribe({ ref }, socket) {
53
- var _a;
53
+ var _a, _b, _c, _d;
54
54
  if (!this.connections.has(socket.id))
55
55
  return;
56
- const refs = [...this.connections.get(socket.id).refs];
57
- for (const ref of refs)
58
- (_a = this.refs.get(ref)) === null || _a === void 0 ? void 0 : _a.delete(socket.id);
56
+ (_b = (_a = this.connections.get(socket.id)) === null || _a === void 0 ? void 0 : _a.refs) === null || _b === void 0 ? void 0 : _b.delete(ref);
57
+ (_c = this.refs.get(ref)) === null || _c === void 0 ? void 0 : _c.delete(socket.id);
58
+ ((_d = this.refs.get(ref)) === null || _d === void 0 ? void 0 : _d.size) == 0 && this.refs.delete(ref);
59
59
  }
60
- listen(connection_id, ref) {
60
+ listen(connection_id, collection_ref, doc_id) {
61
61
  const cnn = this.connections.get(connection_id);
62
62
  if (cnn) {
63
- cnn.refs.add(ref);
64
- if (!this.refs.has(ref))
65
- this.refs.set(ref, new Set());
63
+ const ref = `${collection_ref}${doc_id ? `/${doc_id}` : ''}`;
64
+ !cnn.refs.has(collection_ref) && cnn.refs.add(ref);
65
+ !this.refs.has(ref) && this.refs.set(ref, new Set());
66
66
  this.refs.get(ref).add(connection_id);
67
67
  }
68
68
  }
69
69
  };
70
70
  __decorate([
71
- websockets_1.SubscribeMessage('start'),
72
- __param(0, websockets_1.MessageBody()),
73
- __param(1, websockets_1.ConnectedSocket()),
71
+ (0, websockets_1.SubscribeMessage)('start'),
72
+ __param(0, (0, websockets_1.MessageBody)()),
73
+ __param(1, (0, websockets_1.ConnectedSocket)()),
74
74
  __metadata("design:type", Function),
75
75
  __metadata("design:paramtypes", [Object, Object]),
76
76
  __metadata("design:returntype", void 0)
77
77
  ], LivequeryWebsocketSync.prototype, "subscribe", null);
78
78
  __decorate([
79
- websockets_1.SubscribeMessage('unsubscribe'),
80
- __param(0, websockets_1.MessageBody()),
81
- __param(1, websockets_1.ConnectedSocket()),
79
+ (0, websockets_1.SubscribeMessage)('unsubscribe'),
80
+ __param(0, (0, websockets_1.MessageBody)()),
81
+ __param(1, (0, websockets_1.ConnectedSocket)()),
82
82
  __metadata("design:type", Function),
83
83
  __metadata("design:paramtypes", [Object, Object]),
84
84
  __metadata("design:returntype", void 0)
85
85
  ], LivequeryWebsocketSync.prototype, "unsubscribe", null);
86
86
  LivequeryWebsocketSync = __decorate([
87
- websockets_1.WebSocketGateway({ path: process.env.REALTIME_UPDATE_SOCKET_PATH || '/livequery/realtime-updates' }),
87
+ (0, websockets_1.WebSocketGateway)({ path: process.env.REALTIME_UPDATE_SOCKET_PATH || '/livequery/realtime-updates' }),
88
88
  __metadata("design:paramtypes", [])
89
89
  ], LivequeryWebsocketSync);
90
90
  exports.LivequeryWebsocketSync = LivequeryWebsocketSync;
@@ -1 +1 @@
1
- export declare const COLLECTION_REF_SLICE_INDEX: number;
1
+ export declare const LIVEQUERY_MAGIC_KEY: string;
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.COLLECTION_REF_SLICE_INDEX = void 0;
4
- exports.COLLECTION_REF_SLICE_INDEX = Number(process.env.COLLECTION_REF_SLICE_INDEX || 1);
3
+ exports.LIVEQUERY_MAGIC_KEY = void 0;
4
+ exports.LIVEQUERY_MAGIC_KEY = process.env.LIVEQUERY_MAGIC_KEY || 'livequery';
@@ -0,0 +1,8 @@
1
+ export declare class PathHelper {
2
+ #private;
3
+ static livequeryPathExtractor(path: string): {
4
+ ref: string;
5
+ schema_ref: string;
6
+ };
7
+ static nestjsPathResolver(target: Function, method: string): string[];
8
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _a, _PathHelper_toArray;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.PathHelper = void 0;
10
+ const const_1 = require("./const");
11
+ class PathHelper {
12
+ static livequeryPathExtractor(path) {
13
+ var _b, _c, _d, _e;
14
+ const refs = (_e = (_d = (_c = (_b = path === null || path === void 0 ? void 0 : path.replaceAll(':', '')) === null || _b === void 0 ? void 0 : _b.split(const_1.LIVEQUERY_MAGIC_KEY)) === null || _c === void 0 ? void 0 : _c[1]) === null || _d === void 0 ? void 0 : _d.split('/')) === null || _e === void 0 ? void 0 : _e.filter(s => s.length > 0);
15
+ if (!refs)
16
+ return null;
17
+ const ref = refs.join('/');
18
+ const schema_ref = refs.filter((_, i) => i % 2 == 0).join('/');
19
+ return { ref, schema_ref };
20
+ }
21
+ static nestjsPathResolver(target, method) {
22
+ const collection_paths = __classPrivateFieldGet(this, _a, "f", _PathHelper_toArray).call(this, Reflect.getMetadata('path', target) || '');
23
+ const method_paths = __classPrivateFieldGet(this, _a, "f", _PathHelper_toArray).call(this, Reflect.getMetadata('path', target.prototype[method]) || '');
24
+ return collection_paths.map(collection_path => method_paths.map(method_path => `${collection_path}/${method_path}`)).flat(2);
25
+ }
26
+ }
27
+ exports.PathHelper = PathHelper;
28
+ _a = PathHelper;
29
+ _PathHelper_toArray = { value: (a) => typeof a == 'string' ? [a] : a };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": {
4
4
  "url": "git@github.com:livequery/nestjs.git"
5
5
  },
6
- "version": "1.0.24",
6
+ "version": "1.0.28",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "build/**/*"
12
12
  ],
13
13
  "devDependencies": {
14
- "@livequery/types": "^1.0.25",
14
+ "@livequery/types": "^1.0.29",
15
15
  "@types/express": "^4.17.11",
16
16
  "@types/node": "^14.14.20"
17
17
  },