axe-api 1.0.0-rc9 → 1.0.0
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/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/src/Builders/IndexBuilder.d.ts +7 -0
- package/build/src/Builders/IndexBuilder.js +35 -0
- package/build/src/Builders/ModelTreeBuilder.js +2 -1
- package/build/src/Builders/RouterBuilder.js +22 -11
- package/build/src/Builders/SwaggerBuilder.d.ts +2 -0
- package/build/src/Builders/SwaggerBuilder.js +609 -0
- package/build/src/Enums.d.ts +6 -1
- package/build/src/Enums.js +7 -1
- package/build/src/Handlers/DocsHandler.js +41 -0
- package/build/src/Handlers/ErrorHandler.d.ts +6 -0
- package/build/src/Handlers/ErrorHandler.js +12 -0
- package/build/src/Handlers/Helpers.d.ts +16 -4
- package/build/src/Handlers/Helpers.js +202 -5
- package/build/src/Handlers/RequestHandler.d.ts +4 -1
- package/build/src/Handlers/RequestHandler.js +36 -40
- package/build/src/Handlers/{DocsHTMLHandler.js → SwaggerHandler.js} +8 -7
- package/build/src/Interfaces.d.ts +103 -35
- package/build/src/Middlewares/RateLimit/AdaptorFactory.d.ts +3 -3
- package/build/src/Middlewares/RateLimit/MemoryAdaptor.d.ts +2 -2
- package/build/src/Middlewares/RateLimit/RedisAdaptor.d.ts +12 -4
- package/build/src/Middlewares/RateLimit/RedisAdaptor.js +24 -0
- package/build/src/Middlewares/RateLimit/index.d.ts +22 -3
- package/build/src/Middlewares/RateLimit/index.js +23 -3
- package/build/src/Model.d.ts +256 -10
- package/build/src/Model.js +322 -30
- package/build/src/Phases/All/FetchPhase.d.ts +2 -2
- package/build/src/Phases/All/PreparePhase.d.ts +2 -2
- package/build/src/Phases/All/index.d.ts +1 -1
- package/build/src/Phases/CacheTagCleanPhase.d.ts +3 -0
- package/build/src/{Handlers/MetadataHandler.js → Phases/CacheTagCleanPhase.js} +6 -10
- package/build/src/Phases/Delete/ActionPhase.d.ts +2 -2
- package/build/src/Phases/Delete/ActionPhase.js +21 -2
- package/build/src/Phases/Delete/PreparePhase.d.ts +2 -2
- package/build/src/Phases/Delete/QueryPhase.d.ts +2 -2
- package/build/src/Phases/Delete/ResponsePhase.d.ts +2 -2
- package/build/src/Phases/Delete/ResponsePhase.js +2 -1
- package/build/src/Phases/Delete/index.d.ts +4 -4
- package/build/src/Phases/ForceDelete/ActionPhase.d.ts +2 -2
- package/build/src/Phases/ForceDelete/ActionPhase.js +7 -0
- package/build/src/Phases/ForceDelete/PreparePhase.d.ts +2 -2
- package/build/src/Phases/ForceDelete/QueryPhase.d.ts +2 -2
- package/build/src/Phases/ForceDelete/index.d.ts +3 -3
- package/build/src/Phases/GetCachePhase.d.ts +3 -0
- package/build/src/Phases/GetCachePhase.js +39 -0
- package/build/src/Phases/List/RelationalPhase.d.ts +2 -2
- package/build/src/Phases/List/ResultPhase.d.ts +2 -2
- package/build/src/Phases/List/ResultPhase.js +9 -1
- package/build/src/Phases/List/SerializePhase.d.ts +2 -2
- package/build/src/Phases/List/index.d.ts +3 -3
- package/build/src/Phases/Paginate/FetchPhase.d.ts +2 -2
- package/build/src/Phases/Paginate/PreparePhase.d.ts +2 -2
- package/build/src/Phases/Paginate/index.d.ts +2 -2
- package/build/src/Phases/Patch/PrepareActionPhase.d.ts +2 -2
- package/build/src/Phases/Patch/PrepareActionPhase.js +13 -10
- package/build/src/Phases/Patch/index.d.ts +1 -1
- package/build/src/Phases/Search/FetchPhase.d.ts +3 -0
- package/build/src/Phases/Search/FetchPhase.js +66 -0
- package/build/src/Phases/Search/PreparePhase.d.ts +3 -0
- package/build/src/Phases/Search/PreparePhase.js +19 -0
- package/build/src/Phases/Search/index.d.ts +5 -0
- package/build/src/Phases/Search/index.js +11 -0
- package/build/src/Phases/Show/FetchPhase.d.ts +2 -2
- package/build/src/Phases/Show/FetchPhase.js +11 -1
- package/build/src/Phases/Show/PreparePhase.d.ts +2 -2
- package/build/src/Phases/Show/PreparePhase.js +5 -13
- package/build/src/Phases/Show/index.d.ts +2 -2
- package/build/src/Phases/Single/GetPhase.d.ts +2 -2
- package/build/src/Phases/Single/PrepareGetPhase.d.ts +2 -2
- package/build/src/Phases/Single/RelationalPhase.d.ts +2 -2
- package/build/src/Phases/Single/ResultPhase.d.ts +2 -2
- package/build/src/Phases/Single/ResultPhase.js +9 -1
- package/build/src/Phases/Single/SerializePhase.d.ts +2 -2
- package/build/src/Phases/Single/index.d.ts +5 -5
- package/build/src/Phases/Store/ActionPhase.d.ts +2 -2
- package/build/src/Phases/Store/ActionPhase.js +18 -7
- package/build/src/Phases/Store/PreparePhase.d.ts +2 -2
- package/build/src/Phases/Store/PreparePhase.js +14 -9
- package/build/src/Phases/Store/ResultPhase.d.ts +3 -0
- package/build/src/Phases/Store/ResultPhase.js +23 -0
- package/build/src/Phases/Store/index.d.ts +3 -2
- package/build/src/Phases/Store/index.js +2 -0
- package/build/src/Phases/URLSearchParamPhase.d.ts +3 -0
- package/build/src/Phases/URLSearchParamPhase.js +37 -0
- package/build/src/Phases/Update/ActionPhase.d.ts +2 -2
- package/build/src/Phases/Update/ActionPhase.js +14 -3
- package/build/src/Phases/Update/PrepareActionPhase.d.ts +2 -2
- package/build/src/Phases/Update/PrepareActionPhase.js +13 -10
- package/build/src/Phases/Update/index.d.ts +2 -2
- package/build/src/Resolvers/ModelResolver.d.ts +1 -0
- package/build/src/Resolvers/ModelResolver.js +21 -2
- package/build/src/Resolvers/TransactionResolver.js +3 -15
- package/build/src/Resolvers/VersionConfigResolver.js +6 -1
- package/build/src/Resolvers/VersionResolver.js +1 -0
- package/build/src/Server.d.ts +5 -0
- package/build/src/Server.js +44 -11
- package/build/src/Services/APIService.d.ts +3 -3
- package/build/src/Services/APIService.js +3 -16
- package/build/src/Services/App.d.ts +94 -8
- package/build/src/Services/App.js +94 -29
- package/build/src/Services/AxeRequest.d.ts +51 -2
- package/build/src/Services/AxeRequest.js +51 -4
- package/build/src/Services/AxeResponse.d.ts +28 -1
- package/build/src/Services/AxeResponse.js +28 -0
- package/build/src/Services/ConverterService.d.ts +5 -3
- package/build/src/Services/DocumentationService.d.ts +5 -2
- package/build/src/Services/DocumentationService.js +13 -1
- package/build/src/Services/ElasticService.d.ts +14 -0
- package/build/src/Services/ElasticService.js +74 -0
- package/build/src/Services/IoCService.d.ts +29 -2
- package/build/src/Services/IoCService.js +33 -6
- package/build/src/Services/LimitService.d.ts +18 -0
- package/build/src/Services/LimitService.js +18 -0
- package/build/src/Services/ModelService.d.ts +9 -5
- package/build/src/Services/ModelService.js +11 -0
- package/build/src/Services/QueryService.d.ts +3 -3
- package/build/src/Services/QueryService.js +6 -0
- package/build/src/Services/SchemaValidatorService.d.ts +1 -0
- package/build/src/Services/SchemaValidatorService.js +4 -0
- package/build/src/Services/URLService.d.ts +6 -14
- package/build/src/Services/URLService.js +10 -8
- package/build/src/Steps/Event.d.ts +11 -0
- package/build/src/Steps/Event.js +17 -0
- package/build/src/Steps/Hook.d.ts +11 -0
- package/build/src/Steps/Hook.js +17 -0
- package/build/src/Steps/Phase.d.ts +11 -0
- package/build/src/Steps/Phase.js +18 -0
- package/build/src/Types.d.ts +12 -10
- package/build/src/constants.d.ts +8 -9
- package/build/src/constants.js +215 -152
- package/package.json +46 -36
- package/build/src/Middlewares/RateLimit/IAdaptor.d.ts +0 -6
- package/build/src/Middlewares/RateLimit/IAdaptor.js +0 -2
- /package/build/src/Handlers/{DocsHTMLHandler.d.ts → DocsHandler.d.ts} +0 -0
- /package/build/src/Handlers/{MetadataHandler.d.ts → SwaggerHandler.d.ts} +0 -0
|
@@ -3,24 +3,31 @@
|
|
|
3
3
|
import { Knex } from "knex";
|
|
4
4
|
import { Options as FormOptions } from "formidable";
|
|
5
5
|
import { Column } from "knex-schema-inspector/lib/types/column";
|
|
6
|
-
import { HandlerTypes, HttpMethods, HookFunctionTypes, Extensions, Relationships, SortTypes, ConditionTypes, DependencyTypes, QueryFeature, QueryFeatureType } from "./Enums";
|
|
6
|
+
import { HandlerTypes, HttpMethods, HookFunctionTypes, Extensions, Relationships, SortTypes, ConditionTypes, DependencyTypes, QueryFeature, QueryFeatureType, CacheStrategies } from "./Enums";
|
|
7
7
|
import Model from "./Model";
|
|
8
|
-
import {
|
|
8
|
+
import { AdaptorType, AxeFunction, GeneralFunction, HandlerFunction, ModelHooks, PhaseFunction, SerializationFunction } from "./Types";
|
|
9
9
|
import { ModelListService, QueryService } from "./Services";
|
|
10
10
|
import AxeRequest from "./Services/AxeRequest";
|
|
11
11
|
import AxeResponse from "./Services/AxeResponse";
|
|
12
12
|
import App from "./Services/App";
|
|
13
13
|
import { LoggerOptions } from "pino";
|
|
14
14
|
import { IncomingMessage } from "http";
|
|
15
|
+
import { ErrorHandleFunction } from "connect";
|
|
16
|
+
import { RedisClientOptions } from "redis";
|
|
17
|
+
import { ClientOptions } from "@elastic/elasticsearch";
|
|
15
18
|
export interface IColumn extends Column {
|
|
16
19
|
table_name: string;
|
|
17
20
|
}
|
|
18
21
|
export interface IConfig {
|
|
19
22
|
}
|
|
20
23
|
export interface IHandlerBasedTransactionConfig {
|
|
21
|
-
|
|
24
|
+
handlers: HandlerTypes[];
|
|
22
25
|
transaction: boolean;
|
|
23
26
|
}
|
|
27
|
+
export interface IHandlerBasedCacheConfig {
|
|
28
|
+
handlers: HandlerTypes[];
|
|
29
|
+
cache: ICacheConfiguration;
|
|
30
|
+
}
|
|
24
31
|
interface IHandlerBasedSerializer {
|
|
25
32
|
handler: HandlerTypes[];
|
|
26
33
|
serializer: ((data: any, request: AxeRequest) => void)[];
|
|
@@ -39,42 +46,53 @@ export interface IQueryConfig {
|
|
|
39
46
|
limits: Array<IQueryLimitConfig[]>;
|
|
40
47
|
defaults?: IQueryDefaultConfig;
|
|
41
48
|
}
|
|
42
|
-
export interface IRedisOptions {
|
|
43
|
-
host?: string;
|
|
44
|
-
port?: number;
|
|
45
|
-
password?: string;
|
|
46
|
-
db?: number;
|
|
47
|
-
}
|
|
48
|
-
export interface IRateLimitAdaptorConfig {
|
|
49
|
-
type: AdaptorTypes;
|
|
50
|
-
redis?: IRedisOptions;
|
|
51
|
-
}
|
|
52
49
|
export interface IRateLimitOptions {
|
|
53
50
|
maxRequests: number;
|
|
54
51
|
windowInSeconds: number;
|
|
55
52
|
}
|
|
56
53
|
export interface IRateLimitConfig extends IRateLimitOptions {
|
|
57
54
|
enabled: boolean;
|
|
58
|
-
adaptor:
|
|
55
|
+
adaptor: AdaptorType;
|
|
59
56
|
trustProxyIP: boolean;
|
|
60
57
|
keyGenerator?: (req: IncomingMessage) => string;
|
|
61
58
|
}
|
|
62
|
-
export interface
|
|
59
|
+
export interface AxeVersionConfig {
|
|
63
60
|
transaction: boolean | IHandlerBasedTransactionConfig | IHandlerBasedTransactionConfig[];
|
|
64
61
|
serializers: ((data: any, request: AxeRequest) => void)[] | IHandlerBasedSerializer[];
|
|
65
62
|
supportedLanguages: string[];
|
|
66
63
|
defaultLanguage: string;
|
|
67
64
|
query: IQueryConfig;
|
|
68
65
|
formidable: FormOptions;
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
cache: ICacheConfiguration | null;
|
|
67
|
+
}
|
|
68
|
+
export type IVersionConfig = Partial<AxeVersionConfig>;
|
|
69
|
+
export interface ICacheConfiguration {
|
|
70
|
+
enable?: boolean;
|
|
71
|
+
ttl?: number;
|
|
72
|
+
invalidation?: CacheStrategies;
|
|
73
|
+
cachePrefix?: string;
|
|
74
|
+
tagPrefix?: string;
|
|
75
|
+
responseHeader?: string | null;
|
|
76
|
+
cacheKey?: (req: AxeRequest) => string;
|
|
77
|
+
}
|
|
78
|
+
export interface ISearchConfigutation {
|
|
79
|
+
indexPrefix: string;
|
|
80
|
+
}
|
|
81
|
+
export interface AxeConfig extends IConfig {
|
|
71
82
|
env: string;
|
|
72
83
|
port: number;
|
|
73
84
|
prefix: string;
|
|
74
|
-
database:
|
|
85
|
+
database: Knex.Config;
|
|
75
86
|
pino: LoggerOptions;
|
|
76
87
|
rateLimit: IRateLimitConfig;
|
|
77
|
-
|
|
88
|
+
errorHandler: ErrorHandleFunction;
|
|
89
|
+
docs: boolean;
|
|
90
|
+
redis: RedisClientOptions | undefined;
|
|
91
|
+
cache: ICacheConfiguration;
|
|
92
|
+
elasticSearch: ClientOptions;
|
|
93
|
+
search: ISearchConfigutation;
|
|
94
|
+
}
|
|
95
|
+
export type IApplicationConfig = Partial<AxeConfig>;
|
|
78
96
|
export interface ILanguage {
|
|
79
97
|
title: string;
|
|
80
98
|
language: string;
|
|
@@ -84,7 +102,6 @@ export interface IAcceptedLanguage {
|
|
|
84
102
|
language: ILanguage;
|
|
85
103
|
quality: number;
|
|
86
104
|
}
|
|
87
|
-
export type IDatabaseConfig = Knex.Config;
|
|
88
105
|
export interface IVersionFolder {
|
|
89
106
|
root: string;
|
|
90
107
|
config: string;
|
|
@@ -96,7 +113,7 @@ export interface IVersionFolder {
|
|
|
96
113
|
}
|
|
97
114
|
export interface IVersion {
|
|
98
115
|
name: string;
|
|
99
|
-
config:
|
|
116
|
+
config: AxeVersionConfig;
|
|
100
117
|
folders: IVersionFolder;
|
|
101
118
|
modelList: ModelListService;
|
|
102
119
|
modelTree: IModelService[];
|
|
@@ -105,7 +122,7 @@ export interface IAPI {
|
|
|
105
122
|
rootFolder: string;
|
|
106
123
|
appFolder: string;
|
|
107
124
|
versions: IVersion[];
|
|
108
|
-
config:
|
|
125
|
+
config: AxeConfig;
|
|
109
126
|
}
|
|
110
127
|
export interface IGeneralHooks {
|
|
111
128
|
onBeforeInit: (app: App) => void | null;
|
|
@@ -113,16 +130,12 @@ export interface IGeneralHooks {
|
|
|
113
130
|
}
|
|
114
131
|
export interface IHandlerBaseMiddleware {
|
|
115
132
|
handler: HandlerTypes[];
|
|
116
|
-
middleware:
|
|
117
|
-
}
|
|
118
|
-
export interface IMethodBaseConfig {
|
|
119
|
-
[HttpMethods.POST]?: string[];
|
|
120
|
-
[HttpMethods.PUT]?: string[];
|
|
121
|
-
[HttpMethods.PATCH]?: string[];
|
|
133
|
+
middleware: AxeFunction;
|
|
122
134
|
}
|
|
123
|
-
export interface
|
|
124
|
-
[HttpMethods.POST]?:
|
|
125
|
-
[HttpMethods.PUT]?:
|
|
135
|
+
export interface IMethodBaseConfig<T> {
|
|
136
|
+
[HttpMethods.POST]?: T;
|
|
137
|
+
[HttpMethods.PUT]?: T;
|
|
138
|
+
[HttpMethods.PATCH]?: T;
|
|
126
139
|
}
|
|
127
140
|
export interface IModelService {
|
|
128
141
|
name: string;
|
|
@@ -130,8 +143,8 @@ export interface IModelService {
|
|
|
130
143
|
relations: IRelation[];
|
|
131
144
|
columns: IColumn[];
|
|
132
145
|
columnNames: string[];
|
|
133
|
-
hooks:
|
|
134
|
-
events:
|
|
146
|
+
hooks: ModelHooks;
|
|
147
|
+
events: ModelHooks;
|
|
135
148
|
isRecursive: boolean;
|
|
136
149
|
children: IModelService[];
|
|
137
150
|
queryLimits: IQueryLimitConfig[];
|
|
@@ -140,6 +153,9 @@ export interface IModelService {
|
|
|
140
153
|
setExtensions(type: Extensions, hookFunctionType: HookFunctionTypes, data: PhaseFunction): void;
|
|
141
154
|
setQueryLimits(limits: IQueryLimitConfig[]): void;
|
|
142
155
|
setSerialization(callback: SerializationFunction): void;
|
|
156
|
+
setCacheConfiguration(handler: string, cache: ICacheConfiguration): void;
|
|
157
|
+
getCacheConfiguration(handler: HandlerTypes): ICacheConfiguration | null;
|
|
158
|
+
setAsRecursive(): void;
|
|
143
159
|
}
|
|
144
160
|
export interface IRelation {
|
|
145
161
|
type: Relationships;
|
|
@@ -155,11 +171,12 @@ export interface IRouteData {
|
|
|
155
171
|
parentModel: IModelService | null;
|
|
156
172
|
relation: IRelation | null;
|
|
157
173
|
}
|
|
158
|
-
export interface
|
|
174
|
+
export interface IContext extends IRouteData {
|
|
159
175
|
api: IAPI;
|
|
160
176
|
req: AxeRequest;
|
|
161
177
|
res: AxeResponse;
|
|
162
178
|
database: Knex | Knex.Transaction;
|
|
179
|
+
isTransactionOpen: boolean;
|
|
163
180
|
queryParser?: QueryService;
|
|
164
181
|
conditions?: IQuery;
|
|
165
182
|
query?: Knex.QueryBuilder;
|
|
@@ -171,6 +188,8 @@ export interface IRequestPack extends IRouteData {
|
|
|
171
188
|
export interface IRouteDocumentation {
|
|
172
189
|
version: string;
|
|
173
190
|
handler: string;
|
|
191
|
+
modelService: IModelService;
|
|
192
|
+
parentModel: IModelService | null;
|
|
174
193
|
model: string;
|
|
175
194
|
table: string;
|
|
176
195
|
columns: IColumn[];
|
|
@@ -183,6 +202,10 @@ export interface IRouteDocumentation {
|
|
|
183
202
|
queryLimits: IQueryLimitConfig[];
|
|
184
203
|
queryDefaults: IQueryDefaultConfig;
|
|
185
204
|
}
|
|
205
|
+
export interface ICustomRouteDocumentation {
|
|
206
|
+
method: HttpMethods;
|
|
207
|
+
url: string;
|
|
208
|
+
}
|
|
186
209
|
export interface IRawQuery {
|
|
187
210
|
q: string | null;
|
|
188
211
|
page: string | null;
|
|
@@ -210,6 +233,7 @@ export interface IQuery {
|
|
|
210
233
|
fields: string[];
|
|
211
234
|
with: IWith[];
|
|
212
235
|
trashed: boolean;
|
|
236
|
+
text: string | null;
|
|
213
237
|
}
|
|
214
238
|
export interface IWhere {
|
|
215
239
|
prefix: string | null;
|
|
@@ -236,7 +260,51 @@ export interface AxeRequestResponsePair {
|
|
|
236
260
|
axeResponse: AxeResponse;
|
|
237
261
|
}
|
|
238
262
|
export interface MiddlewareResolution {
|
|
239
|
-
middlewares:
|
|
263
|
+
middlewares: GeneralFunction[];
|
|
240
264
|
handler: HandlerFunction;
|
|
241
265
|
}
|
|
266
|
+
export interface IStepDefinition {
|
|
267
|
+
name: string;
|
|
268
|
+
get(model: IModelService): PhaseFunction;
|
|
269
|
+
isAsync(): boolean;
|
|
270
|
+
}
|
|
271
|
+
export interface ICacheAdaptor {
|
|
272
|
+
get(key: string): Promise<string | null>;
|
|
273
|
+
set(key: string, value: string, ttl: number): Promise<void>;
|
|
274
|
+
decr(key: string, ttl: number): Promise<void>;
|
|
275
|
+
}
|
|
276
|
+
export interface IRateLimitResponse {
|
|
277
|
+
success: boolean;
|
|
278
|
+
limit: number;
|
|
279
|
+
remaining: number;
|
|
280
|
+
}
|
|
281
|
+
export interface IURLPair {
|
|
282
|
+
method: string;
|
|
283
|
+
pattern: string;
|
|
284
|
+
data: IRouteData;
|
|
285
|
+
phases: IPhaseDefinition[];
|
|
286
|
+
hasTransaction: boolean;
|
|
287
|
+
params?: any;
|
|
288
|
+
parentPairs: IRouteParentPair[];
|
|
289
|
+
customHandler?: HandlerFunction;
|
|
290
|
+
}
|
|
291
|
+
export interface BulkTask<DataType, ReturnType> {
|
|
292
|
+
data: DataType;
|
|
293
|
+
promise: Promise<ReturnType>;
|
|
294
|
+
}
|
|
295
|
+
export interface IForeignKeyTask {
|
|
296
|
+
relation: IRelation;
|
|
297
|
+
value: any;
|
|
298
|
+
}
|
|
299
|
+
export interface IRouteParentPair {
|
|
300
|
+
model: IModelService;
|
|
301
|
+
paramName: string;
|
|
302
|
+
}
|
|
303
|
+
export interface IElasticSearchParameters {
|
|
304
|
+
req: AxeRequest;
|
|
305
|
+
model: IModelService;
|
|
306
|
+
relation: IRelation | null;
|
|
307
|
+
parentModel: IModelService | null;
|
|
308
|
+
text: string;
|
|
309
|
+
}
|
|
242
310
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AdaptorType } from "../../Types";
|
|
2
2
|
import RedisAdaptor from "./RedisAdaptor";
|
|
3
3
|
import MemoryAdaptor from "./MemoryAdaptor";
|
|
4
|
-
import {
|
|
5
|
-
declare const _default: (adaptor:
|
|
4
|
+
import { RedisClientOptions } from "redis";
|
|
5
|
+
declare const _default: (adaptor: AdaptorType, redisOptions: RedisClientOptions | undefined, prefix: string) => RedisAdaptor | MemoryAdaptor;
|
|
6
6
|
export default _default;
|
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { createClient } from "redis";
|
|
2
|
+
import { ICacheAdaptor } from "../../Interfaces";
|
|
3
|
+
type RedisClientOptions = Parameters<typeof createClient>[0];
|
|
4
|
+
declare class RedisAdaptor implements ICacheAdaptor {
|
|
4
5
|
private client;
|
|
5
6
|
private prefix;
|
|
6
|
-
constructor(options:
|
|
7
|
+
constructor(options: RedisClientOptions | undefined, prefix: string);
|
|
7
8
|
get(key: string): Promise<string | null>;
|
|
8
9
|
set(key: string, value: string, ttl: number): Promise<void>;
|
|
10
|
+
tags(keys: string[], value: string): Promise<void>;
|
|
11
|
+
getTagMemebers(tag: string): Promise<string[]>;
|
|
12
|
+
delete(keys: string[]): Promise<number>;
|
|
9
13
|
decr(key: string): Promise<void>;
|
|
14
|
+
searchTags(pattern: string): Promise<{
|
|
15
|
+
cursor: number;
|
|
16
|
+
keys: string[];
|
|
17
|
+
}>;
|
|
10
18
|
}
|
|
11
19
|
export default RedisAdaptor;
|
|
@@ -26,10 +26,34 @@ class RedisAdaptor {
|
|
|
26
26
|
yield this.client.setEx(`${this.prefix}${key}`, ttl, value);
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
+
tags(keys, value) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
keys.forEach((key) => {
|
|
32
|
+
this.client.sAdd(key, [value]);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
getTagMemebers(tag) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
return yield this.client.sMembers(tag);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
delete(keys) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
return yield this.client.del(keys);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
29
46
|
decr(key) {
|
|
30
47
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
48
|
yield this.client.decr(`${this.prefix}${key}`);
|
|
32
49
|
});
|
|
33
50
|
}
|
|
51
|
+
searchTags(pattern) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
return yield this.client.scan(0, {
|
|
54
|
+
MATCH: pattern,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
34
58
|
}
|
|
35
59
|
exports.default = RedisAdaptor;
|
|
@@ -1,6 +1,25 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="express" />
|
|
1
3
|
import { IncomingMessage, ServerResponse } from "http";
|
|
2
|
-
import {
|
|
3
|
-
export declare const setupRateLimitAdaptors: (config:
|
|
4
|
-
|
|
4
|
+
import { AxeConfig, IRateLimitOptions, IContext } from "../../Interfaces";
|
|
5
|
+
export declare const setupRateLimitAdaptors: (config: AxeConfig) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Add a rate limit with the `IRateLimitOptions`
|
|
8
|
+
*
|
|
9
|
+
* @param options
|
|
10
|
+
* @returns
|
|
11
|
+
* @example
|
|
12
|
+
* class User extends Model {
|
|
13
|
+
* get middlewares() {
|
|
14
|
+
* return [
|
|
15
|
+
* {
|
|
16
|
+
* handler: [HandlerTypes.INSERT],
|
|
17
|
+
* middleware: rateLimit({ maxRequests: 200, windowInSeconds: 5 }),
|
|
18
|
+
* },
|
|
19
|
+
* ];
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
23
|
+
export declare const rateLimit: (options?: IRateLimitOptions) => (context: IContext) => Promise<void>;
|
|
5
24
|
declare const _default: (req: IncomingMessage, res: ServerResponse, next: any) => Promise<any>;
|
|
6
25
|
export default _default;
|
|
@@ -16,6 +16,7 @@ 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");
|
|
19
|
+
const Enums_1 = require("../../Enums");
|
|
19
20
|
let adaptor;
|
|
20
21
|
const checkRateLimit = function (clientKey, options) {
|
|
21
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -64,11 +65,28 @@ const getClientKeyByConfigurations = (req, config) => {
|
|
|
64
65
|
return `axe-api-rate-limit:${req.socket.remoteAddress || ""}`;
|
|
65
66
|
};
|
|
66
67
|
const setupRateLimitAdaptors = (config) => {
|
|
67
|
-
var _a
|
|
68
|
+
var _a;
|
|
68
69
|
// Creating the correct adaptor by the configuration
|
|
69
|
-
adaptor = (0, AdaptorFactory_1.default)(((_a = config.rateLimit) === null || _a === void 0 ? void 0 : _a.adaptor
|
|
70
|
+
adaptor = (0, AdaptorFactory_1.default)(((_a = config.rateLimit) === null || _a === void 0 ? void 0 : _a.adaptor) || "memory", config.redis, "");
|
|
70
71
|
};
|
|
71
72
|
exports.setupRateLimitAdaptors = setupRateLimitAdaptors;
|
|
73
|
+
/**
|
|
74
|
+
* Add a rate limit with the `IRateLimitOptions`
|
|
75
|
+
*
|
|
76
|
+
* @param options
|
|
77
|
+
* @returns
|
|
78
|
+
* @example
|
|
79
|
+
* class User extends Model {
|
|
80
|
+
* get middlewares() {
|
|
81
|
+
* return [
|
|
82
|
+
* {
|
|
83
|
+
* handler: [HandlerTypes.INSERT],
|
|
84
|
+
* middleware: rateLimit({ maxRequests: 200, windowInSeconds: 5 }),
|
|
85
|
+
* },
|
|
86
|
+
* ];
|
|
87
|
+
* }
|
|
88
|
+
* }
|
|
89
|
+
*/
|
|
72
90
|
const rateLimit = (options) => {
|
|
73
91
|
// For each model middleware, we should use a different ID for the cache key
|
|
74
92
|
const id = (0, nanoid_1.nanoid)();
|
|
@@ -88,7 +106,9 @@ const rateLimit = (options) => {
|
|
|
88
106
|
// Sending an error message if there is an error
|
|
89
107
|
if (isAllowed.success === false) {
|
|
90
108
|
Services_1.LogService.warn(`Rate limit exceeded: ${context.req.url}`);
|
|
91
|
-
context.res
|
|
109
|
+
context.res
|
|
110
|
+
.status(Enums_1.StatusCodes.TOO_MANY_REQUESTS)
|
|
111
|
+
.json({ error: "Rate limit exceeded." });
|
|
92
112
|
}
|
|
93
113
|
});
|
|
94
114
|
};
|