[](https://www.npmjs.com/package/summer-glove) ## Fit like a glove ๐งค Summer-glove is a route manager, providing a quick and easy way to configure an express application. Summer-glo
npm install summer-glovejsx
npm i summer-glove
`
To create a app
`jsx
npx summer-glove --create-app
`
Will be create
`
project/
โโโ App.ts
โโโ Controller.ts
โโโ Server.ts
โโโ tsconfig.json
`
Add the property below to your file tsconfig.json.
`json
{
"compilerOptions": {
//...
"experimentalDecorators": true
//...
}
}
`
Documentation
Below is a walkthrough of all available decorators
$3
Decorate your classes with these so that summer can manage the injection of your dependencies.
All context decorators (@Repository, @Service, @Component, @Configuration) receive an optional parameter. This parameter is used to allocate the instantiated object in the summer context; if not specified, it will be obtained from the class.
* @Repository - Responsible for managing repository classes | String
* @Service - Responsible for managing services classes | String
* @Component - Responsible for managing components classes | String
* @Configuration - Responsible for managing configurations classes | String
* @Injectable - Responsible for injecting the object without attribute | String
$3
* @StartControllers - Receives all the controllers of the application to initialize in the summer context | Object []
* @SwaggerInitializer - Loads Swagger in your express application | String
* @SwaggerEndpoint - Defines the path to access documentation | String
* @ApiDefaultPath - Defines the main path of your API | String
* @Description - Describes your application within documentation | String
* @Title - Puts a title on your documentation | String
* @Version - Defines API version | String
* @GlobalAuth - Defines if the API uses JWT Tokens as a security mechanism | Boolean
* Param:
* AuthType.BEARER_JWT
* AuthType.BASIC
* @ExpressInitializer - Initializes an express app and configures its routes. You can also pass a logger configuration
* Param:
* LoggerConfigTypes.SHOW | LoggerConfigTypes.HIDE
* app configuration:
* Everything you want to pass to your express app. Example:
* app.use( json() )
* app.use( cors() )
* Default configuration: app.use( json() )
$3
* @Controller - Specifies a controller within the express context | String
$3
* @Get - Specifies GET type endpoints | String, middlewares
* @Post - Specifies POST type endpoints | String, middlewares
* @Delete - Specifies DELETE type endpoints | String, middlewares
* @Patch - Specifies PATCH type endpoints | String, middlewares
* @Put - Specifies PUT type endpoints | String, middlewares
* @Connect - Specifies CONNECT type endpoints | String, middlewares
* @Head - Specifies HEAD type endpoints | String, middlewares
* @Options - Specifies OPTIONS type endpoints | String, middlewares
* @Trace - Specifies TRACE type endpoints | String, middlewares
$3
* @StatusResponse - Adds HTTP response codes and description | number
* @Body - Adds a Body as a request object | Object
* @RequireAuth - Tells swagger that the route is protected by authentication
* @ParamPath - Adds a ParamPath as a request object | Object
@FormData - Adds a FormData as a request objet | Object. Utilize FormDataTypes* for grant types
* @Header - Adds a Header as a request objet | object
* @Query - Adds a Query as a request objet | object
*
$3
To configure the themes, use
* @Theme - Specifies Theme type of Swagger | If not specified, use default swagger theme
* ThemesType.FEELING_BLUE
* ThemesType.FLATTOP
* ThemesType.MATERIAL
* ThemesType.MONOKAI
* ThemesType.MUTED
* ThemesType.NEWS_PAPER
* ThemesType.OUTLINE
Usage
$3
`javascript
import Express, {json} from "express";
import {
ApiDefaultPath,
Description,
ExpressInitializer,
GlobalAuth,
AuthType,
LoggerConfigTypes,
SwaggerEndpoint,
SwaggerInitializer,
Theme,
ThemesType,
Title,
Version,
StartControllers
} from "summer-glove";
import MyController from "./Controller";
import cors from "cors";
import rateLimit from "express-rate-limit";
@SwaggerInitializer
@SwaggerEndpoint("/doc")
@Description("API TEST")
@Title("TEST NAME")
@Version("1.0.0")
@ApiDefaultPath("/")
@GlobalAuth(AuthType.BEARER_JWT)
@Theme(ThemesType.NEWS_PAPER)
@StartControllers(
MyController1,
MyController2
)
export default class App {
@ExpressInitializer(LoggerConfigTypes.SHOW,
cors(), // configuration applied in app.use()
rateLimit({
windowMs: 15 60 1000,
max: 10,
message: 'Too many requests from this IP. Please try again later.',
standardHeaders: true,
legacyHeaders: false,
}), //configuration applied in app.use()
json() //configuration applied in app.use()
)
private app: Express.Express;
public getApp(): Express.Express {
return this.app;
}
}
`
$3
`javascript
@Controller("/controller1")
export default class MyController1 {
//health-check
@StatusResponse(200, "Check API successfully")
@StatusResponse(400,"Check API unsuccessfully")
@Get() // It is important to put the Http Method Decorator as the first configuration.
public check(request: Request, response: Response): Promise {
//... implementation
}
}
`
`javascript
@Controller("/controller2")
export default class MyController2 {
//There are 3 ways to inject a dependency into the summer-glove, here they are:
@Injectable()
private myService: MyService
@Injectable("myService")
private myService2: MyService
private myService3: MyService = new MyService();
private myService4: MyService;
constructor(){
this.myService4 = new MyService()
}
@StatusResponse(202) // if you dont pass description, summer-glove add for you
@StatusResponse(400) // if you dont pass description, summer-glove add for you
@Body({email:"Description", password:"Description"})
@Post("/login")// It is important to put the Http Method Decorator as the first configuration.
public login( request: Request, response: Response): Promise {
//... implementation
}
@StatusResponse(200) // if you dont pass description, summer-glove add for you
@StatusResponse(400)// if you dont pass description, summer-glove add for you
@Get("/", authorizationMiddleware)// It is important to put the Http Method Decorator as the first configuration.
public read(request: Request, response: Response): Promise {
//... implementation
}
@StatusResponse(200)// if you dont pass description, summer-glove add for you
@StatusResponse(400)// if you dont pass description, summer-glove add for you
@ParamPath({uuid: "Description"})
@RequireAuth() // Tells swagger that the route is protected by authentication
@Get("/find-by-uuid/{uuid}", authorizationMiddleware)// It is important to put the Http Method Decorator as the first configuration.
public findByUuid(request: Request, response: Response): Promise {
//... implementation
}
@StatusResponse(200)// if you dont pass description, summer-glove add for you
@StatusResponse(400)// if you dont pass description, summer-glove add for you
@Body({
name : "Description",
email : "Description",
password: "Description"
})
// Default = "/"
@Post() // It is important to put the Http Method Decorator as the first configuration.
public create(request: Request, response: Response): Promise {
//... implementation
}
@StatusResponse(200)// if you dont pass description, summer-glove add for you
@StatusResponse(400)// if you dont pass description, summer-glove add for you
@Query({
uuid:"Description"
})
// Default = "/"
@Post("/query-profile") // It is important to put the Http Method Decorator as the first configuration.
public queryProfile(request: Request, response: Response): Promise {
//... implementation
}
@StatusResponse(200)// if you dont pass description, summer-glove add for you
@StatusResponse(400)// if you dont pass description, summer-glove add for you
@Header({
profileType:"Description"
})
// Default = "/"
@Post("/type-profile") // It is important to put the Http Method Decorator as the first configuration.
public headerProfile(request: Request, response: Response): Promise {
//... implementation
}
@StatusResponse(200)// if you dont pass description, summer-glove add for you
@StatusResponse(400)// if you dont pass description, summer-glove add for you
@FormData({
img: FormDataTypes.FILE,
name: FormDataTypes.STRING,
rules: FormDataTypes.ARRAY,
age: FormDataTypes.NUMBER,
isMarried:FormDataTypes.BOOLEAN
})
// Default = "/"
@Post("/create-profile") // It is important to put the Http Method Decorator as the first configuration.
public createProfile(request: Request, response: Response): Promise {
//... implementation
}
}
`
`javascript
@Service()
export default class MyService {
public doAnithing(): Promise {
//... implementation
}
}
``