@opra/nestjs 0.0.5

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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/cjs/constants.js +7 -0
  4. package/cjs/decorators/context.decorator.js +30 -0
  5. package/cjs/enums/handler-paramtype.enum.js +12 -0
  6. package/cjs/factories/params.factory.js +29 -0
  7. package/cjs/factories/service.factory.js +164 -0
  8. package/cjs/index.js +9 -0
  9. package/cjs/interfaces/opra-execution-context.js +10 -0
  10. package/cjs/interfaces/opra-module-options.interface.js +2 -0
  11. package/cjs/opra-core.module.js +149 -0
  12. package/cjs/opra.module.js +25 -0
  13. package/cjs/package.json +3 -0
  14. package/cjs/services/nest-explorer.js +35 -0
  15. package/cjs/services/service-loader.js +88 -0
  16. package/cjs/types.js +2 -0
  17. package/cjs/utils/class.utils.js +25 -0
  18. package/cjs/utils/function.utils.js +23 -0
  19. package/cjs/utils/param.utils.js +22 -0
  20. package/esm/constants.d.ts +4 -0
  21. package/esm/constants.js +4 -0
  22. package/esm/decorators/context.decorator.d.ts +25 -0
  23. package/esm/decorators/context.decorator.js +27 -0
  24. package/esm/enums/handler-paramtype.enum.d.ts +8 -0
  25. package/esm/enums/handler-paramtype.enum.js +9 -0
  26. package/esm/factories/params.factory.d.ts +4 -0
  27. package/esm/factories/params.factory.js +25 -0
  28. package/esm/factories/service.factory.d.ts +15 -0
  29. package/esm/factories/service.factory.js +161 -0
  30. package/esm/index.d.ts +5 -0
  31. package/esm/index.js +6 -0
  32. package/esm/interfaces/opra-execution-context.d.ts +6 -0
  33. package/esm/interfaces/opra-execution-context.js +7 -0
  34. package/esm/interfaces/opra-module-options.interface.d.ts +49 -0
  35. package/esm/interfaces/opra-module-options.interface.js +1 -0
  36. package/esm/opra-core.module.d.ts +18 -0
  37. package/esm/opra-core.module.js +146 -0
  38. package/esm/opra.module.d.ts +6 -0
  39. package/esm/opra.module.js +22 -0
  40. package/esm/services/nest-explorer.d.ts +6 -0
  41. package/esm/services/nest-explorer.js +31 -0
  42. package/esm/services/service-loader.d.ts +18 -0
  43. package/esm/services/service-loader.js +84 -0
  44. package/esm/types.d.ts +2 -0
  45. package/esm/types.js +1 -0
  46. package/esm/utils/class.utils.d.ts +4 -0
  47. package/esm/utils/class.utils.js +19 -0
  48. package/esm/utils/function.utils.d.ts +1 -0
  49. package/esm/utils/function.utils.js +19 -0
  50. package/esm/utils/param.utils.d.ts +8 -0
  51. package/esm/utils/param.utils.js +18 -0
  52. package/package.json +73 -0
