|
1 | | -## Architecture of NodePress v4.x |
| 1 | +## Architecture of NodePress (>= v4.x) |
2 | 2 |
|
3 | 3 | ### API Overview |
4 | 4 |
|
5 | | -**HTTP Status Codes** [`errors`](/src/errors) |
| 5 | +**HTTP Status Codes** |
6 | 6 |
|
7 | 7 | - `400` — Request rejected due to business logic |
8 | 8 | - `401` — Authentication failed |
|
18 | 18 | - `status` |
19 | 19 | - `success` — Successful response |
20 | 20 | - `error` — Error occurred |
21 | | -- `message` — Always present; injected by [`responser.decorator.ts`](/src/decorators/responser.decorator.ts) |
22 | | -- `error` — Required when `status` is `error`, useful for debugging |
23 | | -- `debug` — Stack trace (only in development mode) |
| 21 | +- `message` — Always present; injected by [`success-response.decorator.ts`](/src/decorators/success-response.decorator.ts) |
| 22 | +- `error` — Required when `status` is `error`, usually a simple description of the error |
24 | 23 | - `result` — Required when `status` is `success` |
25 | 24 | - For entity: e.g. `{ title: '', content: '', ... }` |
26 | 25 | - For list: e.g. `{ pagination: {...}, data: [...] }` |
|
60 | 59 | **Request Lifecycle** |
61 | 60 |
|
62 | 61 | 1. `request` — Incoming HTTP request |
63 | | -2. `middleware` — Preprocessing (CORS, origin checks) |
| 62 | +2. `middleware` — Preprocessing (empty in this app) |
64 | 63 | 3. `guard` — Authentication guards |
65 | 64 | 4. `interceptor:before` — Input transformation (empty in this app) |
66 | | -5. `pipe` — Data validation and transformation; attaches payload to context |
| 65 | +5. `pipe` — Data validation and transformation |
67 | 66 | 6. `controller` — Request handler logic |
68 | 67 | 7. `service` — Business logic |
69 | 68 | 8. `interceptor:after` — Output formatting and error handling |
70 | 69 | 9. `filter` — Captures and handles thrown exceptions |
71 | 70 |
|
72 | 71 | **Authentication Flow** |
73 | 72 |
|
74 | | -1. Guard entry point: [`guards`](/src/guards) |
75 | | -2. `canActivate()` — Entry check |
76 | | -3. `JwtStrategy.validate()` — Calls [`jwt.strategy.ts`](/src/modules/auth/jwt.strategy.ts) |
77 | | -4. `handleRequest()` — Accept or reject based on validation result |
| 73 | +1. [`main.ts`](/src/main.ts): Uses Fastify’s `onRequest` to perform authentication and inject auth data into the request |
| 74 | +2. Uses [`Guards`](/src/guards) to validate and intercept each controller method |
| 75 | +3. `handleRequest()` — Accept or reject based on validation result |
78 | 76 |
|
79 | 77 | **Access Control Levels** |
80 | 78 |
|
81 | 79 | - CUD (write) operations require tokens: [`AdminOnlyGuard`](/src/guards/admin-only.guard.ts) |
82 | | -- GET (read) operations auto-detect token presence: [`AdminMaybeGuard`](/src/guards/admin-maybe.guard.ts) |
| 80 | +- GET (read) operations auto-detect token presence: [`AdminOptionalGuard`](/src/guards/admin-optional.guard.ts) |
83 | 81 |
|
84 | 82 | **Validation Rules** |
85 | 83 |
|
|
90 | 88 |
|
91 | 89 | ### Core Components |
92 | 90 |
|
93 | | -**Exception Filter** [`error.filter.ts`](/src/filters/error.filter.ts) |
| 91 | +[**Exception Filter**](/src/filters/exception.filter.ts) |
94 | 92 |
|
95 | | -**Interceptors** [`interceptors`](/src/interceptors) |
| 93 | +[**Interceptors**](/src/interceptors) |
96 | 94 |
|
97 | | -- [Cache](/src/interceptors/cache.interceptor.ts): Adds TTL support |
98 | 95 | - [Transformer](/src/interceptors/transform.interceptor.ts): Formats successful service responses |
99 | | -- [Error](/src/interceptors/error.interceptor.ts): Catches service-layer exceptions |
100 | 96 | - [Logging](/src/interceptors/logging.interceptor.ts): Supplements global logging |
101 | 97 |
|
102 | | -**Decorators** [`decorators`](/src/decorators) |
| 98 | +[**Decorators**](/src/decorators) |
103 | 99 |
|
104 | | -- [Cache](/src/decorators/cache.decorator.ts): Configure cache key/TTL |
105 | | -- [Response](/src/decorators/responsor.decorator.ts): Adds `message`, pagination, etc. |
106 | | -- [QueryParams](/src/decorators/queryparams.decorator.ts): Auto-validate and normalize params |
107 | | -- [Guest](/src/decorators/guest.decorator.ts): Extend sub-fields with metadata for `permission.pipe` |
| 100 | +- [RequestContext](/src/decorators/request-context.decorator.ts): Auto-validate and normalize params |
| 101 | +- [GuestPermission](/src/decorators/guest-permission.decorator.ts): Extend sub-fields with metadata for `permission.pipe` |
| 102 | +- [SuccessResponse](/src/decorators/success-response.decorator.ts): Adds `message`, pagination, etc. |
| 103 | +- [UploadedFile](/src/decorators/uploaded-file.decorator.ts): Reads the uploaded file from `request.file` and converts it to buffer |
108 | 104 |
|
109 | | -**Guards** [`guards`](/src/guards) |
| 105 | +[**Guards**](/src/guards) |
110 | 106 |
|
111 | 107 | - Default: All non-GET requests require [`AdminOnlyGuard`](/src/guards/admin-only.guard.ts) |
112 | | -- Multi-role GET requests use [`AdminMaybeGuard`](/src/guards/admin-maybe.guard.ts) |
| 108 | +- Multi-role GET requests use [`AdminOptionalGuard`](/src/guards/admin-optional.guard.ts) |
113 | 109 |
|
114 | | -**Middlewares** [`middlewares`](/src/middlewares) |
115 | | - |
116 | | -- [`CORS`](/src/middlewares/cors.middleware.ts): Handles cross-origin requests |
117 | | -- [`Origin`](/src/middlewares/origin.middleware.ts): Blocks unknown sources |
118 | | - |
119 | | -**Pipes** [`pipes`](/src/pipes) |
| 110 | +[**Pipes**](/src/pipes) |
120 | 111 |
|
121 | 112 | - `validation.pipe` — Validates DTO schemas |
122 | 113 | - `permission.pipe` — Checks field-level permissions |
123 | 114 |
|
124 | | -**Feature Modules** [`modules`](/src/modules) |
| 115 | +[**Feature Modules**](/src/modules) |
125 | 116 |
|
126 | 117 | - `Announcement`, `Article`, `Category`, `Tag`, `Comment`, `Option` |
127 | | -- `Auth` — Global token and user auth |
| 118 | +- `Admin` — Admin profile and authentication |
128 | 119 | - `Vote` — Reaction logic (like/dislike) |
129 | 120 | - `Disqus` — Third-party integration |
130 | 121 | - `Archive` — Caching layer |
|
133 | 124 | - DB Backup (manual and scheduled) |
134 | 125 | - Miscellaneous third-party services |
135 | 126 |
|
136 | | -**Global/Core Modules** [`processors`](/src/processors) |
137 | | - |
138 | | -- [`database`](/src/processors/database) — DB connection and error handling |
139 | | -- [`cache`](/src/processors/cache) — Cache APIs and strategies |
140 | | -- [`helper`](/src/processors/helper): |
141 | | - - [SEO Submission](/src/processors/helper/helper.service.seo.ts) |
142 | | - - [Spam Detection](/src/processors/helper/helper.service.akismet.ts) |
143 | | - - [Email Service](/src/processors/helper/helper.service.email.ts) |
144 | | - - [IP Geolocation](/src/processors/helper/helper.service.ip.ts) |
145 | | - - [AWS S3 Upload](/src/processors/helper/helper.service.aws.ts) |
| 127 | +[**Global/Core Modules**](/src/core) |
| 128 | + |
| 129 | +- [`database`](/src/core/database) — DB connection and error handling |
| 130 | +- [`cache`](/src/core/cache) — Cache APIs and strategies |
| 131 | +- [`auth`](/src/core/auth) — Responsible for global JWT authentication logic |
| 132 | +- [`helper`](/src/core/helper): |
| 133 | + - [SEO Submission](/src/core/helper/helper.service.seo.ts) |
| 134 | + - [Spam Detection](/src/core/helper/helper.service.akismet.ts) |
| 135 | + - [Email Service](/src/core/helper/helper.service.email.ts) |
| 136 | + - [IP Geolocation](/src/core/helper/helper.service.ip.ts) |
| 137 | + - [AWS S3 Upload](/src/core/helper/helper.service.aws.ts) |
146 | 138 | - Google Service Credentials |
147 | 139 |
|
148 | 140 | --- |
|
0 commit comments