@livequery/nestjs 1.0.29 → 1.0.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.
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/src/LivequeryInterceptor.js +15 -28
- package/build/src/LivequeryWebsocketSync.js +8 -10
- package/build/src/PathHelper.d.ts +11 -0
- package/build/src/PathHelper.js +28 -0
- package/build/src/const.js +1 -1
- package/build/src/pathResolver.d.ts +3 -0
- package/build/src/pathResolver.js +13 -14
- package/package.json +9 -4
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -13,4 +13,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
13
13
|
__exportStar(require("./src/LivequeryRequest"), exports);
|
|
14
14
|
__exportStar(require("./src/LivequeryInterceptor"), exports);
|
|
15
15
|
__exportStar(require("./src/LivequeryWebsocketSync"), exports);
|
|
16
|
-
__exportStar(require("./src/
|
|
16
|
+
__exportStar(require("./src/PathHelper"), exports);
|
|
@@ -11,33 +11,22 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
15
|
-
var t = {};
|
|
16
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
17
|
-
t[p] = s[p];
|
|
18
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
19
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
20
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
21
|
-
t[p[i]] = s[p[i]];
|
|
22
|
-
}
|
|
23
|
-
return t;
|
|
24
|
-
};
|
|
25
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
15
|
exports.UseLivequeryInterceptor = exports.LivequeryInterceptor = void 0;
|
|
27
16
|
const common_1 = require("@nestjs/common");
|
|
28
17
|
const LivequeryWebsocketSync_1 = require("./LivequeryWebsocketSync");
|
|
29
|
-
const const_1 = require("./const");
|
|
30
18
|
const rxjs_1 = require("rxjs");
|
|
31
19
|
const operators_1 = require("rxjs/operators");
|
|
32
20
|
const LivequeryRequest_1 = require("./LivequeryRequest");
|
|
21
|
+
const PathHelper_1 = require("./PathHelper");
|
|
33
22
|
let LivequeryInterceptor = class LivequeryInterceptor {
|
|
23
|
+
LivequeryWebsocketSync;
|
|
34
24
|
constructor(LivequeryWebsocketSync = null) {
|
|
35
25
|
this.LivequeryWebsocketSync = LivequeryWebsocketSync;
|
|
36
26
|
}
|
|
37
27
|
intercept(context, next) {
|
|
38
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
39
28
|
const req = context.switchToHttp().getRequest();
|
|
40
|
-
const
|
|
29
|
+
const { _limit = 20, _cursor, _order_by, _sort, _select, ...rest } = req.query;
|
|
41
30
|
const filters = Object
|
|
42
31
|
.keys(rest)
|
|
43
32
|
.map(key => {
|
|
@@ -49,16 +38,8 @@ let LivequeryInterceptor = class LivequeryInterceptor {
|
|
|
49
38
|
return [name, expression || 'eq', rest[key]];
|
|
50
39
|
}
|
|
51
40
|
});
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
throw new common_1.HttpException('CAN_NOT_DETECT_LIVEQUERY_KEY', 400);
|
|
55
|
-
const ref = refs.join('/');
|
|
56
|
-
const is_collection = refs.length % 2 == 1;
|
|
57
|
-
const collection_ref = refs.slice(0, refs.length - (is_collection ? 0 : 1)).join('/');
|
|
58
|
-
const doc_id = !is_collection && refs[refs.length - 1];
|
|
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);
|
|
41
|
+
const { ref, is_collection, collection_ref, doc_id, } = PathHelper_1.PathHelper.livequeryPathExtractor(req._parsedUrl.pathname);
|
|
42
|
+
const { collection_ref: schema_collection_ref } = PathHelper_1.PathHelper.livequeryPathExtractor(req.route.path);
|
|
62
43
|
req[LivequeryRequest_1.LivequeryRequestKey] = {
|
|
63
44
|
ref,
|
|
64
45
|
collection_ref,
|
|
@@ -66,13 +47,19 @@ let LivequeryInterceptor = class LivequeryInterceptor {
|
|
|
66
47
|
is_collection,
|
|
67
48
|
doc_id,
|
|
68
49
|
filters,
|
|
69
|
-
options:
|
|
50
|
+
options: {
|
|
51
|
+
_limit: Number(_limit),
|
|
52
|
+
_cursor,
|
|
70
53
|
_order_by,
|
|
71
|
-
_sort
|
|
72
|
-
|
|
54
|
+
_sort,
|
|
55
|
+
..._select ? { _select: JSON.parse(_select) } : {}
|
|
56
|
+
},
|
|
57
|
+
keys: req.params,
|
|
58
|
+
body: req.body,
|
|
59
|
+
method: req.method.toLowerCase()
|
|
73
60
|
};
|
|
74
61
|
const socket_id = req.headers.socket_id;
|
|
75
|
-
socket_id &&
|
|
62
|
+
socket_id && this.LivequeryWebsocketSync?.listen(socket_id, collection_ref, doc_id);
|
|
76
63
|
return next.handle().pipe((0, operators_1.map)(data => ({ data })), (0, operators_1.catchError)(error => (0, rxjs_1.of)({ error })));
|
|
77
64
|
}
|
|
78
65
|
};
|
|
@@ -17,10 +17,10 @@ const websockets_1 = require("@nestjs/websockets");
|
|
|
17
17
|
const rxjs_1 = require("rxjs");
|
|
18
18
|
const operators_1 = require("rxjs/operators");
|
|
19
19
|
let LivequeryWebsocketSync = class LivequeryWebsocketSync {
|
|
20
|
+
connections = new Map();
|
|
21
|
+
refs = new Map();
|
|
22
|
+
changes = new rxjs_1.Subject();
|
|
20
23
|
constructor() {
|
|
21
|
-
this.connections = new Map();
|
|
22
|
-
this.refs = new Map();
|
|
23
|
-
this.changes = new rxjs_1.Subject();
|
|
24
24
|
this.changes.pipe((0, operators_1.mergeMap)(change => {
|
|
25
25
|
const map = new Map();
|
|
26
26
|
if (!map.has(change.ref))
|
|
@@ -39,9 +39,8 @@ let LivequeryWebsocketSync = class LivequeryWebsocketSync {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
async handleDisconnect(socket) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
(_b = this.refs.get(ref)) === null || _b === void 0 ? void 0 : _b.delete(socket.id);
|
|
42
|
+
for (const ref of this.connections.get(socket.id)?.refs || []) {
|
|
43
|
+
this.refs.get(ref)?.delete(socket.id);
|
|
45
44
|
}
|
|
46
45
|
this.connections.delete(socket.id);
|
|
47
46
|
}
|
|
@@ -50,12 +49,11 @@ let LivequeryWebsocketSync = class LivequeryWebsocketSync {
|
|
|
50
49
|
socket.id = id;
|
|
51
50
|
}
|
|
52
51
|
unsubscribe({ ref }, socket) {
|
|
53
|
-
var _a, _b, _c, _d;
|
|
54
52
|
if (!this.connections.has(socket.id))
|
|
55
53
|
return;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
this.connections.get(socket.id)?.refs?.delete(ref);
|
|
55
|
+
this.refs.get(ref)?.delete(socket.id);
|
|
56
|
+
this.refs.get(ref)?.size == 0 && this.refs.delete(ref);
|
|
59
57
|
}
|
|
60
58
|
listen(connection_id, collection_ref, doc_id) {
|
|
61
59
|
const cnn = this.connections.get(connection_id);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class PathHelper {
|
|
2
|
+
#private;
|
|
3
|
+
static livequeryPathExtractor(path: string): {
|
|
4
|
+
ref: string;
|
|
5
|
+
schema_ref: string;
|
|
6
|
+
is_collection: boolean;
|
|
7
|
+
doc_id: string;
|
|
8
|
+
collection_ref: string;
|
|
9
|
+
};
|
|
10
|
+
static nestjsPathResolver(target: Function, method: string): string[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PathHelper = void 0;
|
|
4
|
+
const const_1 = require("./const");
|
|
5
|
+
class PathHelper {
|
|
6
|
+
static #toArray = (a) => typeof a == 'string' ? [a] : a;
|
|
7
|
+
static livequeryPathExtractor(path) {
|
|
8
|
+
const refs = path
|
|
9
|
+
?.replaceAll(':', '')
|
|
10
|
+
?.split(const_1.LIVEQUERY_MAGIC_KEY)?.[1]
|
|
11
|
+
?.split('/')
|
|
12
|
+
?.filter(s => s.length > 0);
|
|
13
|
+
if (!refs)
|
|
14
|
+
throw 'LIVEQUERY_MAGIC_KEY_NOT_FOUND';
|
|
15
|
+
const is_collection = refs.length % 2 == 1;
|
|
16
|
+
const ref = refs.join('/');
|
|
17
|
+
const collection_ref = is_collection ? ref : refs.slice(0, refs.length - 1).join('/');
|
|
18
|
+
const schema_ref = refs.filter((_, i) => i % 2 == 0).join('/');
|
|
19
|
+
const doc_id = is_collection ? refs[refs.length - 1] : null;
|
|
20
|
+
return { ref, schema_ref, is_collection, doc_id, collection_ref };
|
|
21
|
+
}
|
|
22
|
+
static nestjsPathResolver(target, method) {
|
|
23
|
+
const collection_paths = this.#toArray(Reflect.getMetadata('path', target) || '');
|
|
24
|
+
const method_paths = this.#toArray(Reflect.getMetadata('path', target.prototype[method]) || '');
|
|
25
|
+
return collection_paths.map(collection_path => method_paths.map(method_path => `${collection_path}/${method_path}`)).flat(2);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.PathHelper = PathHelper;
|
package/build/src/const.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LIVEQUERY_MAGIC_KEY = void 0;
|
|
4
|
-
exports.LIVEQUERY_MAGIC_KEY = process.env.LIVEQUERY_MAGIC_KEY || 'livequery'
|
|
4
|
+
exports.LIVEQUERY_MAGIC_KEY = `${process.env.LIVEQUERY_MAGIC_KEY || 'livequery'}/`;
|
|
@@ -3,6 +3,9 @@ export declare class PathHelper {
|
|
|
3
3
|
static livequeryPathExtractor(path: string): {
|
|
4
4
|
ref: string;
|
|
5
5
|
schema_ref: string;
|
|
6
|
+
is_collection: boolean;
|
|
7
|
+
doc_id: string;
|
|
8
|
+
collection_ref: string;
|
|
6
9
|
};
|
|
7
10
|
static nestjsPathResolver(target: Function, method: string): string[];
|
|
8
11
|
}
|
|
@@ -1,29 +1,28 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
3
|
exports.PathHelper = void 0;
|
|
10
4
|
const const_1 = require("./const");
|
|
11
5
|
class PathHelper {
|
|
6
|
+
static #toArray = (a) => typeof a == 'string' ? [a] : a;
|
|
12
7
|
static livequeryPathExtractor(path) {
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
const refs = path
|
|
9
|
+
?.replaceAll(':', '')
|
|
10
|
+
?.split(const_1.LIVEQUERY_MAGIC_KEY)?.[1]
|
|
11
|
+
?.split('/')
|
|
12
|
+
?.filter(s => s.length > 0);
|
|
15
13
|
if (!refs)
|
|
16
|
-
|
|
14
|
+
throw 'LIVEQUERY_MAGIC_KEY_NOT_FOUND';
|
|
15
|
+
const is_collection = refs.length % 2 == 1;
|
|
17
16
|
const ref = refs.join('/');
|
|
17
|
+
const collection_ref = is_collection ? ref : refs.slice(0, refs.length - 1).join('/');
|
|
18
18
|
const schema_ref = refs.filter((_, i) => i % 2 == 0).join('/');
|
|
19
|
-
|
|
19
|
+
const doc_id = is_collection ? refs[refs.length - 1] : null;
|
|
20
|
+
return { ref, schema_ref, is_collection, doc_id, collection_ref };
|
|
20
21
|
}
|
|
21
22
|
static nestjsPathResolver(target, method) {
|
|
22
|
-
const collection_paths =
|
|
23
|
-
const method_paths =
|
|
23
|
+
const collection_paths = this.#toArray(Reflect.getMetadata('path', target) || '');
|
|
24
|
+
const method_paths = this.#toArray(Reflect.getMetadata('path', target.prototype[method]) || '');
|
|
24
25
|
return collection_paths.map(collection_path => method_paths.map(method_path => `${collection_path}/${method_path}`)).flat(2);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
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.
|
|
6
|
+
"version": "1.0.30",
|
|
7
7
|
"description": "",
|
|
8
8
|
"main": "build/index.js",
|
|
9
9
|
"types": "build/index.d.ts",
|
|
@@ -11,9 +11,14 @@
|
|
|
11
11
|
"build/**/*"
|
|
12
12
|
],
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@livequery/types": "^1.0.
|
|
14
|
+
"@livequery/types": "^1.0.34",
|
|
15
|
+
"@nestjs/common": "^8.2.3",
|
|
16
|
+
"@nestjs/core": "^8.2.3",
|
|
17
|
+
"@nestjs/websockets": "^8.2.3",
|
|
15
18
|
"@types/express": "^4.17.11",
|
|
16
|
-
"@types/node": "^14.14.20"
|
|
19
|
+
"@types/node": "^14.14.20",
|
|
20
|
+
"reflect-metadata": "^0.1.13",
|
|
21
|
+
"rxjs": "^7.4.0"
|
|
17
22
|
},
|
|
18
23
|
"peerDependencies": {
|
|
19
24
|
"@nestjs/common": "^7.6.17",
|
|
@@ -29,4 +34,4 @@
|
|
|
29
34
|
},
|
|
30
35
|
"author": "",
|
|
31
36
|
"license": "ISC"
|
|
32
|
-
}
|
|
37
|
+
}
|