@livequery/nestjs 1.0.39 → 1.0.40

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,5 +1,6 @@
1
- export * from './src/LivequeryRequest';
1
+ export { LivequeryRequest } from './src/LivequeryRequest';
2
2
  export * from './src/LivequeryResponse';
3
3
  export * from './src/LivequeryInterceptor';
4
4
  export * from './src/LivequeryWebsocketSync';
5
- export * from './src/PathHelper';
5
+ export * from './src/createDatasourceMapper';
6
+ export { LivequeryDatasourceInterceptors } from './src/LivequeryDatasourceInterceptors';
package/build/index.js CHANGED
@@ -14,8 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./src/LivequeryRequest"), exports);
17
+ exports.LivequeryDatasourceInterceptors = exports.LivequeryRequest = void 0;
18
+ var LivequeryRequest_1 = require("./src/LivequeryRequest");
19
+ Object.defineProperty(exports, "LivequeryRequest", { enumerable: true, get: function () { return LivequeryRequest_1.LivequeryRequest; } });
18
20
  __exportStar(require("./src/LivequeryResponse"), exports);
19
21
  __exportStar(require("./src/LivequeryInterceptor"), exports);
20
22
  __exportStar(require("./src/LivequeryWebsocketSync"), exports);
21
- __exportStar(require("./src/PathHelper"), exports);
23
+ __exportStar(require("./src/createDatasourceMapper"), exports);
24
+ var LivequeryDatasourceInterceptors_1 = require("./src/LivequeryDatasourceInterceptors");
25
+ Object.defineProperty(exports, "LivequeryDatasourceInterceptors", { enumerable: true, get: function () { return LivequeryDatasourceInterceptors_1.LivequeryDatasourceInterceptors; } });
@@ -0,0 +1,16 @@
1
+ import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
2
+ import { LivequeryRequest } from '@livequery/types';
3
+ import { ModuleRef } from '@nestjs/core';
4
+ export declare type DatasourceOptions<T> = Array<T & {
5
+ refs: string[];
6
+ }>;
7
+ export declare type Datasource<T = any> = {
8
+ query(query: LivequeryRequest): any;
9
+ };
10
+ export declare const DatasourceList: Map<new (...args: any[]) => Datasource, Datasource<any>>;
11
+ export declare const $__datasource_factory_token: unique symbol;
12
+ export declare class LivequeryDatasourceInterceptors implements NestInterceptor {
13
+ private moduleRef;
14
+ constructor(moduleRef: ModuleRef);
15
+ intercept(ctx: ExecutionContext, next: CallHandler): Promise<import("rxjs").Observable<any>>;
16
+ }
@@ -0,0 +1,35 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.LivequeryDatasourceInterceptors = exports.$__datasource_factory_token = exports.DatasourceList = void 0;
13
+ const common_1 = require("@nestjs/common");
14
+ const rxjs_1 = require("rxjs");
15
+ const core_1 = require("@nestjs/core");
16
+ exports.DatasourceList = new Map();
17
+ exports.$__datasource_factory_token = Symbol();
18
+ let LivequeryDatasourceInterceptors = class LivequeryDatasourceInterceptors {
19
+ moduleRef;
20
+ constructor(moduleRef) {
21
+ this.moduleRef = moduleRef;
22
+ }
23
+ async intercept(ctx, next) {
24
+ const token = Reflect.getMetadata(exports.$__datasource_factory_token, ctx.getHandler());
25
+ const datasource = exports.DatasourceList.has(token) ? exports.DatasourceList.get(token) : this.moduleRef.get(token);
26
+ const req = ctx.switchToHttp().getRequest();
27
+ req.livequery_response = await datasource.query(req.livequery);
28
+ return next.handle().pipe((0, rxjs_1.map)(rs => rs ?? req.livequery_response));
29
+ }
30
+ };
31
+ LivequeryDatasourceInterceptors = __decorate([
32
+ (0, common_1.Injectable)(),
33
+ __metadata("design:paramtypes", [core_1.ModuleRef])
34
+ ], LivequeryDatasourceInterceptors);
35
+ exports.LivequeryDatasourceInterceptors = LivequeryDatasourceInterceptors;
@@ -36,8 +36,8 @@ let LivequeryInterceptor = class LivequeryInterceptor {
36
36
  return [name, expression || 'eq', rest[key]];
37
37
  }
38
38
  });
