axe-api 0.31.4 → 1.0.0-rc10
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/dev-kit.d.ts +1 -0
- package/build/dev-kit.js +16 -0
- package/build/index.d.ts +3 -2
- package/build/index.js +6 -1
- package/build/src/Builders/ModelTreeBuilder.js +1 -2
- package/build/src/Builders/RouterBuilder.d.ts +1 -5
- package/build/src/Builders/RouterBuilder.js +17 -112
- package/build/src/Enums.d.ts +0 -7
- package/build/src/Enums.js +14 -22
- package/build/src/Handlers/DocsHTMLHandler.d.ts +3 -2
- package/build/src/Handlers/Helpers.d.ts +7 -7
- package/build/src/Handlers/MetadataHandler.d.ts +3 -2
- package/build/src/Handlers/RequestHandler.d.ts +3 -0
- package/build/src/Handlers/RequestHandler.js +99 -0
- package/build/src/Handlers/RoutesHandler.d.ts +3 -2
- package/build/src/Helpers.d.ts +1 -3
- package/build/src/Helpers.js +4 -68
- package/build/src/Interfaces.d.ts +74 -35
- package/build/src/Middlewares/RateLimit/AdaptorFactory.d.ts +6 -0
- package/build/src/Middlewares/RateLimit/AdaptorFactory.js +17 -0
- package/build/src/Middlewares/RateLimit/IAdaptor.d.ts +6 -0
- package/build/src/Middlewares/RateLimit/IAdaptor.js +2 -0
- package/build/src/Middlewares/RateLimit/MemoryAdaptor.d.ts +10 -0
- package/build/src/Middlewares/RateLimit/MemoryAdaptor.js +41 -0
- package/build/src/Middlewares/RateLimit/RedisAdaptor.d.ts +11 -0
- package/build/src/Middlewares/RateLimit/RedisAdaptor.js +35 -0
- package/build/src/Middlewares/RateLimit/index.d.ts +6 -0
- package/build/src/Middlewares/RateLimit/index.js +118 -0
- package/build/src/Model.d.ts +4 -4
- package/build/src/Phases/All/FetchPhase.d.ts +3 -0
- package/build/src/{Middlewares/acceptLanguageMiddleware.js → Phases/All/FetchPhase.js} +4 -10
- package/build/src/Phases/All/PreparePhase.d.ts +3 -0
- package/build/src/Phases/All/PreparePhase.js +30 -0
- package/build/src/Phases/All/index.d.ts +4 -0
- package/build/src/{Middlewares → Phases/All}/index.js +4 -3
- package/build/src/Phases/Delete/ActionPhase.d.ts +3 -0
- package/build/src/Phases/Delete/ActionPhase.js +24 -0
- package/build/src/Phases/Delete/PreparePhase.d.ts +3 -0
- package/build/src/Phases/Delete/PreparePhase.js +25 -0
- package/build/src/Phases/Delete/QueryPhase.d.ts +3 -0
- package/build/src/Phases/Delete/QueryPhase.js +22 -0
- package/build/src/Phases/Delete/ResponsePhase.d.ts +3 -0
- package/build/src/Phases/Delete/ResponsePhase.js +14 -0
- package/build/src/Phases/Delete/index.d.ts +7 -0
- package/build/src/Phases/Delete/index.js +15 -0
- package/build/src/Phases/ForceDelete/ActionPhase.d.ts +3 -0
- package/build/src/Phases/ForceDelete/ActionPhase.js +16 -0
- package/build/src/Phases/ForceDelete/PreparePhase.d.ts +3 -0
- package/build/src/Phases/ForceDelete/PreparePhase.js +30 -0
- package/build/src/Phases/ForceDelete/QueryPhase.d.ts +3 -0
- package/build/src/Phases/ForceDelete/QueryPhase.js +22 -0
- package/build/src/Phases/ForceDelete/index.d.ts +6 -0
- package/build/src/Phases/ForceDelete/index.js +13 -0
- package/build/src/Phases/List/RelationalPhase.d.ts +3 -0
- package/build/src/Phases/List/RelationalPhase.js +17 -0
- package/build/src/Phases/List/ResultPhase.d.ts +3 -0
- package/build/src/Phases/List/ResultPhase.js +14 -0
- package/build/src/Phases/List/SerializePhase.d.ts +3 -0
- package/build/src/Phases/List/SerializePhase.js +19 -0
- package/build/src/Phases/List/index.d.ts +6 -0
- package/build/src/Phases/List/index.js +13 -0
- package/build/src/Phases/Paginate/FetchPhase.d.ts +3 -0
- package/build/src/Phases/Paginate/FetchPhase.js +30 -0
- package/build/src/Phases/Paginate/PreparePhase.d.ts +3 -0
- package/build/src/Phases/Paginate/PreparePhase.js +22 -0
- package/build/src/Phases/Paginate/index.d.ts +5 -0
- package/build/src/Phases/Paginate/index.js +11 -0
- package/build/src/Phases/Patch/PrepareActionPhase.d.ts +3 -0
- package/build/src/Phases/Patch/PrepareActionPhase.js +37 -0
- package/build/src/Phases/Patch/index.d.ts +4 -0
- package/build/src/Phases/Patch/index.js +9 -0
- package/build/src/Phases/Show/FetchPhase.d.ts +3 -0
- package/build/src/Phases/Show/FetchPhase.js +35 -0
- package/build/src/Phases/Show/PreparePhase.d.ts +3 -0
- package/build/src/Phases/Show/PreparePhase.js +22 -0
- package/build/src/Phases/Show/index.d.ts +5 -0
- package/build/src/Phases/Show/index.js +11 -0
- package/build/src/Phases/Single/GetPhase.d.ts +3 -0
- package/build/src/Phases/Single/GetPhase.js +29 -0
- package/build/src/Phases/Single/PrepareGetPhase.d.ts +3 -0
- package/build/src/Phases/Single/PrepareGetPhase.js +19 -0
- package/build/src/Phases/Single/RelationalPhase.d.ts +3 -0
- package/build/src/Phases/Single/RelationalPhase.js +17 -0
- package/build/src/Phases/Single/ResultPhase.d.ts +3 -0
- package/build/src/Phases/Single/ResultPhase.js +14 -0
- package/build/src/Phases/Single/SerializePhase.d.ts +3 -0
- package/build/src/Phases/Single/SerializePhase.js +18 -0
- package/build/src/Phases/Single/index.d.ts +8 -0
- package/build/src/Phases/Single/index.js +17 -0
- package/build/src/Phases/Store/ActionPhase.d.ts +3 -0
- package/build/src/Phases/Store/ActionPhase.js +29 -0
- package/build/src/Phases/Store/PreparePhase.d.ts +3 -0
- package/build/src/Phases/Store/PreparePhase.js +46 -0
- package/build/src/Phases/Store/index.d.ts +5 -0
- package/build/src/Phases/Store/index.js +11 -0
- package/build/src/Phases/Update/ActionPhase.d.ts +3 -0
- package/build/src/Phases/Update/ActionPhase.js +22 -0
- package/build/src/Phases/Update/PrepareActionPhase.d.ts +3 -0
- package/build/src/Phases/Update/PrepareActionPhase.js +37 -0
- package/build/src/Phases/Update/index.d.ts +5 -0
- package/build/src/Phases/Update/index.js +11 -0
- package/build/src/Resolvers/ModelResolver.js +9 -4
- package/build/src/Resolvers/VersionConfigResolver.js +6 -1
- package/build/src/Resolvers/VersionResolver.js +1 -2
- package/build/src/Server.d.ts +0 -1
- package/build/src/Server.js +29 -24
- package/build/src/Services/APIService.d.ts +3 -3
- package/build/src/Services/APIService.js +3 -8
- package/build/src/Services/App.d.ts +14 -0
- package/build/src/Services/App.js +80 -0
- package/build/src/Services/AxeRequest.d.ts +26 -0
- package/build/src/Services/AxeRequest.js +85 -0
- package/build/src/Services/AxeResponse.d.ts +18 -0
- package/build/src/Services/AxeResponse.js +41 -0
- package/build/src/Services/ConverterService.d.ts +9 -0
- package/build/src/Services/ConverterService.js +49 -0
- package/build/src/Services/LogService.d.ts +8 -12
- package/build/src/Services/LogService.js +16 -34
- package/build/src/Services/ModelService.d.ts +4 -4
- package/build/src/Services/QueryService.d.ts +4 -4
- package/build/src/Services/QueryService.js +14 -11
- package/build/src/Services/SchemaValidatorService.js +1 -2
- package/build/src/Services/URLService.d.ts +29 -0
- package/build/src/Services/URLService.js +144 -0
- package/build/src/Services/index.d.ts +4 -1
- package/build/src/Services/index.js +7 -1
- package/build/src/Types.d.ts +18 -6
- package/build/src/constants.d.ts +12 -14
- package/build/src/constants.js +200 -13
- package/package.json +29 -20
- package/build/src/Handlers/AllHandler.d.ts +0 -3
- package/build/src/Handlers/AllHandler.js +0 -45
- package/build/src/Handlers/DestroyHandler.d.ts +0 -3
- package/build/src/Handlers/DestroyHandler.js +0 -51
- package/build/src/Handlers/ForceDestroyHandler.d.ts +0 -3
- package/build/src/Handlers/ForceDestroyHandler.js +0 -45
- package/build/src/Handlers/HandlerFactory.d.ts +0 -6
- package/build/src/Handlers/HandlerFactory.js +0 -39
- package/build/src/Handlers/PaginateHandler.d.ts +0 -3
- package/build/src/Handlers/PaginateHandler.js +0 -49
- package/build/src/Handlers/PatchHandler.d.ts +0 -3
- package/build/src/Handlers/PatchHandler.js +0 -69
- package/build/src/Handlers/ShowHandler.d.ts +0 -3
- package/build/src/Handlers/ShowHandler.js +0 -55
- package/build/src/Handlers/StoreHandler.d.ts +0 -3
- package/build/src/Handlers/StoreHandler.js +0 -65
- package/build/src/Handlers/UpdateHandler.d.ts +0 -3
- package/build/src/Handlers/UpdateHandler.js +0 -69
- package/build/src/Middlewares/acceptLanguageMiddleware.d.ts +0 -3
- package/build/src/Middlewares/index.d.ts +0 -2
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const formidable_1 = __importDefault(require("formidable"));
|
|
16
|
+
const Helpers_1 = require("../Helpers");
|
|
17
|
+
const Resolvers_1 = require("../Resolvers");
|
|
18
|
+
const LogService_1 = __importDefault(require("./LogService"));
|
|
19
|
+
class AxeRequest {
|
|
20
|
+
constructor(request) {
|
|
21
|
+
this.privateParams = {};
|
|
22
|
+
this.request = request;
|
|
23
|
+
this.urlObject = new URL(request.url || "", "http://127.0.0.1");
|
|
24
|
+
// Application configuration is need for the default setting.
|
|
25
|
+
this.version = (0, Helpers_1.getVersionByRequest)(this.urlObject);
|
|
26
|
+
if (this.version) {
|
|
27
|
+
// Setting the current language by the supported, default and the client prefences
|
|
28
|
+
this.language = Resolvers_1.AcceptLanguageResolver.resolve(this.request.headers["accept-language"] || "", this.version.config.supportedLanguages || ["en"], this.version.config.defaultLanguage || "en");
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.language = {
|
|
32
|
+
title: "en",
|
|
33
|
+
language: "en",
|
|
34
|
+
region: null,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
get url() {
|
|
39
|
+
return this.urlObject;
|
|
40
|
+
}
|
|
41
|
+
get params() {
|
|
42
|
+
return this.privateParams;
|
|
43
|
+
}
|
|
44
|
+
set params(value) {
|
|
45
|
+
this.privateParams = value;
|
|
46
|
+
}
|
|
47
|
+
get query() {
|
|
48
|
+
return this.urlObject.searchParams;
|
|
49
|
+
}
|
|
50
|
+
get path() {
|
|
51
|
+
// TODO: Path
|
|
52
|
+
return this.request.url || "";
|
|
53
|
+
}
|
|
54
|
+
get method() {
|
|
55
|
+
return this.request.method || "GET";
|
|
56
|
+
}
|
|
57
|
+
get body() {
|
|
58
|
+
return this.request.body;
|
|
59
|
+
}
|
|
60
|
+
get currentLanguage() {
|
|
61
|
+
return this.language;
|
|
62
|
+
}
|
|
63
|
+
set currentLanguage(language) {
|
|
64
|
+
this.language = language;
|
|
65
|
+
}
|
|
66
|
+
header(key) {
|
|
67
|
+
return this.request.headers[key];
|
|
68
|
+
}
|
|
69
|
+
files(options) {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
if (!((_a = this.header("content-type")) === null || _a === void 0 ? void 0 : _a.includes("multipart/form-data"))) {
|
|
73
|
+
LogService_1.default.warn(`Content-type must be 'multipart/form-data'.`);
|
|
74
|
+
throw new Error(`Content-type must be 'multipart/form-data'.`);
|
|
75
|
+
}
|
|
76
|
+
LogService_1.default.debug(`Form data is parsing`);
|
|
77
|
+
const form = (0, formidable_1.default)(options || ((_b = this.version) === null || _b === void 0 ? void 0 : _b.config.formidable) || {});
|
|
78
|
+
return yield form.parse(this.request);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
get original() {
|
|
82
|
+
return this.request;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.default = AxeRequest;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="express" />
|
|
3
|
+
import { ServerResponse } from "http";
|
|
4
|
+
import { ILanguage } from "src/Interfaces";
|
|
5
|
+
declare class AxeResponse {
|
|
6
|
+
private response;
|
|
7
|
+
private responseStatus;
|
|
8
|
+
private language;
|
|
9
|
+
constructor(response: ServerResponse, language: ILanguage);
|
|
10
|
+
get original(): ServerResponse<import("http").IncomingMessage>;
|
|
11
|
+
status(statusCode: number): this;
|
|
12
|
+
json(data: object): void;
|
|
13
|
+
send(content: string): void;
|
|
14
|
+
noContent(): void;
|
|
15
|
+
isResponded(): boolean;
|
|
16
|
+
statusCode(): number;
|
|
17
|
+
}
|
|
18
|
+
export default AxeResponse;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class AxeResponse {
|
|
4
|
+
constructor(response, language) {
|
|
5
|
+
this.responseStatus = false;
|
|
6
|
+
this.response = response;
|
|
7
|
+
this.response.statusCode = 200;
|
|
8
|
+
this.language = language;
|
|
9
|
+
}
|
|
10
|
+
get original() {
|
|
11
|
+
return this.response;
|
|
12
|
+
}
|
|
13
|
+
status(statusCode) {
|
|
14
|
+
this.response.statusCode = statusCode;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
json(data) {
|
|
18
|
+
this.response.setHeader("Content-Type", "application/json");
|
|
19
|
+
this.response.setHeader("Content-Language", this.language.language);
|
|
20
|
+
this.response.write(JSON.stringify(data));
|
|
21
|
+
this.response.end();
|
|
22
|
+
this.responseStatus = true;
|
|
23
|
+
}
|
|
24
|
+
send(content) {
|
|
25
|
+
this.response.write(content);
|
|
26
|
+
this.response.end();
|
|
27
|
+
this.responseStatus = true;
|
|
28
|
+
}
|
|
29
|
+
noContent() {
|
|
30
|
+
this.response.statusCode = 204;
|
|
31
|
+
this.response.end();
|
|
32
|
+
this.responseStatus = true;
|
|
33
|
+
}
|
|
34
|
+
isResponded() {
|
|
35
|
+
return this.responseStatus;
|
|
36
|
+
}
|
|
37
|
+
statusCode() {
|
|
38
|
+
return this.response.statusCode;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.default = AxeResponse;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "http";
|
|
2
|
+
import { AxeRequestResponsePair, MiddlewareResolution } from "../Interfaces";
|
|
3
|
+
import { DynamicFunctionType, PhaseFunction, StepTypes } from "src/Types";
|
|
4
|
+
export declare const toAxeRequestResponsePair: (request: IncomingMessage, response: ServerResponse) => AxeRequestResponsePair;
|
|
5
|
+
export declare const resolveMiddlewares: (args: DynamicFunctionType) => MiddlewareResolution;
|
|
6
|
+
export declare const isMiddlewareFunction: (callback: any) => boolean;
|
|
7
|
+
export declare const isHandlerFunction: (callback: any) => boolean;
|
|
8
|
+
export declare const isPhaseFunction: (callback: any) => boolean;
|
|
9
|
+
export declare const toPhaseFunction: (callback: StepTypes) => PhaseFunction;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.toPhaseFunction = exports.isPhaseFunction = exports.isHandlerFunction = exports.isMiddlewareFunction = exports.resolveMiddlewares = exports.toAxeRequestResponsePair = void 0;
|
|
7
|
+
const util_1 = require("util");
|
|
8
|
+
const AxeRequest_1 = __importDefault(require("./AxeRequest"));
|
|
9
|
+
const AxeResponse_1 = __importDefault(require("./AxeResponse"));
|
|
10
|
+
const toAxeRequestResponsePair = (request, response) => {
|
|
11
|
+
const axeRequest = new AxeRequest_1.default(request);
|
|
12
|
+
const axeResponse = new AxeResponse_1.default(response, axeRequest.currentLanguage);
|
|
13
|
+
return {
|
|
14
|
+
axeRequest,
|
|
15
|
+
axeResponse,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
exports.toAxeRequestResponsePair = toAxeRequestResponsePair;
|
|
19
|
+
const resolveMiddlewares = (args) => {
|
|
20
|
+
const middlewares = args.slice(0, -1);
|
|
21
|
+
const handler = args.at(-1);
|
|
22
|
+
return {
|
|
23
|
+
middlewares,
|
|
24
|
+
handler,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
exports.resolveMiddlewares = resolveMiddlewares;
|
|
28
|
+
const isMiddlewareFunction = (callback) => {
|
|
29
|
+
return callback.length === 3;
|
|
30
|
+
};
|
|
31
|
+
exports.isMiddlewareFunction = isMiddlewareFunction;
|
|
32
|
+
const isHandlerFunction = (callback) => {
|
|
33
|
+
return callback.length === 2;
|
|
34
|
+
};
|
|
35
|
+
exports.isHandlerFunction = isHandlerFunction;
|
|
36
|
+
const isPhaseFunction = (callback) => {
|
|
37
|
+
return callback.length === 1;
|
|
38
|
+
};
|
|
39
|
+
exports.isPhaseFunction = isPhaseFunction;
|
|
40
|
+
const toPhaseFunction = (callback) => {
|
|
41
|
+
if ((0, exports.isMiddlewareFunction)(callback)) {
|
|
42
|
+
return (0, util_1.promisify)((context, next) => callback(context.req.original, context.res.original, next));
|
|
43
|
+
}
|
|
44
|
+
if ((0, exports.isHandlerFunction)(callback)) {
|
|
45
|
+
return (context) => callback(context.req, context.res);
|
|
46
|
+
}
|
|
47
|
+
return callback;
|
|
48
|
+
};
|
|
49
|
+
exports.toPhaseFunction = toPhaseFunction;
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import logger from "pino";
|
|
2
2
|
declare class LogService {
|
|
3
|
-
private static
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
static
|
|
7
|
-
static
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
info(message: string): void;
|
|
11
|
-
success(message: string): void;
|
|
12
|
-
debug(message: string): void;
|
|
13
|
-
log(message: string): void;
|
|
3
|
+
private static logger;
|
|
4
|
+
static setInstance(options?: logger.LoggerOptions | undefined): void;
|
|
5
|
+
static instance(): logger.Logger<logger.LoggerOptions>;
|
|
6
|
+
static error(message: string): void;
|
|
7
|
+
static warn(message: string): void;
|
|
8
|
+
static info(message: string): void;
|
|
9
|
+
static debug(message: string): void;
|
|
14
10
|
}
|
|
15
11
|
export default LogService;
|
|
@@ -1,45 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
const constants_1 = require("../constants");
|
|
5
|
-
const { fgRed, fgGreen, fgYellow, fgCyan, fgReset } = constants_1.LOG_COLORS;
|
|
6
|
+
const pino_1 = __importDefault(require("pino"));
|
|
6
7
|
class LogService {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
static setInstance(options) {
|
|
9
|
+
LogService.logger = (0, pino_1.default)(options);
|
|
9
10
|
}
|
|
10
|
-
static
|
|
11
|
-
LogService.
|
|
11
|
+
static instance() {
|
|
12
|
+
return LogService.logger;
|
|
12
13
|
}
|
|
13
|
-
static
|
|
14
|
-
|
|
14
|
+
static error(message) {
|
|
15
|
+
LogService.logger.error(message);
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
console.error(fgRed, "[axe]", message, fgReset);
|
|
19
|
-
}
|
|
17
|
+
static warn(message) {
|
|
18
|
+
LogService.logger.warn(message);
|
|
20
19
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
console.warn(fgYellow, "[axe]", message, fgReset);
|
|
24
|
-
}
|
|
20
|
+
static info(message) {
|
|
21
|
+
LogService.logger.info(message);
|
|
25
22
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
console.info(fgCyan, "[axe]", message, fgReset);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
success(message) {
|
|
32
|
-
if (this.level >= Enums_1.LogLevels.INFO) {
|
|
33
|
-
console.info(fgGreen, "[axe]", message, fgReset);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
debug(message) {
|
|
37
|
-
this.log(message);
|
|
38
|
-
}
|
|
39
|
-
log(message) {
|
|
40
|
-
if (this.level === Enums_1.LogLevels.ALL) {
|
|
41
|
-
console.log(message);
|
|
42
|
-
}
|
|
23
|
+
static debug(message) {
|
|
24
|
+
LogService.logger.debug(message);
|
|
43
25
|
}
|
|
44
26
|
}
|
|
45
27
|
exports.default = LogService;
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { HookFunctionTypes, Extensions } from "../Enums";
|
|
2
2
|
import { IColumn, IModelService, IQueryLimitConfig, IRelation } from "../Interfaces";
|
|
3
3
|
import Model from "./../Model";
|
|
4
|
-
import {
|
|
4
|
+
import { HookFunctions, PhaseFunction, SerializationFunction } from "../Types";
|
|
5
5
|
declare class ModelService implements IModelService {
|
|
6
6
|
name: string;
|
|
7
7
|
instance: Model;
|
|
8
8
|
relations: IRelation[];
|
|
9
9
|
columns: IColumn[];
|
|
10
10
|
columnNames: string[];
|
|
11
|
-
hooks:
|
|
12
|
-
events:
|
|
11
|
+
hooks: HookFunctions;
|
|
12
|
+
events: HookFunctions;
|
|
13
13
|
children: IModelService[];
|
|
14
14
|
isRecursive: boolean;
|
|
15
15
|
queryLimits: IQueryLimitConfig[];
|
|
16
16
|
serialize: SerializationFunction | null;
|
|
17
17
|
constructor(name: string, instance: Model);
|
|
18
18
|
setColumns(columns: IColumn[]): void;
|
|
19
|
-
setExtensions(type: Extensions, hookFunctionType: HookFunctionTypes, data:
|
|
19
|
+
setExtensions(type: Extensions, hookFunctionType: HookFunctionTypes, data: PhaseFunction): void;
|
|
20
20
|
setQueryLimits(limits: IQueryLimitConfig[]): void;
|
|
21
21
|
setSerialization(callback: SerializationFunction): void;
|
|
22
22
|
private setHooks;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IQuery, ISortField, NestedWhere, IWhere, IModelService,
|
|
1
|
+
import { IQuery, ISortField, NestedWhere, IWhere, IModelService, AxeVersionConfig } from "../Interfaces";
|
|
2
2
|
import { Knex } from "knex";
|
|
3
3
|
declare class QueryService {
|
|
4
4
|
model: IModelService;
|
|
@@ -6,13 +6,13 @@ declare class QueryService {
|
|
|
6
6
|
usedConditionColumns: string[];
|
|
7
7
|
relationColumns: string[];
|
|
8
8
|
createdJoins: string[];
|
|
9
|
-
config:
|
|
10
|
-
constructor(model: IModelService, models: IModelService[], config:
|
|
9
|
+
config: AxeVersionConfig;
|
|
10
|
+
constructor(model: IModelService, models: IModelService[], config: AxeVersionConfig);
|
|
11
11
|
applyFields(query: Knex.QueryBuilder, fields: string[]): void;
|
|
12
12
|
applySorting(query: Knex.QueryBuilder, sort: ISortField[]): void;
|
|
13
13
|
applyWheresInsideGroup(sub: Knex.QueryBuilder, ruleSet: NestedWhere | IWhere): void;
|
|
14
14
|
applyWheres(query: Knex.QueryBuilder, ruleSet: NestedWhere): void;
|
|
15
|
-
get(query:
|
|
15
|
+
get(query: URLSearchParams): IQuery;
|
|
16
16
|
private getUsedColumns;
|
|
17
17
|
private applyConditionRule;
|
|
18
18
|
private applyRelatedQueryJoins;
|
|
@@ -164,12 +164,13 @@ class QueryService {
|
|
|
164
164
|
}
|
|
165
165
|
parseSections(sections) {
|
|
166
166
|
var _a, _b, _c, _d;
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
let q = sections.get("q");
|
|
168
|
+
if (q) {
|
|
169
|
+
const queryContent = q.replace(/%20/g, "").replace(/ /g, "");
|
|
169
170
|
// Users can send an unacceptable query string. We shouldn't allow them to
|
|
170
171
|
// send unacceptable structure because of security reasons.
|
|
171
172
|
try {
|
|
172
|
-
|
|
173
|
+
q = JSON.parse(queryContent);
|
|
173
174
|
}
|
|
174
175
|
catch (err) {
|
|
175
176
|
throw new ApiError_1.default(`Unacceptable query string: ${queryContent}`);
|
|
@@ -177,13 +178,15 @@ class QueryService {
|
|
|
177
178
|
}
|
|
178
179
|
const withQueryResolver = new Resolvers_1.WithQueryResolver(this.model, this.models);
|
|
179
180
|
const query = {
|
|
180
|
-
page: this.parsePage(sections.page),
|
|
181
|
-
per_page: this.parsePerPage(sections.per_page),
|
|
182
|
-
fields: this.parseFields(sections.fields),
|
|
183
|
-
sort: this.parseSortingOptions(sections.sort),
|
|
184
|
-
q: this.parseCondition(
|
|
185
|
-
with: withQueryResolver.resolve(
|
|
186
|
-
trashed:
|
|
181
|
+
page: this.parsePage(sections.get("page")),
|
|
182
|
+
per_page: this.parsePerPage(sections.get("per_page")),
|
|
183
|
+
fields: this.parseFields(sections.get("fields")),
|
|
184
|
+
sort: this.parseSortingOptions(sections.get("sort")),
|
|
185
|
+
q: this.parseCondition(q),
|
|
186
|
+
with: withQueryResolver.resolve(sections.get("with") || ""),
|
|
187
|
+
trashed: sections.get("trashed")
|
|
188
|
+
? (0, Helpers_1.isBoolean)(sections.get("trashed"))
|
|
189
|
+
: false,
|
|
187
190
|
};
|
|
188
191
|
const configPerPage = ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.query.defaults) === null || _b === void 0 ? void 0 : _b.perPage) ||
|
|
189
192
|
((_d = (_c = constants_1.DEFAULT_VERSION_CONFIG.query) === null || _c === void 0 ? void 0 : _c.defaults) === null || _d === void 0 ? void 0 : _d.perPage) ||
|
|
@@ -217,7 +220,7 @@ class QueryService {
|
|
|
217
220
|
((_j = constants_1.DEFAULT_VERSION_CONFIG.query.defaults) === null || _j === void 0 ? void 0 : _j.maxPerPage) ||
|
|
218
221
|
100;
|
|
219
222
|
if (isNaN(value) || value < minPerPage || value > maxPerPage) {
|
|
220
|
-
throw new ApiError_1.default(`Unacceptable 'per_page' value! Current value is '${value}'. It should be between ${minPerPage}-${maxPerPage}
|
|
223
|
+
throw new ApiError_1.default(`Unacceptable 'per_page' value! Current value is '${value}'. It should be between ${minPerPage}-${maxPerPage}`, Enums_1.StatusCodes.BAD_REQUEST);
|
|
221
224
|
}
|
|
222
225
|
return value;
|
|
223
226
|
}
|
|
@@ -119,7 +119,6 @@ class SchemaValidatorService {
|
|
|
119
119
|
}
|
|
120
120
|
validate() {
|
|
121
121
|
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
-
const logger = Services_1.LogService.getInstance();
|
|
123
122
|
this.version.modelList.get().forEach((model) => {
|
|
124
123
|
this.checkModelReservedKeywordsOrFail(model);
|
|
125
124
|
this.checkModelColumnsOrFail(model, this.getModelFillableColumns(model));
|
|
@@ -131,7 +130,7 @@ class SchemaValidatorService {
|
|
|
131
130
|
this.checkRelationNamesOrFail(model, this.getQueryLimitRelations(model));
|
|
132
131
|
this.checkRelationColumnsOrFail(this.version.modelList, model);
|
|
133
132
|
});
|
|
134
|
-
|
|
133
|
+
Services_1.LogService.debug(`[${this.version.name}] Database schema has been validated.`);
|
|
135
134
|
});
|
|
136
135
|
}
|
|
137
136
|
checkModelReservedKeywordsOrFail(model) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { HandlerFunction, MiddlewareFunction, StepTypes } from "../Types";
|
|
2
|
+
import { IPhaseDefinition, IRouteData } from "../Interfaces";
|
|
3
|
+
import AxeRequest from "./AxeRequest";
|
|
4
|
+
interface Pair {
|
|
5
|
+
method: string;
|
|
6
|
+
pattern: string;
|
|
7
|
+
data: IRouteData;
|
|
8
|
+
phases: IPhaseDefinition[];
|
|
9
|
+
hasTransaction: boolean;
|
|
10
|
+
params?: any;
|
|
11
|
+
customHandler?: HandlerFunction;
|
|
12
|
+
}
|
|
13
|
+
declare class URLService {
|
|
14
|
+
private static urls;
|
|
15
|
+
static add(method: string, pattern: string, data: IRouteData, middlewares: StepTypes[]): Promise<void>;
|
|
16
|
+
static addHandler(method: string, pattern: string, customHandler: HandlerFunction, middlewares: (MiddlewareFunction | HandlerFunction)[]): Promise<void>;
|
|
17
|
+
static match(request: AxeRequest): {
|
|
18
|
+
params: any;
|
|
19
|
+
method: string;
|
|
20
|
+
pattern: string;
|
|
21
|
+
data: IRouteData;
|
|
22
|
+
phases: IPhaseDefinition[];
|
|
23
|
+
hasTransaction: boolean;
|
|
24
|
+
customHandler?: HandlerFunction | undefined;
|
|
25
|
+
} | undefined;
|
|
26
|
+
static getAllURLs(): Pair[];
|
|
27
|
+
private static getDefaultPhases;
|
|
28
|
+
}
|
|
29
|
+
export default URLService;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const util_1 = require("util");
|
|
16
|
+
const constants_1 = require("../constants");
|
|
17
|
+
const Resolvers_1 = require("../Resolvers");
|
|
18
|
+
const Enums_1 = require("../Enums");
|
|
19
|
+
const LogService_1 = __importDefault(require("./LogService"));
|
|
20
|
+
const ConverterService_1 = require("./ConverterService");
|
|
21
|
+
const check = (url, pattern) => {
|
|
22
|
+
// Escape special characters in the pattern and replace parameter placeholders with regular expression groups
|
|
23
|
+
const regexPattern = new RegExp("^" + pattern.replace(/:[a-zA-Z0-9_]+/g, "([a-zA-Z0-9_-]+)") + "$");
|
|
24
|
+
// Test if the URL matches the pattern
|
|
25
|
+
const match = url.match(regexPattern);
|
|
26
|
+
if (match) {
|
|
27
|
+
// Extract parameter values from the URL
|
|
28
|
+
const params = {};
|
|
29
|
+
const paramNames = pattern.match(/:[a-zA-Z0-9_]+/g) || [];
|
|
30
|
+
paramNames.forEach((param, index) => {
|
|
31
|
+
const paramName = param.slice(1); // Remove the leading ":"
|
|
32
|
+
params[paramName] = match[index + 1]; // index + 1 because match[0] is the whole matched string
|
|
33
|
+
});
|
|
34
|
+
return params;
|
|
35
|
+
}
|
|
36
|
+
return null; // URL does not match the pattern
|
|
37
|
+
};
|
|
38
|
+
class URLService {
|
|
39
|
+
static add(method, pattern, data, middlewares) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
const phases = this.getDefaultPhases(middlewares);
|
|
42
|
+
if (data.handlerType && data.model) {
|
|
43
|
+
for (const cycle of constants_1.HANDLER_CYLES[data.handlerType]) {
|
|
44
|
+
const item = cycle.get(data.model);
|
|
45
|
+
if (item) {
|
|
46
|
+
phases.push({
|
|
47
|
+
name: cycle.name,
|
|
48
|
+
isAsync: cycle.isAsync(),
|
|
49
|
+
callback: item,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
let hasTransaction = false;
|
|
55
|
+
if (data.version && data.model && data.handlerType) {
|
|
56
|
+
hasTransaction = yield new Resolvers_1.TransactionResolver(data.version).resolve(data.model, data.handlerType);
|
|
57
|
+
}
|
|
58
|
+
LogService_1.default.info(`${method} ${pattern}`);
|
|
59
|
+
this.urls.push({
|
|
60
|
+
method,
|
|
61
|
+
pattern,
|
|
62
|
+
data,
|
|
63
|
+
phases,
|
|
64
|
+
hasTransaction,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
static addHandler(method, pattern, customHandler, middlewares) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
LogService_1.default.info(`${method} ${pattern}`);
|
|
71
|
+
const phases = middlewares.map((middleware) => {
|
|
72
|
+
return {
|
|
73
|
+
isAsync: false,
|
|
74
|
+
name: `middleware:test`,
|
|
75
|
+
callback: (context) => __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
if ((0, ConverterService_1.isMiddlewareFunction)(middleware)) {
|
|
77
|
+
// It should be wrapped
|
|
78
|
+
const caller = (0, util_1.promisify)((context, next) => middleware(context.req.original, context.res.original, next));
|
|
79
|
+
yield caller(context);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// We call it directly.
|
|
83
|
+
yield middleware(context.req, context.res);
|
|
84
|
+
}
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
const hasTransaction = false;
|
|
89
|
+
phases.push({
|
|
90
|
+
isAsync: false,
|
|
91
|
+
name: "customHandler",
|
|
92
|
+
callback: (context) => __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
customHandler(context.req, context.res);
|
|
94
|
+
}),
|
|
95
|
+
});
|
|
96
|
+
this.urls.push({
|
|
97
|
+
method,
|
|
98
|
+
pattern,
|
|
99
|
+
phases,
|
|
100
|
+
hasTransaction,
|
|
101
|
+
customHandler,
|
|
102
|
+
data: {
|
|
103
|
+
version: {},
|
|
104
|
+
handlerType: Enums_1.HandlerTypes.INSERT,
|
|
105
|
+
model: {},
|
|
106
|
+
parentModel: null,
|
|
107
|
+
relation: null,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
static match(request) {
|
|
113
|
+
if (!request) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
for (const item of URLService.urls) {
|
|
117
|
+
const found = item.method === request.method &&
|
|
118
|
+
check(request.url.pathname, item.pattern);
|
|
119
|
+
if (found) {
|
|
120
|
+
return Object.assign(Object.assign({}, item), { params: found });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
static getAllURLs() {
|
|
125
|
+
return this.urls;
|
|
126
|
+
}
|
|
127
|
+
static getDefaultPhases(middlewares) {
|
|
128
|
+
// We should convert to all StepTypes functions to PhaseFunctions
|
|
129
|
+
const callbacks = middlewares.map(ConverterService_1.toPhaseFunction);
|
|
130
|
+
const phases = [
|
|
131
|
+
// Internal middlewares
|
|
132
|
+
...callbacks.map((callback) => {
|
|
133
|
+
return {
|
|
134
|
+
isAsync: true,
|
|
135
|
+
name: `middleware:${callback.name || "anonymous"}`,
|
|
136
|
+
callback,
|
|
137
|
+
};
|
|
138
|
+
}),
|
|
139
|
+
];
|
|
140
|
+
return phases;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
URLService.urls = [];
|
|
144
|
+
exports.default = URLService;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import APIService from "./APIService";
|
|
2
|
+
import App from "./App";
|
|
2
3
|
import DocumentationService from "./DocumentationService";
|
|
3
4
|
import IoCService from "./IoCService";
|
|
4
5
|
import { allow, deny } from "./LimitService";
|
|
@@ -7,4 +8,6 @@ import ModelListService from "./ModelListService";
|
|
|
7
8
|
import ModelService from "./ModelService";
|
|
8
9
|
import QueryService from "./QueryService";
|
|
9
10
|
import SchemaValidatorService from "./SchemaValidatorService";
|
|
10
|
-
|
|
11
|
+
import AxeRequest from "./AxeRequest";
|
|
12
|
+
import AxeResponse from "./AxeResponse";
|
|
13
|
+
export { App, AxeRequest, AxeResponse, DocumentationService, APIService, IoCService, LogService, ModelListService, ModelService, QueryService, SchemaValidatorService, allow, deny, };
|
|
@@ -3,9 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.deny = exports.allow = exports.SchemaValidatorService = exports.QueryService = exports.ModelService = exports.ModelListService = exports.LogService = exports.IoCService = exports.APIService = exports.DocumentationService = void 0;
|
|
6
|
+
exports.deny = exports.allow = exports.SchemaValidatorService = exports.QueryService = exports.ModelService = exports.ModelListService = exports.LogService = exports.IoCService = exports.APIService = exports.DocumentationService = exports.AxeResponse = exports.AxeRequest = exports.App = void 0;
|
|
7
7
|
const APIService_1 = __importDefault(require("./APIService"));
|
|
8
8
|
exports.APIService = APIService_1.default;
|
|
9
|
+
const App_1 = __importDefault(require("./App"));
|
|
10
|
+
exports.App = App_1.default;
|
|
9
11
|
const DocumentationService_1 = __importDefault(require("./DocumentationService"));
|
|
10
12
|
exports.DocumentationService = DocumentationService_1.default;
|
|
11
13
|
const IoCService_1 = __importDefault(require("./IoCService"));
|
|
@@ -23,3 +25,7 @@ const QueryService_1 = __importDefault(require("./QueryService"));
|
|
|
23
25
|
exports.QueryService = QueryService_1.default;
|
|
24
26
|
const SchemaValidatorService_1 = __importDefault(require("./SchemaValidatorService"));
|
|
25
27
|
exports.SchemaValidatorService = SchemaValidatorService_1.default;
|
|
28
|
+
const AxeRequest_1 = __importDefault(require("./AxeRequest"));
|
|
29
|
+
exports.AxeRequest = AxeRequest_1.default;
|
|
30
|
+
const AxeResponse_1 = __importDefault(require("./AxeResponse"));
|
|
31
|
+
exports.AxeResponse = AxeResponse_1.default;
|
package/build/src/Types.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="express" />
|
|
3
|
+
import { IncomingMessage, ServerResponse } from "http";
|
|
4
|
+
import { IHandlerBaseMiddleware, IRequestPack } from "./Interfaces";
|
|
5
|
+
import AxeRequest from "./Services/AxeRequest";
|
|
6
|
+
import AxeResponse from "./Services/AxeResponse";
|
|
7
|
+
import { HookFunctionTypes } from "./Enums";
|
|
6
8
|
export type ModelValidation = Record<string, string>;
|
|
7
9
|
export type FieldList = string[];
|
|
8
|
-
export type
|
|
10
|
+
export type DefaultResponse = Promise<void> | void | undefined;
|
|
11
|
+
export type AdaptorTypes = "redis" | "memory";
|
|
12
|
+
export type MiddlewareFunction = (req: IncomingMessage, res: ServerResponse, next: NextFunction) => DefaultResponse;
|
|
13
|
+
export type HandlerFunction = (request: AxeRequest, response: AxeResponse) => DefaultResponse;
|
|
14
|
+
export type PhaseFunction = (context: IRequestPack) => DefaultResponse;
|
|
15
|
+
export type SerializationFunction = (item: any, request: AxeRequest) => any;
|
|
16
|
+
export type HookFunctions = Record<HookFunctionTypes, PhaseFunction>;
|
|
17
|
+
export type NextFunction = (error: any) => void;
|
|
18
|
+
export type DynamicFunctionType = (MiddlewareFunction | HandlerFunction)[];
|
|
19
|
+
export type StepTypes = MiddlewareFunction | HandlerFunction | PhaseFunction;
|
|
20
|
+
export type ModelMiddlewareDefinition = Array<StepTypes | IHandlerBaseMiddleware>;
|