tsledge 0.1.6 → 0.1.9
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/LICENSE +1 -1
- package/dist/fluent-interface/fluent-pattern-handler.d.ts +13 -13
- package/dist/fluent-interface/fluent-pattern-handler.d.ts.map +1 -1
- package/dist/fluent-interface/types.d.ts +8 -2
- package/dist/fluent-interface/types.d.ts.map +1 -1
- package/dist/index.js +26 -20
- package/dist/src/index.js +54 -30
- package/dist/tests/main.js +66 -33
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PromiseDefaultCodec
|
|
1
|
+
import { FluentAPIOption, FluentMiddleware, FluentExecParams } from './types';
|
|
2
|
+
import { PromiseDefaultCodec } from '../core/index';
|
|
3
3
|
export declare class FluentPatternHandler {
|
|
4
4
|
private static _singleton;
|
|
5
|
-
private
|
|
5
|
+
private _options;
|
|
6
|
+
private _execMiddlewareFunctions;
|
|
6
7
|
/**
|
|
7
8
|
* Constructor for QueryPatternExecutor.
|
|
8
|
-
* @param
|
|
9
|
+
* @param options - Array of query pattern options for filtering.
|
|
9
10
|
*/
|
|
10
|
-
constructor(
|
|
11
|
+
constructor(options?: FluentAPIOption[], execMiddleware?: FluentMiddleware[]);
|
|
11
12
|
/**
|
|
12
|
-
* Initializes the singleton instance of FluentPatternHandler with the provided
|
|
13
|
-
* @param
|
|
13
|
+
* Initializes the singleton instance of FluentPatternHandler with the provided options.
|
|
14
|
+
* @param options - Array of query pattern options for filtering.
|
|
14
15
|
* @returns Singleton instance of FluentPatternHandler.
|
|
15
16
|
*/
|
|
16
|
-
static init(
|
|
17
|
+
static init(options?: FluentAPIOption[], execMiddleware?: FluentMiddleware[]): FluentPatternHandler;
|
|
17
18
|
/**
|
|
18
19
|
* Returns the singleton instance of FluentPatternHandler.
|
|
19
20
|
* @returns Singleton instance of FluentPatternHandler.
|
|
@@ -33,7 +34,7 @@ export declare class FluentPatternHandler {
|
|
|
33
34
|
*/
|
|
34
35
|
private _applyParameters;
|
|
35
36
|
/**
|
|
36
|
-
* Retrieves filter fields for the given model from the
|
|
37
|
+
* Retrieves filter fields for the given model from the options configuration.
|
|
37
38
|
* @param model - The Mongoose model.
|
|
38
39
|
* @returns Array of filter fields.
|
|
39
40
|
*/
|
|
@@ -45,11 +46,10 @@ export declare class FluentPatternHandler {
|
|
|
45
46
|
*/
|
|
46
47
|
private _buildExecutionConfig;
|
|
47
48
|
/**
|
|
48
|
-
* Executes the query builder with
|
|
49
|
-
* @param
|
|
50
|
-
* @param queryBuilder
|
|
49
|
+
* Executes the query builder with applied filters and returns the result.
|
|
50
|
+
* @param params Execution parameters including the query builder and request query.
|
|
51
51
|
* @returns
|
|
52
52
|
*/
|
|
53
|
-
exec<T = any>(
|
|
53
|
+
exec<T = any>(params: FluentExecParams): PromiseDefaultCodec;
|
|
54
54
|
}
|
|
55
55
|
//# sourceMappingURL=fluent-pattern-handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluent-pattern-handler.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/fluent-pattern-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,
|
|
1
|
+
{"version":3,"file":"fluent-pattern-handler.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/fluent-pattern-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,eAAe,EAEf,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AACjB,OAAO,EAA8B,mBAAmB,EAAgB,MAAM,eAAe,CAAC;AAY9F,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,MAAM,CAAC,UAAU,CAAuB;IAChD,OAAO,CAAC,QAAQ,CAAoB;IACpC,OAAO,CAAC,wBAAwB,CAA0B;IAE1D;;;OAGG;gBACS,OAAO,GAAE,eAAe,EAAO,EAAE,cAAc,GAAE,gBAAgB,EAAO;IAWpF;;;;OAIG;WACW,IAAI,CAChB,OAAO,GAAE,eAAe,EAAO,EAC/B,cAAc,GAAE,gBAAgB,EAAO,GACtC,oBAAoB;IAQvB;;;OAGG;WACW,WAAW,IAAI,oBAAoB;IASjD;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAsChC;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAoCxB;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAYhC;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;IAa/B;;;;OAIG;IACY,IAAI,CAAC,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,gBAAgB,GAAG,mBAAmB;CAgB1E"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
2
|
import { Request, Response } from 'express';
|
|
3
|
-
import { DefaultResponseBody, KeysEnum } from '../core/index';
|
|
3
|
+
import { DefaultResponseBody, KeysEnum, QueryBuilder } from '../core/index';
|
|
4
4
|
export interface FluentRequestParams extends Record<string, string> {
|
|
5
5
|
collection: string;
|
|
6
6
|
}
|
|
@@ -19,10 +19,16 @@ export interface FluentRequestQuery {
|
|
|
19
19
|
* Attributes of FluentRequestQuery used for validation and parsing.
|
|
20
20
|
*/
|
|
21
21
|
export declare const fluentRequestQueryAttributes: KeysEnum<FluentRequestQuery>;
|
|
22
|
-
export interface
|
|
22
|
+
export interface FluentAPIOption<T = any> {
|
|
23
23
|
model: mongoose.Model<T>;
|
|
24
24
|
filters?: string[];
|
|
25
25
|
}
|
|
26
26
|
export type FluentExpressRequest = Request<FluentRequestParams, DefaultResponseBody, FluentRequestBody, FluentRequestQuery>;
|
|
27
27
|
export type FluentExpressResponse = Response<DefaultResponseBody>;
|
|
28
|
+
export type FluentExecParams = {
|
|
29
|
+
req: FluentExpressRequest;
|
|
30
|
+
res: FluentExpressResponse;
|
|
31
|
+
queryBuilder: QueryBuilder;
|
|
32
|
+
};
|
|
33
|
+
export type FluentMiddleware = (params: FluentExecParams) => void;
|
|
28
34
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/types.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/types.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE5E,MAAM,WAAW,mBAAoB,SAAQ,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IACjE,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB;CAAG;AAErC,MAAM,WAAW,kBAAkB;IACjC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,eAAO,MAAM,4BAA4B,EAAE,QAAQ,CAAC,kBAAkB,CAQrE,CAAC;AAEF,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,GAAG;IAEtC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAEzB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,MAAM,MAAM,oBAAoB,GAAG,OAAO,CACxC,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,CACnB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAC;AAElE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,GAAG,EAAE,oBAAoB,CAAC;IAC1B,GAAG,EAAE,qBAAqB,CAAC;IAC3B,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,gBAAgB,KAAK,IAAI,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -808,27 +808,29 @@ async function httpRequest(config) {
|
|
|
808
808
|
var FluentPatternHandler = class _FluentPatternHandler {
|
|
809
809
|
/**
|
|
810
810
|
* Constructor for QueryPatternExecutor.
|
|
811
|
-
* @param
|
|
811
|
+
* @param options - Array of query pattern options for filtering.
|
|
812
812
|
*/
|
|
813
|
-
constructor(
|
|
813
|
+
constructor(options = [], execMiddleware = []) {
|
|
814
|
+
this._execMiddlewareFunctions = [];
|
|
814
815
|
if (_FluentPatternHandler._singleton) {
|
|
815
816
|
throw new Error(
|
|
816
817
|
"FluentPatternHandler is a singleton class. Use FluentPatternHandler.getInstance() to access the instance."
|
|
817
818
|
);
|
|
818
819
|
}
|
|
819
|
-
this.
|
|
820
|
+
this._options = options;
|
|
821
|
+
this._execMiddlewareFunctions = execMiddleware;
|
|
820
822
|
_FluentPatternHandler._singleton = this;
|
|
821
823
|
}
|
|
822
824
|
/**
|
|
823
|
-
* Initializes the singleton instance of FluentPatternHandler with the provided
|
|
824
|
-
* @param
|
|
825
|
+
* Initializes the singleton instance of FluentPatternHandler with the provided options.
|
|
826
|
+
* @param options - Array of query pattern options for filtering.
|
|
825
827
|
* @returns Singleton instance of FluentPatternHandler.
|
|
826
828
|
*/
|
|
827
|
-
static init(
|
|
829
|
+
static init(options = [], execMiddleware = []) {
|
|
828
830
|
if (_FluentPatternHandler._singleton != void 0) {
|
|
829
831
|
throw new Error("FluentPatternHandler is already initialized");
|
|
830
832
|
}
|
|
831
|
-
_FluentPatternHandler._singleton = new _FluentPatternHandler(
|
|
833
|
+
_FluentPatternHandler._singleton = new _FluentPatternHandler(options, execMiddleware);
|
|
832
834
|
return _FluentPatternHandler._singleton;
|
|
833
835
|
}
|
|
834
836
|
/**
|
|
@@ -919,15 +921,15 @@ var FluentPatternHandler = class _FluentPatternHandler {
|
|
|
919
921
|
}
|
|
920
922
|
}
|
|
921
923
|
/**
|
|
922
|
-
* Retrieves filter fields for the given model from the
|
|
924
|
+
* Retrieves filter fields for the given model from the options configuration.
|
|
923
925
|
* @param model - The Mongoose model.
|
|
924
926
|
* @returns Array of filter fields.
|
|
925
927
|
*/
|
|
926
928
|
_getFilterFieldsForModel(model) {
|
|
927
|
-
for (const
|
|
928
|
-
if (
|
|
929
|
-
if (
|
|
930
|
-
return
|
|
929
|
+
for (const option of this._options) {
|
|
930
|
+
if (option.model.collection.name === model.collection.name) {
|
|
931
|
+
if (option.filters) {
|
|
932
|
+
return option.filters;
|
|
931
933
|
}
|
|
932
934
|
return [];
|
|
933
935
|
}
|
|
@@ -948,17 +950,21 @@ var FluentPatternHandler = class _FluentPatternHandler {
|
|
|
948
950
|
};
|
|
949
951
|
}
|
|
950
952
|
/**
|
|
951
|
-
* Executes the query builder with
|
|
952
|
-
* @param
|
|
953
|
-
* @
|
|
954
|
-
* @returns
|
|
953
|
+
* Executes the query builder with applied filters and returns the result.
|
|
954
|
+
* @param params Execution parameters including the query builder and request query.
|
|
955
|
+
* @returns
|
|
955
956
|
*/
|
|
956
|
-
async exec(
|
|
957
|
+
async exec(params) {
|
|
957
958
|
try {
|
|
958
|
-
|
|
959
|
-
|
|
959
|
+
if (this._execMiddlewareFunctions && this._execMiddlewareFunctions.length > 0) {
|
|
960
|
+
this._execMiddlewareFunctions.forEach((func) => {
|
|
961
|
+
func(params);
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
const queryParams = this._parseFluentRequestQuery(params.req.query);
|
|
965
|
+
this._applyParameters(params.queryBuilder, queryParams);
|
|
960
966
|
const execConfig = this._buildExecutionConfig(queryParams);
|
|
961
|
-
return await queryBuilder.exec(execConfig);
|
|
967
|
+
return await params.queryBuilder.exec(execConfig);
|
|
962
968
|
} catch (err) {
|
|
963
969
|
console.error("[ERROR - QueryPatternExecutor]", err);
|
|
964
970
|
return new Codec({ data: [], meta: { total: 0 } }, 500);
|
package/dist/src/index.js
CHANGED
|
@@ -270,8 +270,8 @@ async function validateJwt(req, res, next, jwtSecret) {
|
|
|
270
270
|
console.log("[WARN] JWT token for blocked user");
|
|
271
271
|
return res.sendStatus(FORBIDDEN);
|
|
272
272
|
}
|
|
273
|
-
|
|
274
|
-
|
|
273
|
+
res.locals.user = result.payload;
|
|
274
|
+
res.locals.token = token;
|
|
275
275
|
next();
|
|
276
276
|
} catch (err) {
|
|
277
277
|
console.log("[ERROR] JWT validation error:", err);
|
|
@@ -335,7 +335,7 @@ async function generateCredentials(auth) {
|
|
|
335
335
|
appUser
|
|
336
336
|
};
|
|
337
337
|
}
|
|
338
|
-
async function
|
|
338
|
+
async function authRegister(req, res, next) {
|
|
339
339
|
let { identifier = void 0, secret = void 0 } = req.body || {};
|
|
340
340
|
if (!identifier || !secret) {
|
|
341
341
|
return res.sendStatus(FORBIDDEN2);
|
|
@@ -345,14 +345,13 @@ async function register(req, res, next) {
|
|
|
345
345
|
if (user) {
|
|
346
346
|
return res.sendStatus(BAD_REQUEST);
|
|
347
347
|
}
|
|
348
|
-
|
|
348
|
+
res.locals.authUser = new AuthUser({
|
|
349
349
|
identifier,
|
|
350
350
|
secretHash: await bcrypt.hash(secret, 10)
|
|
351
351
|
});
|
|
352
|
-
authUser.save();
|
|
353
352
|
next();
|
|
354
353
|
}
|
|
355
|
-
async function
|
|
354
|
+
async function authLogin(req, res, next) {
|
|
356
355
|
let { identifier = void 0, secret = void 0 } = req.body || {};
|
|
357
356
|
if (!identifier || !secret) {
|
|
358
357
|
return res.sendStatus(FORBIDDEN2);
|
|
@@ -376,9 +375,9 @@ async function login(req, res, next) {
|
|
|
376
375
|
res.locals.credentials = credentials;
|
|
377
376
|
next();
|
|
378
377
|
}
|
|
379
|
-
async function
|
|
378
|
+
async function authLogout(req, res, next) {
|
|
380
379
|
await jwtRefreshRequired(req, res, async () => {
|
|
381
|
-
const refreshToken =
|
|
380
|
+
const refreshToken = res.locals.token;
|
|
382
381
|
if (!refreshToken) {
|
|
383
382
|
return res.sendStatus(BAD_REQUEST);
|
|
384
383
|
}
|
|
@@ -404,8 +403,10 @@ async function logout(req, res, next) {
|
|
|
404
403
|
next();
|
|
405
404
|
});
|
|
406
405
|
}
|
|
407
|
-
async function
|
|
408
|
-
|
|
406
|
+
async function authRefresh(req, res, next) {
|
|
407
|
+
await jwtRefreshRequired(req, res, async () => {
|
|
408
|
+
});
|
|
409
|
+
const refreshToken = res.locals.token;
|
|
409
410
|
if (!refreshToken) {
|
|
410
411
|
return res.sendStatus(BAD_REQUEST);
|
|
411
412
|
}
|
|
@@ -436,6 +437,7 @@ async function refreshJWT(req, res, next) {
|
|
|
436
437
|
if (!credentials) {
|
|
437
438
|
return res.sendStatus(BAD_REQUEST);
|
|
438
439
|
}
|
|
440
|
+
res.locals.credentials = credentials;
|
|
439
441
|
next();
|
|
440
442
|
} catch (err) {
|
|
441
443
|
console.log("[WARN] refreshing JWT:", err);
|
|
@@ -806,17 +808,35 @@ async function httpRequest(config) {
|
|
|
806
808
|
var FluentPatternHandler = class _FluentPatternHandler {
|
|
807
809
|
/**
|
|
808
810
|
* Constructor for QueryPatternExecutor.
|
|
809
|
-
* @param
|
|
811
|
+
* @param options - Array of query pattern options for filtering.
|
|
810
812
|
*/
|
|
811
|
-
constructor(
|
|
813
|
+
constructor(options = [], execMiddleware = []) {
|
|
814
|
+
this._execMiddlewareFunctions = [];
|
|
812
815
|
if (_FluentPatternHandler._singleton) {
|
|
813
816
|
throw new Error(
|
|
814
817
|
"FluentPatternHandler is a singleton class. Use FluentPatternHandler.getInstance() to access the instance."
|
|
815
818
|
);
|
|
816
819
|
}
|
|
817
|
-
this.
|
|
820
|
+
this._options = options;
|
|
821
|
+
this._execMiddlewareFunctions = execMiddleware;
|
|
818
822
|
_FluentPatternHandler._singleton = this;
|
|
819
823
|
}
|
|
824
|
+
/**
|
|
825
|
+
* Initializes the singleton instance of FluentPatternHandler with the provided options.
|
|
826
|
+
* @param options - Array of query pattern options for filtering.
|
|
827
|
+
* @returns Singleton instance of FluentPatternHandler.
|
|
828
|
+
*/
|
|
829
|
+
static init(options = [], execMiddleware = []) {
|
|
830
|
+
if (_FluentPatternHandler._singleton != void 0) {
|
|
831
|
+
throw new Error("FluentPatternHandler is already initialized");
|
|
832
|
+
}
|
|
833
|
+
_FluentPatternHandler._singleton = new _FluentPatternHandler(options, execMiddleware);
|
|
834
|
+
return _FluentPatternHandler._singleton;
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Returns the singleton instance of FluentPatternHandler.
|
|
838
|
+
* @returns Singleton instance of FluentPatternHandler.
|
|
839
|
+
*/
|
|
820
840
|
static getInstance() {
|
|
821
841
|
if (_FluentPatternHandler._singleton == void 0) {
|
|
822
842
|
throw new Error(
|
|
@@ -901,15 +921,15 @@ var FluentPatternHandler = class _FluentPatternHandler {
|
|
|
901
921
|
}
|
|
902
922
|
}
|
|
903
923
|
/**
|
|
904
|
-
* Retrieves filter fields for the given model from the
|
|
924
|
+
* Retrieves filter fields for the given model from the options configuration.
|
|
905
925
|
* @param model - The Mongoose model.
|
|
906
926
|
* @returns Array of filter fields.
|
|
907
927
|
*/
|
|
908
928
|
_getFilterFieldsForModel(model) {
|
|
909
|
-
for (const
|
|
910
|
-
if (
|
|
911
|
-
if (
|
|
912
|
-
return
|
|
929
|
+
for (const option of this._options) {
|
|
930
|
+
if (option.model.collection.name === model.collection.name) {
|
|
931
|
+
if (option.filters) {
|
|
932
|
+
return option.filters;
|
|
913
933
|
}
|
|
914
934
|
return [];
|
|
915
935
|
}
|
|
@@ -930,17 +950,21 @@ var FluentPatternHandler = class _FluentPatternHandler {
|
|
|
930
950
|
};
|
|
931
951
|
}
|
|
932
952
|
/**
|
|
933
|
-
* Executes the query builder with
|
|
934
|
-
* @param
|
|
935
|
-
* @
|
|
936
|
-
* @returns
|
|
953
|
+
* Executes the query builder with applied filters and returns the result.
|
|
954
|
+
* @param params Execution parameters including the query builder and request query.
|
|
955
|
+
* @returns
|
|
937
956
|
*/
|
|
938
|
-
async exec(
|
|
957
|
+
async exec(params) {
|
|
939
958
|
try {
|
|
940
|
-
|
|
941
|
-
|
|
959
|
+
if (this._execMiddlewareFunctions && this._execMiddlewareFunctions.length > 0) {
|
|
960
|
+
this._execMiddlewareFunctions.forEach((func) => {
|
|
961
|
+
func(params);
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
const queryParams = this._parseFluentRequestQuery(params.req.query);
|
|
965
|
+
this._applyParameters(params.queryBuilder, queryParams);
|
|
942
966
|
const execConfig = this._buildExecutionConfig(queryParams);
|
|
943
|
-
return await queryBuilder.exec(execConfig);
|
|
967
|
+
return await params.queryBuilder.exec(execConfig);
|
|
944
968
|
} catch (err) {
|
|
945
969
|
console.error("[ERROR - QueryPatternExecutor]", err);
|
|
946
970
|
return new Codec({ data: [], meta: { total: 0 } }, 500);
|
|
@@ -973,6 +997,10 @@ export {
|
|
|
973
997
|
JwtSecret,
|
|
974
998
|
QueryBuilder,
|
|
975
999
|
TokenBlocklist,
|
|
1000
|
+
authLogin,
|
|
1001
|
+
authLogout,
|
|
1002
|
+
authRefresh,
|
|
1003
|
+
authRegister,
|
|
976
1004
|
connectMongoDB,
|
|
977
1005
|
createApp,
|
|
978
1006
|
diskFileUpload,
|
|
@@ -987,12 +1015,8 @@ export {
|
|
|
987
1015
|
isNonEmptyObjectOrArray,
|
|
988
1016
|
jwtRefreshRequired,
|
|
989
1017
|
jwtRequired,
|
|
990
|
-
login,
|
|
991
|
-
logout,
|
|
992
1018
|
memoryFileUpload,
|
|
993
1019
|
mergeCollectionRelations,
|
|
994
|
-
refreshJWT,
|
|
995
|
-
register,
|
|
996
1020
|
requestLogger,
|
|
997
1021
|
socketToken,
|
|
998
1022
|
validateString,
|
package/dist/tests/main.js
CHANGED
|
@@ -195,8 +195,8 @@ async function validateJwt(req, res, next, jwtSecret) {
|
|
|
195
195
|
console.log("[WARN] JWT token for blocked user");
|
|
196
196
|
return res.sendStatus(FORBIDDEN);
|
|
197
197
|
}
|
|
198
|
-
|
|
199
|
-
|
|
198
|
+
res.locals.user = result.payload;
|
|
199
|
+
res.locals.token = token;
|
|
200
200
|
next();
|
|
201
201
|
} catch (err) {
|
|
202
202
|
console.log("[ERROR] JWT validation error:", err);
|
|
@@ -239,7 +239,7 @@ async function generateCredentials(auth) {
|
|
|
239
239
|
appUser
|
|
240
240
|
};
|
|
241
241
|
}
|
|
242
|
-
async function
|
|
242
|
+
async function authRegister(req, res, next) {
|
|
243
243
|
let { identifier = void 0, secret = void 0 } = req.body || {};
|
|
244
244
|
if (!identifier || !secret) {
|
|
245
245
|
return res.sendStatus(FORBIDDEN2);
|
|
@@ -249,14 +249,13 @@ async function register(req, res, next) {
|
|
|
249
249
|
if (user) {
|
|
250
250
|
return res.sendStatus(BAD_REQUEST);
|
|
251
251
|
}
|
|
252
|
-
|
|
252
|
+
res.locals.authUser = new AuthUser({
|
|
253
253
|
identifier,
|
|
254
254
|
secretHash: await bcrypt.hash(secret, 10)
|
|
255
255
|
});
|
|
256
|
-
authUser.save();
|
|
257
256
|
next();
|
|
258
257
|
}
|
|
259
|
-
async function
|
|
258
|
+
async function authLogin(req, res, next) {
|
|
260
259
|
let { identifier = void 0, secret = void 0 } = req.body || {};
|
|
261
260
|
if (!identifier || !secret) {
|
|
262
261
|
return res.sendStatus(FORBIDDEN2);
|
|
@@ -280,9 +279,9 @@ async function login(req, res, next) {
|
|
|
280
279
|
res.locals.credentials = credentials;
|
|
281
280
|
next();
|
|
282
281
|
}
|
|
283
|
-
async function
|
|
282
|
+
async function authLogout(req, res, next) {
|
|
284
283
|
await jwtRefreshRequired(req, res, async () => {
|
|
285
|
-
const refreshToken =
|
|
284
|
+
const refreshToken = res.locals.token;
|
|
286
285
|
if (!refreshToken) {
|
|
287
286
|
return res.sendStatus(BAD_REQUEST);
|
|
288
287
|
}
|
|
@@ -308,8 +307,10 @@ async function logout(req, res, next) {
|
|
|
308
307
|
next();
|
|
309
308
|
});
|
|
310
309
|
}
|
|
311
|
-
async function
|
|
312
|
-
|
|
310
|
+
async function authRefresh(req, res, next) {
|
|
311
|
+
await jwtRefreshRequired(req, res, async () => {
|
|
312
|
+
});
|
|
313
|
+
const refreshToken = res.locals.token;
|
|
313
314
|
if (!refreshToken) {
|
|
314
315
|
return res.sendStatus(BAD_REQUEST);
|
|
315
316
|
}
|
|
@@ -340,6 +341,7 @@ async function refreshJWT(req, res, next) {
|
|
|
340
341
|
if (!credentials) {
|
|
341
342
|
return res.sendStatus(BAD_REQUEST);
|
|
342
343
|
}
|
|
344
|
+
res.locals.credentials = credentials;
|
|
343
345
|
next();
|
|
344
346
|
} catch (err) {
|
|
345
347
|
console.log("[WARN] refreshing JWT:", err);
|
|
@@ -642,17 +644,35 @@ var QueryBuilder = class {
|
|
|
642
644
|
var FluentPatternHandler = class _FluentPatternHandler {
|
|
643
645
|
/**
|
|
644
646
|
* Constructor for QueryPatternExecutor.
|
|
645
|
-
* @param
|
|
647
|
+
* @param options - Array of query pattern options for filtering.
|
|
646
648
|
*/
|
|
647
|
-
constructor(
|
|
649
|
+
constructor(options = [], execMiddleware = []) {
|
|
650
|
+
this._execMiddlewareFunctions = [];
|
|
648
651
|
if (_FluentPatternHandler._singleton) {
|
|
649
652
|
throw new Error(
|
|
650
653
|
"FluentPatternHandler is a singleton class. Use FluentPatternHandler.getInstance() to access the instance."
|
|
651
654
|
);
|
|
652
655
|
}
|
|
653
|
-
this.
|
|
656
|
+
this._options = options;
|
|
657
|
+
this._execMiddlewareFunctions = execMiddleware;
|
|
654
658
|
_FluentPatternHandler._singleton = this;
|
|
655
659
|
}
|
|
660
|
+
/**
|
|
661
|
+
* Initializes the singleton instance of FluentPatternHandler with the provided options.
|
|
662
|
+
* @param options - Array of query pattern options for filtering.
|
|
663
|
+
* @returns Singleton instance of FluentPatternHandler.
|
|
664
|
+
*/
|
|
665
|
+
static init(options = [], execMiddleware = []) {
|
|
666
|
+
if (_FluentPatternHandler._singleton != void 0) {
|
|
667
|
+
throw new Error("FluentPatternHandler is already initialized");
|
|
668
|
+
}
|
|
669
|
+
_FluentPatternHandler._singleton = new _FluentPatternHandler(options, execMiddleware);
|
|
670
|
+
return _FluentPatternHandler._singleton;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Returns the singleton instance of FluentPatternHandler.
|
|
674
|
+
* @returns Singleton instance of FluentPatternHandler.
|
|
675
|
+
*/
|
|
656
676
|
static getInstance() {
|
|
657
677
|
if (_FluentPatternHandler._singleton == void 0) {
|
|
658
678
|
throw new Error(
|
|
@@ -737,15 +757,15 @@ var FluentPatternHandler = class _FluentPatternHandler {
|
|
|
737
757
|
}
|
|
738
758
|
}
|
|
739
759
|
/**
|
|
740
|
-
* Retrieves filter fields for the given model from the
|
|
760
|
+
* Retrieves filter fields for the given model from the options configuration.
|
|
741
761
|
* @param model - The Mongoose model.
|
|
742
762
|
* @returns Array of filter fields.
|
|
743
763
|
*/
|
|
744
764
|
_getFilterFieldsForModel(model) {
|
|
745
|
-
for (const
|
|
746
|
-
if (
|
|
747
|
-
if (
|
|
748
|
-
return
|
|
765
|
+
for (const option of this._options) {
|
|
766
|
+
if (option.model.collection.name === model.collection.name) {
|
|
767
|
+
if (option.filters) {
|
|
768
|
+
return option.filters;
|
|
749
769
|
}
|
|
750
770
|
return [];
|
|
751
771
|
}
|
|
@@ -766,17 +786,21 @@ var FluentPatternHandler = class _FluentPatternHandler {
|
|
|
766
786
|
};
|
|
767
787
|
}
|
|
768
788
|
/**
|
|
769
|
-
* Executes the query builder with
|
|
770
|
-
* @param
|
|
771
|
-
* @
|
|
772
|
-
* @returns
|
|
789
|
+
* Executes the query builder with applied filters and returns the result.
|
|
790
|
+
* @param params Execution parameters including the query builder and request query.
|
|
791
|
+
* @returns
|
|
773
792
|
*/
|
|
774
|
-
async exec(
|
|
793
|
+
async exec(params) {
|
|
775
794
|
try {
|
|
776
|
-
|
|
777
|
-
|
|
795
|
+
if (this._execMiddlewareFunctions && this._execMiddlewareFunctions.length > 0) {
|
|
796
|
+
this._execMiddlewareFunctions.forEach((func) => {
|
|
797
|
+
func(params);
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
const queryParams = this._parseFluentRequestQuery(params.req.query);
|
|
801
|
+
this._applyParameters(params.queryBuilder, queryParams);
|
|
778
802
|
const execConfig = this._buildExecutionConfig(queryParams);
|
|
779
|
-
return await queryBuilder.exec(execConfig);
|
|
803
|
+
return await params.queryBuilder.exec(execConfig);
|
|
780
804
|
} catch (err) {
|
|
781
805
|
console.error("[ERROR - QueryPatternExecutor]", err);
|
|
782
806
|
return new Codec({ data: [], meta: { total: 0 } }, 500);
|
|
@@ -857,7 +881,11 @@ router2.get(
|
|
|
857
881
|
let queryBuilder = new QueryBuilder({
|
|
858
882
|
model
|
|
859
883
|
});
|
|
860
|
-
let codec = await FluentPatternHandler.getInstance().exec(
|
|
884
|
+
let codec = await FluentPatternHandler.getInstance().exec({
|
|
885
|
+
req,
|
|
886
|
+
res,
|
|
887
|
+
queryBuilder
|
|
888
|
+
});
|
|
861
889
|
codec.sendToClient(res);
|
|
862
890
|
} catch (e) {
|
|
863
891
|
console.log(e);
|
|
@@ -870,16 +898,21 @@ var fluent_api_default = router2;
|
|
|
870
898
|
// tests/auth.ts
|
|
871
899
|
import express4 from "express";
|
|
872
900
|
var router3 = express4.Router();
|
|
873
|
-
router3.post(
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
901
|
+
router3.post(
|
|
902
|
+
"/register",
|
|
903
|
+
authRegister,
|
|
904
|
+
async (req, res) => {
|
|
905
|
+
res.locals.authUser.save();
|
|
906
|
+
res.status(200).json({});
|
|
907
|
+
}
|
|
908
|
+
);
|
|
909
|
+
router3.post("/login", authLogin, async (req, res) => {
|
|
877
910
|
res.status(200).json(res.locals.credentials);
|
|
878
911
|
});
|
|
879
|
-
router3.post("/logout",
|
|
912
|
+
router3.post("/logout", authLogout, async (req, res) => {
|
|
880
913
|
res.status(200).json({});
|
|
881
914
|
});
|
|
882
|
-
router3.post("/refresh",
|
|
915
|
+
router3.post("/refresh", authRefresh, async (req, res) => {
|
|
883
916
|
res.status(200).json({});
|
|
884
917
|
});
|
|
885
918
|
router3.get("/secure", jwtRequired, async (req, res) => {
|
package/package.json
CHANGED