speedly 2.0.38 → 2.0.42

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.
Files changed (77) hide show
  1. package/README.md +470 -171
  2. package/dist/cjs/config/init.js +2 -2
  3. package/dist/cjs/kit/db/db.js +16 -0
  4. package/dist/cjs/model/translation.d.ts +37 -25
  5. package/dist/cjs/util/translator.d.ts +1 -1
  6. package/dist/config/init.d.ts +23 -0
  7. package/dist/config/init.js +93 -0
  8. package/dist/document/document.d.ts +5 -0
  9. package/dist/document/document.js +270 -0
  10. package/dist/document/index.d.ts +2 -0
  11. package/dist/document/index.js +7 -0
  12. package/dist/document/parser.d.ts +1 -0
  13. package/dist/document/parser.js +11 -0
  14. package/dist/esm/config/init.js +2 -2
  15. package/dist/esm/kit/db/db.js +16 -0
  16. package/dist/esm/model/translation.d.ts +37 -25
  17. package/dist/esm/util/translator.d.ts +1 -1
  18. package/dist/index.d.ts +5 -0
  19. package/dist/index.js +13 -0
  20. package/dist/{cjs → kit}/auth/auth.js +1 -1
  21. package/dist/{cjs → kit}/db/db.d.ts +15 -0
  22. package/dist/{cjs → kit}/db/db.js +44 -19
  23. package/dist/kit/index.d.ts +5 -0
  24. package/dist/kit/index.js +14 -0
  25. package/dist/{cjs → kit}/uploader/uploader.d.ts +2 -2
  26. package/dist/{esm → kit}/uploader/uploader.js +22 -17
  27. package/dist/model/index.d.ts +2 -0
  28. package/dist/model/index.js +8 -0
  29. package/dist/model/translation.d.ts +71 -0
  30. package/dist/model/translation.js +13 -0
  31. package/dist/modules/index.d.ts +2 -0
  32. package/dist/modules/index.js +8 -0
  33. package/dist/modules/translation/translation.routes.d.ts +2 -0
  34. package/dist/modules/translation/translation.routes.js +24 -0
  35. package/dist/modules/translation/translation.validator.d.ts +15 -0
  36. package/dist/modules/translation/translation.validator.js +22 -0
  37. package/dist/util/getConfig.d.ts +4 -0
  38. package/dist/util/getConfig.js +40 -0
  39. package/dist/util/index.d.ts +2 -0
  40. package/dist/util/index.js +8 -0
  41. package/dist/util/makeOptional.d.ts +10 -0
  42. package/dist/util/makeOptional.js +47 -0
  43. package/dist/util/strToObj.d.ts +2 -0
  44. package/dist/util/strToObj.js +9 -0
  45. package/dist/util/translator.d.ts +2 -0
  46. package/dist/util/translator.js +74 -0
  47. package/examples/blog-routes/blog/blog.routes.js +15 -0
  48. package/examples/blog-routes/blog/blog.validator.js +35 -0
  49. package/examples/blog-routes/role/role.routes.js +14 -0
  50. package/examples/blog-routes/role/role.validator.js +28 -0
  51. package/examples/blog-routes/user/user.controller.js +97 -0
  52. package/examples/blog-routes/user/user.routes.js +18 -0
  53. package/examples/blog-routes/user/user.validator.js +53 -0
  54. package/package.json +65 -66
  55. package/dist/cjs/auth/auth2.d.ts +0 -18
  56. package/dist/cjs/auth/auth2.js +0 -93
  57. package/dist/cjs/uploader/uploader.js +0 -145
  58. package/dist/cjs/yup.config.d.ts +0 -2
  59. package/dist/cjs/yup.config.js +0 -24
  60. package/dist/esm/auth/auth.d.ts +0 -3
  61. package/dist/esm/auth/auth.js +0 -38
  62. package/dist/esm/auth/types.d.ts +0 -19
  63. package/dist/esm/auth/types.js +0 -2
  64. package/dist/esm/db/db.d.ts +0 -182
  65. package/dist/esm/db/db.js +0 -594
  66. package/dist/esm/db/utils.d.ts +0 -3
  67. package/dist/esm/db/utils.js +0 -15
  68. package/dist/esm/uploader/uploader.d.ts +0 -24
  69. package/dist/esm/validator/validator.d.ts +0 -9
  70. package/dist/esm/validator/validator.js +0 -36
  71. /package/dist/{cjs → kit}/auth/auth.d.ts +0 -0
  72. /package/dist/{cjs → kit}/auth/types.d.ts +0 -0
  73. /package/dist/{cjs → kit}/auth/types.js +0 -0
  74. /package/dist/{cjs → kit}/db/utils.d.ts +0 -0
  75. /package/dist/{cjs → kit}/db/utils.js +0 -0
  76. /package/dist/{cjs → kit}/validator/validator.d.ts +0 -0
  77. /package/dist/{cjs → kit}/validator/validator.js +0 -0
