speedly 1.3.0 → 2.0.6

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 (67) hide show
  1. package/dist/cjs/auth/auth2.d.ts +18 -0
  2. package/dist/cjs/auth/auth2.js +93 -0
  3. package/dist/cjs/config/init.d.ts +15 -0
  4. package/dist/cjs/config/init.js +58 -0
  5. package/dist/cjs/db/db.d.ts +65 -67
  6. package/dist/cjs/db/db.js +15 -30
  7. package/dist/cjs/document/index.d.ts +2 -0
  8. package/dist/cjs/document/index.js +7 -0
  9. package/dist/cjs/index.d.ts +3 -53
  10. package/dist/cjs/index.js +9 -22
  11. package/dist/cjs/kit/auth/auth.d.ts +3 -0
  12. package/dist/cjs/kit/auth/auth.js +38 -0
  13. package/dist/cjs/kit/auth/types.d.ts +19 -0
  14. package/dist/cjs/kit/auth/types.js +2 -0
  15. package/dist/cjs/kit/db/db.d.ts +182 -0
  16. package/dist/cjs/kit/db/db.js +594 -0
  17. package/dist/cjs/kit/db/utils.d.ts +3 -0
  18. package/dist/cjs/kit/db/utils.js +15 -0
  19. package/dist/cjs/kit/index.d.ts +5 -0
  20. package/dist/cjs/kit/index.js +14 -0
  21. package/dist/cjs/kit/uploader/uploader.d.ts +24 -0
  22. package/dist/cjs/kit/uploader/uploader.js +148 -0
  23. package/dist/cjs/kit/validator/validator.d.ts +9 -0
  24. package/dist/cjs/kit/validator/validator.js +36 -0
  25. package/dist/cjs/model/index.d.ts +2 -0
  26. package/dist/cjs/model/index.js +8 -0
  27. package/dist/cjs/model/translation.d.ts +24 -6
  28. package/dist/cjs/modules/index.d.ts +2 -0
  29. package/dist/cjs/modules/index.js +8 -0
  30. package/dist/cjs/modules/translation/translation.routes.js +12 -8
  31. package/dist/cjs/util/index.d.ts +2 -0
  32. package/dist/cjs/util/index.js +8 -0
  33. package/dist/cjs/util/makeOptional.js +17 -7
  34. package/dist/cjs/yup.config.d.ts +2 -0
  35. package/dist/cjs/yup.config.js +24 -0
  36. package/dist/esm/config/init.d.ts +15 -0
  37. package/dist/esm/config/init.js +58 -0
  38. package/dist/esm/db/db.d.ts +65 -67
  39. package/dist/esm/db/db.js +15 -30
  40. package/dist/esm/document/index.d.ts +2 -0
  41. package/dist/esm/document/index.js +7 -0
  42. package/dist/esm/index.d.ts +3 -53
  43. package/dist/esm/index.js +9 -22
  44. package/dist/esm/kit/auth/auth.d.ts +3 -0
  45. package/dist/esm/kit/auth/auth.js +38 -0
  46. package/dist/esm/kit/auth/types.d.ts +19 -0
  47. package/dist/esm/kit/auth/types.js +2 -0
  48. package/dist/esm/kit/db/db.d.ts +182 -0
  49. package/dist/esm/kit/db/db.js +594 -0
  50. package/dist/esm/kit/db/utils.d.ts +3 -0
  51. package/dist/esm/kit/db/utils.js +15 -0
  52. package/dist/esm/kit/index.d.ts +5 -0
  53. package/dist/esm/kit/index.js +14 -0
  54. package/dist/esm/kit/uploader/uploader.d.ts +24 -0
  55. package/dist/esm/kit/uploader/uploader.js +148 -0
  56. package/dist/esm/kit/validator/validator.d.ts +9 -0
  57. package/dist/esm/kit/validator/validator.js +36 -0
  58. package/dist/esm/model/index.d.ts +2 -0
  59. package/dist/esm/model/index.js +8 -0
  60. package/dist/esm/model/translation.d.ts +24 -6
  61. package/dist/esm/modules/index.d.ts +2 -0
  62. package/dist/esm/modules/index.js +8 -0
  63. package/dist/esm/modules/translation/translation.routes.js +12 -8
  64. package/dist/esm/util/index.d.ts +2 -0
  65. package/dist/esm/util/index.js +8 -0
  66. package/dist/esm/util/makeOptional.js +17 -7
  67. package/package.json +1 -1