@@ -0,0 +1,84 @@
1
+ var _a;
2
+ import { __decorate, __metadata } from "tslib";
3
+ import { Inject, Logger } from '@nestjs/common';
4
+ import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
5
+ import { OpraExpressAdapter } from '@opra/core';
6
+ import { I18n } from '@opra/i18n';
7
+ import { joinPath, normalizePath } from '@opra/url';
8
+ import { OPRA_MODULE_OPTIONS } from '../constants.js';
9
+ import { ServiceFactory } from '../factories/service.factory.js';
10
+ export class OpraServiceLoader {
11
+ logger = new Logger(OpraServiceLoader.name, { timestamp: true });
12
+ adapter;
13
+ httpAdapterHost;
14
+ applicationConfig;
15
+ opraFactory;
16
+ opraModuleOptions;
17
+ i18n;
18
+ async initialize(rootModule) {
19
+ const httpAdapter = this.httpAdapterHost?.httpAdapter;
20
+ const globalPrefix = this.applicationConfig.getGlobalPrefix();
21
+ const platformName = httpAdapter.getType();
22
+ const moduleOptions = this.opraModuleOptions;
23
+ const prefix = '/' + normalizePath(joinPath((moduleOptions.useGlobalPrefix !== false ? globalPrefix : ''), moduleOptions.prefix || ''), true);
24
+ const name = moduleOptions.info?.title || 'untitled service';
25
+ const options = {
26
+ ...moduleOptions,
27
+ prefix
28
+ };
29
+ try {
30
+ const serviceHost = await this.opraFactory.generateService(rootModule, options, 'http');
31
+ if (!Object.keys(serviceHost.resources).length) {
32
+ this.logger.warn(`No resources found (${name})`);
33
+ return;
34
+ }
35
+ // serviceConfiguration.i18n = this.i18n;
36
+ if (platformName === 'express') {
37
+ this.adapter = await this.registerExpress(serviceHost, options);
38
+ // else if (platformName === 'fastify')
39
+ // await this.registerFastify();
40
+ }
41
+ else { // noinspection ExceptionCaughtLocallyJS
42
+ throw new Error(`No support for current HttpAdapter: ${platformName}`);
43
+ }
44
+ this.logger.log(`Mapped {${prefix}} to "${name}" service`);
45
+ }
46
+ catch (e) {
47
+ this.logger.error(e);
48
+ throw e;
49
+ }
50
+ }
51
+ async stop() {
52
+ //
53
+ }
54
+ async registerExpress(service, moduleOptions) {
55
+ const httpAdapter = this.httpAdapterHost.httpAdapter;
56
+ if (!httpAdapter)
57
+ return;
58
+ const app = httpAdapter.getInstance();
59
+ return OpraExpressAdapter.init(app, service, {
60
+ i18n: this.i18n,
61
+ prefix: moduleOptions.prefix
62
+ });
63
+ }
64
+ }
65
+ __decorate([
66
+ Inject(),
67
+ __metadata("design:type", HttpAdapterHost)
68
+ ], OpraServiceLoader.prototype, "httpAdapterHost", void 0);
69
+ __decorate([
70
+ Inject(),
71
+ __metadata("design:type", ApplicationConfig)
72
+ ], OpraServiceLoader.prototype, "applicationConfig", void 0);
73
+ __decorate([
74
+ Inject(),
75
+ __metadata("design:type", ServiceFactory)
76
+ ], OpraServiceLoader.prototype, "opraFactory", void 0);
77
+ __decorate([
78
+ Inject(OPRA_MODULE_OPTIONS),
79
+ __metadata("design:type", Object)
80
+ ], OpraServiceLoader.prototype, "opraModuleOptions", void 0);
81
+ __decorate([
82
+ Inject(),
83
+ __metadata("design:type", typeof (_a = typeof I18n !== "undefined" && I18n) === "function" ? _a : Object)
84
+ ], OpraServiceLoader.prototype, "i18n", void 0);
package/esm/types.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { DynamicModule, ForwardReference, Type } from '@nestjs/common';
2
+ export declare type ModuleThunk = Type | DynamicModule | Promise<DynamicModule> | ForwardReference;
package/esm/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { Type } from '@nestjs/common';
2
+ export declare function isConstructor(obj: any): obj is Type;
3
+ export declare function getClassOrUndefined(typeOrFunc: Function | Type): Type | undefined;
4
+ export declare function getClassName(nameOrType: string | Function | Type): string | undefined;
@@ -0,0 +1,19 @@
1
+ import { isFunction, isString } from '@nestjs/common/utils/shared.utils';
2
+ export function isConstructor(obj) {
3
+ return typeof obj === 'function' &&
4
+ !!(obj.prototype && obj.prototype.constructor);
5
+ }
6
+ export function getClassOrUndefined(typeOrFunc) {
7
+ return isConstructor(typeOrFunc)
8
+ ? typeOrFunc
9
+ : isFunction(typeOrFunc)
10
+ ? typeOrFunc()
11
+ : undefined;
12
+ }
13
+ export function getClassName(nameOrType) {
14
+ if (isString(nameOrType)) {
15
+ return nameOrType;
16
+ }
17
+ const classOrUndefined = getClassOrUndefined(nameOrType);
18
+ return classOrUndefined && classOrUndefined.name;
19
+ }
@@ -0,0 +1 @@
1
+ export declare function getNumberOfArguments(fn: Function): number | undefined;
@@ -0,0 +1,19 @@
1
+ import { splitString } from 'fast-tokenizer';
2
+ const anythingEnclosedInParenthesesRegex = /\((.+)\)/;
3
+ export function getNumberOfArguments(fn) {
4
+ const functionAsString = fn.toString();
5
+ const parametersEnclosedInParentheses = functionAsString.match(anythingEnclosedInParenthesesRegex);
6
+ if (parametersEnclosedInParentheses) {
7
+ const matchString = parametersEnclosedInParentheses[1];
8
+ const argumentsArray = splitString(matchString, {
9
+ brackets: {
10
+ '(': ')',
11
+ '{': '}',
12
+ '[': ']',
13
+ '/*': '*/'
14
+ }, delimiters: ','
15
+ });
16
+ return argumentsArray.length;
17
+ }
18
+ return 0;
19
+ }
@@ -0,0 +1,8 @@
1
+ import 'reflect-metadata';
2
+ import { HandlerParamType } from '../enums/handler-paramtype.enum.js';
3
+ export declare type ParamData = object | string | number;
4
+ export declare type ParamsMetadata = Record<number, {
5
+ index: number;
6
+ data?: ParamData;
7
+ }>;
8
+ export declare function createOpraParamDecorator(paramType: HandlerParamType): ParameterDecorator;
@@ -0,0 +1,18 @@
1
+ import 'reflect-metadata';
2
+ import { PARAM_ARGS_METADATA } from '../constants.js';
3
+ function assignMetadata(args, paramType, index, data, ...pipes) {
4
+ return {
5
+ ...args,
6
+ [`${paramType}:${index}`]: {
7
+ index,
8
+ data,
9
+ pipes,
10
+ },
11
+ };
12
+ }
13
+ export function createOpraParamDecorator(paramType) {
14
+ return (target, key, index) => {
15
+ const args = Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
16
+ Reflect.defineMetadata(PARAM_ARGS_METADATA, assignMetadata(args, paramType, index), target.constructor, key);
17
+ };
18
+ }
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@opra/nestjs",
3
+ "version": "0.0.5",
4
+ "description": "Opra NestJS module",
5
+ "author": "Panates",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/panates/opra.git",
10
+ "directory": "packages/nestjs"
11
+ },
12
+ "scripts": {
13
+ "compile": "tsc",
14
+ "prebuild": "npm run lint && npm run clean",
15
+ "build": "npm run build:cjs && npm run build:esm",
16
+ "build:cjs": "tsc -b tsconfig-build-cjs.json",
17
+ "build:esm": "tsc -b tsconfig-build-esm.json",
18
+ "postbuild": "cp README.md package.json ../../LICENSE ../../build/nestjs && cp ../../package.cjs.json ../../build/nestjs/cjs/package.json",
19
+ "lint": "eslint .",
20
+ "test": "jest",
21
+ "cover": "jest --collect-coverage",
22
+ "clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
23
+ "clean:src": "ts-cleanup -s src --all",
24
+ "clean:dist": "rimraf ../../build/nestjs",
25
+ "clean:cover": "rimraf ../../coverage/nestjs"
26
+ },
27
+ "dependencies": {
28
+ "@opra/url": "^0.x.x",
29
+ "@opra/i18n": "^0.x.x",
30
+ "@opra/core": "^0.x.x",
31
+ "@opra/schema": "^0.x.x",
32
+ "fast-tokenizer": "^1.1.0",
33
+ "lodash": "^4.17.21",
34
+ "reflect-metadata": "^0.1.13"
35
+ },
36
+ "peerDependencies": {
37
+ "@nestjs/common": "^8.x.x || ^9.x.x"
38
+ },
39
+ "devDependencies": {
40
+ "@nestjs/testing": "^9.0.11",
41
+ "@nestjs/platform-express": "^9.0.11",
42
+ "@types/lodash": "^4.14.185",
43
+ "filedirname": "^2.7.0",
44
+ "supertest": "^6.2.4"
45
+ },
46
+ "type": "module",
47
+ "main": "cjs/index.js",
48
+ "module": "esm/index.js",
49
+ "types": "esm/index.d.ts",
50
+ "exports": {
51
+ ".": {
52
+ "require": "./cjs/index.js",
53
+ "default": "./esm/index.js"
54
+ },
55
+ "./cjs": "./cjs/index.js",
56
+ "./esm": "./esm/index.js"
57
+ },
58
+ "engines": {
59
+ "node": ">=16.0",
60
+ "npm": ">=7.0.0"
61
+ },
62
+ "files": [
63
+ "bin/",
64
+ "cjs/",
65
+ "esm/",
66
+ "LICENSE",
67
+ "README.md"
68
+ ],
69
+ "keywords": [
70
+ "opra",
71
+ "nestjs"
72
+ ]
73
+ }