39
- const { ref, is_collection, collection_ref, doc_id, } = PathHelper_1.PathHelper.livequeryPathExtractor(req._parsedUrl.pathname);
40
- const { collection_ref: schema_collection_ref, ref: schema_ref } = PathHelper_1.PathHelper.livequeryPathExtractor(req.route.path);
39
+ const { ref, is_collection, collection_ref, doc_id, } = PathHelper_1.PathHelper.parseHttpRequestPath(req._parsedUrl.pathname);
40
+ const { collection_ref: schema_collection_ref, ref: schema_ref } = PathHelper_1.PathHelper.parseHttpRequestPath(req.route.path);
41
41
  req.livequery = {
42
42
  ref,
43
43
  schema_ref,
@@ -1,11 +1,11 @@
1
1
  export declare class PathHelper {
2
- #private;
3
- static livequeryPathExtractor(path: string): {
2
+ static trimLivequeryHotkey(path: string): string;
3
+ static parseHttpRequestPath(path: string): {
4
4
  ref: string;
5
5
  schema_ref: string;
6
6
  is_collection: boolean;
7
7
  doc_id: string;
8
8
  collection_ref: string;
9
9
  };
10
- static nestjsPathResolver(target: Function, method: string): string[];
10
+ static join(a: string | string[], b: string | string[]): string[];
11
11
  }
@@ -3,9 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PathHelper = void 0;
4
4
  const const_1 = require("./const");
5
5
  class PathHelper {
6
- static #toArray = (a) => typeof a == 'string' ? [a] : a;
7
- static livequeryPathExtractor(path) {
6
+ static trimLivequeryHotkey(path) {
7
+ const ref = path
8
+ ?.split('~')[0]
9
+ ?.replaceAll(':', '')
10
+ ?.split(const_1.LIVEQUERY_MAGIC_KEY)?.[1]
11
+ ?.split('/')
12
+ ?.filter(s => s.length > 0)
13
+ ?.join('/');
14
+ if (!ref)
15
+ throw 'LIVEQUERY_MAGIC_KEY_NOT_FOUND';
16
+ return ref;
17
+ }
18
+ static parseHttpRequestPath(path) {
8
19
  const refs = path
20
+ ?.split('~')[0]
9
21
  ?.replaceAll(':', '')
10
22
  ?.split(const_1.LIVEQUERY_MAGIC_KEY)?.[1]
11
23
  ?.split('/')
@@ -19,10 +31,14 @@ class PathHelper {
19
31
  const doc_id = is_collection ? null : refs[refs.length - 1];
20
32
  return { ref, schema_ref, is_collection, doc_id, collection_ref };
21
33
  }
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);
34
+ static join(a, b) {
35
+ return [a || '']
36
+ .flat(2)
37
+ .map(r1 => [b || ''].flat(2).map(r2 => `${r1}/${r2}`))
38
+ .flat(2)
39
+ .map(r => r.split('/')
40
+ .filter(r => r != '')
41
+ .join('/'));
26
42
  }
27
43
  }
28
44
  exports.PathHelper = PathHelper;
@@ -0,0 +1,6 @@
1
+ import { Provider } from "@nestjs/common";
2
+ import { Datasource, DatasourceOptions } from "./LivequeryDatasourceInterceptors";
3
+ export declare const createDatasourceMapper: <T extends {}>(datasource_factory: new (...args: any[]) => Datasource) => [({ useFactory, inject }: {
4
+ useFactory: (options: DatasourceOptions<T>, ...injects: any[]) => Datasource<T> | Promise<Datasource<T>>;
5
+ inject?: any[];
6
+ }) => Provider<any>, (options: T) => <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void];
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDatasourceMapper = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const LivequeryDatasourceInterceptors_1 = require("./LivequeryDatasourceInterceptors");
6
+ const LivequeryInterceptor_1 = require("./LivequeryInterceptor");
7
+ const PathHelper_1 = require("./PathHelper");
8
+ const createDatasourceMapper = (datasource_factory) => {
9
+ const RouteConfigList = [];
10
+ const decorator = (options) => (0, common_1.applyDecorators)((target, method) => RouteConfigList.push({ target, method, options }), (0, LivequeryInterceptor_1.UseLivequeryInterceptor)(), (0, common_1.UseInterceptors)(LivequeryDatasourceInterceptors_1.LivequeryDatasourceInterceptors), (target, method, descriptor) => {
11
+ Reflect.defineMetadata(LivequeryDatasourceInterceptors_1.$__datasource_factory_token, datasource_factory, descriptor.value);
12
+ });
13
+ const UseDatasource = ({ useFactory, inject }) => {
14
+ return {
15
+ provide: datasource_factory,
16
+ inject,
17
+ useFactory: async (...injects) => {
18
+ const options = RouteConfigList.map(config => {
19
+ return {
20
+ ...(config.options || {}),
21
+ refs: PathHelper_1.PathHelper.join(Reflect.getMetadata('path', config.target.constructor), Reflect.getMetadata('path', config.target[config.method])).map(PathHelper_1.PathHelper.trimLivequeryHotkey)
22
+ };
23
+ });
24
+ const datasource = await useFactory(options, ...injects);
25
+ LivequeryDatasourceInterceptors_1.DatasourceList.set(datasource_factory, datasource);
26
+ return datasource;
27
+ }
28
+ };
29
+ };
30
+ return [UseDatasource, decorator];
31
+ };
32
+ exports.createDatasourceMapper = createDatasourceMapper;
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.39",
6
+ "version": "1.0.40",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",
@@ -11,14 +11,14 @@
11
11
  "build/**/*"
12
12
  ],
13
13
  "devDependencies": {
14
- "@livequery/types": "^1.0.34",
15
- "@nestjs/common": "^8.2.3",
16
- "@nestjs/core": "^8.2.3",
17
- "@nestjs/websockets": "^8.2.3",
18
- "@types/express": "^4.17.11",
19
- "@types/node": "^14.14.20",
20
- "reflect-metadata": "^0.1.13",
21
- "rxjs": "^7.4.0"
14
+ "@livequery/types": "1.x",
15
+ "@nestjs/common": "8.x",
16
+ "@nestjs/core": "8.x",
17
+ "@nestjs/websockets": "8.x",
18
+ "@types/express": "4.x",
19
+ "@types/node": "14.x",
20
+ "reflect-metadata": "0.x",
21
+ "rxjs": "^.x"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@nestjs/common": "^7.6.17",
@@ -34,4 +34,4 @@
34
34
  },
35
35
  "author": "",
36
36
  "license": "ISC"
37
- }
37
+ }