@@ -0,0 +1,18 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ type Handler = (req: Request, res: Response, next: (errorMessage?: string) => unknown) => unknown;
3
+ type UseAuth = (req: Request, res: Response, next: NextFunction) => UseAuth | {
4
+ user?: (...handlers: Handler[]) => unknown;
5
+ admin?: (...handlers: [{
6
+ permission: string;
7
+ }, ...Handler[]] | Handler[]) => unknown;
8
+ any?: (...handlers: Handler[]) => unknown;
9
+ };
10
+ declare const auth: (config?: {
11
+ admin: {
12
+ role: string;
13
+ model: string;
14
+ };
15
+ jwtSecretEnv: string;
16
+ customValidator: (req: Request, key: string) => boolean;
17
+ }) => UseAuth;
18
+ export default auth;
@@ -0,0 +1,93 @@
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 holders = {};
16
+ const auth = (config = gConfig) => {
17
+ // const adminModel = require('../models/admin')
18
+ let handlerState = {};
19
+ let useAuth = async (req, res, next) => {
20
+ try {
21
+ const nextFunc = (handlers, index = 0) => (errorMessage = "") => {
22
+ if (errorMessage)
23
+ return next(errorMessage);
24
+ if (!handlers.length || !handlers[index + 1])
25
+ return next();
26
+ handlers[index + 1](req, res, nextFunc(handlers, index + 1));
27
+ };
28
+ const keys = Object.keys(handlerState);
29
+ for (let i = 0; i < keys.length; i++) {
30
+ const key = keys[i];
31
+ if (!handlerState[key]?.handlers?.length)
32
+ continue;
33
+ if (await gConfig?.customValidator?.(req, key)) {
34
+ return await handlerState[key].handlers[0](req, res, nextFunc(handlerState[key].handlers));
35
+ }
36
+ else if ((await gConfig?.customValidator?.(req, key)) == null) {
37
+ return next({ status: 401, json: { message: "unauthorized" } });
38
+ }
39
+ else if (i === keys.length - 1) {
40
+ next({
41
+ status: 405,
42
+ json: { message: "you don't have access to this section" },
43
+ });
44
+ }
45
+ else
46
+ continue;
47
+ }
48
+ }
49
+ catch (error) {
50
+ console.log("auth", 42, error);
51
+ next({
52
+ status: 403,
53
+ json: {
54
+ message: error instanceof Error
55
+ ? error.message
56
+ : "error on authentication please login again",
57
+ },
58
+ });
59
+ }
60
+ };
61
+ holders.admin = (...handlers) => {
62
+ if (!Array.isArray(handlers))
63
+ throw new Error("handlers must be an array");
64
+ const hasConfig = typeof handlers[0] === "object" && "permission" in handlers[0];
65
+ const configObj = hasConfig
66
+ ? handlers[0]
67
+ : undefined;
68
+ const handlerFns = hasConfig
69
+ ? handlers.slice(1)
70
+ : handlers;
71
+ handlerState[`admin${configObj?.permission ? `:${configObj.permission}` : ""}`] = {
72
+ ...(configObj ? { config: configObj } : {}),
73
+ handlers: handlerFns,
74
+ };
75
+ return useAuth;
76
+ };
77
+ holders.user = (...handlers) => {
78
+ if (!Array.isArray(handlers))
79
+ throw new Error("handlers must be an array");
80
+ handlerState.user = { handlers };
81
+ return useAuth;
82
+ };
83
+ holders.any = (...handlers) => {
84
+ if (!Array.isArray(handlers))
85
+ throw new Error("handlers must be an array");
86
+ handlerState.any = { handlers };
87
+ return useAuth;
88
+ };
89
+ useAuth = Object.assign(useAuth, holders);
90
+ return useAuth;
91
+ };
92
+ console.log("auth", 81, typeof auth);
93
+ exports.default = auth;
@@ -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
+ }
@@ -13,8 +13,6 @@ type ConfigsType = {
13
13
  dbType?: string;
14
14
  path?: string;
15
15
  dbEnv?: string;
16
- response?: boolean;
17
- first?: boolean;
18
16
  type: "internal" | "external";
19
17
  pagination?: {
20
18
  quantity?: number;
@@ -28,157 +26,157 @@ declare const db: (collectionName: string, config?: ConfigsType) => {
28
26
  (req: Request, res: Response, next: NextFunction): Promise<void>;
29
27
  select(value: string | {
30
28
  [key: string]: -1 | 1;
31
- }): any;
29
+ }): /*elided*/ any;
32
30
  sort(value: string | {
33
31
  [key: string]: -1 | 1;
34
- }): any;
35
- skip(value: number): any;
36
- limit(value: number): any;
37
- populate(value: string | object | (string | object)[]): any;
32
+ }): /*elided*/ any;
33
+ skip(value: number): /*elided*/ any;
34
+ limit(value: number): /*elided*/ any;
35
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
38
36
  };
