speedly 1.1.0 → 1.1.2
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/util/translator.js +0 -3
- package/dist/esm/auth/auth.js +7 -2
- package/dist/esm/db/db.js +19 -14
- package/dist/esm/index.js +44 -5
- package/dist/esm/model/translation.d.ts +58 -1
- package/dist/esm/model/translation.js +9 -4
- package/dist/esm/uploader/uploader.js +27 -22
- package/dist/esm/util/getConfig.js +8 -3
- package/dist/esm/util/strToObj.js +3 -1
- package/dist/esm/util/translator.d.ts +1 -1
- package/dist/esm/util/translator.js +14 -12
- package/dist/esm/validator/validator.js +5 -1
- package/package.json +1 -1
|
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const axios_1 = __importDefault(require("axios"));
|
|
7
7
|
const translation_1 = __importDefault(require("./../model/translation"));
|
|
8
|
-
const translate_1 = require("translate");
|
|
9
8
|
const getConfig_1 = __importDefault(require("./getConfig"));
|
|
10
9
|
const configs = { ...(0, getConfig_1.default)('translate') };
|
|
11
10
|
async function firstSuccessful(promises) {
|
|
@@ -25,7 +24,6 @@ async function firstSuccessful(promises) {
|
|
|
25
24
|
});
|
|
26
25
|
}
|
|
27
26
|
exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
28
|
-
const translateInstance = (0, translate_1.Translate)({ to: lang, engine: "google", });
|
|
29
27
|
const formattedText = text
|
|
30
28
|
.replaceAll(/[\-\_]/g, " ")
|
|
31
29
|
.replaceAll(/[A-Z]/g, " $&");
|
|
@@ -57,7 +55,6 @@ exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
|
57
55
|
throw new Error(res.data.message);
|
|
58
56
|
}
|
|
59
57
|
})());
|
|
60
|
-
translationPromises.push(translateInstance(formattedText));
|
|
61
58
|
try {
|
|
62
59
|
const result = await firstSuccessful(translationPromises);
|
|
63
60
|
await translation_1.default.create({ text: formattedText, lang, translatedText: result });
|
package/dist/esm/auth/auth.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
7
|
+
const gConfig = { admin: { role: 'ADMIN', model: '../models/admin' }, jwtSecretEnv: 'JWT_KEY', customValidator: (req, key) => { return true; }, ...(0, getConfig_1.default)('auth') };
|
|
3
8
|
const auth = (config = gConfig) => {
|
|
4
9
|
// const adminModel = require('../models/admin')
|
|
5
10
|
let handlerState = {};
|
package/dist/esm/db/db.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
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 });
|
|
1
6
|
let __path = "./models/";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const strToObj_1 = __importDefault(require("../util/strToObj"));
|
|
9
|
+
const getConfig_1 = __importDefault(require("../util/getConfig"));
|
|
10
|
+
const translator_1 = __importDefault(require("../util/translator"));
|
|
6
11
|
let configs = {
|
|
7
12
|
dbType: "mongodb",
|
|
8
13
|
path: "../models",
|
|
9
14
|
dbEnv: "DB_URL",
|
|
10
|
-
...
|
|
15
|
+
...(0, getConfig_1.default)("db"),
|
|
11
16
|
};
|
|
12
17
|
const usingMongoDb = (collectionName, config = {}) => {
|
|
13
18
|
let model;
|
|
@@ -16,7 +21,7 @@ const usingMongoDb = (collectionName, config = {}) => {
|
|
|
16
21
|
};
|
|
17
22
|
if (config?.path)
|
|
18
23
|
__path = config.path;
|
|
19
|
-
model = require(
|
|
24
|
+
model = require(path_1.default.join(__path, collectionName));
|
|
20
25
|
const actionHandler = {
|
|
21
26
|
find: (match = {}) => {
|
|
22
27
|
queryState.action = "find";
|
|
@@ -133,7 +138,7 @@ const usingMongoDb = (collectionName, config = {}) => {
|
|
|
133
138
|
const sortStr = Array.isArray(req.query.sort)
|
|
134
139
|
? req.query.sort[0]
|
|
135
140
|
: req.query.sort;
|
|
136
|
-
const sortObject = typeof sortStr === "string" ?
|
|
141
|
+
const sortObject = typeof sortStr === "string" ? (0, strToObj_1.default)(sortStr) : {};
|
|
137
142
|
if (sortQueryIndex == -1)
|
|
138
143
|
realTimeQueries.push({
|
|
139
144
|
type: "sort",
|
|
@@ -350,7 +355,7 @@ const usingMongoDb = (collectionName, config = {}) => {
|
|
|
350
355
|
if (res.logger && req.user) {
|
|
351
356
|
if (queryState.action && queryState.action.match(/create|update|delete/i)?.[0]) {
|
|
352
357
|
res.logger(`${{ log: "لاگها" }[collectionName] ||
|
|
353
|
-
(await
|
|
358
|
+
(await (0, translator_1.default)(`${collectionName}`))} ${data.name || data.title || data.id || data._id || ""} توسط ${(req.user?.firstName ? req.user?.firstName + " " : "") +
|
|
354
359
|
(req.user?.lastName || "") ||
|
|
355
360
|
`ادمین با شماره ${req.user.phoneNumber}`} ${{ create: "ایجاد", update: "بروزرسانی", delete: "حذف", nothing: '' }[queryState.action
|
|
356
361
|
.match(/create|update|delete/i)?.[0]
|
|
@@ -362,7 +367,7 @@ const usingMongoDb = (collectionName, config = {}) => {
|
|
|
362
367
|
req.document._doc[key]?.toString?.() !==
|
|
363
368
|
value?.toString?.())
|
|
364
369
|
.map(async (item) => {
|
|
365
|
-
const translatedField = await
|
|
370
|
+
const translatedField = await (0, translator_1.default)(item[0], "fa");
|
|
366
371
|
if (req.document[item[0]] == undefined) {
|
|
367
372
|
return `فیلد ${translatedField} اضافه شد`;
|
|
368
373
|
}
|
|
@@ -374,7 +379,7 @@ const usingMongoDb = (collectionName, config = {}) => {
|
|
|
374
379
|
? false
|
|
375
380
|
: data._doc[key] == undefined)
|
|
376
381
|
.map(async (item) => {
|
|
377
|
-
const translatedField = await
|
|
382
|
+
const translatedField = await (0, translator_1.default)(item[0], "fa");
|
|
378
383
|
return `فیلد ${translatedField} حذف شد`;
|
|
379
384
|
})))
|
|
380
385
|
: []);
|
|
@@ -413,14 +418,14 @@ const usingMongoDb = (collectionName, config = {}) => {
|
|
|
413
418
|
handler.select = (value) => {
|
|
414
419
|
queryState.queries.push({
|
|
415
420
|
type: "select",
|
|
416
|
-
value: typeof value == "string" ?
|
|
421
|
+
value: typeof value == "string" ? (0, strToObj_1.default)(value, 0) : value,
|
|
417
422
|
});
|
|
418
423
|
return handler;
|
|
419
424
|
};
|
|
420
425
|
handler.sort = (value) => {
|
|
421
426
|
queryState.queries.push({
|
|
422
427
|
type: "sort",
|
|
423
|
-
value: typeof value == "string" ?
|
|
428
|
+
value: typeof value == "string" ? (0, strToObj_1.default)(value) : value,
|
|
424
429
|
});
|
|
425
430
|
return handler;
|
|
426
431
|
};
|
|
@@ -465,7 +470,7 @@ const db = (collectionName, config = configs) => {
|
|
|
465
470
|
dbType: "mongodb",
|
|
466
471
|
path: "../models",
|
|
467
472
|
dbEnv: "DB_URL",
|
|
468
|
-
...
|
|
473
|
+
...(0, getConfig_1.default)("db"),
|
|
469
474
|
};
|
|
470
475
|
Object.entries(config).forEach(([key, value]) => {
|
|
471
476
|
generatedConfig[key] = value;
|
|
@@ -477,4 +482,4 @@ const db = (collectionName, config = configs) => {
|
|
|
477
482
|
// return usingMySql(collectionName, generatedConfig);
|
|
478
483
|
}
|
|
479
484
|
};
|
|
480
|
-
|
|
485
|
+
exports.default = db;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.validator = exports.uploader = exports.db = exports.auth = void 0;
|
|
37
|
+
const auth = __importStar(require("./auth/auth"));
|
|
38
|
+
exports.auth = auth;
|
|
39
|
+
const db = __importStar(require("./db/db"));
|
|
40
|
+
exports.db = db;
|
|
41
|
+
const uploader = __importStar(require("./uploader/uploader"));
|
|
42
|
+
exports.uploader = uploader;
|
|
43
|
+
const validator = __importStar(require("./validator/validator"));
|
|
44
|
+
exports.validator = validator;
|
|
@@ -1,2 +1,59 @@
|
|
|
1
|
-
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
declare const model: mongoose.Model<{
|
|
3
|
+
createdAt: NativeDate;
|
|
4
|
+
updatedAt: NativeDate;
|
|
5
|
+
} & {
|
|
6
|
+
text: string;
|
|
7
|
+
lang: string;
|
|
8
|
+
translatedText: string;
|
|
9
|
+
}, {}, {}, {}, mongoose.Document<unknown, {}, {
|
|
10
|
+
createdAt: NativeDate;
|
|
11
|
+
updatedAt: NativeDate;
|
|
12
|
+
} & {
|
|
13
|
+
text: string;
|
|
14
|
+
lang: string;
|
|
15
|
+
translatedText: string;
|
|
16
|
+
}, {}, {
|
|
17
|
+
timestamps: true;
|
|
18
|
+
}> & {
|
|
19
|
+
createdAt: NativeDate;
|
|
20
|
+
updatedAt: NativeDate;
|
|
21
|
+
} & {
|
|
22
|
+
text: string;
|
|
23
|
+
lang: string;
|
|
24
|
+
translatedText: string;
|
|
25
|
+
} & {
|
|
26
|
+
_id: mongoose.Types.ObjectId;
|
|
27
|
+
} & {
|
|
28
|
+
__v: number;
|
|
29
|
+
}, mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
|
|
30
|
+
timestamps: true;
|
|
31
|
+
}, {
|
|
32
|
+
createdAt: NativeDate;
|
|
33
|
+
updatedAt: NativeDate;
|
|
34
|
+
} & {
|
|
35
|
+
text: string;
|
|
36
|
+
lang: string;
|
|
37
|
+
translatedText: string;
|
|
38
|
+
}, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
|
|
39
|
+
createdAt: NativeDate;
|
|
40
|
+
updatedAt: NativeDate;
|
|
41
|
+
} & {
|
|
42
|
+
text: string;
|
|
43
|
+
lang: string;
|
|
44
|
+
translatedText: string;
|
|
45
|
+
}>, {}, mongoose.ResolveSchemaOptions<{
|
|
46
|
+
timestamps: true;
|
|
47
|
+
}>> & mongoose.FlatRecord<{
|
|
48
|
+
createdAt: NativeDate;
|
|
49
|
+
updatedAt: NativeDate;
|
|
50
|
+
} & {
|
|
51
|
+
text: string;
|
|
52
|
+
lang: string;
|
|
53
|
+
translatedText: string;
|
|
54
|
+
}> & {
|
|
55
|
+
_id: mongoose.Types.ObjectId;
|
|
56
|
+
} & {
|
|
57
|
+
__v: number;
|
|
58
|
+
}>>;
|
|
2
59
|
export default model;
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 = __importDefault(require("mongoose"));
|
|
7
|
+
const schema = new mongoose_1.default.Schema({
|
|
3
8
|
text: { type: String, required: true },
|
|
4
9
|
lang: { type: String, required: true, },
|
|
5
10
|
translatedText: { type: String, required: true },
|
|
6
11
|
}, { timestamps: true });
|
|
7
|
-
const model =
|
|
8
|
-
|
|
12
|
+
const model = mongoose_1.default.model('translation', schema);
|
|
13
|
+
exports.default = model;
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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"));
|
|
5
10
|
const getConfig = require("../util/getConfig");
|
|
6
11
|
const relativePath = '../../../public';
|
|
7
12
|
let configs = {
|
|
@@ -18,22 +23,22 @@ module.exports = (destination = "/image", config = configs) => {
|
|
|
18
23
|
Object.entries(config).forEach(([key, val]) => {
|
|
19
24
|
configs[key] = val;
|
|
20
25
|
});
|
|
21
|
-
const storage =
|
|
26
|
+
const storage = multer_1.default.diskStorage({
|
|
22
27
|
destination: function (req, file, cb) {
|
|
23
28
|
try {
|
|
24
29
|
dest = typeof destination === "function"
|
|
25
30
|
? destination(req, file)
|
|
26
31
|
: destination;
|
|
27
32
|
const splitPath = dest.split("/");
|
|
28
|
-
let currentPath =
|
|
33
|
+
let currentPath = path_1.default.join(__dirname, relativePath);
|
|
29
34
|
splitPath.forEach(folder => {
|
|
30
|
-
currentPath =
|
|
31
|
-
console.log('uploader', 39, currentPath, !
|
|
32
|
-
if (!
|
|
33
|
-
|
|
35
|
+
currentPath = path_1.default.join(currentPath, folder);
|
|
36
|
+
console.log('uploader', 39, currentPath, !fs_1.default.existsSync(currentPath));
|
|
37
|
+
if (!fs_1.default.existsSync(currentPath)) {
|
|
38
|
+
fs_1.default.mkdirSync(currentPath);
|
|
34
39
|
}
|
|
35
40
|
});
|
|
36
|
-
cb(null,
|
|
41
|
+
cb(null, path_1.default.join(__dirname, relativePath, dest));
|
|
37
42
|
}
|
|
38
43
|
catch (err) {
|
|
39
44
|
cb(err, "");
|
|
@@ -41,22 +46,22 @@ module.exports = (destination = "/image", config = configs) => {
|
|
|
41
46
|
},
|
|
42
47
|
filename: function (req, file, cb) {
|
|
43
48
|
try {
|
|
44
|
-
const ext =
|
|
49
|
+
const ext = path_1.default.extname(file.originalname);
|
|
45
50
|
if (!ext.slice(1).match(configs.format)) {
|
|
46
51
|
return cb(new Error("File format not acceptable"), '');
|
|
47
52
|
}
|
|
48
53
|
const originalName = Buffer.from(file.originalname, "latin1").toString("utf8");
|
|
49
54
|
const fileName = (configs.prefix ? configs.prefix + "-" : "") +
|
|
50
55
|
originalName.replace(/\.\w+$/, "") + ext;
|
|
51
|
-
const filePath =
|
|
56
|
+
const filePath = path_1.default.join(__dirname, relativePath, dest, fileName);
|
|
52
57
|
console.log('uploader', 65, filePath);
|
|
53
58
|
try {
|
|
54
|
-
|
|
59
|
+
fs_1.default.existsSync(filePath);
|
|
55
60
|
}
|
|
56
61
|
catch (error) {
|
|
57
62
|
console.log('uploader', 70, error);
|
|
58
63
|
}
|
|
59
|
-
if (
|
|
64
|
+
if (fs_1.default.existsSync(filePath)) {
|
|
60
65
|
return cb(new Error("File already exists in the destination folder"), '');
|
|
61
66
|
}
|
|
62
67
|
cb(null, fileName);
|
|
@@ -66,7 +71,7 @@ module.exports = (destination = "/image", config = configs) => {
|
|
|
66
71
|
}
|
|
67
72
|
},
|
|
68
73
|
});
|
|
69
|
-
const uploader =
|
|
74
|
+
const uploader = (0, multer_1.default)({
|
|
70
75
|
storage,
|
|
71
76
|
limits: { fileSize: (configs.limit || 5) * 1024 * 1024 }, // MB
|
|
72
77
|
});
|
|
@@ -79,11 +84,11 @@ module.exports = (destination = "/image", config = configs) => {
|
|
|
79
84
|
}
|
|
80
85
|
if (req.file) {
|
|
81
86
|
if (configs.saveInDb) {
|
|
82
|
-
const db =
|
|
87
|
+
const db = mongoose_1.default.connection;
|
|
83
88
|
const collection = db.collection("media");
|
|
84
89
|
const duplicate = await collection.findOne({ alt: req.body.alt });
|
|
85
90
|
if (duplicate) {
|
|
86
|
-
|
|
91
|
+
fs_1.default.rmSync(req.file.path);
|
|
87
92
|
return res.status(405).json({ message: "alt is repetitive" });
|
|
88
93
|
}
|
|
89
94
|
const mediaDoc = await collection.insertOne({
|
|
@@ -97,8 +102,8 @@ module.exports = (destination = "/image", config = configs) => {
|
|
|
97
102
|
console.log('uploader', 101, mediaDoc);
|
|
98
103
|
req.mediaId = mediaDoc.insertedId;
|
|
99
104
|
}
|
|
100
|
-
req.body[fieldName] =
|
|
101
|
-
.join("/static",
|
|
105
|
+
req.body[fieldName] = path_1.default
|
|
106
|
+
.join("/static", path_1.default.relative(path_1.default.join(__dirname, relativePath), req.file.path))
|
|
102
107
|
.replaceAll(/\\/g, "/");
|
|
103
108
|
}
|
|
104
109
|
next();
|
|
@@ -109,8 +114,8 @@ module.exports = (destination = "/image", config = configs) => {
|
|
|
109
114
|
if (err)
|
|
110
115
|
return res.status(405).json({ message: err.message });
|
|
111
116
|
if (req.files && Array.isArray(req.files) && req.files.length) {
|
|
112
|
-
req.body[fieldName] = req.files.map(file =>
|
|
113
|
-
.join("/static",
|
|
117
|
+
req.body[fieldName] = req.files.map(file => path_1.default
|
|
118
|
+
.join("/static", path_1.default.relative(path_1.default.join(__dirname, relativePath), file.path))
|
|
114
119
|
.replaceAll(/\\/g, "/"));
|
|
115
120
|
}
|
|
116
121
|
next();
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
|
|
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 fs_1 = __importDefault(require("fs"));
|
|
2
7
|
// const { path } = require('../../app');
|
|
3
|
-
|
|
8
|
+
exports.default = (configField) => {
|
|
4
9
|
const stackPath = new Error().stack?.split('\n')[2].trim().match(/\(.+\)/)?.[0].replace(/([\(\)]|\:\d+\:\d+)/g, '');
|
|
5
10
|
for (let i = 1; i < 4; i++) {
|
|
6
11
|
const filePath = stackPath?.replace(/\\/g, '/').split('/').slice(0, -i).join('/') + '/speedly.config.js';
|
|
7
|
-
if (
|
|
12
|
+
if (fs_1.default.existsSync(filePath)) {
|
|
8
13
|
const config = require(filePath);
|
|
9
14
|
if (config && config[configField]) {
|
|
10
15
|
return config[configField];
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (value, falseValue = -1, splitValue = ' ') => {
|
|
2
4
|
return value.split(splitValue).reduce((prev, curr) => {
|
|
3
5
|
if (curr[0] == '-')
|
|
4
6
|
return { ...prev, [curr.slice(1)]: falseValue };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: (text?: string, lang?: string) => Promise<
|
|
1
|
+
declare const _default: (text?: string, lang?: string) => Promise<unknown>;
|
|
2
2
|
export default _default;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 axios_1 = __importDefault(require("axios"));
|
|
7
|
+
const translation_1 = __importDefault(require("./../model/translation"));
|
|
8
|
+
const getConfig_1 = __importDefault(require("./getConfig"));
|
|
9
|
+
const configs = { ...(0, getConfig_1.default)('translate') };
|
|
6
10
|
async function firstSuccessful(promises) {
|
|
7
11
|
return new Promise((resolve, reject) => {
|
|
8
12
|
let rejections = 0;
|
|
@@ -19,24 +23,23 @@ async function firstSuccessful(promises) {
|
|
|
19
23
|
});
|
|
20
24
|
});
|
|
21
25
|
}
|
|
22
|
-
|
|
23
|
-
const translateInstance = Translate({ to: lang, engine: "google", });
|
|
26
|
+
exports.default = async (text = "unspecified text", lang = "fa") => {
|
|
24
27
|
const formattedText = text
|
|
25
28
|
.replaceAll(/[\-\_]/g, " ")
|
|
26
29
|
.replaceAll(/[A-Z]/g, " $&");
|
|
27
|
-
const translationDoc = await
|
|
30
|
+
const translationDoc = await translation_1.default.findOne({ text: formattedText, lang });
|
|
28
31
|
if (translationDoc)
|
|
29
32
|
return translationDoc.translatedText;
|
|
30
33
|
const translationPromises = [];
|
|
31
34
|
translationPromises.push((async () => {
|
|
32
|
-
const res = await
|
|
35
|
+
const res = await axios_1.default.get(`https://655.mtis.workers.dev/translate?text=${encodeURIComponent(formattedText)}&source_lang=en&target_lang=${lang}`, { timeout: 2000 });
|
|
33
36
|
if (!res.data?.response?.translated_text)
|
|
34
37
|
throw new Error("Translation failed");
|
|
35
38
|
return res.data.response.translated_text;
|
|
36
39
|
})());
|
|
37
40
|
if (configs.one_api_token)
|
|
38
41
|
translationPromises.push((async () => {
|
|
39
|
-
const res = await
|
|
42
|
+
const res = await axios_1.default.post("https://api.one-api.ir/translate/v1/microsoft", {
|
|
40
43
|
text: formattedText,
|
|
41
44
|
target: lang,
|
|
42
45
|
}, {
|
|
@@ -52,10 +55,9 @@ export default async (text = "unspecified text", lang = "fa") => {
|
|
|
52
55
|
throw new Error(res.data.message);
|
|
53
56
|
}
|
|
54
57
|
})());
|
|
55
|
-
translationPromises.push(translateInstance(formattedText));
|
|
56
58
|
try {
|
|
57
59
|
const result = await firstSuccessful(translationPromises);
|
|
58
|
-
await
|
|
60
|
+
await translation_1.default.create({ text: formattedText, lang, translatedText: result });
|
|
59
61
|
return result;
|
|
60
62
|
}
|
|
61
63
|
catch (err) {
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validator = void 0;
|
|
1
4
|
// این تابع validator generic هست
|
|
2
|
-
|
|
5
|
+
const validator = (schemas) => async (req, res, next) => {
|
|
3
6
|
try {
|
|
4
7
|
if (schemas.body) {
|
|
5
8
|
req.body = (await schemas.body.validate(req.body, {
|
|
@@ -26,3 +29,4 @@ export const validator = (schemas) => async (req, res, next) => {
|
|
|
26
29
|
});
|
|
27
30
|
}
|
|
28
31
|
};
|
|
32
|
+
exports.validator = validator;
|