@modular-rest/server 1.11.14 → 1.11.15
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/dist/application.d.ts +29 -0
- package/dist/application.js +217 -0
- package/dist/class/cms_trigger.d.ts +52 -0
- package/dist/class/cms_trigger.js +47 -0
- package/dist/class/collection_definition.d.ts +112 -0
- package/dist/class/collection_definition.js +87 -0
- package/dist/class/combinator.d.ts +43 -0
- package/dist/class/combinator.js +174 -0
- package/dist/class/database_trigger.d.ts +90 -0
- package/dist/class/database_trigger.js +64 -0
- package/dist/class/db_schemas.d.ts +25 -0
- package/dist/class/db_schemas.js +28 -0
- package/dist/class/directory.d.ts +20 -0
- package/dist/class/directory.js +87 -0
- package/dist/class/paginator.d.ts +31 -0
- package/dist/class/paginator.js +43 -0
- package/dist/class/reply.d.ts +29 -0
- package/dist/class/reply.js +44 -0
- package/dist/class/security.d.ts +186 -0
- package/dist/class/security.js +178 -0
- package/dist/class/trigger_operator.d.ts +92 -0
- package/dist/class/trigger_operator.js +99 -0
- package/dist/class/user.d.ts +81 -0
- package/dist/class/user.js +151 -0
- package/dist/class/validator.d.ts +19 -0
- package/dist/class/validator.js +101 -0
- package/dist/config.d.ts +113 -0
- package/dist/config.js +26 -0
- package/dist/defult-permissions.d.ts +2 -0
- package/dist/defult-permissions.js +31 -0
- package/dist/events.d.ts +23 -0
- package/dist/events.js +47 -0
- package/dist/helper/data_insertion.d.ts +38 -0
- package/dist/helper/data_insertion.js +110 -0
- package/dist/helper/presetup_services.d.ts +60 -0
- package/dist/helper/presetup_services.js +108 -0
- package/dist/index.d.ts +118 -0
- package/dist/middlewares.d.ts +53 -0
- package/dist/middlewares.js +106 -0
- package/dist/play-test.d.ts +1 -0
- package/dist/play-test.js +9 -0
- package/dist/services/data_provider/router.d.ts +4 -0
- package/dist/services/data_provider/router.js +412 -0
- package/dist/services/data_provider/service.d.ts +132 -0
- package/dist/services/data_provider/service.js +253 -0
- package/dist/services/data_provider/typeCasters.d.ts +9 -0
- package/dist/services/data_provider/typeCasters.js +18 -0
- package/dist/services/file/db.d.ts +1 -0
- package/dist/services/file/db.js +31 -0
- package/dist/services/file/router.d.ts +4 -0
- package/dist/services/file/router.js +115 -0
- package/dist/services/file/service.d.ts +204 -0
- package/dist/services/file/service.js +341 -0
- package/dist/services/functions/router.d.ts +4 -0
- package/dist/services/functions/router.js +68 -0
- package/dist/services/functions/service.d.ts +132 -0
- package/dist/services/functions/service.js +159 -0
- package/dist/services/jwt/router.d.ts +4 -0
- package/dist/services/jwt/router.js +99 -0
- package/dist/services/jwt/service.d.ts +97 -0
- package/dist/services/jwt/service.js +135 -0
- package/dist/services/user_manager/db.d.ts +1 -0
- package/dist/services/user_manager/db.js +75 -0
- package/dist/services/user_manager/permissionManager.d.ts +19 -0
- package/dist/services/user_manager/permissionManager.js +42 -0
- package/dist/services/user_manager/router.d.ts +4 -0
- package/dist/services/user_manager/router.js +195 -0
- package/dist/services/user_manager/service.d.ts +317 -0
- package/dist/services/user_manager/service.js +632 -0
- package/package.json +3 -3
- package/src/application.ts +1 -1
- package/src/class/cms_trigger.ts +8 -14
- package/src/class/database_trigger.ts +10 -4
- package/src/class/user.ts +1 -1
- package/src/services/data_provider/router.ts +293 -0
- package/src/services/data_provider/service.ts +2 -1
- package/src/services/functions/router.ts +3 -2
- package/src/services/user_manager/db.ts +5 -5
- package/src/services/user_manager/service.ts +20 -15
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
const koa_router_1 = __importDefault(require("koa-router"));
|
|
39
|
+
const directory = __importStar(require("./directory"));
|
|
40
|
+
const service_1 = require("../services/functions/service");
|
|
41
|
+
class Combinator {
|
|
42
|
+
async combineRoutesByFilePath(rootDirectory, app) {
|
|
43
|
+
// find route paths
|
|
44
|
+
const option = {
|
|
45
|
+
name: 'router',
|
|
46
|
+
filter: ['.js'],
|
|
47
|
+
};
|
|
48
|
+
let routerPaths = [];
|
|
49
|
+
try {
|
|
50
|
+
routerPaths = await directory.find(rootDirectory, option);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
console.log(e);
|
|
54
|
+
}
|
|
55
|
+
// create and combine routes into the app
|
|
56
|
+
for (let i = 0; i < routerPaths.length; i++) {
|
|
57
|
+
const service = require(routerPaths[i]);
|
|
58
|
+
const name = service.name;
|
|
59
|
+
const serviceRouter = new koa_router_1.default();
|
|
60
|
+
serviceRouter.use(`/${name}`, service.main.routes());
|
|
61
|
+
app.use(serviceRouter.routes());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Combine modules from files in a directory
|
|
66
|
+
* @param options - Configuration options
|
|
67
|
+
*/
|
|
68
|
+
async combineModulesByFilePath({ rootDirectory, filename, combineWithRoot, convertToArray, }) {
|
|
69
|
+
// find route paths
|
|
70
|
+
let rootObject_temp;
|
|
71
|
+
const option = {
|
|
72
|
+
name: filename.name,
|
|
73
|
+
filter: [filename.extension],
|
|
74
|
+
};
|
|
75
|
+
let modulesPath = [];
|
|
76
|
+
try {
|
|
77
|
+
modulesPath = await directory.find(rootDirectory, option);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
console.log(e);
|
|
81
|
+
}
|
|
82
|
+
// create and combine routes into the app
|
|
83
|
+
for (let i = 0; i < modulesPath.length; i++) {
|
|
84
|
+
const moduleObject = require(modulesPath[i]);
|
|
85
|
+
// act by otherOption
|
|
86
|
+
if (combineWithRoot) {
|
|
87
|
+
if (moduleObject.name)
|
|
88
|
+
delete moduleObject.name;
|
|
89
|
+
if (Array.isArray(moduleObject)) {
|
|
90
|
+
if (!rootObject_temp)
|
|
91
|
+
rootObject_temp = [];
|
|
92
|
+
rootObject_temp = [...rootObject_temp, ...moduleObject];
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
rootObject_temp = this.extendObj(rootObject_temp, moduleObject);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// default act
|
|
99
|
+
else {
|
|
100
|
+
const name = moduleObject.name;
|
|
101
|
+
rootObject_temp[name] = moduleObject;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// options
|
|
105
|
+
// convertToArray
|
|
106
|
+
if (convertToArray) {
|
|
107
|
+
rootObject_temp = Object.values(rootObject_temp);
|
|
108
|
+
}
|
|
109
|
+
// set result to main rootObject
|
|
110
|
+
return rootObject_temp;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Combine functions from files in a directory
|
|
114
|
+
* @param options - Function options
|
|
115
|
+
*/
|
|
116
|
+
async combineFunctionsByFilePath({ rootDirectory, filename }) {
|
|
117
|
+
// find route paths
|
|
118
|
+
const option = {
|
|
119
|
+
name: filename.name,
|
|
120
|
+
filter: [filename.extension],
|
|
121
|
+
};
|
|
122
|
+
let functionsPaths = [];
|
|
123
|
+
try {
|
|
124
|
+
functionsPaths = await directory.find(rootDirectory, option);
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
console.log(e);
|
|
128
|
+
}
|
|
129
|
+
// create and combine routes into the app
|
|
130
|
+
for (let i = 0; i < functionsPaths.length; i++) {
|
|
131
|
+
const modularFunctions = require(functionsPaths[i]);
|
|
132
|
+
if (!modularFunctions.functions) {
|
|
133
|
+
throw new Error(`Module file ${functionsPaths[i]} does not have functions property.`);
|
|
134
|
+
}
|
|
135
|
+
// if array
|
|
136
|
+
if (Array.isArray(modularFunctions.functions)) {
|
|
137
|
+
for (const moduleFunction of modularFunctions.functions) {
|
|
138
|
+
(0, service_1.addFunction)(moduleFunction);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
(0, service_1.addFunction)(modularFunctions.functions);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Add functions from an array
|
|
148
|
+
* @param functionList - List of functions to add
|
|
149
|
+
*/
|
|
150
|
+
addFunctionsByArray(functionList) {
|
|
151
|
+
for (const functionItem of functionList) {
|
|
152
|
+
(0, service_1.addFunction)(functionItem);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Extend an object with properties from another
|
|
157
|
+
* @param obj - Target object
|
|
158
|
+
* @param src - Source object
|
|
159
|
+
* @returns Extended object
|
|
160
|
+
*/
|
|
161
|
+
extendObj(obj, src) {
|
|
162
|
+
obj = obj || {};
|
|
163
|
+
for (const key in src) {
|
|
164
|
+
if (Object.prototype.hasOwnProperty.call(src, key))
|
|
165
|
+
obj[key] = src[key];
|
|
166
|
+
}
|
|
167
|
+
return obj;
|
|
168
|
+
}
|
|
169
|
+
static get instance() {
|
|
170
|
+
return instance;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const instance = new Combinator();
|
|
174
|
+
module.exports = instance;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type for database operations that can trigger a callback
|
|
3
|
+
*/
|
|
4
|
+
export type DatabaseOperation = 'find' | 'find-one' | 'count' | 'update-one' | 'insert-one' | 'remove-one' | 'aggregate';
|
|
5
|
+
/**
|
|
6
|
+
* Context interface for database trigger callbacks
|
|
7
|
+
* @interface DatabaseTriggerContext
|
|
8
|
+
* @property {Record<string, any>} doc - The document data for insert/update operations
|
|
9
|
+
* @property {Record<string, any>} query - The query data for find/find-one operations
|
|
10
|
+
* @property {Record<string, any>} update - The update data for update operations
|
|
11
|
+
* @property {Record<string, any>[]} pipelines - The aggregation pipelines for aggregate operations
|
|
12
|
+
* @property {Record<string, any> } queryResult - The result of the database operation
|
|
13
|
+
*/
|
|
14
|
+
export interface DatabaseTriggerContext {
|
|
15
|
+
doc?: Record<string, any>;
|
|
16
|
+
query?: Record<string, any>;
|
|
17
|
+
update?: Record<string, any>;
|
|
18
|
+
pipelines?: Record<string, any>[];
|
|
19
|
+
queryResult: Record<string, any>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The callback function to be executed on specific database operations
|
|
23
|
+
* @param {DatabaseTriggerContext} context - The context of the database operation
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const trigger = new DatabaseTrigger('insert-one', (context) => {
|
|
27
|
+
* console.log('New document inserted:', context.queryResult);
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
type DatabaseTriggerCallback = (context: DatabaseTriggerContext) => void;
|
|
32
|
+
/**
|
|
33
|
+
* in a complex application, you may need to perform additional actions after a database operation.
|
|
34
|
+
* this is where DatabaseTrigger comes in. so you can define a callback to be executed on specific database operations for a collection.
|
|
35
|
+
*
|
|
36
|
+
* Supported triggers are:
|
|
37
|
+
*
|
|
38
|
+
* | Trigger | Description |
|
|
39
|
+
* | ------------ | ------------------------------------------------------------ |
|
|
40
|
+
* | `find` | Triggered when a find query is executed on collection. |
|
|
41
|
+
* | `find-one` | Triggered when a find one query is executed on collection. |
|
|
42
|
+
* | `count` | Triggered when a count query is executed on collection. |
|
|
43
|
+
* | `update-one` | Triggered when a update one query is executed on collection. |
|
|
44
|
+
* | `insert-one` | Triggered when a insert one query is executed on collection. |
|
|
45
|
+
* | `remove-one` | Triggered when a remove one query is executed on collection. |
|
|
46
|
+
* | `aggregate` | Triggered when a aggregate query is executed on collection. |
|
|
47
|
+
*
|
|
48
|
+
* @property {DatabaseOperation} operation - The database operation that triggers the callback
|
|
49
|
+
* @property {DatabaseTriggerCallback} callback - The callback function to be executed
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import { DatabaseTrigger } from '@server-ts/database';
|
|
54
|
+
*
|
|
55
|
+
* const trigger = new DatabaseTrigger('insert-one', ({ query, queryResult }) => {
|
|
56
|
+
* console.log('New document inserted:', queryResult);
|
|
57
|
+
*
|
|
58
|
+
* try {
|
|
59
|
+
* // Perform additional actions after document insertion
|
|
60
|
+
* } catch (error) {
|
|
61
|
+
* console.error('Error performing additional actions:', error);
|
|
62
|
+
* }
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // Use the trigger in a collection definition
|
|
66
|
+
* const collection = new CollectionDefinition({
|
|
67
|
+
* triggers: [trigger]
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare class DatabaseTrigger {
|
|
72
|
+
operation: DatabaseOperation;
|
|
73
|
+
callback: (context: DatabaseTriggerContext) => void;
|
|
74
|
+
/**
|
|
75
|
+
* @hidden
|
|
76
|
+
*
|
|
77
|
+
* Creates a new DatabaseTrigger instance
|
|
78
|
+
* @param {DatabaseOperation} operation - The database operation to trigger on
|
|
79
|
+
* @param {DatabaseTriggerCallback} [callback=() => {}] - The callback function to execute
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const trigger = new DatabaseTrigger('insert-one', (context) => {
|
|
84
|
+
* console.log('New document inserted:', context.queryResult);
|
|
85
|
+
* });
|
|
86
|
+
*
|
|
87
|
+
*/
|
|
88
|
+
constructor(operation: DatabaseOperation, callback?: DatabaseTriggerCallback);
|
|
89
|
+
}
|
|
90
|
+
export default DatabaseTrigger;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseTrigger = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* in a complex application, you may need to perform additional actions after a database operation.
|
|
6
|
+
* this is where DatabaseTrigger comes in. so you can define a callback to be executed on specific database operations for a collection.
|
|
7
|
+
*
|
|
8
|
+
* Supported triggers are:
|
|
9
|
+
*
|
|
10
|
+
* | Trigger | Description |
|
|
11
|
+
* | ------------ | ------------------------------------------------------------ |
|
|
12
|
+
* | `find` | Triggered when a find query is executed on collection. |
|
|
13
|
+
* | `find-one` | Triggered when a find one query is executed on collection. |
|
|
14
|
+
* | `count` | Triggered when a count query is executed on collection. |
|
|
15
|
+
* | `update-one` | Triggered when a update one query is executed on collection. |
|
|
16
|
+
* | `insert-one` | Triggered when a insert one query is executed on collection. |
|
|
17
|
+
* | `remove-one` | Triggered when a remove one query is executed on collection. |
|
|
18
|
+
* | `aggregate` | Triggered when a aggregate query is executed on collection. |
|
|
19
|
+
*
|
|
20
|
+
* @property {DatabaseOperation} operation - The database operation that triggers the callback
|
|
21
|
+
* @property {DatabaseTriggerCallback} callback - The callback function to be executed
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { DatabaseTrigger } from '@server-ts/database';
|
|
26
|
+
*
|
|
27
|
+
* const trigger = new DatabaseTrigger('insert-one', ({ query, queryResult }) => {
|
|
28
|
+
* console.log('New document inserted:', queryResult);
|
|
29
|
+
*
|
|
30
|
+
* try {
|
|
31
|
+
* // Perform additional actions after document insertion
|
|
32
|
+
* } catch (error) {
|
|
33
|
+
* console.error('Error performing additional actions:', error);
|
|
34
|
+
* }
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Use the trigger in a collection definition
|
|
38
|
+
* const collection = new CollectionDefinition({
|
|
39
|
+
* triggers: [trigger]
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
class DatabaseTrigger {
|
|
44
|
+
/**
|
|
45
|
+
* @hidden
|
|
46
|
+
*
|
|
47
|
+
* Creates a new DatabaseTrigger instance
|
|
48
|
+
* @param {DatabaseOperation} operation - The database operation to trigger on
|
|
49
|
+
* @param {DatabaseTriggerCallback} [callback=() => {}] - The callback function to execute
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const trigger = new DatabaseTrigger('insert-one', (context) => {
|
|
54
|
+
* console.log('New document inserted:', context.queryResult);
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
constructor(operation, callback = () => { }) {
|
|
59
|
+
this.operation = operation;
|
|
60
|
+
this.callback = callback;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.DatabaseTrigger = DatabaseTrigger;
|
|
64
|
+
exports.default = DatabaseTrigger;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
/**
|
|
3
|
+
* File schema interface
|
|
4
|
+
*/
|
|
5
|
+
export interface IFile {
|
|
6
|
+
originalName: string;
|
|
7
|
+
fileName: string;
|
|
8
|
+
owner: string;
|
|
9
|
+
format: string;
|
|
10
|
+
tag: string;
|
|
11
|
+
size: number;
|
|
12
|
+
createdAt?: Date;
|
|
13
|
+
updatedAt?: Date;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* File schema
|
|
17
|
+
*/
|
|
18
|
+
export declare const fileSchema: mongoose.Schema<IFile, mongoose.Model<IFile, any, any>, undefined, {}>;
|
|
19
|
+
/**
|
|
20
|
+
* Schema definitions
|
|
21
|
+
*/
|
|
22
|
+
export declare const schemas: {
|
|
23
|
+
file: mongoose.Schema<IFile, mongoose.Model<IFile, any, any>, undefined, {}>;
|
|
24
|
+
};
|
|
25
|
+
export default schemas;
|
|
@@ -0,0 +1,28 @@
|
|
|
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.schemas = exports.fileSchema = void 0;
|
|
7
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
8
|
+
const { Schema } = mongoose_1.default;
|
|
9
|
+
/**
|
|
10
|
+
* File schema
|
|
11
|
+
*/
|
|
12
|
+
exports.fileSchema = new Schema({
|
|
13
|
+
originalName: String,
|
|
14
|
+
fileName: String,
|
|
15
|
+
owner: String,
|
|
16
|
+
format: String,
|
|
17
|
+
// Tag being used as the parent dir for files
|
|
18
|
+
// uploadDir/$format/$tag/timestamp.format
|
|
19
|
+
tag: String,
|
|
20
|
+
size: Number,
|
|
21
|
+
}, { timestamps: true });
|
|
22
|
+
/**
|
|
23
|
+
* Schema definitions
|
|
24
|
+
*/
|
|
25
|
+
exports.schemas = {
|
|
26
|
+
file: exports.fileSchema,
|
|
27
|
+
};
|
|
28
|
+
exports.default = exports.schemas;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface DirectorySettings {
|
|
2
|
+
name?: string;
|
|
3
|
+
filter?: string[];
|
|
4
|
+
}
|
|
5
|
+
type WalkCallback = (err: Error | null, results: string[]) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Walk through a directory and its subdirectories
|
|
8
|
+
* @param dir - Directory to walk
|
|
9
|
+
* @param settings - Settings for filtering files
|
|
10
|
+
* @param done - Callback function
|
|
11
|
+
*/
|
|
12
|
+
declare function walk(dir: string, settings: DirectorySettings, done: WalkCallback): void;
|
|
13
|
+
/**
|
|
14
|
+
* Find files in a directory with Promise API
|
|
15
|
+
* @param dir - Directory to search
|
|
16
|
+
* @param settings - Settings for filtering files
|
|
17
|
+
* @returns Promise resolving to an array of file paths
|
|
18
|
+
*/
|
|
19
|
+
declare function find(dir: string, settings: DirectorySettings): Promise<string[]>;
|
|
20
|
+
export { walk, find };
|
|
@@ -0,0 +1,87 @@
|
|
|
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.walk = walk;
|
|
7
|
+
exports.find = find;
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
/**
|
|
11
|
+
* Walk through a directory and its subdirectories
|
|
12
|
+
* @param dir - Directory to walk
|
|
13
|
+
* @param settings - Settings for filtering files
|
|
14
|
+
* @param done - Callback function
|
|
15
|
+
*/
|
|
16
|
+
function walk(dir, settings, done) {
|
|
17
|
+
let results = [];
|
|
18
|
+
// Read director file and folders
|
|
19
|
+
fs_1.default.readdir(dir, function (err, list) {
|
|
20
|
+
if (err)
|
|
21
|
+
return done(err, results);
|
|
22
|
+
let pending = list.length;
|
|
23
|
+
if (!pending)
|
|
24
|
+
return done(null, results);
|
|
25
|
+
list.forEach(function (file) {
|
|
26
|
+
file = path_1.default.join(dir, file);
|
|
27
|
+
fs_1.default.stat(file, function (err, stat) {
|
|
28
|
+
if (err) {
|
|
29
|
+
// Handle file stat error but continue with other files
|
|
30
|
+
console.error(`Error reading file stats for ${file}:`, err);
|
|
31
|
+
if (!--pending)
|
|
32
|
+
done(null, results);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// If directory, execute a recursive call
|
|
36
|
+
if (stat && stat.isDirectory()) {
|
|
37
|
+
// Add directory to array [comment if you need to remove the directories from the array]
|
|
38
|
+
// results.push(file);
|
|
39
|
+
walk(file, settings, function (err, res) {
|
|
40
|
+
results = results.concat(res);
|
|
41
|
+
if (!--pending)
|
|
42
|
+
done(null, results);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// file filter
|
|
47
|
+
const extension = path_1.default.extname(file);
|
|
48
|
+
const fileName = path_1.default.basename(file).split('.')[0];
|
|
49
|
+
let fileNameKey = true;
|
|
50
|
+
// name filter
|
|
51
|
+
if (settings.name && settings.name === fileName)
|
|
52
|
+
fileNameKey = true;
|
|
53
|
+
else
|
|
54
|
+
fileNameKey = false;
|
|
55
|
+
// extension filter
|
|
56
|
+
if (settings.filter && fileNameKey) {
|
|
57
|
+
settings.filter.forEach(function (element) {
|
|
58
|
+
if (element.toLowerCase() === extension.toLowerCase())
|
|
59
|
+
results.push(file);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
// push any file if no option
|
|
63
|
+
else if (fileNameKey)
|
|
64
|
+
results.push(file);
|
|
65
|
+
if (!--pending)
|
|
66
|
+
done(null, results);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Find files in a directory with Promise API
|
|
74
|
+
* @param dir - Directory to search
|
|
75
|
+
* @param settings - Settings for filtering files
|
|
76
|
+
* @returns Promise resolving to an array of file paths
|
|
77
|
+
*/
|
|
78
|
+
function find(dir, settings) {
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
walk(dir, settings, (err, result) => {
|
|
81
|
+
if (err)
|
|
82
|
+
reject(err);
|
|
83
|
+
else
|
|
84
|
+
resolve(result);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pagination result interface
|
|
3
|
+
*/
|
|
4
|
+
export interface PaginationResult {
|
|
5
|
+
pages: number;
|
|
6
|
+
page: number;
|
|
7
|
+
from: number;
|
|
8
|
+
to: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a pagination object based on the given parameters.
|
|
12
|
+
* @param count - The total number of items to paginate.
|
|
13
|
+
* @param perPage - The number of items to display per page.
|
|
14
|
+
* @param page - The current page number.
|
|
15
|
+
* @returns An object containing pagination information.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { paginator } from '@modular-rest/server';
|
|
20
|
+
*
|
|
21
|
+
* const pagination = paginator.create(100, 10, 1);
|
|
22
|
+
* // json response will be like this
|
|
23
|
+
* // {
|
|
24
|
+
* // pages: 10,
|
|
25
|
+
* // page: 1,
|
|
26
|
+
* // from: 0,
|
|
27
|
+
* // to: 10,
|
|
28
|
+
* // }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function create(count: number, perPage: number, page: number): PaginationResult;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.create = create;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a pagination object based on the given parameters.
|
|
6
|
+
* @param count - The total number of items to paginate.
|
|
7
|
+
* @param perPage - The number of items to display per page.
|
|
8
|
+
* @param page - The current page number.
|
|
9
|
+
* @returns An object containing pagination information.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { paginator } from '@modular-rest/server';
|
|
14
|
+
*
|
|
15
|
+
* const pagination = paginator.create(100, 10, 1);
|
|
16
|
+
* // json response will be like this
|
|
17
|
+
* // {
|
|
18
|
+
* // pages: 10,
|
|
19
|
+
* // page: 1,
|
|
20
|
+
* // from: 0,
|
|
21
|
+
* // to: 10,
|
|
22
|
+
* // }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function create(count, perPage, page) {
|
|
26
|
+
const totalPages = Math.ceil(count / perPage);
|
|
27
|
+
if (page > totalPages)
|
|
28
|
+
page = 1;
|
|
29
|
+
let from = 0;
|
|
30
|
+
if (perPage === 1)
|
|
31
|
+
from = page - 1;
|
|
32
|
+
else
|
|
33
|
+
from = perPage * page - perPage;
|
|
34
|
+
if (page <= 1)
|
|
35
|
+
from = 0;
|
|
36
|
+
const result = {
|
|
37
|
+
pages: totalPages,
|
|
38
|
+
page: page,
|
|
39
|
+
from: from,
|
|
40
|
+
to: perPage,
|
|
41
|
+
};
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response status type
|
|
3
|
+
*/
|
|
4
|
+
export type ResponseStatus = 's' | 'f' | 'e';
|
|
5
|
+
/**
|
|
6
|
+
* Response object interface
|
|
7
|
+
*/
|
|
8
|
+
export interface ResponseObject {
|
|
9
|
+
status: 'success' | 'fail' | 'error';
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a response object with the given status and detail.
|
|
14
|
+
*
|
|
15
|
+
* @param status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
|
|
16
|
+
* @param detail - The detail of the response. Can contain any additional information about the response.
|
|
17
|
+
* @returns The response object with the given status and detail.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { reply } from '@modular-rest/server';
|
|
22
|
+
*
|
|
23
|
+
* // inside the router
|
|
24
|
+
* const response = reply.create("s", { message: "Hello, world!" });
|
|
25
|
+
* ctx.body = response;
|
|
26
|
+
* ctx.status = 200;
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function create(status: ResponseStatus, detail?: Record<string, any>): ResponseObject;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.create = create;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a response object with the given status and detail.
|
|
6
|
+
*
|
|
7
|
+
* @param status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
|
|
8
|
+
* @param detail - The detail of the response. Can contain any additional information about the response.
|
|
9
|
+
* @returns The response object with the given status and detail.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { reply } from '@modular-rest/server';
|
|
14
|
+
*
|
|
15
|
+
* // inside the router
|
|
16
|
+
* const response = reply.create("s", { message: "Hello, world!" });
|
|
17
|
+
* ctx.body = response;
|
|
18
|
+
* ctx.status = 200;
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function create(status, detail = {}) {
|
|
22
|
+
// Initialize with a default status that will be overwritten
|
|
23
|
+
const result = {
|
|
24
|
+
status: 'success',
|
|
25
|
+
...detail,
|
|
26
|
+
};
|
|
27
|
+
// define status
|
|
28
|
+
switch (status) {
|
|
29
|
+
case 's':
|
|
30
|
+
result.status = 'success';
|
|
31
|
+
break;
|
|
32
|
+
case 'f':
|
|
33
|
+
result.status = 'fail';
|
|
34
|
+
break;
|
|
35
|
+
case 'e':
|
|
36
|
+
result.status = 'error';
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
result.status = 'success';
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
// return
|
|
43
|
+
return result;
|
|
44
|
+
}
|