axe-api 1.4.8 → 1.5.0-rc-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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
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
+ const dotenv_1 = __importDefault(require("dotenv"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const index_1 = require("./index");
9
+ console.log("Axe API dev-kit (1.0.1)");
10
+ dotenv_1.default.config();
11
+ const server = new index_1.Server();
12
+ server.start(path_1.default.join(__dirname, "dev-kit"));
package/build/index.d.ts CHANGED
@@ -4,8 +4,8 @@ import ApiError from "./src/Exceptions/ApiError";
4
4
  import RedisAdaptor from "./src/Middlewares/RateLimit/RedisAdaptor";
5
5
  import { DEFAULT_HANDLERS, DEFAULT_VERSION_CONFIG } from "./src/constants";
6
6
  import { IoCService, allow, deny, App, AxeRequest, AxeResponse } from "./src/Services";
7
- import { rateLimit } from "./src/Middlewares/RateLimit";
7
+ import { rateLimit, createRateLimitter } from "./src/Middlewares/RateLimit";
8
8
  export * from "./src/Enums";
9
9
  export * from "./src/Interfaces";
10
10
  export * from "./src/Types";
11
- export { App, AxeRequest, AxeResponse, Server, Model, ApiError, RedisAdaptor, DEFAULT_HANDLERS, DEFAULT_VERSION_CONFIG, IoCService, allow, deny, rateLimit, };
11
+ export { App, AxeRequest, AxeResponse, Server, Model, ApiError, RedisAdaptor, DEFAULT_HANDLERS, DEFAULT_VERSION_CONFIG, IoCService, allow, deny, rateLimit, createRateLimitter, };
package/build/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.rateLimit = exports.deny = exports.allow = exports.IoCService = exports.DEFAULT_VERSION_CONFIG = exports.DEFAULT_HANDLERS = exports.RedisAdaptor = exports.ApiError = exports.Model = exports.Server = exports.AxeResponse = exports.AxeRequest = exports.App = void 0;
20
+ exports.createRateLimitter = exports.rateLimit = exports.deny = exports.allow = exports.IoCService = exports.DEFAULT_VERSION_CONFIG = exports.DEFAULT_HANDLERS = exports.RedisAdaptor = exports.ApiError = exports.Model = exports.Server = exports.AxeResponse = exports.AxeRequest = exports.App = void 0;
21
21
  const Server_1 = __importDefault(require("./src/Server"));
22
22
  exports.Server = Server_1.default;
23
23
  const Model_1 = __importDefault(require("./src/Model"));
@@ -38,6 +38,7 @@ Object.defineProperty(exports, "AxeRequest", { enumerable: true, get: function (
38
38
  Object.defineProperty(exports, "AxeResponse", { enumerable: true, get: function () { return Services_1.AxeResponse; } });
39
39
  const RateLimit_1 = require("./src/Middlewares/RateLimit");
40
40
  Object.defineProperty(exports, "rateLimit", { enumerable: true, get: function () { return RateLimit_1.rateLimit; } });
41
+ Object.defineProperty(exports, "createRateLimitter", { enumerable: true, get: function () { return RateLimit_1.createRateLimitter; } });
41
42
  __exportStar(require("./src/Enums"), exports);
42
43
  __exportStar(require("./src/Interfaces"), exports);
43
44
  __exportStar(require("./src/Types"), exports);
@@ -54,7 +54,9 @@ exports.default = (request, response, next) => __awaiter(void 0, void 0, void 0,
54
54
  const validator = yield Services_1.IoCService.use("Validator");
55
55
  const context = Object.assign(Object.assign({}, match.data), { params: match.params, api, req: axeRequest, res: axeResponse, isTransactionOpen: match.hasTransaction, database: match.hasTransaction && trx ? trx : database, validator });
56
56
  response.setHeader("Content-Type", "application/json");
57
- response.setHeader("x-powered-by", "Axe API");
57
+ if (api.config.disableXPoweredByHeader === false) {
58
+ response.setHeader("x-powered-by", "Axe API");
59
+ }
58
60
  for (const phase of match.phases) {
59
61
  // If there is an non-async phase, it should be an Event function
60
62
  if (phase.isAsync === false) {
@@ -44,6 +44,11 @@ export interface IQueryConfig {
44
44
  limits: Array<IQueryLimitConfig[]>;
45
45
  defaults?: IQueryDefaultConfig;
46
46
  }
47
+ export interface IRateLimitMiddleware {
48
+ name: string;
49
+ clientKey: string;
50
+ setResponseHeaders?: boolean;
51
+ }
47
52
  export interface IRateLimitOptions {
48
53
  maxRequests: number;
49
54
  windowInSeconds: number;
@@ -85,6 +90,7 @@ export interface AxeConfig extends IConfig {
85
90
  rateLimit: IRateLimitConfig;
86
91
  errorHandler: ErrorHandleFunction;
87
92
  docs: boolean;
93
+ disableXPoweredByHeader: boolean;
88
94
  redis: RedisClientOptions | undefined;
89
95
  cache: ICacheConfiguration;
90
96
  elasticSearch: ClientOptions;
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from "http";
2
- import { AxeConfig, IRateLimitOptions, IContext } from "../../Interfaces";
2
+ import { AxeConfig, IRateLimitOptions, IContext, IRateLimitMiddleware } from "../../Interfaces";
3
3
  export declare const setupRateLimitAdaptors: (config: AxeConfig) => Promise<void>;
4
4
  /**
5
5
  * Add a rate limit with the `IRateLimitOptions`
@@ -19,5 +19,17 @@ export declare const setupRateLimitAdaptors: (config: AxeConfig) => Promise<void
19
19
  * }
20
20
  */
21
21
  export declare const rateLimit: (options?: IRateLimitOptions) => (context: IContext) => Promise<void>;
22
+ /**
23
+ * Create a rate-limitter middleware (a connect middleware) with a middleware
24
+ * configurations
25
+ *
26
+ * @param middleware IRateLimitMiddleware
27
+ * @param options IRateLimitOptions
28
+ * @param req IncomingMessage
29
+ * @param res ServerResponse
30
+ * @param next NextFunction
31
+ * @returns
32
+ */
33
+ export declare const createRateLimitter: (middleware: IRateLimitMiddleware, options: IRateLimitOptions, req: IncomingMessage, res: ServerResponse, next: any) => Promise<any>;
22
34
  declare const _default: (req: IncomingMessage, res: ServerResponse, next: any) => Promise<any>;
23
35
  export default _default;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.rateLimit = exports.setupRateLimitAdaptors = void 0;
15
+ exports.createRateLimitter = exports.rateLimit = exports.setupRateLimitAdaptors = void 0;
16
16
  const AdaptorFactory_1 = __importDefault(require("./AdaptorFactory"));
17
17
  const Services_1 = require("../../Services");
18
18
  const nanoid_1 = require("nanoid");
@@ -113,6 +113,37 @@ const rateLimit = (options) => {
113
113
  });
114
114
  };
115
115
  exports.rateLimit = rateLimit;
116
+ /**
117
+ * Create a rate-limitter middleware (a connect middleware) with a middleware
118
+ * configurations
119
+ *
120
+ * @param middleware IRateLimitMiddleware
121
+ * @param options IRateLimitOptions
122
+ * @param req IncomingMessage
123
+ * @param res ServerResponse
124
+ * @param next NextFunction
125
+ * @returns
126
+ */
127
+ const createRateLimitter = (middleware, options, req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
128
+ // Checking the rate limits.
129
+ const isAllowed = yield checkRateLimit(middleware.clientKey, options);
130
+ // Setting the HTTP Response headers.
131
+ if (middleware.setResponseHeaders) {
132
+ res.setHeader(`X-${middleware.name}-Limit`, isAllowed.limit);
133
+ res.setHeader(`X-${middleware.name}-Remaining`, isAllowed.remaining);
134
+ }
135
+ // If it is allowed, the next function would be called.
136
+ if (isAllowed.success) {
137
+ return next();
138
+ }
139
+ // Sending an error message.
140
+ Services_1.LogService.warn(`Rate limit exceeded: ${req.url}`);
141
+ res.writeHead(429, { "Content-Type": "application/json" });
142
+ res.end(JSON.stringify({
143
+ error: "Rate limit exceeded.",
144
+ }));
145
+ });
146
+ exports.createRateLimitter = createRateLimitter;
116
147
  exports.default = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
117
148
  const api = Services_1.APIService.getInstance();
118
149
  // Generating the client key
@@ -3,7 +3,6 @@ import { StatusCodes } from "src/Enums";
3
3
  import { ILanguage } from "src/Interfaces";
4
4
  declare class AxeResponse {
5
5
  private response;
6
- private responseStatus;
7
6
  private language;
8
7
  constructor(response: ServerResponse, language: ILanguage);
9
8
  /**
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class AxeResponse {
4
4
  constructor(response, language) {
5
- this.responseStatus = false;
6
5
  this.response = response;
7
6
  this.response.statusCode = 200;
8
7
  this.language = language;
@@ -36,7 +35,7 @@ class AxeResponse {
36
35
  this.response.setHeader("Content-Language", this.language.language);
37
36
  this.response.write(JSON.stringify(data));
38
37
  this.response.end();
39
- this.responseStatus = true;
38
+ this.response.isResponded = true;
40
39
  }
41
40
  /**
42
41
  * Set the HTTP Response Data as string
@@ -46,7 +45,7 @@ class AxeResponse {
46
45
  send(content) {
47
46
  this.response.write(content);
48
47
  this.response.end();
49
- this.responseStatus = true;
48
+ this.response.isResponded = true;
50
49
  }
51
50
  /**
52
51
  * Set the no-content to the HTTP Response with 204 status code.
@@ -54,16 +53,16 @@ class AxeResponse {
54
53
  noContent() {
55
54
  this.response.statusCode = 204;
56
55
  this.response.end();
57
- this.responseStatus = true;
56
+ this.response.isResponded = true;
58
57
  }
59
58
  isResponded() {
60
- return this.responseStatus;
59
+ return !!this.response.isResponded;
61
60
  }
62
61
  statusCode() {
63
62
  return this.response.statusCode;
64
63
  }
65
64
  header(key, value) {
66
- this.original.setHeader(key, value);
65
+ this.response.setHeader(key, value);
67
66
  }
68
67
  }
69
68
  exports.default = AxeResponse;
@@ -284,6 +284,7 @@ exports.DEFAULT_APP_CONFIG = {
284
284
  env: "production",
285
285
  port: 3000,
286
286
  docs: true,
287
+ disableXPoweredByHeader: false,
287
288
  pino: {
288
289
  level: "error",
289
290
  transport: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axe-api",
3
- "version": "1.4.8",
3
+ "version": "1.5.0-rc-2",
4
4
  "description": "AXE API is a simple tool to create Rest APIs quickly.",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",