39
37
  create: (body?: {}) => {
40
38
  (req: Request, res: Response, next: NextFunction): Promise<void>;
41
39
  select(value: string | {
42
40
  [key: string]: -1 | 1;
43
- }): any;
41
+ }): /*elided*/ any;
44
42
  sort(value: string | {
45
43
  [key: string]: -1 | 1;
46
- }): any;
47
- skip(value: number): any;
48
- limit(value: number): any;
49
- populate(value: string | object | (string | object)[]): any;
44
+ }): /*elided*/ any;
45
+ skip(value: number): /*elided*/ any;
46
+ limit(value: number): /*elided*/ any;
47
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
50
48
  };
51
49
  updateOne: (match?: {}, body?: {}) => {
52
50
  (req: Request, res: Response, next: NextFunction): Promise<void>;
53
51
  select(value: string | {
54
52
  [key: string]: -1 | 1;
55
- }): any;
53
+ }): /*elided*/ any;
56
54
  sort(value: string | {
57
55
  [key: string]: -1 | 1;
58
- }): any;
59
- skip(value: number): any;
60
- limit(value: number): any;
61
- populate(value: string | object | (string | object)[]): any;
56
+ }): /*elided*/ any;
57
+ skip(value: number): /*elided*/ any;
58
+ limit(value: number): /*elided*/ any;
59
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
62
60
  };
63
61
  updateMany: (match?: {}, body?: {}) => {
64
62
  (req: Request, res: Response, next: NextFunction): Promise<void>;
65
63
  select(value: string | {
66
64
  [key: string]: -1 | 1;
67
- }): any;
65
+ }): /*elided*/ any;
68
66
  sort(value: string | {
69
67
  [key: string]: -1 | 1;
70
- }): any;
71
- skip(value: number): any;
72
- limit(value: number): any;
73
- populate(value: string | object | (string | object)[]): any;
68
+ }): /*elided*/ any;
69
+ skip(value: number): /*elided*/ any;
70
+ limit(value: number): /*elided*/ any;
71
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
74
72
  };
75
73
  deleteOne: (match?: {}) => {
76
74
  (req: Request, res: Response, next: NextFunction): Promise<void>;
77
75
  select(value: string | {
78
76
  [key: string]: -1 | 1;
79
- }): any;
77
+ }): /*elided*/ any;
80
78
  sort(value: string | {
81
79
  [key: string]: -1 | 1;
82
- }): any;
83
- skip(value: number): any;
84
- limit(value: number): any;
85
- populate(value: string | object | (string | object)[]): any;
80
+ }): /*elided*/ any;
81
+ skip(value: number): /*elided*/ any;
82
+ limit(value: number): /*elided*/ any;
83
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
86
84
  };