@@ -1,145 +0,0 @@
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 = typeof destination === "function"
29
- ? destination(req, file)
30
- : destination;
31
- const splitPath = dest.split("/");
32
- let currentPath = path_1.default.join(configs.path);
33
- splitPath.forEach(folder => {
34
- currentPath = path_1.default.join(currentPath, folder);
35
- if (!fs_1.default.existsSync(currentPath)) {
36
- fs_1.default.mkdirSync(currentPath);
37
- }
38
- });
39
- cb(null, path_1.default.join(configs.path, dest));
40
- }
41
- catch (err) {
42
- cb(err, "");
43
- }
44
- },
45
- filename: function (req, file, cb) {
46
- try {
47
- const ext = path_1.default.extname(file.originalname);
48
- if (!ext.slice(1).match(configs.format)) {
49
- return cb(new Error("File format not acceptable"), '');
50
- }
51
- const originalName = Buffer.from(file.originalname, "latin1").toString("utf8");
52
- const fileName = (configs.prefix ? configs.prefix + "-" : "") +
53
- originalName.replace(/\.\w+$/, "") + ext;
54
- const filePath = path_1.default.join(configs.path, dest, fileName);
55
- try {
56
- fs_1.default.existsSync(filePath);
57
- }
58
- catch (error) {
59
- }
60
- if (fs_1.default.existsSync(filePath)) {
61
- return cb(new Error("File already exists in the destination folder"), '');
62
- }
63
- cb(null, fileName);
64
- }
65
- catch (err) {
66
- cb(err, '');
67
- }
68
- },
69
- });
70
- const uploader = (0, multer_1.default)({
71
- storage,
72
- limits: { fileSize: (configs.limit || 5) * 1024 * 1024 }, // MB
73
- });
74
- return {
75
- single: (fieldName) => (req, res, next) => {
76
- uploader.single(fieldName)(req, res, async (err) => {
77
- if (err) {
78
- console.log('uploader', 85, err);
79
- return next({ status: 405, json: { message: err.message } });
80
- }
81
- if (req.file) {
82
- if (configs.saveInDb) {
83
- const db = mongoose_1.default.connection;
84
- const collection = db.collection("media");
85
- const duplicate = await collection.findOne({ alt: req.body.alt });
86
- if (duplicate) {
87
- fs_1.default.rmSync(req.file.path);
88
- return res.status(405).json({ message: "alt is repetitive" });
89
- }
90
- const mediaDoc = await collection.insertOne({
91
- type: req.file.mimetype.split("/")[0],
92
- name: req.file.filename,
93
- dirPath: dest + "/",
94
- alt: req.body.alt,
95
- desc: req.body.desc,
96
- url: "/static/" + dest + "/" + req.file.filename,
97
- });
98
- req.mediaId = mediaDoc.insertedId;
99
- }
100
- req.body[fieldName] = path_1.default
101
- .join("/static", path_1.default.relative(path_1.default.join(configs.path), req.file.path))
102
- .replaceAll(/\\/g, "/");
103
- }
104
- next();
105
- });
106
- },
107
- array: (fieldName, maxCount = Infinity) => (req, res, next) => {
108
- uploader.array(fieldName, maxCount)(req, res, (err) => {
109
- if (err)
110
- return res.status(405).json({ message: err.message });
111
- if (req.files && Array.isArray(req.files) && req.files.length) {
112
- req.body[fieldName] = req.files.map(file => path_1.default
113
- .join("/static", path_1.default.relative(path_1.default.join(configs.path), file.path))
114
- .replaceAll(/\\/g, "/"));
115
- }
116
- next();
117
- });
118
- },
119
- fields: (fields) => (req, res, next) => {
120
- uploader.fields(fields)(req, res, (err) => {
121
- if (err)
122
- return res.status(405).json({ message: err.message });
123
- next();
124
- });
125
- },
126
- any: () => (req, res, next) => {
127
- uploader.any()(req, res, (err) => {
128
- if (err)
129
- return res.status(405).json({ message: err.message });
130
- next();
131
- });
132
- },
133
- none: () => (req, res, next) => {
134
- uploader.none()(req, res, (err) => {
135
- if (err)
136
- return res.status(405).json({ message: err.message });
137
- next();
138
- });
139
- },
140
- };
141
- }
142
- catch (error) {
143
- console.log("uploader init error", error);
144
- }
145
- };
@@ -1,2 +0,0 @@
1
- import yup from "yup";
2
- export default yup;
@@ -1,24 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- import * as Types from "./types";
2
- declare const auth: Types.Auth;
3
- export default auth;
@@ -1,38 +0,0 @@
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 = {
8
- admin: { role: "ADMIN", model: "../models/admin" },
9
- jwtSecretEnv: "JWT_KEY",
10
- customValidator: (req, key) => {
11
- return true;
12
- },
13
- ...(0, getConfig_1.default)("auth"),
14
- };
15
- const executer = (authType) => {
16
- const mw = async (req, res, next) => {
17
- const accessResult = await gConfig?.customValidator?.(req, authType);
18
- if (accessResult == null)
19
- return next({ status: 401, json: { message: "Unauthorized" } });
20
- if (!accessResult)
21
- return next({ status: 403, json: { message: "Forbidden" } });
22
- return next();
23
- };
24
- Object.defineProperty(mw, "name", { value: `auth:${authType}` });
25
- return mw;
26
- };
27
- const auth = {
28
- user: () => {
29
- return executer("user");
30
- },
31
- admin: (config) => {
32
- return executer(`admin${config?.permission ? `:${config.permission}` : ""}`);
33
- },
34
- any: () => {
35
- return executer("any");
36
- },
37
- };
38
- exports.default = auth;
@@ -1,19 +0,0 @@
1
- import { Request, Response, NextFunction } from 'express';
2
- type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
3
- type Executer = (authField: string) => (req: Request, res: Response, next: NextFunction) => unknown;
4
- type Auth = {
5
- user: () => Handler;
6
- admin: (config?: {
7
- permission: string;
8
- }) => Handler;
9
- any: () => Handler;
10
- };
11
- type ConfigType = {
12
- customValidator?: (req: Request, key: string) => Promise<boolean | null>;
13
- jwtSecretEnv?: string;
14
- admin?: {
15
- role: string;
16
- model: string;
17
- };
18
- };
19
- export { Handler, Executer, Auth, ConfigType };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,182 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- declare module "express-serve-static-core" {
3
- interface Request {
4
- document?: any;
5
- user?: any;
6
- }
7
- interface Response {
8
- logger?: (message: string, details?: any) => void;
9
- success?: (statusCode: number, body: any) => void;
10
- }
11
- }
12
- type ConfigsType = {
13
- dbType?: string;
14
- path?: string;
15
- dbEnv?: string;
16
- type: "internal" | "external";
17
- pagination?: {
18
- quantity?: number;
19
- detailed?: boolean;
20
- [key: string]: unknown;
21
- };
22
- [key: string]: unknown;
23
- };
24
- declare const db: (collectionName: string, config?: ConfigsType) => {
25
- find: (match?: {}) => {
26
- (req: Request, res: Response, next: NextFunction): Promise<void>;
27
- select(value: string | {
28
- [key: string]: -1 | 1;
29
- }): /*elided*/ any;
30
- sort(value: string | {
31
- [key: string]: -1 | 1;
32
- }): /*elided*/ any;
33
- skip(value: number): /*elided*/ any;
34
- limit(value: number): /*elided*/ any;
35
- populate(value: string | object | (string | object)[]): /*elided*/ any;
36
- };
37
- create: (body?: {}) => {
38
- (req: Request, res: Response, next: NextFunction): Promise<void>;
39
- select(value: string | {
40
- [key: string]: -1 | 1;
41
- }): /*elided*/ any;
42
- sort(value: string | {
43
- [key: string]: -1 | 1;
44
- }): /*elided*/ any;
45
- skip(value: number): /*elided*/ any;
46
- limit(value: number): /*elided*/ any;
47
- populate(value: string | object | (string | object)[]): /*elided*/ any;
48
- };
49
- updateOne: (match?: {}, body?: {}) => {
50
- (req: Request, res: Response, next: NextFunction): Promise<void>;
51
- select(value: string | {
52
- [key: string]: -1 | 1;
53
- }): /*elided*/ any;
54
- sort(value: string | {
55
- [key: string]: -1 | 1;
56
- }): /*elided*/ any;
57
- skip(value: number): /*elided*/ any;
58
- limit(value: number): /*elided*/ any;
59
- populate(value: string | object | (string | object)[]): /*elided*/ any;
60
- };
61
- updateMany: (match?: {}, body?: {}) => {
62
- (req: Request, res: Response, next: NextFunction): Promise<void>;
63
- select(value: string | {
64
- [key: string]: -1 | 1;
65
- }): /*elided*/ any;
66
- sort(value: string | {
67
- [key: string]: -1 | 1;
68
- }): /*elided*/ any;
69
- skip(value: number): /*elided*/ any;
70
- limit(value: number): /*elided*/ any;
71
- populate(value: string | object | (string | object)[]): /*elided*/ any;
72
- };
73
- deleteOne: (match?: {}) => {
74
- (req: Request, res: Response, next: NextFunction): Promise<void>;
75
- select(value: string | {
76
- [key: string]: -1 | 1;
77
- }): /*elided*/ any;
78
- sort(value: string | {
79
- [key: string]: -1 | 1;
80
- }): /*elided*/ any;
81
- skip(value: number): /*elided*/ any;
82
- limit(value: number): /*elided*/ any;
83
- populate(value: string | object | (string | object)[]): /*elided*/ any;
84
- };
85
- deleteMany: (match?: {}) => {
86
- (req: Request, res: Response, next: NextFunction): Promise<void>;
87
- select(value: string | {
88
- [key: string]: -1 | 1;
89
- }): /*elided*/ any;
90
- sort(value: string | {
91
- [key: string]: -1 | 1;
92
- }): /*elided*/ any;
93
- skip(value: number): /*elided*/ any;
94
- limit(value: number): /*elided*/ any;
95
- populate(value: string | object | (string | object)[]): /*elided*/ any;
96
- };
97
- findOne: (match?: {}) => {
98
- (req: Request, res: Response, next: NextFunction): Promise<void>;
99
- select(value: string | {
100
- [key: string]: -1 | 1;
101
- }): /*elided*/ any;
102
- sort(value: string | {
103
- [key: string]: -1 | 1;
104
- }): /*elided*/ any;
105
- skip(value: number): /*elided*/ any;
106
- limit(value: number): /*elided*/ any;
107
- populate(value: string | object | (string | object)[]): /*elided*/ any;
108
- };
109
- findOneAndUpdate: (match?: {}, body?: {}) => {
110
- (req: Request, res: Response, next: NextFunction): Promise<void>;
111
- select(value: string | {
112
- [key: string]: -1 | 1;
113
- }): /*elided*/ any;
114
- sort(value: string | {
115
- [key: string]: -1 | 1;
116
- }): /*elided*/ any;
117
- skip(value: number): /*elided*/ any;
118
- limit(value: number): /*elided*/ any;
119
- populate(value: string | object | (string | object)[]): /*elided*/ any;
120
- };
121
- aggregate: (pipeline?: never[]) => {
122
- (req: Request, res: Response, next: NextFunction): Promise<void>;
123
- select(value: string | {
124
- [key: string]: -1 | 1;
125
- }): /*elided*/ any;
126
- sort(value: string | {
127
- [key: string]: -1 | 1;
128
- }): /*elided*/ any;
129
- skip(value: number): /*elided*/ any;
130
- limit(value: number): /*elided*/ any;
131
- populate(value: string | object | (string | object)[]): /*elided*/ any;
132
- };
133
- findOneAndDelete: (match?: {}) => {
134
- (req: Request, res: Response, next: NextFunction): Promise<void>;
135
- select(value: string | {
136
- [key: string]: -1 | 1;
137
- }): /*elided*/ any;
138
- sort(value: string | {
139
- [key: string]: -1 | 1;
140
- }): /*elided*/ any;
141
- skip(value: number): /*elided*/ any;
142
- limit(value: number): /*elided*/ any;
143
- populate(value: string | object | (string | object)[]): /*elided*/ any;
144
- };
145
- findById: (id?: string) => {
146
- (req: Request, res: Response, next: NextFunction): Promise<void>;
147
- select(value: string | {
148
- [key: string]: -1 | 1;
149
- }): /*elided*/ any;
150
- sort(value: string | {
151
- [key: string]: -1 | 1;
152
- }): /*elided*/ any;
153
- skip(value: number): /*elided*/ any;
154
- limit(value: number): /*elided*/ any;
155
- populate(value: string | object | (string | object)[]): /*elided*/ any;
156
- };
157
- findByIdAndUpdate: (id?: string, body?: {}) => {
158
- (req: Request, res: Response, next: NextFunction): Promise<void>;
159
- select(value: string | {
160
- [key: string]: -1 | 1;
161
- }): /*elided*/ any;
162
- sort(value: string | {
163
- [key: string]: -1 | 1;
164
- }): /*elided*/ any;
165
- skip(value: number): /*elided*/ any;
166
- limit(value: number): /*elided*/ any;
167
- populate(value: string | object | (string | object)[]): /*elided*/ any;
168
- };
169
- findByIdAndDelete: (id?: string) => {
170
- (req: Request, res: Response, next: NextFunction): Promise<void>;
171
- select(value: string | {
172
- [key: string]: -1 | 1;
173
- }): /*elided*/ any;
174
- sort(value: string | {
175
- [key: string]: -1 | 1;
176
- }): /*elided*/ any;
177
- skip(value: number): /*elided*/ any;
178
- limit(value: number): /*elided*/ any;
179
- populate(value: string | object | (string | object)[]): /*elided*/ any;
180
- };
181
- } | undefined;
182
- export default db;