@livequery/nestjs 1.0.29 → 1.0.33

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
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,16 +37,8 @@ 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);
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);
62
42
  req[LivequeryRequest_1.LivequeryRequestKey] = {
63
43
  ref,
64
44
  collection_ref,
@@ -66,14 +46,20 @@ let LivequeryInterceptor = class LivequeryInterceptor {
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 ? null : refs[refs.length - 1];
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,28 @@
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
+ private static ControllerList;
11
+ constructor(moduleRef: ModuleRef, reflector: Reflector);
12
+ static createDatasourceMapper<T = {}>(factory: {
13
+ new (): Datasource;
14
+ }): [(options: T & {
15
+ _manual_query?: boolean;
16
+ }) => <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void, () => {
17
+ paths: {
18
+ ref: string;
19
+ schema_ref: string;
20
+ is_collection: boolean;
21
+ doc_id: string;
22
+ collection_ref: string;
23
+ }[];
24
+ options: T;
25
+ }[]];
26
+ intercept(context: ExecutionContext, next: CallHandler): Promise<import("rxjs").Observable<any>>;
27
+ }
28
+ export {};
@@ -0,0 +1,59 @@
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 ControllerList = [];
24
+ constructor(moduleRef, reflector) {
25
+ this.moduleRef = moduleRef;
26
+ this.reflector = reflector;
27
+ }
28
+ static createDatasourceMapper(factory) {
29
+ const decorator = (options) => (0, common_1.applyDecorators)((0, common_1.UseInterceptors)(LivequeryInterceptor_1.LivequeryInterceptor), ...options._manual_query == true ? [] : [
30
+ (0, common_1.SetMetadata)(UseDatasource_1, { options, factory }),
31
+ (0, common_1.UseInterceptors)(this)
32
+ ], (target, method) => this.ControllerList.push({
33
+ target: target.constructor,
34
+ method,
35
+ options
36
+ }));
37
+ const list = () => this.ControllerList.map(({ method, target, options }) => {
38
+ const paths = PathHelper_1.PathHelper.nestjsPathResolver(target, method).map(path => {
39
+ return PathHelper_1.PathHelper.livequeryPathExtractor(path);
40
+ });
41
+ return { paths, options: options };
42
+ }).flat(2);
43
+ return [decorator, list];
44
+ }
45
+ async intercept(context, next) {
46
+ const request = context.switchToHttp().getRequest();
47
+ const livequery = request[LivequeryRequest_1.LivequeryRequestKey];
48
+ const { factory } = this.reflector.get(UseDatasource_1, context.getHandler());
49
+ const ds = await this.moduleRef.get(factory);
50
+ const result = await ds.query(livequery);
51
+ return next.handle().pipe((0, rxjs_1.map)(data => data ?? result));
52
+ }
53
+ };
54
+ UseDatasource = UseDatasource_1 = __decorate([
55
+ (0, common_1.Injectable)(),
56
+ __metadata("design:paramtypes", [core_1.ModuleRef,
57
+ core_1.Reflector])
58
+ ], UseDatasource);
59
+ 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'}/`;
@@ -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
- 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);
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
- return null;
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
- return { ref, schema_ref };
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 = __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]) || '');
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.29",
6
+ "version": "1.0.33",
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
+ }