@livequery/nestjs 1.0.27 → 1.0.31
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 +2 -1
- package/build/index.js +2 -1
- package/build/src/LivequeryInterceptor.d.ts +0 -2
- package/build/src/LivequeryInterceptor.js +18 -32
- 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/UseDatasource.d.ts +27 -0
- package/build/src/UseDatasource.js +57 -0
- package/build/src/const.js +1 -1
- package/build/src/pathResolver.d.ts +11 -4
- package/build/src/pathResolver.js +25 -13
- package/package.json +9 -4
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -13,4 +13,5 @@ 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);
|
|
17
|
+
__exportStar(require("./src/UseDatasource"), exports);
|
|
@@ -5,8 +5,6 @@ export declare class LivequeryInterceptor implements NestInterceptor {
|
|
|
5
5
|
constructor(LivequeryWebsocketSync?: LivequeryWebsocketSync);
|
|
6
6
|
intercept(context: ExecutionContext, next: CallHandler): import("rxjs").Observable<{
|
|
7
7
|
data: any;
|
|
8
|
-
} | {
|
|
9
|
-
error: any;
|
|
10
8
|
}>;
|
|
11
9
|
}
|
|
12
10
|
export declare const UseLivequeryInterceptor: () => MethodDecorator & ClassDecorator;
|
|
@@ -11,33 +11,21 @@ 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
|
-
const rxjs_1 = require("rxjs");
|
|
31
18
|
const operators_1 = require("rxjs/operators");
|
|
32
|
-
const
|
|
19
|
+
const LivequeryRequest_1 = require("./LivequeryRequest");
|
|
20
|
+
const PathHelper_1 = require("./PathHelper");
|
|
33
21
|
let LivequeryInterceptor = class LivequeryInterceptor {
|
|
22
|
+
LivequeryWebsocketSync;
|
|
34
23
|
constructor(LivequeryWebsocketSync = null) {
|
|
35
24
|
this.LivequeryWebsocketSync = LivequeryWebsocketSync;
|
|
36
25
|
}
|
|
37
26
|
intercept(context, next) {
|
|
38
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
39
27
|
const req = context.switchToHttp().getRequest();
|
|
40
|
-
const
|
|
28
|
+
const { _limit = 20, _cursor, _order_by, _sort, _select, ...rest } = req.query;
|
|
41
29
|
const filters = Object
|
|
42
30
|
.keys(rest)
|
|
43
31
|
.map(key => {
|
|
@@ -49,31 +37,29 @@ let LivequeryInterceptor = class LivequeryInterceptor {
|
|
|
49
37
|
return [name, expression || 'eq', rest[key]];
|
|
50
38
|
}
|
|
51
39
|
});
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
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);
|
|
62
|
-
req[index_1.LivequeryRequestKey] = {
|
|
40
|
+
const { ref, is_collection, collection_ref, doc_id, } = PathHelper_1.PathHelper.livequeryPathExtractor(req._parsedUrl.pathname);
|
|
41
|
+
const { collection_ref: schema_collection_ref } = PathHelper_1.PathHelper.livequeryPathExtractor(req.route.path);
|
|
42
|
+
req[LivequeryRequest_1.LivequeryRequestKey] = {
|
|
63
43
|
ref,
|
|
64
44
|
collection_ref,
|
|
65
45
|
schema_collection_ref,
|
|
66
46
|
is_collection,
|
|
67
47
|
doc_id,
|
|
68
48
|
filters,
|
|
69
|
-
options:
|
|
49
|
+
options: {
|
|
50
|
+
_limit: Number(_limit),
|
|
51
|
+
_cursor,
|
|
70
52
|
_order_by,
|
|
71
|
-
_sort
|
|
72
|
-
|
|
53
|
+
_sort,
|
|
54
|
+
..._select ? { _select: JSON.parse(_select) } : {}
|
|
55
|
+
},
|
|
56
|
+
keys: req.params,
|
|
57
|
+
body: req.body,
|
|
58
|
+
method: req.method.toLowerCase()
|
|
73
59
|
};
|
|
74
60
|
const socket_id = req.headers.socket_id;
|
|
75
|
-
socket_id &&
|
|
76
|
-
return next.handle().pipe((0, operators_1.map)(data => ({ data }))
|
|
61
|
+
socket_id && this.LivequeryWebsocketSync?.listen(socket_id, collection_ref, doc_id);
|
|
62
|
+
return next.handle().pipe((0, operators_1.map)(data => ({ data })));
|
|
77
63
|
}
|
|
78
64
|
};
|
|
79
65
|
LivequeryInterceptor = __decorate([
|
|
@@ -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;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { LivequeryRequest } from "@livequery/types";
|
|
2
|
+
import { CallHandler, ExecutionContext } from "@nestjs/common";
|
|
3
|
+
import { ModuleRef, Reflector } from "@nestjs/core";
|
|
4
|
+
declare type Datasource = {
|
|
5
|
+
query(req: LivequeryRequest): Promise<any>;
|
|
6
|
+
};
|
|
7
|
+
export declare class UseDatasource {
|
|
8
|
+
private moduleRef;
|
|
9
|
+
private reflector;
|
|
10
|
+
static readonly UseDatasourceMetadata: unique symbol;
|
|
11
|
+
private static ControllerList;
|
|
12
|
+
constructor(moduleRef: ModuleRef, reflector: Reflector);
|
|
13
|
+
static createDatasourceMapper<T>(factory: {
|
|
14
|
+
new (): Datasource;
|
|
15
|
+
}): [(options: T) => <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void, () => {
|
|
16
|
+
paths: {
|
|
17
|
+
ref: string;
|
|
18
|
+
schema_ref: string;
|
|
19
|
+
is_collection: boolean;
|
|
20
|
+
doc_id: string;
|
|
21
|
+
collection_ref: string;
|
|
22
|
+
}[];
|
|
23
|
+
options: T;
|
|
24
|
+
}[]];
|
|
25
|
+
intercept(context: ExecutionContext, next: CallHandler): Promise<import("rxjs").Observable<any>>;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var UseDatasource_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.UseDatasource = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const core_1 = require("@nestjs/core");
|
|
16
|
+
const LivequeryInterceptor_1 = require("./LivequeryInterceptor");
|
|
17
|
+
const LivequeryRequest_1 = require("./LivequeryRequest");
|
|
18
|
+
const PathHelper_1 = require("./PathHelper");
|
|
19
|
+
const rxjs_1 = require("rxjs");
|
|
20
|
+
let UseDatasource = UseDatasource_1 = class UseDatasource {
|
|
21
|
+
moduleRef;
|
|
22
|
+
reflector;
|
|
23
|
+
static UseDatasourceMetadata = Symbol();
|
|
24
|
+
static ControllerList = [];
|
|
25
|
+
constructor(moduleRef, reflector) {
|
|
26
|
+
this.moduleRef = moduleRef;
|
|
27
|
+
this.reflector = reflector;
|
|
28
|
+
}
|
|
29
|
+
static createDatasourceMapper(factory) {
|
|
30
|
+
const decorator = (options) => (0, common_1.applyDecorators)((0, common_1.UseInterceptors)(LivequeryInterceptor_1.LivequeryInterceptor), (0, common_1.UseInterceptors)(this), (0, common_1.SetMetadata)(this.UseDatasourceMetadata, { options, factory }), (target, method) => this.ControllerList.push({
|
|
31
|
+
target: target.constructor,
|
|
32
|
+
method,
|
|
33
|
+
options
|
|
34
|
+
}));
|
|
35
|
+
const list = () => this.ControllerList.map(({ method, target, options }) => {
|
|
36
|
+
const paths = PathHelper_1.PathHelper.nestjsPathResolver(target, method).map(path => {
|
|
37
|
+
return PathHelper_1.PathHelper.livequeryPathExtractor(path);
|
|
38
|
+
});
|
|
39
|
+
return { paths, options: options };
|
|
40
|
+
}).flat(2);
|
|
41
|
+
return [decorator, list];
|
|
42
|
+
}
|
|
43
|
+
async intercept(context, next) {
|
|
44
|
+
const request = context.switchToHttp().getRequest();
|
|
45
|
+
const livequery = request[LivequeryRequest_1.LivequeryRequestKey];
|
|
46
|
+
const { factory } = this.reflector.get(UseDatasource_1.UseDatasourceMetadata, context.getHandler());
|
|
47
|
+
const ds = await this.moduleRef.get(factory);
|
|
48
|
+
const result = await ds.query(livequery);
|
|
49
|
+
return next.handle().pipe((0, rxjs_1.map)(data => data ?? result));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
UseDatasource = UseDatasource_1 = __decorate([
|
|
53
|
+
(0, common_1.Injectable)(),
|
|
54
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
55
|
+
core_1.Reflector])
|
|
56
|
+
], UseDatasource);
|
|
57
|
+
exports.UseDatasource = UseDatasource;
|
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'}/`;
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
}
|
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PathHelper = void 0;
|
|
4
4
|
const const_1 = require("./const");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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/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.31",
|
|
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
|
+
}
|