@radatek/microserver 2.3.2 → 2.3.4
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/microserver.d.ts +15 -6
- package/microserver.js +34 -21
- package/package.json +1 -1
package/microserver.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.4
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -199,8 +199,8 @@ export declare class WebSocket extends EventEmitter {
|
|
|
199
199
|
* ```
|
|
200
200
|
*/
|
|
201
201
|
export declare class Controller<T extends Model<any> = any> {
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
req: ServerRequest<T>;
|
|
203
|
+
res: ServerResponse<T>;
|
|
204
204
|
get model(): T | undefined;
|
|
205
205
|
constructor(req: ServerRequest<T>, res: ServerResponse<T>);
|
|
206
206
|
/** Generate routes for this controller */
|
|
@@ -231,7 +231,7 @@ export declare class Router extends EventEmitter {
|
|
|
231
231
|
/** bind middleware or create one from string like: 'redirect:302,https://redirect.to', 'error:422', 'param:name=value', 'acl:users/get', 'model:User', 'group:Users', 'user:admin' */
|
|
232
232
|
bind(fn: string | Function | object): Function;
|
|
233
233
|
/** Handler */
|
|
234
|
-
handler(req: ServerRequest, res: ServerResponse, next: Function, method?: string):
|
|
234
|
+
handler(req: ServerRequest, res: ServerResponse, next: Function, method?: string): void;
|
|
235
235
|
/** Clear routes and middlewares */
|
|
236
236
|
clear(): this;
|
|
237
237
|
/**
|
|
@@ -733,13 +733,22 @@ export type ModelDocument<T extends ModelSchema> = {
|
|
|
733
733
|
export declare interface ModelCollections {
|
|
734
734
|
collection(name: string): Promise<MicroCollection>;
|
|
735
735
|
}
|
|
736
|
+
export declare class Models {
|
|
737
|
+
}
|
|
736
738
|
export declare class Model<TSchema extends ModelSchema> {
|
|
737
739
|
static collections: ModelCollections;
|
|
738
|
-
static models:
|
|
740
|
+
static models: Models;
|
|
739
741
|
static set db(db: any);
|
|
740
742
|
static get db(): any;
|
|
743
|
+
/** Dynamic model extension */
|
|
744
|
+
static dynamic<T extends Model<any>>(model: T, options: {
|
|
745
|
+
controller?: Controller;
|
|
746
|
+
collection?: MicroCollection<any>;
|
|
747
|
+
req?: ServerRequest;
|
|
748
|
+
} | Controller): T;
|
|
749
|
+
static register<K extends string, T extends Model<any>>(name: K, model: T): typeof Model;
|
|
741
750
|
/** Define model */
|
|
742
|
-
static define<T extends ModelSchema>(name:
|
|
751
|
+
static define<K extends string, T extends ModelSchema>(name: K, schema: T, options?: {
|
|
743
752
|
collection?: MicroCollection | Promise<MicroCollection>;
|
|
744
753
|
class?: typeof Model;
|
|
745
754
|
}): Model<T>;
|
package/microserver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.4
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -838,10 +838,6 @@ export class Controller {
|
|
|
838
838
|
method = keyMatch[1];
|
|
839
839
|
url = keyMatch[2].startsWith('/') ? keyMatch[2] : ('/' + prefix + keyMatch[2]);
|
|
840
840
|
}
|
|
841
|
-
if (!url && !method) {
|
|
842
|
-
method = 'GET';
|
|
843
|
-
url = '/' + prefix + key;
|
|
844
|
-
}
|
|
845
841
|
if (!method)
|
|
846
842
|
return;
|
|
847
843
|
let autoAcl = method.toLowerCase();
|
|
@@ -908,14 +904,7 @@ export class Controller {
|
|
|
908
904
|
req.model = modelName instanceof Model ? modelName : Model.models[modelName];
|
|
909
905
|
if (!obj.model)
|
|
910
906
|
throw new InvalidData(modelName, 'model');
|
|
911
|
-
|
|
912
|
-
req.model = new Proxy(req.model, {
|
|
913
|
-
get: (target, prop) => {
|
|
914
|
-
if (prop === 'options')
|
|
915
|
-
return modelOptions;
|
|
916
|
-
return target[prop];
|
|
917
|
-
}
|
|
918
|
-
});
|
|
907
|
+
req.model = Model.dynamic(req.model, { controller: obj });
|
|
919
908
|
}
|
|
920
909
|
return func.apply(obj, req.paramsList);
|
|
921
910
|
});
|
|
@@ -1077,13 +1066,10 @@ export class Router extends EventEmitter {
|
|
|
1077
1066
|
handler(req, res, next, method) {
|
|
1078
1067
|
const nextAfter = next;
|
|
1079
1068
|
next = () => this._walkStack(this._stackAfter, req, res, nextAfter);
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
!this._walkTree(this._tree['*'], req, res, next))
|
|
1085
|
-
next();
|
|
1086
|
-
};
|
|
1069
|
+
const walkTree = (method) => this._walkTree(this._tree[method], req, res, next);
|
|
1070
|
+
const walk = method ?
|
|
1071
|
+
() => { !walkTree(method) && next(); } :
|
|
1072
|
+
() => { !walkTree(req.method || 'GET') && !walkTree('*') && next(); };
|
|
1087
1073
|
req.rewrite = (url) => {
|
|
1088
1074
|
if (req.originalUrl)
|
|
1089
1075
|
res.error(508);
|
|
@@ -2592,6 +2578,8 @@ class ModelCollectionsInternal {
|
|
|
2592
2578
|
return db.collection(name);
|
|
2593
2579
|
}
|
|
2594
2580
|
}
|
|
2581
|
+
export class Models {
|
|
2582
|
+
}
|
|
2595
2583
|
export class Model {
|
|
2596
2584
|
static set db(db) {
|
|
2597
2585
|
this.collections.db = db;
|
|
@@ -2599,6 +2587,31 @@ export class Model {
|
|
|
2599
2587
|
static get db() {
|
|
2600
2588
|
return this.collections.db;
|
|
2601
2589
|
}
|
|
2590
|
+
/** Dynamic model extension */
|
|
2591
|
+
static dynamic(model, options) {
|
|
2592
|
+
if (options instanceof Controller)
|
|
2593
|
+
options = { controller: options };
|
|
2594
|
+
const collection = options?.collection || model.collection;
|
|
2595
|
+
const modelOptions = { ...model.options };
|
|
2596
|
+
const req = options.req || options.controller?.req;
|
|
2597
|
+
if (req) {
|
|
2598
|
+
modelOptions.user = req.user;
|
|
2599
|
+
modelOptions.params = req.params;
|
|
2600
|
+
}
|
|
2601
|
+
return new Proxy(model, {
|
|
2602
|
+
get: (target, prop) => {
|
|
2603
|
+
if (prop === 'collection')
|
|
2604
|
+
return collection;
|
|
2605
|
+
if (prop === 'options')
|
|
2606
|
+
return modelOptions;
|
|
2607
|
+
return target[prop];
|
|
2608
|
+
}
|
|
2609
|
+
});
|
|
2610
|
+
}
|
|
2611
|
+
static register(name, model) {
|
|
2612
|
+
Model.models[name] = model;
|
|
2613
|
+
return this;
|
|
2614
|
+
}
|
|
2602
2615
|
/** Define model */
|
|
2603
2616
|
static define(name, schema, options) {
|
|
2604
2617
|
options = options || {};
|
|
@@ -2607,7 +2620,7 @@ export class Model {
|
|
|
2607
2620
|
const inst = options?.class
|
|
2608
2621
|
? new options.class(schema, { name, ...options })
|
|
2609
2622
|
: new Model(schema, { name, ...options });
|
|
2610
|
-
|
|
2623
|
+
this.register(name, inst);
|
|
2611
2624
|
return inst;
|
|
2612
2625
|
}
|
|
2613
2626
|
/** Create model acording to description */
|