@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 CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './src/LivequeryRequest';
2
2
  export * from './src/LivequeryInterceptor';
3
3
  export * from './src/LivequeryWebsocketSync';
4
- export * from './src/pathResolver';
4
+ export * from './src/PathHelper';
5
+ export * from './src/UseDatasource';
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/pathResolver"), exports);
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 index_1 = require("index");
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 _l = req.query, { _limit = 20, _cursor, _order_by, _sort, _select } = _l, rest = __rest(_l, ["_limit", "_cursor", "_order_by", "_sort", "_select"]);
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 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);
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: Object.assign({ _limit: Number(_limit), _cursor,
49
+ options: {
50
+ _limit: Number(_limit),
51
+ _cursor,
70
52
  _order_by,
71
- _sort }, _select ? { _select: JSON.parse(_select) } : {}),
72
- keys: req.params
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 && ((_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 })));
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
- var _a, _b;
43
- for (const ref of ((_a = this.connections.get(socket.id)) === null || _a === void 0 ? void 0 : _a.refs) || []) {
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
- (_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);
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;
@@ -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 const pathResolver: (target: Function, method: string) => {
2
- ref: string;
3
- schema_ref: string;
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.pathResolver = void 0;
3
+ exports.PathHelper = void 0;
4
4
  const const_1 = require("./const");
5
- const pathResolver = (target, method) => {
6
- var _a;
7
- const collection_path = Reflect.getMetadata('path', target) || '';
8
- const method_path = Reflect.getMetadata('path', target.prototype[method]) || '';
9
- const refs = (_a = `${collection_path}/${method_path}`
10
- .replaceAll(':', '')
11
- .split(const_1.LIVEQUERY_MAGIC_KEY)) === null || _a === void 0 ? void 0 : _a[1].split('/').filter(s => s.length > 0);
12
- const ref = refs.join('/');
13
- const schema_ref = refs.filter((_, i) => i % 2 == 0).join('/');
14
- return { ref, schema_ref };
15
- };
16
- exports.pathResolver = pathResolver;
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.27",
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.29",
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
+ }