@zenofolio/hyper-decor 1.0.64 → 1.0.65
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 +63 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -185,7 +185,70 @@ As a result, we get:
|
|
|
185
185
|
- [Middleware](./examples/middleware.ts)
|
|
186
186
|
- [File](./examples//upload-file.ts)
|
|
187
187
|
|
|
188
|
+
## Services & Dependency Injection
|
|
189
|
+
You can use `@HyperService()` with `tsyringe` to inject dependencies into controllers or other services.
|
|
188
190
|
|
|
191
|
+
```typescript
|
|
192
|
+
import { injectable } from "tsyringe";
|
|
193
|
+
|
|
194
|
+
@injectable()
|
|
195
|
+
@HyperService()
|
|
196
|
+
class UserService {
|
|
197
|
+
getUsers() { return ["User1", "User2"]; }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
@HyperController("/users")
|
|
201
|
+
class UserController {
|
|
202
|
+
constructor(private userService: UserService) {}
|
|
203
|
+
|
|
204
|
+
@Get("/")
|
|
205
|
+
getUsers(@Res() res: Response) {
|
|
206
|
+
res.json(this.userService.getUsers());
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Agnostic Body Validation & Transformation (`@Transform`)
|
|
212
|
+
You can use `@Transform` to validate and transform incoming requests agnostic of the validation library (like Zod) while seamlessly syncing with OpenAPI definitions.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const ZodTransformer = {
|
|
216
|
+
transform: ({ data, schema }) => {
|
|
217
|
+
if (schema && schema._type === "zod") {
|
|
218
|
+
// Validate and return the parsed data
|
|
219
|
+
return { ...data, parsed: true };
|
|
220
|
+
}
|
|
221
|
+
return data;
|
|
222
|
+
},
|
|
223
|
+
getOpenApiSchema: (schema) => {
|
|
224
|
+
if (schema._type === "zod") {
|
|
225
|
+
return { type: "object", properties: { /* derived from schema */ } };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const app = await createApplication(Application);
|
|
231
|
+
app.useTransform(ZodTransformer); // Register your custom transformer globally
|
|
232
|
+
|
|
233
|
+
@HyperController("/users")
|
|
234
|
+
class UserController {
|
|
235
|
+
@Post("/")
|
|
236
|
+
@Transform({ _type: "zod" /* pass your schema */ })
|
|
237
|
+
createUser(@Body() data: any, @Res() res: Response) {
|
|
238
|
+
res.json(data); // `data` is automatically intercepted and transformed!
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## OpenAPI Generation
|
|
244
|
+
Generate a complete OpenAPI specification out-of-the-box leveraging your application tree and decorators footprint.
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import { getOpenAPI } from "@zenofolio/hyper-decor/lib/openapi";
|
|
248
|
+
|
|
249
|
+
const openApiDoc = getOpenAPI(Application);
|
|
250
|
+
console.log(openApiDoc.info.title, openApiDoc.paths);
|
|
251
|
+
```
|
|
189
252
|
|
|
190
253
|
# All for now
|
|
191
254
|
More documentation will be added here late.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenofolio/hyper-decor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.65",
|
|
4
4
|
"description": "Project core with utilities and features",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "zenozaga",
|
|
@@ -63,4 +63,4 @@
|
|
|
63
63
|
"bin": {
|
|
64
64
|
"hyper-clean-uws": "./scripts/clean.js"
|
|
65
65
|
}
|
|
66
|
-
}
|
|
66
|
+
}
|