@nattyjs/common 0.0.1-beta.4 → 0.0.1-beta.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/dist/index.cjs +177 -11
- package/dist/index.d.ts +99 -5
- package/dist/index.mjs +173 -12
- package/package.json +8 -4
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,8 @@ const fs = require('fs');
|
|
|
4
4
|
const dotenv = require('dotenv');
|
|
5
5
|
const dotenvExpand = require('dotenv-expand');
|
|
6
6
|
const path = require('path');
|
|
7
|
+
const child_process = require('child_process');
|
|
8
|
+
const getPortPlease = require('get-port-please');
|
|
7
9
|
|
|
8
10
|
function _interopNamespaceCompat(e) {
|
|
9
11
|
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
@@ -25,6 +27,17 @@ const path__namespace = /*#__PURE__*/_interopNamespaceCompat(path);
|
|
|
25
27
|
const commonContainer = new class {
|
|
26
28
|
constructor() {
|
|
27
29
|
this.metadataConfig = { services: /* @__PURE__ */ new Map() };
|
|
30
|
+
this.types = {};
|
|
31
|
+
}
|
|
32
|
+
registerType(type) {
|
|
33
|
+
if (type) {
|
|
34
|
+
if (typeof type === "string")
|
|
35
|
+
type = JSON.parse(type);
|
|
36
|
+
this.types[type.name] = type;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
setupCliConfig(config) {
|
|
40
|
+
this.nattyCliConfig = config;
|
|
28
41
|
}
|
|
29
42
|
setupConfig(config) {
|
|
30
43
|
const modelBinding = {
|
|
@@ -35,7 +48,8 @@ const commonContainer = new class {
|
|
|
35
48
|
}
|
|
36
49
|
}
|
|
37
50
|
};
|
|
38
|
-
|
|
51
|
+
const secureConfig = { sensitiveProps: ["password", "mobileNo", "email"] };
|
|
52
|
+
this.nattyConfig = { ...{ api: { rootPath: "api" }, autoGeneratePort: true, modelBinding, globalConfig: {}, secure: secureConfig }, ...config };
|
|
39
53
|
}
|
|
40
54
|
setupBuildOptions(options) {
|
|
41
55
|
this.buildOptions = options;
|
|
@@ -225,20 +239,29 @@ function getPath(pathCollection, isIncludeRoot = true, isCreateFolder = false) {
|
|
|
225
239
|
return currentPath;
|
|
226
240
|
}
|
|
227
241
|
|
|
242
|
+
function resolvePath(path$1) {
|
|
243
|
+
return path.resolve(commonContainer.buildOptions.rootDir, path$1);
|
|
244
|
+
}
|
|
245
|
+
|
|
228
246
|
async function readEnv() {
|
|
229
|
-
|
|
247
|
+
const envConfig = commonContainer.nattyCliConfig.env;
|
|
248
|
+
let filePath = envConfig?.dictionary ? void 0 : getPath([ENVIRONMENTS, commonContainer.buildOptions.mode && commonContainer.buildOptions.mode !== "dev" ? `.env.${commonContainer.buildOptions.mode}` : `.env`]);
|
|
249
|
+
if (envConfig && envConfig.path)
|
|
250
|
+
filePath = resolvePath(envConfig.path);
|
|
230
251
|
let parsedEnvTsDefinition = {};
|
|
231
|
-
|
|
252
|
+
let parsedEnv = commonContainer.nattyCliConfig.env?.dictionary;
|
|
253
|
+
if (!parsedEnv && filePath && fs.existsSync(filePath)) {
|
|
232
254
|
const { parsed, error } = dotenv__namespace.config({
|
|
233
255
|
debug: !!process.env.DEBUG || void 0,
|
|
234
256
|
path: filePath
|
|
235
257
|
});
|
|
236
|
-
|
|
237
|
-
parsedEnvTsDefinition = info.parsedEnvTsDefinition;
|
|
238
|
-
commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
|
|
239
|
-
commonContainer.setEnvValueInfo(info.envVariableValueInfo);
|
|
240
|
-
dotenvExpand__namespace.expand({ parsed });
|
|
258
|
+
parsedEnv = parsed;
|
|
241
259
|
}
|
|
260
|
+
const info = getEnvTsDefinition(parsedEnv);
|
|
261
|
+
parsedEnvTsDefinition = info.parsedEnvTsDefinition;
|
|
262
|
+
commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
|
|
263
|
+
commonContainer.setEnvValueInfo(info.envVariableValueInfo);
|
|
264
|
+
dotenvExpand__namespace.expand({ parsed: parsedEnv });
|
|
242
265
|
return parsedEnvTsDefinition;
|
|
243
266
|
}
|
|
244
267
|
|
|
@@ -261,6 +284,9 @@ class AuthorizationFilter {
|
|
|
261
284
|
}
|
|
262
285
|
|
|
263
286
|
class ActionFilter {
|
|
287
|
+
onModelBinding(context) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
264
290
|
onActionExecuting(context) {
|
|
265
291
|
return;
|
|
266
292
|
}
|
|
@@ -286,10 +312,10 @@ function readEnvKey(key) {
|
|
|
286
312
|
}
|
|
287
313
|
|
|
288
314
|
class UserIdentity {
|
|
289
|
-
constructor(isAuthenticate, id,
|
|
315
|
+
constructor(isAuthenticate, id, claims) {
|
|
290
316
|
this.isAuthenticate = isAuthenticate;
|
|
291
317
|
this.id = id;
|
|
292
|
-
this.
|
|
318
|
+
this.claims = claims;
|
|
293
319
|
}
|
|
294
320
|
}
|
|
295
321
|
|
|
@@ -381,7 +407,7 @@ class List {
|
|
|
381
407
|
if (this.count()) {
|
|
382
408
|
return predicate ? this.where(predicate).first() : this._entities[0];
|
|
383
409
|
} else {
|
|
384
|
-
|
|
410
|
+
return void 0;
|
|
385
411
|
}
|
|
386
412
|
}
|
|
387
413
|
firstOrDefault(predicate) {
|
|
@@ -566,18 +592,155 @@ class List {
|
|
|
566
592
|
}
|
|
567
593
|
}
|
|
568
594
|
|
|
595
|
+
var FrameworkType = /* @__PURE__ */ ((FrameworkType2) => {
|
|
596
|
+
FrameworkType2[FrameworkType2["Express"] = 0] = "Express";
|
|
597
|
+
FrameworkType2[FrameworkType2["Fastify"] = 1] = "Fastify";
|
|
598
|
+
FrameworkType2[FrameworkType2["AzureFunction"] = 2] = "AzureFunction";
|
|
599
|
+
FrameworkType2[FrameworkType2["Firebase"] = 3] = "Firebase";
|
|
600
|
+
FrameworkType2[FrameworkType2["Lambda"] = 4] = "Lambda";
|
|
601
|
+
return FrameworkType2;
|
|
602
|
+
})(FrameworkType || {});
|
|
603
|
+
|
|
604
|
+
function registerType(type) {
|
|
605
|
+
commonContainer.registerType(type);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
class AbstractRunner {
|
|
609
|
+
stop() {
|
|
610
|
+
if (this.childProcess)
|
|
611
|
+
return this.childProcess.kill();
|
|
612
|
+
}
|
|
613
|
+
async exec(command, args, cwd = process.cwd()) {
|
|
614
|
+
const options = {
|
|
615
|
+
cwd,
|
|
616
|
+
stdio: "pipe",
|
|
617
|
+
shell: true
|
|
618
|
+
};
|
|
619
|
+
return new Promise((resolve, reject) => {
|
|
620
|
+
const child = child_process.spawn(
|
|
621
|
+
`${command}`,
|
|
622
|
+
[...args],
|
|
623
|
+
options
|
|
624
|
+
);
|
|
625
|
+
this.childProcess = child;
|
|
626
|
+
child.stdout.on(
|
|
627
|
+
"data",
|
|
628
|
+
(data) => {
|
|
629
|
+
const writeData = data.toString().replace(/\r\n|\n/, "");
|
|
630
|
+
if (writeData.indexOf("[NATTYJS]") > -1 || writeData.indexOf("[NATTYJS:LOGGER]") > -1) {
|
|
631
|
+
console.log(writeData);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
);
|
|
635
|
+
child.stderr.on("data", (data) => {
|
|
636
|
+
const writeData = data.toString().replace(/\r\n|\n/, "");
|
|
637
|
+
console.log(writeData);
|
|
638
|
+
});
|
|
639
|
+
child.on("close", (code) => {
|
|
640
|
+
if (code === 0) {
|
|
641
|
+
resolve(null);
|
|
642
|
+
} else {
|
|
643
|
+
console.error(
|
|
644
|
+
`${command} ${args}`
|
|
645
|
+
);
|
|
646
|
+
reject();
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
async function getPort() {
|
|
654
|
+
const portNumber = commonContainer.nattyConfig?.port || 3200;
|
|
655
|
+
if (portNumber)
|
|
656
|
+
return portNumber;
|
|
657
|
+
const port = await getPortPlease.getPort({ ports: [portNumber, ...Array(50).fill(3001).map((fillValue, index) => fillValue + index)] });
|
|
658
|
+
commonContainer.nattyConfig.port = port;
|
|
659
|
+
return port;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const reset = "\x1B[0m";
|
|
663
|
+
const NATTY_LOGGER = `[NATTYJS:LOGGER] `;
|
|
664
|
+
const log = {
|
|
665
|
+
green: (text) => console.log("\x1B[32m" + NATTY_LOGGER + text + reset),
|
|
666
|
+
red: (text) => console.log("\x1B[31m" + NATTY_LOGGER + text + reset),
|
|
667
|
+
blue: (text) => console.log("\x1B[34m" + NATTY_LOGGER + text + reset),
|
|
668
|
+
yellow: (text) => console.log("\x1B[33m" + NATTY_LOGGER + text + reset)
|
|
669
|
+
};
|
|
670
|
+
class AbstractConsoleLogger {
|
|
671
|
+
log(message) {
|
|
672
|
+
log.yellow(message);
|
|
673
|
+
}
|
|
674
|
+
info(message) {
|
|
675
|
+
log.blue(message);
|
|
676
|
+
}
|
|
677
|
+
warn(message) {
|
|
678
|
+
log.yellow(message);
|
|
679
|
+
}
|
|
680
|
+
error(message) {
|
|
681
|
+
log.red(message);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
class ConsoleLogger extends AbstractConsoleLogger {
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function typed() {
|
|
689
|
+
return function(OriginalClass) {
|
|
690
|
+
return class extends OriginalClass {
|
|
691
|
+
constructor(...args) {
|
|
692
|
+
super(...args);
|
|
693
|
+
const typeInfo = commonContainer.types[OriginalClass.name];
|
|
694
|
+
defineProps(this, typeInfo?.props, OriginalClass.name);
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
const types = ["string", "number", "boolean"];
|
|
700
|
+
function defineProps(instance, props, modelName) {
|
|
701
|
+
if (props && Array.isArray(props)) {
|
|
702
|
+
for (const prop of props) {
|
|
703
|
+
if (prop.type && types.indexOf(prop.type) > -1) {
|
|
704
|
+
overrideProp(instance, prop, modelName);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function overrideProp(instance, prop, modelName) {
|
|
710
|
+
let descriptor = Object.getOwnPropertyDescriptor(
|
|
711
|
+
Object.getPrototypeOf(instance),
|
|
712
|
+
prop.name
|
|
713
|
+
);
|
|
714
|
+
let value = instance[prop.name];
|
|
715
|
+
Object.defineProperty(instance, prop.name, {
|
|
716
|
+
get: () => {
|
|
717
|
+
return descriptor ? descriptor.get.call(instance) : value;
|
|
718
|
+
},
|
|
719
|
+
set: (v) => {
|
|
720
|
+
const type = typeof v;
|
|
721
|
+
if (prop.type == type)
|
|
722
|
+
value = v;
|
|
723
|
+
else
|
|
724
|
+
throw new Error(`Incorrect value type of ${modelName} class property '${prop.name}' `);
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
|
|
569
729
|
exports.ALLOW_METHODS = ALLOW_METHODS;
|
|
730
|
+
exports.AbstractRunner = AbstractRunner;
|
|
570
731
|
exports.ActionFilter = ActionFilter;
|
|
571
732
|
exports.AuthenticationFilter = AuthenticationFilter;
|
|
572
733
|
exports.AuthorizationFilter = AuthorizationFilter;
|
|
573
734
|
exports.BACK_SLASH_REGEX = BACK_SLASH_REGEX;
|
|
574
735
|
exports.BLANK = BLANK;
|
|
575
736
|
exports.CONTROLLER = CONTROLLER;
|
|
737
|
+
exports.ConsoleLogger = ConsoleLogger;
|
|
576
738
|
exports.DEFAULT_ACTIONS = DEFAULT_ACTIONS;
|
|
577
739
|
exports.DEFAULT_CHILD_PATH = DEFAULT_CHILD_PATH;
|
|
578
740
|
exports.DELETE = DELETE;
|
|
579
741
|
exports.ENVIRONMENTS = ENVIRONMENTS;
|
|
580
742
|
exports.ExceptionFilter = ExceptionFilter;
|
|
743
|
+
exports.FrameworkType = FrameworkType;
|
|
581
744
|
exports.GET = GET;
|
|
582
745
|
exports.HTTP_METHOD_ROUTES = HTTP_METHOD_ROUTES;
|
|
583
746
|
exports.IGNORE_METHODS = IGNORE_METHODS;
|
|
@@ -596,10 +759,13 @@ exports.commonContainer = commonContainer;
|
|
|
596
759
|
exports.createPath = createPath;
|
|
597
760
|
exports.createTestServer = createTestServer;
|
|
598
761
|
exports.getPath = getPath;
|
|
762
|
+
exports.getPort = getPort;
|
|
599
763
|
exports.isConstructor = isConstructor;
|
|
600
764
|
exports.isEqual = isEqual;
|
|
601
765
|
exports.isFunction = isFunction;
|
|
602
766
|
exports.isObject = isObject;
|
|
603
767
|
exports.readEnv = readEnv;
|
|
604
768
|
exports.readEnvKey = readEnvKey;
|
|
769
|
+
exports.registerType = registerType;
|
|
605
770
|
exports.typeContainer = typeContainer;
|
|
771
|
+
exports.typed = typed;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { RequestRouteInfo, IHttpResult, IModelBindingContext, ProblemDetail, IExceptionContext, HttpResponseInit, NattyTestModule, ModelBinding, BuildOptions, ClassTypeInfo } from '@nattyjs/types';
|
|
1
|
+
import { RequestRouteInfo, IHttpResult, Cookie, IModelBindingContext, ProblemDetail, IExceptionContext, HttpResponseInit, NattyTestModule, ModelBinding, BuildOptions, TypesInfo, ClassTypeInfo } from '@nattyjs/types';
|
|
2
|
+
import { ChildProcess } from 'child_process';
|
|
2
3
|
|
|
3
4
|
interface ClassType<T> extends Function {
|
|
4
5
|
new (...args: any[]): T;
|
|
@@ -7,8 +8,8 @@ interface ClassType<T> extends Function {
|
|
|
7
8
|
declare class UserIdentity<T> {
|
|
8
9
|
isAuthenticate: boolean;
|
|
9
10
|
id?: any;
|
|
10
|
-
|
|
11
|
-
constructor(isAuthenticate: boolean, id?: any,
|
|
11
|
+
claims?: T;
|
|
12
|
+
constructor(isAuthenticate: boolean, id?: any, claims?: T);
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
interface IExecutionContext {
|
|
@@ -21,6 +22,11 @@ interface IExecutionContext {
|
|
|
21
22
|
|
|
22
23
|
interface IActionExecutedContext extends IExecutionContext {
|
|
23
24
|
content: any;
|
|
25
|
+
get response(): {
|
|
26
|
+
cookies: Array<Cookie>;
|
|
27
|
+
headers: Headers;
|
|
28
|
+
status: number;
|
|
29
|
+
};
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
interface IActionExecutingContext extends IExecutionContext {
|
|
@@ -28,6 +34,7 @@ interface IActionExecutingContext extends IExecutionContext {
|
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
declare abstract class ActionFilter {
|
|
37
|
+
onModelBinding(context: IActionExecutingContext): Promise<void>;
|
|
31
38
|
onActionExecuting(context: IActionExecutingContext): Promise<void>;
|
|
32
39
|
onActionExecuted(context: IActionExecutedContext): Promise<void>;
|
|
33
40
|
}
|
|
@@ -37,8 +44,12 @@ declare abstract class AuthenticationFilter {
|
|
|
37
44
|
onFailedResponse(): ProblemDetail;
|
|
38
45
|
}
|
|
39
46
|
|
|
47
|
+
interface IAuthorizationContext extends IActionExecutingContext {
|
|
48
|
+
config: any;
|
|
49
|
+
}
|
|
50
|
+
|
|
40
51
|
declare abstract class AuthorizationFilter {
|
|
41
|
-
abstract onAuthorization(httpContext:
|
|
52
|
+
abstract onAuthorization(httpContext: IAuthorizationContext): Promise<boolean>;
|
|
42
53
|
onFailedAuthorization(): ProblemDetail;
|
|
43
54
|
}
|
|
44
55
|
|
|
@@ -53,6 +64,22 @@ interface GlobalConfig {
|
|
|
53
64
|
onException?: ClassType<ExceptionFilter>;
|
|
54
65
|
}
|
|
55
66
|
|
|
67
|
+
interface CorsConfig {
|
|
68
|
+
origin: string[];
|
|
69
|
+
methods: string;
|
|
70
|
+
preflightContinue: boolean;
|
|
71
|
+
optionsSuccessStatus: number;
|
|
72
|
+
credentials: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface PayloadConfig {
|
|
76
|
+
limit?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface SecureConfig {
|
|
80
|
+
sensitiveProps?: String[];
|
|
81
|
+
}
|
|
82
|
+
|
|
56
83
|
interface NattyConfig {
|
|
57
84
|
app: any;
|
|
58
85
|
testModule?: NattyTestModule;
|
|
@@ -62,12 +89,47 @@ interface NattyConfig {
|
|
|
62
89
|
modelBinding?: ModelBinding;
|
|
63
90
|
global?: GlobalConfig;
|
|
64
91
|
autoGeneratePort?: boolean;
|
|
92
|
+
port?: number;
|
|
93
|
+
cors?: CorsConfig;
|
|
94
|
+
payload?: PayloadConfig;
|
|
95
|
+
secure?: SecureConfig;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
declare abstract class AbstractRunner {
|
|
99
|
+
abstract run(): void;
|
|
100
|
+
childProcess: ChildProcess;
|
|
101
|
+
stop(): boolean;
|
|
102
|
+
protected exec(command: string, args: any[], cwd?: string): Promise<null | string>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface EnvConfig {
|
|
106
|
+
path: string;
|
|
107
|
+
dictionary: {
|
|
108
|
+
[key: string]: any;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
interface PackageJsonConfig {
|
|
113
|
+
addExports: string[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
interface LibraryConfig {
|
|
117
|
+
packageJson: PackageJsonConfig;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface NattyCliConfig {
|
|
121
|
+
library?: Partial<LibraryConfig>;
|
|
122
|
+
env?: Partial<EnvConfig>;
|
|
123
|
+
runner?: ClassType<AbstractRunner>;
|
|
124
|
+
port?: number;
|
|
65
125
|
}
|
|
66
126
|
|
|
67
127
|
declare const commonContainer: {
|
|
68
128
|
setupConfig(config?: NattyConfig): void;
|
|
129
|
+
setupCliConfig(config: NattyCliConfig): void;
|
|
69
130
|
setupBuildOptions(options: BuildOptions): any;
|
|
70
131
|
get nattyConfig(): NattyConfig;
|
|
132
|
+
get nattyCliConfig(): NattyCliConfig;
|
|
71
133
|
get buildOptions(): BuildOptions;
|
|
72
134
|
get envTsDefinition(): {
|
|
73
135
|
[key: string]: string;
|
|
@@ -84,6 +146,8 @@ declare const commonContainer: {
|
|
|
84
146
|
setMetadata(key: string, value: any, propName: string): void;
|
|
85
147
|
getMetadataValue(key: string, propName: string): any;
|
|
86
148
|
get globalConfig(): GlobalConfig;
|
|
149
|
+
registerType(type: TypesInfo): void;
|
|
150
|
+
types: TypesInfo;
|
|
87
151
|
};
|
|
88
152
|
|
|
89
153
|
declare const typeContainer: {
|
|
@@ -207,4 +271,34 @@ declare class List<T> {
|
|
|
207
271
|
declare function isObject(value: any): boolean;
|
|
208
272
|
declare function isEqual(first: any, second: any): boolean;
|
|
209
273
|
|
|
210
|
-
|
|
274
|
+
declare enum FrameworkType {
|
|
275
|
+
Express = 0,
|
|
276
|
+
Fastify = 1,
|
|
277
|
+
AzureFunction = 2,
|
|
278
|
+
Firebase = 3,
|
|
279
|
+
Lambda = 4
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
interface NattyAppConfig extends NattyConfig {
|
|
283
|
+
framework: FrameworkType;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
declare function registerType(type: TypesInfo): void;
|
|
287
|
+
|
|
288
|
+
declare function getPort(): Promise<number>;
|
|
289
|
+
|
|
290
|
+
declare abstract class AbstractConsoleLogger {
|
|
291
|
+
log(message: any): void;
|
|
292
|
+
info(message: any): void;
|
|
293
|
+
warn(message: any): void;
|
|
294
|
+
error(message: any): void;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
declare class ConsoleLogger extends AbstractConsoleLogger {
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
declare function typed(): <T extends {
|
|
301
|
+
new (...args: any[]): {};
|
|
302
|
+
}>(OriginalClass: T) => T;
|
|
303
|
+
|
|
304
|
+
export { ALLOW_METHODS, AbstractRunner, ActionFilter, AuthenticationFilter, AuthorizationFilter, BACK_SLASH_REGEX, BLANK, CONTROLLER, Claim, ClassType, ConsoleLogger, DEFAULT_ACTIONS, DEFAULT_CHILD_PATH, DELETE, ENVIRONMENTS, ExceptionFilter, FrameworkType, GET, GlobalConfig, HTTP_METHOD_ROUTES, IActionExecutedContext, IActionExecutingContext, IAuthorizationContext, IExecutionContext, IGNORE_METHODS, List, MetaConfigProps, Middleware, NattyAppConfig, NattyCliConfig, NattyConfig, POST, PUT, RIGHT_SLASH, ROUTE_INSTANCES, ROUTE_METHODS, ROUTE_PATHS, TS_EXTENSION, UserIdentity, commonContainer, createPath, createTestServer, getPath, getPort, isConstructor, isEqual, isFunction, isObject, readEnv, readEnvKey, registerType, typeContainer, typed };
|
package/dist/index.mjs
CHANGED
|
@@ -3,10 +3,24 @@ import { existsSync } from 'fs';
|
|
|
3
3
|
import * as dotenv from 'dotenv';
|
|
4
4
|
import * as dotenvExpand from 'dotenv-expand';
|
|
5
5
|
import * as path from 'path';
|
|
6
|
+
import { resolve } from 'path';
|
|
7
|
+
import { spawn } from 'child_process';
|
|
8
|
+
import { getPort as getPort$1 } from 'get-port-please';
|
|
6
9
|
|
|
7
10
|
const commonContainer = new class {
|
|
8
11
|
constructor() {
|
|
9
12
|
this.metadataConfig = { services: /* @__PURE__ */ new Map() };
|
|
13
|
+
this.types = {};
|
|
14
|
+
}
|
|
15
|
+
registerType(type) {
|
|
16
|
+
if (type) {
|
|
17
|
+
if (typeof type === "string")
|
|
18
|
+
type = JSON.parse(type);
|
|
19
|
+
this.types[type.name] = type;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
setupCliConfig(config) {
|
|
23
|
+
this.nattyCliConfig = config;
|
|
10
24
|
}
|
|
11
25
|
setupConfig(config) {
|
|
12
26
|
const modelBinding = {
|
|
@@ -17,7 +31,8 @@ const commonContainer = new class {
|
|
|
17
31
|
}
|
|
18
32
|
}
|
|
19
33
|
};
|
|
20
|
-
|
|
34
|
+
const secureConfig = { sensitiveProps: ["password", "mobileNo", "email"] };
|
|
35
|
+
this.nattyConfig = { ...{ api: { rootPath: "api" }, autoGeneratePort: true, modelBinding, globalConfig: {}, secure: secureConfig }, ...config };
|
|
21
36
|
}
|
|
22
37
|
setupBuildOptions(options) {
|
|
23
38
|
this.buildOptions = options;
|
|
@@ -207,20 +222,29 @@ function getPath(pathCollection, isIncludeRoot = true, isCreateFolder = false) {
|
|
|
207
222
|
return currentPath;
|
|
208
223
|
}
|
|
209
224
|
|
|
225
|
+
function resolvePath(path) {
|
|
226
|
+
return resolve(commonContainer.buildOptions.rootDir, path);
|
|
227
|
+
}
|
|
228
|
+
|
|
210
229
|
async function readEnv() {
|
|
211
|
-
|
|
230
|
+
const envConfig = commonContainer.nattyCliConfig.env;
|
|
231
|
+
let filePath = envConfig?.dictionary ? void 0 : getPath([ENVIRONMENTS, commonContainer.buildOptions.mode && commonContainer.buildOptions.mode !== "dev" ? `.env.${commonContainer.buildOptions.mode}` : `.env`]);
|
|
232
|
+
if (envConfig && envConfig.path)
|
|
233
|
+
filePath = resolvePath(envConfig.path);
|
|
212
234
|
let parsedEnvTsDefinition = {};
|
|
213
|
-
|
|
235
|
+
let parsedEnv = commonContainer.nattyCliConfig.env?.dictionary;
|
|
236
|
+
if (!parsedEnv && filePath && existsSync(filePath)) {
|
|
214
237
|
const { parsed, error } = dotenv.config({
|
|
215
238
|
debug: !!process.env.DEBUG || void 0,
|
|
216
239
|
path: filePath
|
|
217
240
|
});
|
|
218
|
-
|
|
219
|
-
parsedEnvTsDefinition = info.parsedEnvTsDefinition;
|
|
220
|
-
commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
|
|
221
|
-
commonContainer.setEnvValueInfo(info.envVariableValueInfo);
|
|
222
|
-
dotenvExpand.expand({ parsed });
|
|
241
|
+
parsedEnv = parsed;
|
|
223
242
|
}
|
|
243
|
+
const info = getEnvTsDefinition(parsedEnv);
|
|
244
|
+
parsedEnvTsDefinition = info.parsedEnvTsDefinition;
|
|
245
|
+
commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
|
|
246
|
+
commonContainer.setEnvValueInfo(info.envVariableValueInfo);
|
|
247
|
+
dotenvExpand.expand({ parsed: parsedEnv });
|
|
224
248
|
return parsedEnvTsDefinition;
|
|
225
249
|
}
|
|
226
250
|
|
|
@@ -243,6 +267,9 @@ class AuthorizationFilter {
|
|
|
243
267
|
}
|
|
244
268
|
|
|
245
269
|
class ActionFilter {
|
|
270
|
+
onModelBinding(context) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
246
273
|
onActionExecuting(context) {
|
|
247
274
|
return;
|
|
248
275
|
}
|
|
@@ -268,10 +295,10 @@ function readEnvKey(key) {
|
|
|
268
295
|
}
|
|
269
296
|
|
|
270
297
|
class UserIdentity {
|
|
271
|
-
constructor(isAuthenticate, id,
|
|
298
|
+
constructor(isAuthenticate, id, claims) {
|
|
272
299
|
this.isAuthenticate = isAuthenticate;
|
|
273
300
|
this.id = id;
|
|
274
|
-
this.
|
|
301
|
+
this.claims = claims;
|
|
275
302
|
}
|
|
276
303
|
}
|
|
277
304
|
|
|
@@ -363,7 +390,7 @@ class List {
|
|
|
363
390
|
if (this.count()) {
|
|
364
391
|
return predicate ? this.where(predicate).first() : this._entities[0];
|
|
365
392
|
} else {
|
|
366
|
-
|
|
393
|
+
return void 0;
|
|
367
394
|
}
|
|
368
395
|
}
|
|
369
396
|
firstOrDefault(predicate) {
|
|
@@ -548,4 +575,138 @@ class List {
|
|
|
548
575
|
}
|
|
549
576
|
}
|
|
550
577
|
|
|
551
|
-
|
|
578
|
+
var FrameworkType = /* @__PURE__ */ ((FrameworkType2) => {
|
|
579
|
+
FrameworkType2[FrameworkType2["Express"] = 0] = "Express";
|
|
580
|
+
FrameworkType2[FrameworkType2["Fastify"] = 1] = "Fastify";
|
|
581
|
+
FrameworkType2[FrameworkType2["AzureFunction"] = 2] = "AzureFunction";
|
|
582
|
+
FrameworkType2[FrameworkType2["Firebase"] = 3] = "Firebase";
|
|
583
|
+
FrameworkType2[FrameworkType2["Lambda"] = 4] = "Lambda";
|
|
584
|
+
return FrameworkType2;
|
|
585
|
+
})(FrameworkType || {});
|
|
586
|
+
|
|
587
|
+
function registerType(type) {
|
|
588
|
+
commonContainer.registerType(type);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
class AbstractRunner {
|
|
592
|
+
stop() {
|
|
593
|
+
if (this.childProcess)
|
|
594
|
+
return this.childProcess.kill();
|
|
595
|
+
}
|
|
596
|
+
async exec(command, args, cwd = process.cwd()) {
|
|
597
|
+
const options = {
|
|
598
|
+
cwd,
|
|
599
|
+
stdio: "pipe",
|
|
600
|
+
shell: true
|
|
601
|
+
};
|
|
602
|
+
return new Promise((resolve, reject) => {
|
|
603
|
+
const child = spawn(
|
|
604
|
+
`${command}`,
|
|
605
|
+
[...args],
|
|
606
|
+
options
|
|
607
|
+
);
|
|
608
|
+
this.childProcess = child;
|
|
609
|
+
child.stdout.on(
|
|
610
|
+
"data",
|
|
611
|
+
(data) => {
|
|
612
|
+
const writeData = data.toString().replace(/\r\n|\n/, "");
|
|
613
|
+
if (writeData.indexOf("[NATTYJS]") > -1 || writeData.indexOf("[NATTYJS:LOGGER]") > -1) {
|
|
614
|
+
console.log(writeData);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
);
|
|
618
|
+
child.stderr.on("data", (data) => {
|
|
619
|
+
const writeData = data.toString().replace(/\r\n|\n/, "");
|
|
620
|
+
console.log(writeData);
|
|
621
|
+
});
|
|
622
|
+
child.on("close", (code) => {
|
|
623
|
+
if (code === 0) {
|
|
624
|
+
resolve(null);
|
|
625
|
+
} else {
|
|
626
|
+
console.error(
|
|
627
|
+
`${command} ${args}`
|
|
628
|
+
);
|
|
629
|
+
reject();
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
async function getPort() {
|
|
637
|
+
const portNumber = commonContainer.nattyConfig?.port || 3200;
|
|
638
|
+
if (portNumber)
|
|
639
|
+
return portNumber;
|
|
640
|
+
const port = await getPort$1({ ports: [portNumber, ...Array(50).fill(3001).map((fillValue, index) => fillValue + index)] });
|
|
641
|
+
commonContainer.nattyConfig.port = port;
|
|
642
|
+
return port;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
const reset = "\x1B[0m";
|
|
646
|
+
const NATTY_LOGGER = `[NATTYJS:LOGGER] `;
|
|
647
|
+
const log = {
|
|
648
|
+
green: (text) => console.log("\x1B[32m" + NATTY_LOGGER + text + reset),
|
|
649
|
+
red: (text) => console.log("\x1B[31m" + NATTY_LOGGER + text + reset),
|
|
650
|
+
blue: (text) => console.log("\x1B[34m" + NATTY_LOGGER + text + reset),
|
|
651
|
+
yellow: (text) => console.log("\x1B[33m" + NATTY_LOGGER + text + reset)
|
|
652
|
+
};
|
|
653
|
+
class AbstractConsoleLogger {
|
|
654
|
+
log(message) {
|
|
655
|
+
log.yellow(message);
|
|
656
|
+
}
|
|
657
|
+
info(message) {
|
|
658
|
+
log.blue(message);
|
|
659
|
+
}
|
|
660
|
+
warn(message) {
|
|
661
|
+
log.yellow(message);
|
|
662
|
+
}
|
|
663
|
+
error(message) {
|
|
664
|
+
log.red(message);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
class ConsoleLogger extends AbstractConsoleLogger {
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function typed() {
|
|
672
|
+
return function(OriginalClass) {
|
|
673
|
+
return class extends OriginalClass {
|
|
674
|
+
constructor(...args) {
|
|
675
|
+
super(...args);
|
|
676
|
+
const typeInfo = commonContainer.types[OriginalClass.name];
|
|
677
|
+
defineProps(this, typeInfo?.props, OriginalClass.name);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
const types = ["string", "number", "boolean"];
|
|
683
|
+
function defineProps(instance, props, modelName) {
|
|
684
|
+
if (props && Array.isArray(props)) {
|
|
685
|
+
for (const prop of props) {
|
|
686
|
+
if (prop.type && types.indexOf(prop.type) > -1) {
|
|
687
|
+
overrideProp(instance, prop, modelName);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
function overrideProp(instance, prop, modelName) {
|
|
693
|
+
let descriptor = Object.getOwnPropertyDescriptor(
|
|
694
|
+
Object.getPrototypeOf(instance),
|
|
695
|
+
prop.name
|
|
696
|
+
);
|
|
697
|
+
let value = instance[prop.name];
|
|
698
|
+
Object.defineProperty(instance, prop.name, {
|
|
699
|
+
get: () => {
|
|
700
|
+
return descriptor ? descriptor.get.call(instance) : value;
|
|
701
|
+
},
|
|
702
|
+
set: (v) => {
|
|
703
|
+
const type = typeof v;
|
|
704
|
+
if (prop.type == type)
|
|
705
|
+
value = v;
|
|
706
|
+
else
|
|
707
|
+
throw new Error(`Incorrect value type of ${modelName} class property '${prop.name}' `);
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
export { ALLOW_METHODS, AbstractRunner, ActionFilter, AuthenticationFilter, AuthorizationFilter, BACK_SLASH_REGEX, BLANK, CONTROLLER, ConsoleLogger, DEFAULT_ACTIONS, DEFAULT_CHILD_PATH, DELETE, ENVIRONMENTS, ExceptionFilter, FrameworkType, GET, HTTP_METHOD_ROUTES, IGNORE_METHODS, List, MetaConfigProps, Middleware, POST, PUT, RIGHT_SLASH, ROUTE_INSTANCES, ROUTE_METHODS, ROUTE_PATHS, TS_EXTENSION, UserIdentity, commonContainer, createPath, createTestServer, getPath, getPort, isConstructor, isEqual, isFunction, isObject, readEnv, readEnvKey, registerType, typeContainer, typed };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nattyjs/common",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.40",
|
|
4
4
|
"description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "ajayojha <ojhaajay@outlook.com>",
|
|
@@ -14,10 +14,14 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "unbuild"
|
|
16
16
|
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"dotenv": "16.3.1",
|
|
19
|
+
"dotenv-expand": "10.0.0",
|
|
20
|
+
"get-port-please": "3.1.1"
|
|
21
|
+
},
|
|
17
22
|
"devDependencies": {
|
|
18
23
|
"@types/node": "20.3.1",
|
|
19
|
-
"@nattyjs/types": "0.0.1-beta.
|
|
20
|
-
"unbuild": "1.2.1"
|
|
21
|
-
"dotenv-expand": "10.0.0"
|
|
24
|
+
"@nattyjs/types": "0.0.1-beta.40",
|
|
25
|
+
"unbuild": "1.2.1"
|
|
22
26
|
}
|
|
23
27
|
}
|