87
85
  deleteMany: (match?: {}) => {
88
86
  (req: Request, res: Response, next: NextFunction): Promise<void>;
89
87
  select(value: string | {
90
88
  [key: string]: -1 | 1;
91
- }): any;
89
+ }): /*elided*/ any;
92
90
  sort(value: string | {
93
91
  [key: string]: -1 | 1;
94
- }): any;
95
- skip(value: number): any;
96
- limit(value: number): any;
97
- populate(value: string | object | (string | object)[]): any;
92
+ }): /*elided*/ any;
93
+ skip(value: number): /*elided*/ any;
94
+ limit(value: number): /*elided*/ any;
95
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
98
96
  };
99
97
  findOne: (match?: {}) => {
100
98
  (req: Request, res: Response, next: NextFunction): Promise<void>;
101
99
  select(value: string | {
102
100
  [key: string]: -1 | 1;
103
- }): any;
101
+ }): /*elided*/ any;
104
102
  sort(value: string | {
105
103
  [key: string]: -1 | 1;
106
- }): any;
107
- skip(value: number): any;
108
- limit(value: number): any;
109
- populate(value: string | object | (string | object)[]): any;
104
+ }): /*elided*/ any;
105
+ skip(value: number): /*elided*/ any;
106
+ limit(value: number): /*elided*/ any;
107
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
110
108
  };
111
109
  findOneAndUpdate: (match?: {}, body?: {}) => {
112
110
  (req: Request, res: Response, next: NextFunction): Promise<void>;
113
111
  select(value: string | {
114
112
  [key: string]: -1 | 1;
115
- }): any;
113
+ }): /*elided*/ any;
116
114
  sort(value: string | {
117
115
  [key: string]: -1 | 1;
118
- }): any;
119
- skip(value: number): any;
120
- limit(value: number): any;
121
- populate(value: string | object | (string | object)[]): any;
116
+ }): /*elided*/ any;
117
+ skip(value: number): /*elided*/ any;
118
+ limit(value: number): /*elided*/ any;
119
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
122
120
  };
123
121
  aggregate: (pipeline?: never[]) => {
124
122
  (req: Request, res: Response, next: NextFunction): Promise<void>;
125
123
  select(value: string | {
126
124
  [key: string]: -1 | 1;
127
- }): any;
125
+ }): /*elided*/ any;
128
126
  sort(value: string | {
129
127
  [key: string]: -1 | 1;
130
- }): any;
131
- skip(value: number): any;
132
- limit(value: number): any;
133
- populate(value: string | object | (string | object)[]): any;
128
+ }): /*elided*/ any;
129
+ skip(value: number): /*elided*/ any;
130
+ limit(value: number): /*elided*/ any;
131
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
134
132
  };
135
133
  findOneAndDelete: (match?: {}) => {
136
134
  (req: Request, res: Response, next: NextFunction): Promise<void>;
137
135
  select(value: string | {
138
136
  [key: string]: -1 | 1;
139
- }): any;
137
+ }): /*elided*/ any;
140
138
  sort(value: string | {
141
139
  [key: string]: -1 | 1;
142
- }): any;
143
- skip(value: number): any;
144
- limit(value: number): any;
145
- populate(value: string | object | (string | object)[]): any;
140
+ }): /*elided*/ any;
141
+ skip(value: number): /*elided*/ any;
142
+ limit(value: number): /*elided*/ any;
143
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
146
144
  };
147
145
  findById: (id?: string) => {
148
146
  (req: Request, res: Response, next: NextFunction): Promise<void>;
149
147
  select(value: string | {
150
148
  [key: string]: -1 | 1;
151
- }): any;
149
+ }): /*elided*/ any;
152
150
  sort(value: string | {
153
151
  [key: string]: -1 | 1;
154
- }): any;
155
- skip(value: number): any;
156
- limit(value: number): any;
157
- populate(value: string | object | (string | object)[]): any;
152
+ }): /*elided*/ any;
153
+ skip(value: number): /*elided*/ any;
154
+ limit(value: number): /*elided*/ any;
155
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
158
156
  };
