speedly 1.2.48 → 2.0.5
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/cjs/auth/auth2.d.ts +18 -0
- package/dist/cjs/auth/auth2.js +93 -0
- package/dist/cjs/config/init.d.ts +15 -0
- package/dist/cjs/config/init.js +58 -0
- package/dist/cjs/db/db.d.ts +65 -66
- package/dist/cjs/db/db.js +15 -22
- package/dist/cjs/document/index.d.ts +2 -0
- package/dist/cjs/document/index.js +7 -0
- package/dist/cjs/index.d.ts +3 -53
- package/dist/cjs/index.js +7 -22
- package/dist/cjs/kit/auth/auth.d.ts +3 -0
- package/dist/cjs/kit/auth/auth.js +38 -0
- package/dist/cjs/kit/auth/types.d.ts +19 -0
- package/dist/cjs/kit/auth/types.js +2 -0
- package/dist/cjs/kit/db/db.d.ts +182 -0
- package/dist/cjs/kit/db/db.js +594 -0
- package/dist/cjs/kit/db/utils.d.ts +3 -0
- package/dist/cjs/kit/db/utils.js +15 -0
- package/dist/cjs/kit/index.d.ts +5 -0
- package/dist/cjs/kit/index.js +14 -0
- package/dist/cjs/kit/uploader/uploader.d.ts +24 -0
- package/dist/cjs/kit/uploader/uploader.js +148 -0
- package/dist/cjs/kit/validator/validator.d.ts +9 -0
- package/dist/cjs/kit/validator/validator.js +36 -0
- package/dist/cjs/model/index.d.ts +2 -0
- package/dist/cjs/model/index.js +8 -0
- package/dist/cjs/model/translation.d.ts +24 -6
- package/dist/cjs/modules/index.d.ts +2 -0
- package/dist/cjs/modules/index.js +8 -0
- package/dist/cjs/modules/translation/translation.routes.js +12 -8
- package/dist/cjs/util/index.d.ts +2 -0
- package/dist/cjs/util/index.js +8 -0
- package/dist/cjs/util/makeOptional.js +17 -7
- package/dist/cjs/yup.config.d.ts +2 -0
- package/dist/cjs/yup.config.js +24 -0
- package/dist/esm/config/init.d.ts +15 -0
- package/dist/esm/config/init.js +58 -0
- package/dist/esm/db/db.d.ts +65 -66
- package/dist/esm/db/db.js +15 -22
- package/dist/esm/document/index.d.ts +2 -0
- package/dist/esm/document/index.js +7 -0
- package/dist/esm/index.d.ts +3 -53
- package/dist/esm/index.js +7 -22
- package/dist/esm/kit/auth/auth.d.ts +3 -0
- package/dist/esm/kit/auth/auth.js +38 -0
- package/dist/esm/kit/auth/types.d.ts +19 -0
- package/dist/esm/kit/auth/types.js +2 -0
- package/dist/esm/kit/db/db.d.ts +182 -0
- package/dist/esm/kit/db/db.js +594 -0
- package/dist/esm/kit/db/utils.d.ts +3 -0
- package/dist/esm/kit/db/utils.js +15 -0
- package/dist/esm/kit/index.d.ts +5 -0
- package/dist/esm/kit/index.js +14 -0
- package/dist/esm/kit/uploader/uploader.d.ts +24 -0
- package/dist/esm/kit/uploader/uploader.js +148 -0
- package/dist/esm/kit/validator/validator.d.ts +9 -0
- package/dist/esm/kit/validator/validator.js +36 -0
- package/dist/esm/model/index.d.ts +2 -0
- package/dist/esm/model/index.js +8 -0
- package/dist/esm/model/translation.d.ts +24 -6
- package/dist/esm/modules/index.d.ts +2 -0
- package/dist/esm/modules/index.js +8 -0
- package/dist/esm/modules/translation/translation.routes.js +12 -8
- package/dist/esm/util/index.d.ts +2 -0
- package/dist/esm/util/index.js +8 -0
- package/dist/esm/util/makeOptional.js +17 -7
- package/package.json +1 -1
|
@@ -0,0 +1,148 @@
|
|
|
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 path_1 = __importDefault(require("path"));
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const multer_1 = __importDefault(require("multer"));
|
|
9
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
10
|
+
const getConfig_1 = __importDefault(require("../../util/getConfig"));
|
|
11
|
+
let configs = {
|
|
12
|
+
saveInDb: false,
|
|
13
|
+
prefix: "",
|
|
14
|
+
limit: 5,
|
|
15
|
+
format: /png|jpg|webp|jpeg/i,
|
|
16
|
+
path: "../../../public",
|
|
17
|
+
...(0, getConfig_1.default)("uploader"),
|
|
18
|
+
};
|
|
19
|
+
exports.default = (destination = "/image", config = configs) => {
|
|
20
|
+
let dest;
|
|
21
|
+
try {
|
|
22
|
+
Object.entries(config).forEach(([key, val]) => {
|
|
23
|
+
configs[key] = val;
|
|
24
|
+
});
|
|
25
|
+
const storage = multer_1.default.diskStorage({
|
|
26
|
+
destination: function (req, file, cb) {
|
|
27
|
+
try {
|
|
28
|
+
dest =
|
|
29
|
+
typeof destination === "function"
|
|
30
|
+
? destination(req, file)
|
|
31
|
+
: destination;
|
|
32
|
+
const splitPath = dest.split("/");
|
|
33
|
+
let currentPath = path_1.default.join(configs.path);
|
|
34
|
+
splitPath.forEach((folder) => {
|
|
35
|
+
currentPath = path_1.default.join(currentPath, folder);
|
|
36
|
+
if (!fs_1.default.existsSync(currentPath)) {
|
|
37
|
+
fs_1.default.mkdirSync(currentPath);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
cb(null, path_1.default.join(configs.path, dest));
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
cb(err, "");
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
filename: function (req, file, cb) {
|
|
47
|
+
try {
|
|
48
|
+
const ext = path_1.default.extname(file.originalname);
|
|
49
|
+
if (!ext.slice(1).match(configs.format)) {
|
|
50
|
+
return cb(new Error("File format not acceptable"), "");
|
|
51
|
+
}
|
|
52
|
+
const originalName = Buffer.from(file.originalname, "latin1").toString("utf8");
|
|
53
|
+
const fileName = (configs.prefix ? configs.prefix + "-" : "") +
|
|
54
|
+
originalName.replace(/\.\w+$/, "") +
|
|
55
|
+
ext;
|
|
56
|
+
const filePath = path_1.default.join(configs.path, dest, fileName);
|
|
57
|
+
try {
|
|
58
|
+
fs_1.default.existsSync(filePath);
|
|
59
|
+
}
|
|
60
|
+
catch (error) { }
|
|
61
|
+
if (fs_1.default.existsSync(filePath)) {
|
|
62
|
+
return cb(new Error("File already exists in the destination folder"), "");
|
|
63
|
+
}
|
|
64
|
+
cb(null, fileName);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
cb(err, "");
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
const uploader = (0, multer_1.default)({
|
|
72
|
+
storage,
|
|
73
|
+
limits: { fileSize: (configs.limit || 5) * 1024 * 1024 }, // MB
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
single: (fieldName) => (req, res, next) => {
|
|
77
|
+
uploader.single(fieldName)(req, res, async (err) => {
|
|
78
|
+
if (err) {
|
|
79
|
+
console.log("uploader", 85, err);
|
|
80
|
+
return next({ status: 405, json: { message: err.message } });
|
|
81
|
+
}
|
|
82
|
+
if (req.file) {
|
|
83
|
+
if (configs.saveInDb) {
|
|
84
|
+
const db = mongoose_1.default.connection;
|
|
85
|
+
const collection = db.collection("media");
|
|
86
|
+
const duplicate = await collection.findOne({
|
|
87
|
+
alt: req.body.alt,
|
|
88
|
+
});
|
|
89
|
+
if (duplicate) {
|
|
90
|
+
fs_1.default.rmSync(req.file.path);
|
|
91
|
+
return res.status(405).json({ message: "alt is repetitive" });
|
|
92
|
+
}
|
|
93
|
+
const mediaDoc = await collection.insertOne({
|
|
94
|
+
type: req.file.mimetype.split("/")[0],
|
|
95
|
+
name: req.file.filename,
|
|
96
|
+
dirPath: dest + "/",
|
|
97
|
+
alt: req.body.alt,
|
|
98
|
+
desc: req.body.desc,
|
|
99
|
+
url: "/static/" + dest + "/" + req.file.filename,
|
|
100
|
+
});
|
|
101
|
+
req.mediaId = mediaDoc.insertedId;
|
|
102
|
+
}
|
|
103
|
+
req.body[fieldName] = path_1.default
|
|
104
|
+
.join("/static", path_1.default.relative(path_1.default.join(configs.path), req.file.path))
|
|
105
|
+
.replaceAll(/\\/g, "/");
|
|
106
|
+
}
|
|
107
|
+
next();
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
array: (fieldName, maxCount = Infinity) => (req, res, next) => {
|
|
111
|
+
uploader.array(fieldName, maxCount)(req, res, (err) => {
|
|
112
|
+
if (err)
|
|
113
|
+
return res.status(405).json({ message: err.message });
|
|
114
|
+
if (req.files && Array.isArray(req.files) && req.files.length) {
|
|
115
|
+
req.body[fieldName] = req.files.map((file) => path_1.default
|
|
116
|
+
.join("/static", path_1.default.relative(path_1.default.join(configs.path), file.path))
|
|
117
|
+
.replaceAll(/\\/g, "/"));
|
|
118
|
+
}
|
|
119
|
+
next();
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
fields: (fields) => (req, res, next) => {
|
|
123
|
+
uploader.fields(fields)(req, res, (err) => {
|
|
124
|
+
if (err)
|
|
125
|
+
return res.status(405).json({ message: err.message });
|
|
126
|
+
next();
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
any: () => (req, res, next) => {
|
|
130
|
+
uploader.any()(req, res, (err) => {
|
|
131
|
+
if (err)
|
|
132
|
+
return res.status(405).json({ message: err.message });
|
|
133
|
+
next();
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
none: () => (req, res, next) => {
|
|
137
|
+
uploader.none()(req, res, (err) => {
|
|
138
|
+
if (err)
|
|
139
|
+
return res.status(405).json({ message: err.message });
|
|
140
|
+
next();
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.log("uploader init error", error);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RequestHandler } from "express";
|
|
2
|
+
import * as yup from "yup";
|
|
3
|
+
type ValidationSchema = {
|
|
4
|
+
body?: yup.AnyObjectSchema;
|
|
5
|
+
params?: yup.AnyObjectSchema;
|
|
6
|
+
query?: yup.AnyObjectSchema;
|
|
7
|
+
};
|
|
8
|
+
declare const validator: <T extends ValidationSchema>(schemas: T) => RequestHandler;
|
|
9
|
+
export default validator;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// این تابع validator generic هست
|
|
4
|
+
const validator = (schemas) => {
|
|
5
|
+
const mw = async (req, res, next) => {
|
|
6
|
+
try {
|
|
7
|
+
if (schemas.body) {
|
|
8
|
+
req.body = (await schemas.body.validate(req.body, {
|
|
9
|
+
stripUnknown: true,
|
|
10
|
+
}));
|
|
11
|
+
}
|
|
12
|
+
if (schemas.params) {
|
|
13
|
+
req.params = (await schemas.params.validate(req.params, {
|
|
14
|
+
stripUnknown: true,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
if (schemas.query) {
|
|
18
|
+
req.query = (await schemas.query.validate(req.query, {
|
|
19
|
+
stripUnknown: true,
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
next();
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
return next({
|
|
26
|
+
status: 405,
|
|
27
|
+
json: { message: err.message },
|
|
28
|
+
section: "validation",
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(mw, "__validationSchema", { value: schemas });
|
|
33
|
+
Object.defineProperty(mw, "name", { value: `validator` });
|
|
34
|
+
return mw;
|
|
35
|
+
};
|
|
36
|
+
exports.default = validator;
|
|
@@ -0,0 +1,8 @@
|
|
|
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.translation = void 0;
|
|
7
|
+
const translation_1 = __importDefault(require("./translation"));
|
|
8
|
+
exports.translation = translation_1.default;
|
|
@@ -1,39 +1,57 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
2
|
declare const model: mongoose.Model<{
|
|
3
|
+
createdAt: NativeDate;
|
|
4
|
+
updatedAt: NativeDate;
|
|
5
|
+
} & {
|
|
3
6
|
text: string;
|
|
4
7
|
lang: string;
|
|
5
8
|
translatedText: string;
|
|
6
|
-
}
|
|
9
|
+
}, {}, {}, {}, mongoose.Document<unknown, {}, {
|
|
10
|
+
createdAt: NativeDate;
|
|
11
|
+
updatedAt: NativeDate;
|
|
12
|
+
} & {
|
|
7
13
|
text: string;
|
|
8
14
|
lang: string;
|
|
9
15
|
translatedText: string;
|
|
10
|
-
}
|
|
16
|
+
}, {}, {
|
|
11
17
|
timestamps: true;
|
|
12
18
|
}> & {
|
|
19
|
+
createdAt: NativeDate;
|
|
20
|
+
updatedAt: NativeDate;
|
|
21
|
+
} & {
|
|
13
22
|
text: string;
|
|
14
23
|
lang: string;
|
|
15
24
|
translatedText: string;
|
|
16
|
-
} &
|
|
25
|
+
} & {
|
|
17
26
|
_id: mongoose.Types.ObjectId;
|
|
18
27
|
} & {
|
|
19
28
|
__v: number;
|
|
20
29
|
}, mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
|
|
21
30
|
timestamps: true;
|
|
22
31
|
}, {
|
|
32
|
+
createdAt: NativeDate;
|
|
33
|
+
updatedAt: NativeDate;
|
|
34
|
+
} & {
|
|
23
35
|
text: string;
|
|
24
36
|
lang: string;
|
|
25
37
|
translatedText: string;
|
|
26
|
-
}
|
|
38
|
+
}, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
|
|
39
|
+
createdAt: NativeDate;
|
|
40
|
+
updatedAt: NativeDate;
|
|
41
|
+
} & {
|
|
27
42
|
text: string;
|
|
28
43
|
lang: string;
|
|
29
44
|
translatedText: string;
|
|
30
|
-
}
|
|
45
|
+
}>, {}, mongoose.ResolveSchemaOptions<{
|
|
31
46
|
timestamps: true;
|
|
32
47
|
}>> & mongoose.FlatRecord<{
|
|
48
|
+
createdAt: NativeDate;
|
|
49
|
+
updatedAt: NativeDate;
|
|
50
|
+
} & {
|
|
33
51
|
text: string;
|
|
34
52
|
lang: string;
|
|
35
53
|
translatedText: string;
|
|
36
|
-
}
|
|
54
|
+
}> & {
|
|
37
55
|
_id: mongoose.Types.ObjectId;
|
|
38
56
|
} & {
|
|
39
57
|
__v: number;
|
|
@@ -0,0 +1,8 @@
|
|
|
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.translation = void 0;
|
|
7
|
+
const translation_routes_1 = __importDefault(require("./translation/translation.routes"));
|
|
8
|
+
exports.translation = translation_routes_1.default;
|
|
@@ -4,17 +4,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const express_1 = __importDefault(require("express"));
|
|
7
|
-
const auth_1 = __importDefault(require("../../auth/auth"));
|
|
8
|
-
const db_1 = __importDefault(require("../../db/db"));
|
|
9
|
-
const validator_1 = __importDefault(require("../../validator/validator"));
|
|
10
|
-
const v = require(
|
|
7
|
+
const auth_1 = __importDefault(require("../../kit/auth/auth"));
|
|
8
|
+
const db_1 = __importDefault(require("../../kit/db/db"));
|
|
9
|
+
const validator_1 = __importDefault(require("../../kit/validator/validator"));
|
|
10
|
+
const v = require("./translation.validator");
|
|
11
11
|
const router = express_1.default.Router();
|
|
12
|
-
router
|
|
12
|
+
router
|
|
13
|
+
.route("/")
|
|
14
|
+
.get((0, db_1.default)("translation", { type: "internal" }).find())
|
|
13
15
|
.post((req, res, next) => {
|
|
14
|
-
if (!req.body.auth || req.body.auth !==
|
|
16
|
+
if (!req.body.auth || req.body.auth !== "OKPJWSJD_Sdki") {
|
|
15
17
|
return res.status(403).json({ message: "Access Denied" });
|
|
16
18
|
}
|
|
17
19
|
next();
|
|
18
|
-
}, (0, db_1.default)(
|
|
19
|
-
router
|
|
20
|
+
}, (0, db_1.default)("translation", { type: "internal" }).create());
|
|
21
|
+
router
|
|
22
|
+
.route("/:id")
|
|
23
|
+
.put(auth_1.default.admin(), (0, validator_1.default)(v.put), (0, db_1.default)("translation", { type: "internal" }).findByIdAndUpdate());
|
|
20
24
|
exports.default = router;
|
|
@@ -0,0 +1,8 @@
|
|
|
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.translator = void 0;
|
|
7
|
+
const translator_1 = __importDefault(require("./translator"));
|
|
8
|
+
exports.translator = translator_1.default;
|
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
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
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
exports.default = default_1;
|
|
27
37
|
const yup = __importStar(require("yup"));
|
|
@@ -0,0 +1,24 @@
|
|
|
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 mongoose_1 = require("mongoose");
|
|
7
|
+
const yup_1 = __importDefault(require("yup"));
|
|
8
|
+
yup_1.default.addMethod(yup_1.default.string, "oid", function (errorMessage = "آیدی درست نیست") {
|
|
9
|
+
return this.test({
|
|
10
|
+
name: "oid",
|
|
11
|
+
message: errorMessage,
|
|
12
|
+
test(value) {
|
|
13
|
+
if (!value)
|
|
14
|
+
return true;
|
|
15
|
+
if (value == "$$REMOVE")
|
|
16
|
+
return true;
|
|
17
|
+
if (!value || !(0, mongoose_1.isValidObjectId)(value)) {
|
|
18
|
+
return false; // Invalid ObjectId
|
|
19
|
+
}
|
|
20
|
+
return true; // Valid ObjectId
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
exports.default = yup_1.default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
type InitConfig = {
|
|
3
|
+
notFoundHandler?: boolean;
|
|
4
|
+
errorHandler?: boolean;
|
|
5
|
+
jsonParser?: boolean;
|
|
6
|
+
urlEncodedParser?: boolean;
|
|
7
|
+
cookieParser?: boolean;
|
|
8
|
+
staticFiles?: boolean;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
};
|
|
11
|
+
export default function speedly(config?: InitConfig): express.Express & {
|
|
12
|
+
speedlyConfig: InitConfig;
|
|
13
|
+
registerFallbacks: () => void;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
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.default = speedly;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const defaultConfig = {
|
|
9
|
+
notFoundHandler: true,
|
|
10
|
+
errorHandler: true,
|
|
11
|
+
jsonParser: true,
|
|
12
|
+
urlEncodedParser: true,
|
|
13
|
+
cookieParser: true,
|
|
14
|
+
staticFiles: true,
|
|
15
|
+
};
|
|
16
|
+
function speedly(config = {}) {
|
|
17
|
+
const finalConfig = { ...defaultConfig, ...config };
|
|
18
|
+
const app = (0, express_1.default)();
|
|
19
|
+
if (finalConfig.jsonParser)
|
|
20
|
+
app.use(express_1.default.json());
|
|
21
|
+
if (finalConfig.urlEncodedParser)
|
|
22
|
+
app.use(express_1.default.urlencoded({ extended: true }));
|
|
23
|
+
if (finalConfig.cookieParser) {
|
|
24
|
+
try {
|
|
25
|
+
const cookieParser = require("cookie-parser");
|
|
26
|
+
app.use(cookieParser());
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error.code === "MODULE_NOT_FOUND") {
|
|
30
|
+
console.warn("cookie-parser module not found. Please install it to use cookieParser middleware.");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (finalConfig.staticFiles)
|
|
35
|
+
app.use("/static", express_1.default.static("public"));
|
|
36
|
+
// user can call this manually if needed
|
|
37
|
+
const registerFallbacks = () => {
|
|
38
|
+
if (finalConfig.notFoundHandler) {
|
|
39
|
+
app.use((req, res) => res.status(404).json({ message: "Not Found" }));
|
|
40
|
+
}
|
|
41
|
+
if (finalConfig.errorHandler) {
|
|
42
|
+
app.use((error, req, res, next) => {
|
|
43
|
+
console.error("Speedly Error:", error);
|
|
44
|
+
res.status(500).json({ message: "Internal Server Error" });
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
// ⛔ we intercept listen, add fallback before server starts
|
|
49
|
+
const originalListen = app.listen.bind(app);
|
|
50
|
+
app.listen = (...args) => {
|
|
51
|
+
registerFallbacks();
|
|
52
|
+
return originalListen(...args);
|
|
53
|
+
};
|
|
54
|
+
// 🔓 expose config + extend points for override
|
|
55
|
+
app.speedlyConfig = finalConfig;
|
|
56
|
+
app.registerFallbacks = registerFallbacks;
|
|
57
|
+
return app;
|
|
58
|
+
}
|