simpledi-app-generator 0.0.7 → 0.0.9
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/README.md +9 -9
- package/dist/cli.js +3 -4
- package/dist/cli.js.map +1 -1
- package/dist/create_module.d.ts.map +1 -1
- package/dist/create_module.js +3 -0
- package/dist/create_module.js.map +1 -1
- package/dist/generate_crud_use_cases.d.ts.map +1 -1
- package/dist/generate_crud_use_cases.js +571 -130
- package/dist/generate_crud_use_cases.js.map +1 -1
- package/dist/generate_skeleton.d.ts.map +1 -1
- package/dist/generate_skeleton.js +57 -2
- package/dist/generate_skeleton.js.map +1 -1
- package/dist/templates/schema.ts +2 -0
- package/dist/templates/src/core/user/IUserRepository.ts +8 -0
- package/dist/templates/src/core/user/IUserService.ts +8 -0
- package/dist/templates/src/core/user/User.ts +71 -0
- package/dist/templates/src/core/user/UserModule.ts +7 -0
- package/dist/templates/src/core/user/UserRepository.spec.ts +63 -0
- package/dist/templates/src/core/user/UserRepository.ts +31 -0
- package/dist/templates/src/core/user/UserRepositoryModule.ts +15 -0
- package/dist/templates/src/core/user/UserService.ts +34 -0
- package/dist/templates/src/core/user/UserServiceModule.ts +14 -0
- package/dist/templates/src/core/user/baseZodUserSchema.ts +31 -0
- package/dist/templates/src/lib/functions/getContextUser.ts +5 -0
- package/dist/templates/src/lib/functions/test-related/createSignedUpUser.ts +44 -0
- package/dist/templates/src/lib/functions/test-related/getOneUserSignupData.ts +28 -0
- package/dist/templates/src/lib/functions/test-related/getTestServer.ts +28 -0
- package/dist/templates/src/lib/types/AdminRoleEnum.ts +6 -0
- package/dist/templates/src/lib/types/AnyRoleEnum.ts +5 -0
- package/dist/templates/src/lib/types/PhoneNumberTypeEnum.ts +7 -0
- package/dist/templates/src/lib/types/UserRoleEnum.ts +4 -0
- package/dist/templates/src/lib/types/UserTypeEnum.ts +4 -0
- package/dist/templates/src/middlewares/authGuard.ts +46 -0
- package/dist/templates/src/middlewares/index.ts +2 -0
- package/dist/templates/src/middlewares/roleGuard.ts +16 -0
- package/package.json +1 -1
- package/user-guide.md +16 -4
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { inject } from '@kanian77/simple-di';
|
|
2
|
+
import type { Context, Next } from 'hono';
|
|
3
|
+
import { StatusCodes } from 'http-status-codes';
|
|
4
|
+
import { USER_SERVICE_INTERFACE } from '@root/core/user/IUserService';
|
|
5
|
+
import type { UserService } from '@root/core/user/UserService';
|
|
6
|
+
import type { TokenPayload } from '@root/lib';
|
|
7
|
+
import { AuthenticationUtils } from '@root/lib/AuthenticationUtils';
|
|
8
|
+
|
|
9
|
+
export const authGuard = () => {
|
|
10
|
+
return async (c: Context, next: Next) => {
|
|
11
|
+
const authHeader = c.req.header('Authorization');
|
|
12
|
+
|
|
13
|
+
if (!authHeader?.startsWith('Bearer ')) {
|
|
14
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const token = authHeader.split(' ')[1];
|
|
18
|
+
|
|
19
|
+
if (!token) {
|
|
20
|
+
console.log(' no token')
|
|
21
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const payload = AuthenticationUtils.verifyToken(token) as TokenPayload;
|
|
25
|
+
|
|
26
|
+
if (!payload) {
|
|
27
|
+
console.log(' no payload')
|
|
28
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const user = await inject<UserService>(USER_SERVICE_INTERFACE).findById(
|
|
32
|
+
payload.userId,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (!user) {
|
|
36
|
+
console.log(' no user')
|
|
37
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
c.set('user', user);
|
|
41
|
+
await next();
|
|
42
|
+
} catch {
|
|
43
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnyRoleEnum } from '@root/lib/types/AnyRoleEnum';
|
|
2
|
+
import type { Context, Next } from 'hono';
|
|
3
|
+
import { StatusCodes } from 'http-status-codes';
|
|
4
|
+
|
|
5
|
+
export const roleGuard = (roles: AnyRoleEnum[]) => {
|
|
6
|
+
return async (c: Context, next: Next) => {
|
|
7
|
+
const user = c.get('user');
|
|
8
|
+
if (!user) {
|
|
9
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
10
|
+
}
|
|
11
|
+
if (!roles.includes(user.role)) {
|
|
12
|
+
return c.json({ error: 'Unauthorized' }, StatusCodes.UNAUTHORIZED);
|
|
13
|
+
}
|
|
14
|
+
await next();
|
|
15
|
+
};
|
|
16
|
+
};
|
package/package.json
CHANGED
package/user-guide.md
CHANGED
|
@@ -73,24 +73,36 @@ simpledi module blog-post
|
|
|
73
73
|
| `<Entity>Module.ts` | Main module (combines all) |
|
|
74
74
|
| `<Entity>Repository.spec.ts` | Test file |
|
|
75
75
|
|
|
76
|
+
**Generated Use Cases** in `src/use-case/<entity-name>/`:
|
|
77
|
+
|
|
78
|
+
| Use Case | Route | Method |
|
|
79
|
+
| ---------------- | ------ | -------- |
|
|
80
|
+
| `Create<Entity>` | `/` | `POST` |
|
|
81
|
+
| `Update<Entity>` | `/:id` | `PUT` |
|
|
82
|
+
| `Get<Entity>` | `/:id` | `GET` |
|
|
83
|
+
| `List<Entity>s` | `/` | `GET` |
|
|
84
|
+
| `Delete<Entity>` | `/:id` | `DELETE` |
|
|
85
|
+
|
|
76
86
|
**Auto-registration:**
|
|
77
87
|
|
|
78
88
|
- Adds export to `src/schema.ts`
|
|
79
89
|
- Adds module to `src/core/CoreModule.ts`
|
|
90
|
+
- Adds use case module to `src/use-case/UseCaseModule.ts`
|
|
91
|
+
- Registers routes in `src/main.routes.ts`
|
|
80
92
|
|
|
81
93
|
---
|
|
82
94
|
|
|
83
95
|
### `simpledi use-case <name> [imports=entity1,entity2,...]`
|
|
84
96
|
|
|
85
|
-
Generates a use case with routes and typed outputs.
|
|
97
|
+
Generates a custom use case with routes and typed outputs.
|
|
86
98
|
|
|
87
99
|
```bash
|
|
88
100
|
# Simple use case
|
|
89
|
-
simpledi use-case get-
|
|
101
|
+
simpledi use-case get-dashboard-stats
|
|
90
102
|
|
|
91
103
|
# Use case with module imports
|
|
92
|
-
simpledi use-case
|
|
93
|
-
simpledi use-case
|
|
104
|
+
simpledi use-case publish-post imports=blog-post
|
|
105
|
+
simpledi use-case assign-role imports=user
|
|
94
106
|
```
|
|
95
107
|
|
|
96
108
|
**Generated files** in `src/use-case/<name>/`:
|