159
157
  findByIdAndUpdate: (id?: string, body?: {}) => {
160
158
  (req: Request, res: Response, next: NextFunction): Promise<void>;
161
159
  select(value: string | {
162
160
  [key: string]: -1 | 1;
163
- }): any;
161
+ }): /*elided*/ any;
164
162
  sort(value: string | {
165
163
  [key: string]: -1 | 1;
166
- }): any;
167
- skip(value: number): any;
168
- limit(value: number): any;
169
- populate(value: string | object | (string | object)[]): any;
164
+ }): /*elided*/ any;
165
+ skip(value: number): /*elided*/ any;
166
+ limit(value: number): /*elided*/ any;
167
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
170
168
  };
171
169
  findByIdAndDelete: (id?: string) => {
172
170
  (req: Request, res: Response, next: NextFunction): Promise<void>;
173
171
  select(value: string | {
174
172
  [key: string]: -1 | 1;
175
- }): any;
173
+ }): /*elided*/ any;
176
174
  sort(value: string | {
177
175
  [key: string]: -1 | 1;
178
- }): any;
179
- skip(value: number): any;
180
- limit(value: number): any;
181
- populate(value: string | object | (string | object)[]): any;
176
+ }): /*elided*/ any;
177
+ skip(value: number): /*elided*/ any;
178
+ limit(value: number): /*elided*/ any;
179
+ populate(value: string | object | (string | object)[]): /*elided*/ any;
182
180
  };
183
181
  } | undefined;
184
182
  export default db;
package/dist/cjs/db/db.js CHANGED
@@ -14,8 +14,6 @@ let configs = {
14
14
  path: "../models",
15
15
  type: "external",
16
16
  dbEnv: "DB_URL",
17
- first: false,
18
- response: true,
19
17
  ...(0, getConfig_1.default)("db"),
20
18
  };
21
19
  const usingMongoDb = (collectionName, config = { type: "external" }) => {
@@ -487,32 +485,21 @@ const usingMongoDb = (collectionName, config = { type: "external" }) => {
487
485
  : []);
488
486
  }
489
487
  }
490
- if (config?.response) {
491
- const action = queryState.action?.match(/create|update|delet/i)?.[0] || "find";
492
- if (queryState.action == "aggregate" && config.first && data[0]?.content.length)
493
- data[0].content.length = data[0].content[0];
494
- const resBody = queryState.action == "aggregate"
495
- ? {
496
- message: config?.message ||
497
- `the ${collectionName} was found successfully`,
498
- content: [],
499
- ...data[0],
500
- }
501
- : {
502
- content: data,
503
- ...{
504
- detail: Object.keys(detail).length ? detail : undefined,
505
- },
506
- message: config?.message ||
507
- `the ${collectionName} was ${action == "find" ? "found" : action + "ed"}`,
508
- };
509
- res.success
510
- ? res.success(200, resBody)
511
- : res.status(200).json(resBody);
512
- }
513
- else {
514
- next();
515
- }
488
+ const action = queryState.action?.match(/create|update|delet/i)?.[0] || "find";
489
+ const resBody = queryState.action == "aggregate"
490
+ ? {
491
+ message: config?.message ||
492
+ `the ${collectionName} was found successfully`,
493
+ content: [],
494
+ ...data[0],
495
+ }
496
+ : {
497
+ content: data,
498
+ ...{ detail: Object.keys(detail).length ? detail : undefined },
499
+ message: config?.message ||
500
+ `the ${collectionName} was ${action == "find" ? "found" : action + "ed"}`,
501
+ };
502
+ res.success ? res.success(200, resBody) : res.status(200).json(resBody);
516
503
  }
517
504
  }
