mythix 2.4.14 → 2.5.2
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/package.json +9 -7
- package/src/application.d.ts +88 -0
- package/src/application.js +5 -4
- package/src/cli/cli-utils.d.ts +63 -0
- package/src/cli/cli-utils.js +6 -3
- package/src/cli/index.d.ts +1 -0
- package/src/cli/routes-command.js +1 -1
- package/src/controllers/controller-base.d.ts +59 -0
- package/src/controllers/controller-base.js +7 -6
- package/src/controllers/controller-module.d.ts +11 -0
- package/src/controllers/controller-module.js +1 -1
- package/src/controllers/controller-utils.d.ts +51 -0
- package/src/controllers/controller-utils.js +3 -16
- package/src/controllers/generate-client-api-interface.d.ts +13 -0
- package/src/controllers/generate-client-api-interface.js +1 -1
- package/src/controllers/index.d.ts +4 -0
- package/src/http-server/http-errors.d.ts +17 -0
- package/src/http-server/http-server-module.d.ts +9 -0
- package/src/http-server/http-server-module.js +1 -3
- package/src/http-server/http-server.d.ts +33 -0
- package/src/http-server/http-server.js +7 -1
- package/src/http-server/index.d.ts +3 -0
- package/src/http-server/index.js +1 -6
- package/src/index.d.ts +49 -0
- package/src/index.js +9 -8
- package/src/interfaces/common.ts +3 -0
- package/src/logger.d.ts +53 -0
- package/src/logger.js +1 -3
- package/src/models/index.d.ts +4 -0
- package/src/models/index.js +4 -1
- package/src/models/migration-model.d.ts +8 -0
- package/src/models/migration-model.js +29 -12
- package/src/models/model-module.d.ts +9 -0
- package/src/models/model-module.js +2 -4
- package/src/models/model-utils.d.ts +20 -0
- package/src/models/model-utils.js +32 -27
- package/src/models/model.d.ts +17 -0
- package/src/models/model.js +5 -34
- package/src/modules/base-module.d.ts +27 -0
- package/src/modules/base-module.js +5 -3
- package/src/modules/database-module.d.ts +14 -0
- package/src/modules/database-module.js +1 -3
- package/src/modules/file-watcher-module.d.ts +13 -0
- package/src/modules/file-watcher-module.js +1 -3
- package/src/modules/index.d.ts +3 -0
- package/src/modules/index.js +5 -5
- package/src/tasks/index.d.ts +3 -0
- package/src/tasks/task-base.d.ts +43 -0
- package/src/tasks/task-base.js +41 -46
- package/src/tasks/task-module.d.ts +17 -0
- package/src/tasks/task-module.js +2 -4
- package/src/tasks/task-utils.d.ts +46 -0
- package/src/tasks/task-utils.js +13 -13
- package/src/utils/config-utils.d.ts +3 -0
- package/src/utils/crypto-utils.d.ts +6 -0
- package/src/utils/file-utils.d.ts +10 -0
- package/src/utils/http-interface.d.ts +25 -0
- package/src/utils/http-utils.d.ts +3 -0
- package/src/utils/index.d.ts +7 -0
- package/src/utils/index.js +2 -2
- package/src/utils/mime-utils.d.ts +5 -0
- package/src/utils/test-utils.d.ts +3 -0
- package/src/utils/test-utils.js +9 -11
- package/src/http-server/middleware/default-middleware.js +0 -120
- package/src/http-server/middleware/index.js +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mythix",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.2",
|
|
4
4
|
"description": "Mythix is a NodeJS web-app framework",
|
|
5
5
|
"main": "src/index",
|
|
6
6
|
"scripts": {
|
|
@@ -20,19 +20,21 @@
|
|
|
20
20
|
"homepage": "https://github.com/th317erd/mythix#readme",
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@spothero/eslint-plugin-spothero": "github:spothero/eslint-plugin-spothero",
|
|
23
|
+
"@types/node": "^18.7.17",
|
|
23
24
|
"colors": "^1.4.0",
|
|
24
25
|
"diff": "^5.1.0",
|
|
25
|
-
"eslint": "^8.
|
|
26
|
-
"jasmine": "^4.0
|
|
26
|
+
"eslint": "^8.23.1",
|
|
27
|
+
"jasmine": "^4.4.0"
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
30
|
+
"@types/events": "^3.0.0",
|
|
29
31
|
"chokidar": "^3.5.3",
|
|
30
|
-
"cmded": "^1.2.
|
|
31
|
-
"express": "^4.
|
|
32
|
+
"cmded": "^1.2.3",
|
|
33
|
+
"express": "^4.18.1",
|
|
32
34
|
"express-busboy": "github:th317erd/express-busboy#0754a570d7979097b31e48655b80d3fcd628d4e4",
|
|
33
35
|
"form-data": "^4.0.0",
|
|
34
|
-
"mythix-orm": "^1.
|
|
35
|
-
"nife": "^1.
|
|
36
|
+
"mythix-orm": "^1.6.2",
|
|
37
|
+
"nife": "^1.12.1",
|
|
36
38
|
"prompts": "^2.4.2"
|
|
37
39
|
}
|
|
38
40
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ConnectionBase, ModelClass, Models } from 'mythix-orm';
|
|
2
|
+
import { ControllerClass } from './controllers/controller-base';
|
|
3
|
+
import { HTTPServer } from './http-server/http-server';
|
|
4
|
+
import { GenericObject } from './interfaces/common';
|
|
5
|
+
import { Logger, LoggerClass, LoggerOptions } from './logger';
|
|
6
|
+
import { Modules, ModuleClasses, BaseModuleClass } from './modules/base-module';
|
|
7
|
+
|
|
8
|
+
export declare type ApplicationClass = typeof Application;
|
|
9
|
+
|
|
10
|
+
export declare interface ApplicationOptions {
|
|
11
|
+
environment: string;
|
|
12
|
+
appName: string;
|
|
13
|
+
rootPath: string;
|
|
14
|
+
configPath: string;
|
|
15
|
+
migrationsPath: string;
|
|
16
|
+
modelsPath: string;
|
|
17
|
+
seedersPath: string;
|
|
18
|
+
controllersPath: string;
|
|
19
|
+
templatesPath: string;
|
|
20
|
+
commandsPath: string;
|
|
21
|
+
tasksPath: string;
|
|
22
|
+
modules: ModuleClasses;
|
|
23
|
+
autoReload: boolean;
|
|
24
|
+
exitOnShutdown: number | null;
|
|
25
|
+
runTasks: boolean;
|
|
26
|
+
testMode: boolean;
|
|
27
|
+
noInternalMigrationTable: boolean;
|
|
28
|
+
logger: LoggerOptions;
|
|
29
|
+
database: boolean | GenericObject;
|
|
30
|
+
httpServer: boolean | GenericObject;
|
|
31
|
+
tempPath: string;
|
|
32
|
+
routeParserTypes: { [ key: string ]: (value: string, param: GenericObject, index?: number) => any };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export declare class Application {
|
|
36
|
+
declare public static APP_NAME: string;
|
|
37
|
+
|
|
38
|
+
public static getDefaultModules(): ModuleClasses;
|
|
39
|
+
public static findModuleIndex(modules: ModuleClasses, moduleKlass: BaseModuleClass): number;
|
|
40
|
+
public static replaceModule(modules: ModuleClasses, moduleKlass: BaseModuleClass, replacementModuleKlass: BaseModuleClass): ModuleClasses;
|
|
41
|
+
|
|
42
|
+
public constructor(options?: ApplicationOptions);
|
|
43
|
+
public getTempPath(): string | null;
|
|
44
|
+
public getModules(): Modules;
|
|
45
|
+
public initializeModules(modules: ModuleClasses): Promise<void>;
|
|
46
|
+
public startAllModules(options: ApplicationOptions): Promise<void>;
|
|
47
|
+
public stopAllModules(options: ApplicationOptions): Promise<Array<Error>>;
|
|
48
|
+
public bindToProcessSignals(): void;
|
|
49
|
+
public getOptions(): ApplicationOptions;
|
|
50
|
+
public setOptions(options: ApplicationOptions): Application;
|
|
51
|
+
public loadConfig(configPath: string): GenericObject;
|
|
52
|
+
public getConfigValue(key: string, defaultValue?: any, type?: string): any;
|
|
53
|
+
public getConfig(): GenericObject;
|
|
54
|
+
public setConfig(options: GenericObject): Application;
|
|
55
|
+
public getApplicationName(): string;
|
|
56
|
+
public getRoutes(): GenericObject;
|
|
57
|
+
public getCustomRouteParserTypes(): { [ key: string ]: (value: string, param: GenericObject, index?: number) => any };
|
|
58
|
+
public createLogger(loggerOptions: LoggerOptions, LoggerClass: LoggerClass): Logger;
|
|
59
|
+
public getLogger(): Logger;
|
|
60
|
+
public start(): Promise<void>;
|
|
61
|
+
public stop(exitCode?: number): Promise<void>;
|
|
62
|
+
|
|
63
|
+
// From DatabaseModule
|
|
64
|
+
public getDBConfig(): GenericObject;
|
|
65
|
+
public getDBTablePrefix(): string | null;
|
|
66
|
+
public getDBConnection(): ConnectionBase;
|
|
67
|
+
|
|
68
|
+
// From FileWatcherModule
|
|
69
|
+
public autoReload(enable?: boolean, shuttingDown?: boolean): Promise<void>;
|
|
70
|
+
|
|
71
|
+
// From ModelModule
|
|
72
|
+
public getModel(modelName?: string): ModelClass | undefined;
|
|
73
|
+
public getModels(): Models;
|
|
74
|
+
|
|
75
|
+
// From ControllerModule
|
|
76
|
+
public getController(name: string): { controller: ControllerClass, controllerMethod: string | undefined };
|
|
77
|
+
|
|
78
|
+
// From HTTPServerModule
|
|
79
|
+
public getHTTPServer(): HTTPServer | null;
|
|
80
|
+
public getHTTPServerConfig(): GenericObject;
|
|
81
|
+
|
|
82
|
+
declare public isStarted: boolean;
|
|
83
|
+
declare public isStopping: boolean;
|
|
84
|
+
declare public options: ApplicationOptions;
|
|
85
|
+
declare public moduleInstances: Modules;
|
|
86
|
+
declare public config: GenericObject;
|
|
87
|
+
declare public logger: Logger;
|
|
88
|
+
}
|
package/src/application.js
CHANGED
|
@@ -232,7 +232,10 @@ class Application extends EventEmitter {
|
|
|
232
232
|
try {
|
|
233
233
|
let appOptions = this.getOptions();
|
|
234
234
|
|
|
235
|
-
|
|
235
|
+
let config = require(configPath);
|
|
236
|
+
if (config.__esModule)
|
|
237
|
+
config = config['default'];
|
|
238
|
+
|
|
236
239
|
return wrapConfig(Object.assign({}, config || {}, { environment: (appOptions.environment || config.environment || 'development')}));
|
|
237
240
|
} catch (error) {
|
|
238
241
|
this.getLogger().error(`Error while trying to load application configuration ${configPath}: `, error);
|
|
@@ -351,6 +354,4 @@ class Application extends EventEmitter {
|
|
|
351
354
|
}
|
|
352
355
|
}
|
|
353
356
|
|
|
354
|
-
module.exports = {
|
|
355
|
-
Application,
|
|
356
|
-
};
|
|
357
|
+
module.exports = { Application };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { HelpInterface, Runner } from 'cmded';
|
|
2
|
+
import { ConnectionBase } from 'mythix-orm';
|
|
3
|
+
import { Application, ApplicationClass, ApplicationOptions } from '../application';
|
|
4
|
+
import { GenericObject } from '../interfaces/common';
|
|
5
|
+
import { Logger } from '../logger';
|
|
6
|
+
|
|
7
|
+
export declare type CommandClass = typeof CommandBase;
|
|
8
|
+
|
|
9
|
+
export declare type CommandClasses = { [ key: string ]: CommandClass };
|
|
10
|
+
|
|
11
|
+
export declare class CommandBase {
|
|
12
|
+
declare public static commands: GenericObject;
|
|
13
|
+
declare public static commandName: string;
|
|
14
|
+
declare public static applicationConfig?: GenericObject | (() => GenericObject);
|
|
15
|
+
declare public static commandArguments?: () => { help: HelpInterface, runner: Runner };
|
|
16
|
+
declare public static runtimeArguments?: { [ key: string ]: Array<string> };
|
|
17
|
+
public static execute(): Promise<void>;
|
|
18
|
+
|
|
19
|
+
declare public application: Application;
|
|
20
|
+
declare public options: GenericObject;
|
|
21
|
+
|
|
22
|
+
constructor(application: Application, options?: GenericObject);
|
|
23
|
+
getOptions(): GenericObject;
|
|
24
|
+
getApplication(): Application;
|
|
25
|
+
getLogger(): Logger;
|
|
26
|
+
getDBConnection(): ConnectionBase;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export declare interface DefineCommandContext {
|
|
30
|
+
Parent: CommandClass;
|
|
31
|
+
commandName: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export declare function loadMythixConfig(
|
|
35
|
+
mythixConfigPath: string,
|
|
36
|
+
appRootPath: string
|
|
37
|
+
): GenericObject;
|
|
38
|
+
|
|
39
|
+
export declare function loadCommand(name: string): CommandClass;
|
|
40
|
+
|
|
41
|
+
export declare function loadCommands(
|
|
42
|
+
applicationCommandsPath: string,
|
|
43
|
+
skip: Array<string>
|
|
44
|
+
): CommandClasses;
|
|
45
|
+
|
|
46
|
+
export declare function defineCommand(
|
|
47
|
+
commandName: string,
|
|
48
|
+
definer: (context: DefineCommandContext) => CommandClass,
|
|
49
|
+
parent?: string | CommandClass,
|
|
50
|
+
): CommandClass;
|
|
51
|
+
|
|
52
|
+
export declare function createApplication(
|
|
53
|
+
applicationClass: ApplicationClass,
|
|
54
|
+
options?: ApplicationOptions
|
|
55
|
+
): Application;
|
|
56
|
+
|
|
57
|
+
export declare function executeCommand(
|
|
58
|
+
config: GenericObject,
|
|
59
|
+
appOptions: ApplicationOptions,
|
|
60
|
+
commandContext: GenericObject,
|
|
61
|
+
CommandKlass: CommandClass,
|
|
62
|
+
argv: Array<string>
|
|
63
|
+
): void;
|
package/src/cli/cli-utils.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
const Path = require('path');
|
|
6
6
|
const FileSystem = require('fs');
|
|
7
7
|
const Nife = require('nife');
|
|
8
|
-
const
|
|
8
|
+
const Logger = require('../logger');
|
|
9
9
|
|
|
10
10
|
const {
|
|
11
11
|
walkDir,
|
|
@@ -204,8 +204,8 @@ function defineCommand(_commandName, definer, _parent) {
|
|
|
204
204
|
return Klass;
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
async function createApplication(
|
|
208
|
-
let application = new
|
|
207
|
+
async function createApplication(ApplicationClass, opts) {
|
|
208
|
+
let application = new ApplicationClass(Object.assign({ cli: true }, opts || {}));
|
|
209
209
|
|
|
210
210
|
if (Nife.isNotEmpty(opts))
|
|
211
211
|
application.setOptions(Object.assign(opts || {}));
|
|
@@ -217,6 +217,9 @@ function loadCommand(name) {
|
|
|
217
217
|
let fullPath = require.resolve(name);
|
|
218
218
|
let CommandKlass = require(fullPath);
|
|
219
219
|
|
|
220
|
+
if (CommandKlass && typeof CommandKlass !== 'function' && typeof CommandKlass.default === 'function')
|
|
221
|
+
CommandKlass = CommandKlass.default;
|
|
222
|
+
|
|
220
223
|
CommandKlass.path = fullPath;
|
|
221
224
|
|
|
222
225
|
return CommandKlass;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cli-utils';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Application } from '../application';
|
|
2
|
+
import { Logger } from '../logger';
|
|
3
|
+
import { Request, Response } from 'express';
|
|
4
|
+
import { GenericObject } from '../interfaces/common';
|
|
5
|
+
import { ConnectionBase, ModelClass, Models } from 'mythix-orm';
|
|
6
|
+
import { HTTPBaseError } from '../http-server/http-errors';
|
|
7
|
+
|
|
8
|
+
export declare type ControllerClass = typeof ControllerBase;
|
|
9
|
+
|
|
10
|
+
export declare type ControllerClasses = { [ key: string ]: ControllerClass };
|
|
11
|
+
|
|
12
|
+
export declare interface ControllerContext {
|
|
13
|
+
params: GenericObject;
|
|
14
|
+
query: GenericObject;
|
|
15
|
+
route: GenericObject;
|
|
16
|
+
controller: ControllerClass;
|
|
17
|
+
controllerMethod: string;
|
|
18
|
+
controllerInstance: ControllerBase;
|
|
19
|
+
startTime: number;
|
|
20
|
+
body?: any;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export declare class ControllerBase {
|
|
24
|
+
public static getControllerName(): string;
|
|
25
|
+
|
|
26
|
+
declare public application: Application;
|
|
27
|
+
declare public logger: Logger;
|
|
28
|
+
declare public request: Request;
|
|
29
|
+
declare public response: Response;
|
|
30
|
+
declare public route: GenericObject;
|
|
31
|
+
declare public method: string;
|
|
32
|
+
declare public contentType: string | null;
|
|
33
|
+
declare public responseStatusCode: number;
|
|
34
|
+
|
|
35
|
+
public constructor(application: Application, logger: Logger, request: Request, response: Response);
|
|
36
|
+
public getApplication(): Application;
|
|
37
|
+
public getLogger(): Logger;
|
|
38
|
+
public getModel(name?: string): ModelClass | undefined;
|
|
39
|
+
public getModels(): Models;
|
|
40
|
+
public getDBConnection(): ConnectionBase;
|
|
41
|
+
public prepareToThrowError(ErrorClass: Function, args: Array<any>): HTTPBaseError;
|
|
42
|
+
public throwNotFoundError(...args: Array<any>): void;
|
|
43
|
+
public throwBadRequestError(...args: Array<any>): void;
|
|
44
|
+
public throwUnauthorizedError(...args: Array<any>): void;
|
|
45
|
+
public throwForbiddenError(...args: Array<any>): void;
|
|
46
|
+
public throwInternalServerError(...args: Array<any>): void;
|
|
47
|
+
public isHTTPError(error: any): boolean;
|
|
48
|
+
public redirectTo(url: string, status?: number);
|
|
49
|
+
public getCookie(name: string);
|
|
50
|
+
public setCookie(name: string, value: any, options?: GenericObject);
|
|
51
|
+
public getHeader(name: string): string;
|
|
52
|
+
public getHeaders(names: Array<string>): GenericObject;
|
|
53
|
+
public setHeader(name: string, value: any): void;
|
|
54
|
+
public setHeaders(headers: GenericObject): void;
|
|
55
|
+
public setContentType(contentType: string): void;
|
|
56
|
+
public setStatusCode(code: number): void;
|
|
57
|
+
public handleIncomingRequest(request: Request, response: Response, context: ControllerContext): Promise<any>;
|
|
58
|
+
public handleOutgoingResponse(controllerResult: any, request: Request, response: Response, context: ControllerContext): Promise<void>;
|
|
59
|
+
}
|
|
@@ -183,16 +183,17 @@ class ControllerBase {
|
|
|
183
183
|
this.responseStatusCode = parseInt(code, 10);
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
async handleIncomingRequest(request, response,
|
|
187
|
-
this.route =
|
|
186
|
+
async handleIncomingRequest(request, response, context) {
|
|
187
|
+
this.route = context.route;
|
|
188
188
|
|
|
189
|
-
if (typeof this[
|
|
190
|
-
this.throwInternalServerError(`Specified route handler named "${this.constructor.name}::${
|
|
189
|
+
if (typeof this[context.controllerMethod] !== 'function')
|
|
190
|
+
this.throwInternalServerError(`Specified route handler named "${this.constructor.name}::${context.controllerMethod}" not found.`);
|
|
191
191
|
|
|
192
|
-
return await this[
|
|
192
|
+
return await this[context.controllerMethod].call(this, { body: request.body, request, response, ...context }, this.getModels());
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
// eslint-disable-next-line no-unused-vars
|
|
196
|
+
async handleOutgoingResponse(_controllerResult, request, response, context) {
|
|
196
197
|
// Has a response already been sent?
|
|
197
198
|
if (response.statusMessage)
|
|
198
199
|
return;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { HTTPServer } from '../http-server';
|
|
2
|
+
import { GenericObject } from '../interfaces/common';
|
|
3
|
+
import { BaseModule } from '../modules/base-module';
|
|
4
|
+
import { ControllerClass, ControllerClasses } from './controller-base';
|
|
5
|
+
|
|
6
|
+
export class ControllerModule extends BaseModule {
|
|
7
|
+
public getControllerFilePaths(controllersPath: string): Array<string>;
|
|
8
|
+
public loadControllers(controllersPath: string): ControllerClasses;
|
|
9
|
+
public getController(name: string): { controller: ControllerClass, controllerMethod: string | undefined };
|
|
10
|
+
public buildRoutes(httpServer: HTTPServer, routes: Array<GenericObject>): Array<GenericObject>;
|
|
11
|
+
}
|
|
@@ -80,7 +80,7 @@ class ControllerModule extends BaseModule {
|
|
|
80
80
|
|
|
81
81
|
try {
|
|
82
82
|
let controllerGenerator = require(controllerFile);
|
|
83
|
-
if (controllerGenerator
|
|
83
|
+
if (controllerGenerator.__esModule)
|
|
84
84
|
controllerGenerator = controllerGenerator['default'];
|
|
85
85
|
|
|
86
86
|
Object.assign(controllers, controllerGenerator(args));
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Application } from '../application';
|
|
2
|
+
import { HTTPServer } from '../http-server';
|
|
3
|
+
import { GenericObject } from '../interfaces/common';
|
|
4
|
+
import { ControllerClass } from './controller-base';
|
|
5
|
+
|
|
6
|
+
export declare interface BuildPatternMatcherOptions {
|
|
7
|
+
strict?: boolean;
|
|
8
|
+
sanitize?: (value: string) => string;
|
|
9
|
+
flags?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export declare interface PatternMatcherMethod {
|
|
13
|
+
(value: string): boolean;
|
|
14
|
+
regexp: RegExp;
|
|
15
|
+
directPatterns: Array<RegExp>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export declare interface PathMatcherMethod {
|
|
19
|
+
(pathPart: string): GenericObject | undefined;
|
|
20
|
+
regexp: RegExp;
|
|
21
|
+
params: Array<string>;
|
|
22
|
+
sanitizedPath: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export declare type RoutePatterns = string | RegExp | Array<string | RegExp>;
|
|
26
|
+
|
|
27
|
+
export declare function buildPatternMatcher(
|
|
28
|
+
patterns: RoutePatterns,
|
|
29
|
+
options?: BuildPatternMatcherOptions
|
|
30
|
+
): PatternMatcherMethod;
|
|
31
|
+
|
|
32
|
+
export declare interface DefineControllerContext<T = ControllerClass> {
|
|
33
|
+
Parent: T;
|
|
34
|
+
application: Application;
|
|
35
|
+
server: HTTPServer;
|
|
36
|
+
controllerName: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export declare function buildMethodMatcher(patterns: RoutePatterns): PatternMatcherMethod;
|
|
40
|
+
export declare function buildContentTypeMatcher(patterns: RoutePatterns): PatternMatcherMethod;
|
|
41
|
+
export declare function buildPathMatcher(routeName: string, customParserTypes: GenericObject): PathMatcherMethod;
|
|
42
|
+
export declare function buildRoutes(routes: GenericObject | Array<GenericObject>, customParserTypes?: GenericObject): Array<GenericObject>;
|
|
43
|
+
|
|
44
|
+
export declare function defineController<T = ControllerClass>(
|
|
45
|
+
controllerName: string,
|
|
46
|
+
definer: (context: DefineControllerContext<T>) => ControllerClass,
|
|
47
|
+
parent?: T
|
|
48
|
+
): (context: {
|
|
49
|
+
application: Application,
|
|
50
|
+
server: HTTPServer,
|
|
51
|
+
}) => ControllerClass;
|
|
@@ -14,6 +14,8 @@ function defineController(controllerName, definer, _parent) {
|
|
|
14
14
|
controllerName,
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
+
Klass.getControllerName = () => controllerName;
|
|
18
|
+
|
|
17
19
|
return { [controllerName]: Klass };
|
|
18
20
|
};
|
|
19
21
|
}
|
|
@@ -62,7 +64,7 @@ function buildPatternMatcher(_patterns, _opts) {
|
|
|
62
64
|
writable: false,
|
|
63
65
|
enumerable: false,
|
|
64
66
|
configurable: false,
|
|
65
|
-
value:
|
|
67
|
+
value: [],
|
|
66
68
|
},
|
|
67
69
|
});
|
|
68
70
|
|
|
@@ -273,21 +275,6 @@ function buildPathMatcher(routeName, customParserTypes) {
|
|
|
273
275
|
return matchFunc;
|
|
274
276
|
}
|
|
275
277
|
|
|
276
|
-
function getRouteProperties(route) {
|
|
277
|
-
let props = {};
|
|
278
|
-
|
|
279
|
-
for (let i = 0, il = ROUTE_PROPERTIES.length; i < il; i++) {
|
|
280
|
-
let propName = ROUTE_PROPERTIES[i];
|
|
281
|
-
let value = route[propName];
|
|
282
|
-
if (value === undefined)
|
|
283
|
-
continue;
|
|
284
|
-
|
|
285
|
-
props[propName] = value;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return props;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
278
|
function compileRoutes(routes, customParserTypes, _context) {
|
|
292
279
|
const addRoute = (theseRoutes, route, path, priority) => {
|
|
293
280
|
let newRoute = Object.assign(
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Application } from '../application';
|
|
2
|
+
import { GenericObject } from '../interfaces/common';
|
|
3
|
+
|
|
4
|
+
export declare interface GenerateClientInterfaceOptions {
|
|
5
|
+
globalName?: string;
|
|
6
|
+
environment?: string;
|
|
7
|
+
defaultRouteOptions?: GenericObject;
|
|
8
|
+
mode?: string;
|
|
9
|
+
domain?: string;
|
|
10
|
+
routeFilter?: ((value: any, index?: number) => boolean) | RegExp | string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export declare function generateClientAPIInterface(application: Application, options?: GenerateClientInterfaceOptions): string;
|
|
@@ -374,7 +374,7 @@ function generateRoutes(_routes, _options) {
|
|
|
374
374
|
if (clientOptions == null) {
|
|
375
375
|
var contentType = route.accept;
|
|
376
376
|
if (Nife.isEmpty(contentType))
|
|
377
|
-
contentType = 'application/json
|
|
377
|
+
contentType = 'application/json';
|
|
378
378
|
else if (Array.isArray(contentType))
|
|
379
379
|
contentType = contentType[0];
|
|
380
380
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { GenericObject } from '../interfaces/common';
|
|
2
|
+
|
|
3
|
+
export declare class HTTPBaseError extends Error {
|
|
4
|
+
declare public route: GenericObject;
|
|
5
|
+
declare public statusCode: number;
|
|
6
|
+
declare public headers: GenericObject;
|
|
7
|
+
|
|
8
|
+
constructor(route?: GenericObject | null | undefined, message?: string, statusCode?: number);
|
|
9
|
+
getMessage(): string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export declare class HTTPNotFoundError extends HTTPBaseError { }
|
|
13
|
+
export declare class HTTPBadRequestError extends HTTPBaseError { }
|
|
14
|
+
export declare class HTTPBadContentTypeError extends HTTPBaseError { }
|
|
15
|
+
export declare class HTTPUnauthorizedError extends HTTPBaseError { }
|
|
16
|
+
export declare class HTTPForbiddenError extends HTTPBaseError { }
|
|
17
|
+
export declare class HTTPInternalServerError extends HTTPBaseError { }
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseModule } from '../modules/base-module';
|
|
2
|
+
import { GenericObject } from '../interfaces/common';
|
|
3
|
+
import { HTTPServer } from './http-server';
|
|
4
|
+
|
|
5
|
+
export declare class HTTPServerModule extends BaseModule {
|
|
6
|
+
public getHTTPServer(): HTTPServer | null;
|
|
7
|
+
public getHTTPServerConfig(): GenericObject;
|
|
8
|
+
public createHTTPServer(httpServerConfig: GenericObject): HTTPServer;
|
|
9
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { GenericObject } from '../interfaces/common';
|
|
2
|
+
import { Application } from '../application';
|
|
3
|
+
import { Application as ExpressApplication, Request, Response } from 'express';
|
|
4
|
+
import { Logger } from '../logger';
|
|
5
|
+
import { ControllerClass, ControllerContext } from '../controllers/controller-base';
|
|
6
|
+
|
|
7
|
+
export declare class HTTPServer {
|
|
8
|
+
declare public application: Application;
|
|
9
|
+
declare public server: ExpressApplication | null;
|
|
10
|
+
declare public options: GenericObject;
|
|
11
|
+
declare public routes: Array<GenericObject>;
|
|
12
|
+
declare public middleware: Array<Function> | null;
|
|
13
|
+
|
|
14
|
+
public constructor(application: Application, options?: GenericObject);
|
|
15
|
+
public getApplication(): Application;
|
|
16
|
+
public getLogger(): Logger;
|
|
17
|
+
public getOptions(): GenericObject;
|
|
18
|
+
public getHTTPSCredentials(options): { key: string; cert: string; }
|
|
19
|
+
public setRoutes(routes): void;
|
|
20
|
+
public executeMiddleware(middleware: Array<Function>, request: Request, response: Response): Promise<void>;
|
|
21
|
+
public baseMiddleware(request: Request, response: Response, rootNext: Function): void;
|
|
22
|
+
public findFirstMatchingRoute(request: Request, routes: Array<GenericObject>): GenericObject;
|
|
23
|
+
public getRouteController(controller: string | Function, route, params, request): { controller: ControllerClass | undefined } | undefined;
|
|
24
|
+
public createRequestLogger(application: Application, request: Request): Logger;
|
|
25
|
+
public validateQueryParam(route: GenericObject, query: GenericObject, paramName: string, queryValue: any, queryParams: GenericObject): boolean;
|
|
26
|
+
public compileQueryParams(route: GenericObject, query: GenericObject, queryParams: GenericObject): GenericObject;
|
|
27
|
+
public sendRequestToController(request: Request, response: Response, context: ControllerContext): Promise<any>;
|
|
28
|
+
public baseRouter(request: Request, response: Response, next: Function): void;
|
|
29
|
+
public errorHandler(error: Error | any, message: string | null | undefined, statusCode: number | null | undefined, response: Response, request: Request): void;
|
|
30
|
+
public createExpressApplication(options?: GenericObject): ExpressApplication;
|
|
31
|
+
public start(): Promise<ExpressApplication>;
|
|
32
|
+
public stop(): Promise<void>;
|
|
33
|
+
}
|
|
@@ -434,7 +434,13 @@ class HTTPServer {
|
|
|
434
434
|
app.use((request, response, next) => {
|
|
435
435
|
if ((/application\/json/i).test(request.headers['content-type']) && Buffer.isBuffer(request.body)) {
|
|
436
436
|
let bodyStr = request.body.toString('utf8');
|
|
437
|
-
request._rawBody =
|
|
437
|
+
request._rawBody = bodyStr;
|
|
438
|
+
|
|
439
|
+
try {
|
|
440
|
+
request.body = JSON.parse(bodyStr);
|
|
441
|
+
} catch (error) {
|
|
442
|
+
request.body = bodyStr;
|
|
443
|
+
}
|
|
438
444
|
}
|
|
439
445
|
|
|
440
446
|
next();
|
package/src/http-server/index.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
HTTPServer,
|
|
5
|
-
} = require('./http-server');
|
|
6
|
-
|
|
7
3
|
const {
|
|
8
4
|
statusCodeToMessage,
|
|
9
5
|
} = require('../utils/http-utils');
|
|
10
6
|
|
|
7
|
+
const { HTTPServer } = require('./http-server');
|
|
11
8
|
const HTTPErrors = require('./http-errors');
|
|
12
|
-
const Middleware = require('./middleware');
|
|
13
9
|
const { HTTPServerModule } = require('./http-server-module');
|
|
14
10
|
|
|
15
11
|
module.exports = {
|
|
16
12
|
HTTPErrors,
|
|
17
13
|
HTTPServer,
|
|
18
14
|
HTTPServerModule,
|
|
19
|
-
Middleware,
|
|
20
15
|
statusCodeToMessage,
|
|
21
16
|
};
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Application
|
|
2
|
+
export * from './interfaces/common';
|
|
3
|
+
export * from './application';
|
|
4
|
+
export * from './logger';
|
|
5
|
+
|
|
6
|
+
// CLI Utils
|
|
7
|
+
export * as CLI from './cli';
|
|
8
|
+
export { defineCommand } from './cli';
|
|
9
|
+
|
|
10
|
+
// Utils
|
|
11
|
+
export * as Utils from './utils';
|
|
12
|
+
export {
|
|
13
|
+
HTTPUtils,
|
|
14
|
+
CryptoUtils,
|
|
15
|
+
TestUtils,
|
|
16
|
+
MimeUtils,
|
|
17
|
+
} from './utils';
|
|
18
|
+
|
|
19
|
+
// Models
|
|
20
|
+
export * as Models from './models';
|
|
21
|
+
export * from './models';
|
|
22
|
+
|
|
23
|
+
// Controllers
|
|
24
|
+
export * as Controllers from './controllers';
|
|
25
|
+
export * from './controllers';
|
|
26
|
+
|
|
27
|
+
// HTTPServer
|
|
28
|
+
export * as HTTP from './http-server';
|
|
29
|
+
export * from './http-server';
|
|
30
|
+
|
|
31
|
+
// Tasks
|
|
32
|
+
export * as Tasks from './tasks';
|
|
33
|
+
export * from './tasks';
|
|
34
|
+
|
|
35
|
+
import * as _Modules from './modules';
|
|
36
|
+
import * as _ModelModule from './models/model-module';
|
|
37
|
+
import * as _ControllerModule from './controllers/controller-module';
|
|
38
|
+
import * as _HTTPServerModule from './http-server/http-server-module';
|
|
39
|
+
import * as _TaskModule from './tasks/task-module';
|
|
40
|
+
|
|
41
|
+
export declare namespace Modules {
|
|
42
|
+
export import ModelModule = _ModelModule.ModelModule;
|
|
43
|
+
export import ControllerModule = _ControllerModule.ControllerModule;
|
|
44
|
+
export import BaseModule = _Modules.BaseModule;
|
|
45
|
+
export import DatabaseModule = _Modules.DatabaseModule;
|
|
46
|
+
export import FileWatcherModule = _Modules.FileWatcherModule;
|
|
47
|
+
export import HTTPServerModule = _HTTPServerModule.HTTPServerModule;
|
|
48
|
+
export import TaskModule = _TaskModule.TaskModule;
|
|
49
|
+
}
|