项目作者: printjs

项目描述 :
MVC fastify decorator Dependency injection Inversion of Control Typescript
高级语言: TypeScript
项目地址: git://github.com/printjs/fastify-wrapper.git
创建时间: 2020-02-01T12:56:25Z
项目社区:https://github.com/printjs/fastify-wrapper

开源协议:MIT License

下载


fastify-wrapper

Welcome to fastify-wrapper, if you know fastify well, you will easily use this library.

Install

  1. yarn add fastify fastify-wrapper

The following decorators are available

  • BodyLimit
  • Body
  • Config
  • Controller
  • Delete
  • Done
  • Get
  • Head
  • Injectable
  • LogLevel
  • Options
  • Param
  • Post
  • PrefixTrailingSlash
  • Put
  • Query
  • Reply
  • Request
  • Version
  • GuardWidget
  • OnRequestWidget
  • OnResponseWidget
  • OnSendWidget
  • PreHandlerWidget
  • PreParsingWidget
  • preSerializationWidget
  • SchemaWidget

The following code will show you how to use fastify-wrapper

  1. import Fastify, { FastifyReply, FastifyRequest } from "fastify";
  2. import { ServerResponse } from "http";
  3. import { GlobalGuard, Guard, Guard1 } from "./guard";
  4. import { ResInterceptor } from "./res";
  5. import {
  6. Injectable,
  7. Controller,
  8. Get,
  9. SchemaWidget,
  10. GuardWidget,
  11. Query,
  12. Post,
  13. FastifyContainer,
  14. Request,
  15. Reply,
  16. } from "fastify-wrapper";
  17. const fastify = Fastify({ logger: true });
  18. @Injectable
  19. class AnimalService {
  20. public animal: string = "The animal";
  21. }
  22. @Injectable
  23. class CatService {
  24. constructor(
  25. private readonly animalService: AnimalService,
  26. ) { }
  27. public getType() { return `${this.animalService.animal} is red cat.`; }
  28. }
  29. @Injectable
  30. class DogService {
  31. constructor(
  32. private readonly animalService: AnimalService,
  33. ) { }
  34. public getType() { return `${this.animalService.animal} is black dog.`; }
  35. }
  36. @Injectable
  37. export class ZoomService {
  38. constructor(
  39. private readonly catService: CatService,
  40. private readonly dogService: DogService,
  41. ) { }
  42. public getDog() {
  43. return this.dogService.getType();
  44. }
  45. public getCat() {
  46. return this.catService.getType();
  47. }
  48. }
  49. @Controller()
  50. class ApplicationController {
  51. constructor(
  52. private readonly zoomService: ZoomService,
  53. private readonly animalService: AnimalService,
  54. ) { }
  55. @Get("/name")
  56. @SchemaWidget({ querystring: { name: { type: "string" } } })
  57. @GuardWidget(new Guard(), new Guard1())
  58. public async function1(
  59. @Query() query: any,
  60. @Query("name") name: any,
  61. ) {
  62. return {
  63. query,
  64. name,
  65. message: `${this.animalService.animal} is ${name}`,
  66. };
  67. }
  68. @Get("/reply")
  69. public reply(
  70. @Request() request: FastifyRequest,
  71. @Reply() reply: FastifyReply<ServerResponse>
  72. ) {
  73. reply.send(this.zoomService.getDog());
  74. }
  75. @Post()
  76. @SchemaWidget({
  77. body: {
  78. name: { type: "string" },
  79. required: ["name"],
  80. },
  81. })
  82. public async formatResponse() {
  83. return {
  84. type: this.zoomService.getCat(),
  85. };
  86. }
  87. }
  88. FastifyContainer({
  89. fastify,
  90. controllers: [ApplicationController],
  91. useGlobalGuard: [new GlobalGuard()],
  92. useGlobalResponseInterceptor: [new ResInterceptor()],
  93. baseUrl: "/app",
  94. });
  95. fastify.addContentTypeParser("application/x-www-form-urlencoded", (req, done) => {
  96. done(null, req);
  97. });
  98. fastify.listen(9999);