518
505
  catch (err) {
@@ -592,8 +579,6 @@ const db = (collectionName, config = configs) => {
592
579
  path: "../models",
593
580
  dbEnv: "DB_URL",
594
581
  type: "external",
595
- first: false,
596
- response: true,
597
582
  ...(0, getConfig_1.default)("db"),
598
583
  };
599
584
  Object.entries(config).forEach(([key, value]) => {
@@ -0,0 +1,2 @@
1
+ import document from "./document";
2
+ export default document;
@@ -0,0 +1,7 @@
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 document_1 = __importDefault(require("./document"));
7
+ exports.default = document_1.default;
@@ -1,53 +1,3 @@
1
- import auth from "./auth/auth";
2
- import db from "./db/db";
3
- import uploader from "./uploader/uploader";
4
- import validator from "./validator/validator";
5
- import document from "./document/document";
6
- declare const utils: {
7
- translator: (text?: string, lang?: string) => Promise<unknown>;
8
- };
9
- declare const models: {
10
- translation: import("mongoose").Model<{
11
- text: string;
12
- lang: string;
13
- translatedText: string;
14
- } & import("mongoose").DefaultTimestampProps, {}, {}, {}, import("mongoose").Document<unknown, {}, {
15
- text: string;
16
- lang: string;
17
- translatedText: string;
18
- } & import("mongoose").DefaultTimestampProps, {}, {
19
- timestamps: true;
20
- }> & {
21
- text: string;
22
- lang: string;
23
- translatedText: string;
24
- } & import("mongoose").DefaultTimestampProps & {
25
- _id: import("mongoose").Types.ObjectId;
26
- } & {
27
- __v: number;
28
- }, import("mongoose").Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
29
- timestamps: true;
30
- }, {
31
- text: string;
32
- lang: string;
33
- translatedText: string;
34
- } & import("mongoose").DefaultTimestampProps, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
35
- text: string;
36
- lang: string;
37
- translatedText: string;
38
- } & import("mongoose").DefaultTimestampProps>, {}, import("mongoose").ResolveSchemaOptions<{
39
- timestamps: true;
40
- }>> & import("mongoose").FlatRecord<{
41
- text: string;
42
- lang: string;
43
- translatedText: string;
44
- } & import("mongoose").DefaultTimestampProps> & {
45
- _id: import("mongoose").Types.ObjectId;
46
- } & {
47
- __v: number;
48
- }>>;
49
- };
50
- declare const modules: {
51
- translation: import("express-serve-static-core").Router;
52
- };
53
- export { auth, db, uploader, validator, models, modules, utils, document };
1
+ import speedly from "./config/init";
2
+ export { Request, Response, NextFunction, Application, Router, RequestHandler, ErrorRequestHandler, } from "express";
3
+ export default speedly;
package/dist/cjs/index.js CHANGED
@@ -3,25 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.document = exports.utils = exports.modules = exports.models = exports.validator = exports.uploader = exports.db = exports.auth = void 0;
7
- const auth_1 = __importDefault(require("./auth/auth"));
8
- exports.auth = auth_1.default;
9
- const db_1 = __importDefault(require("./db/db"));
10
- exports.db = db_1.default;
11
- const uploader_1 = __importDefault(require("./uploader/uploader"));
12
- exports.uploader = uploader_1.default;
13
- const validator_1 = __importDefault(require("./validator/validator"));
14
- exports.validator = validator_1.default;
15
- const translation_1 = __importDefault(require("./model/translation"));
16
- const translation_routes_1 = __importDefault(require("./modules/translation/translation.routes"));
17
- const document_1 = __importDefault(require("./document/document"));
18
- exports.document = document_1.default;
19
- const translator_1 = __importDefault(require("./util/translator"));
20
- const utils = {
21
- translator: translator_1.default,
22
- };
23
- exports.utils = utils;
24
- const models = { translation: translation_1.default };
25
- exports.models = models;
26
- const modules = { translation: translation_routes_1.default };
27
- exports.modules = modules;
6
+ exports.Router = void 0;
7
+ const init_1 = __importDefault(require("./config/init"));
8
+ // Export express types and classes
9
+ var express_1 = require("express");
10
+ Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return express_1.Router; } });
11
+ // Override the default express() with our speedly`s functions
12
+ exports.default = init_1.default;
13
+ module.exports = init_1.default;
14
+ module.exports.default = init_1.default;
@@ -0,0 +1,3 @@
1
+ import * as Types from "./types";
2
+ declare const auth: Types.Auth;
3
+ export default auth;
@@ -0,0 +1,38 @@
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;
@@ -0,0 +1,19 @@
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 };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });