@fncdev/route-composer v1.0.21
Installation
Install with npm
npm install @fncdev/route-composer
Or yarn
yarn add @fncdev/route-composer
About
this plugin used for well documented, typed, easy to use application creation
Quick Start
initialize composer instance
import { routeComposer, TPaths } from '@fncdev/route-composer'
const containerFiles: TPaths = {
asClass: ['./controller/*.ts'],
}
export const routeComposerInstance = routeComposer({ containerFiles })
routeComposer
properties
Name | Description |
---|---|
containerFiles | glob path to the modules for awilix di container: asClass , asValue |
routeComposerInstance
returned methods and properties
Name | Description |
---|---|
renderRoutes | fastify plugin |
container | awilix container |
register plugin
fastify.register(routeComposerInstance.renderRoutes, {
path: './controller/*.ts',
})
decorate controller that match glob path
export class User {
@DataProp<string>({ required: true })
name: string
}
class Params {
@DataProp<number>({ required: true })
id: number
}
@Controller('/test')
export default class TestController {
constructor() {
autoBind(this)
}
@Get('/users')
async get(@Params params: Params): Promise<SuccessResponse<User>> {
return { statusCode: httpStatus.OK, response: { name: 'test' } }
}
}
Decorators
Route decorators
@Controller
required decorator(add controller to plugin) with base path
@Controller('/path') //
class TestController {
@Get('/') // get http method
get() {}
}
HTTP Methods
@Controller('/path')
class TestController {
@Get('/') // get http method
get() {}
@Post('/')
post() {}
@Put('/:id')
put() {}
@Patch('/')
patch() {}
@Head('/')
head() {}
@Options('/')
options() {}
@Delete('/')
deleteOne() {}
}
@Guard
auth middleware function for authorized requests
return user object that can be cached by @User
param decorator
@Guard(guard)
class TestController {
@Get('/')
get(@User user: UserType) {}
}
@Roles
access roles that will be used in @Guard
function(optional)
@Guard(guard)
@Controller('/path')
class TestController {
@Post('/')
@Roles(['testRole'])
post() {}
}
@ApiResponse
fastify schema response decorator
@Guard(guard)
@Controller('/path')
class TestController {
@Patch('/')
@ApiResponse({ statusCode: 404, type: FailedResponse })
patch() {}
// array
@Get('/')
@ApiResponse({ statusCode: 404, type: User, isArray: true })
patch() {}
}
@RouteOptions
fastify router object additional options
@Controller('/path')
class TestController {
@Post('/')
@RoutesOptions({ attachValidation: false, exposeHeadRoute: false })
post() {}
}
Param decorators
use @Query
, @Body
, @Params
, @Headers
decorators to enable swagger documentation
class TestController {
get(@Request req: object, @Response res: object) {}
post(@Headers headers: object, @Query query: object, @Body body: object) {}
put(@Params params: object, @User user: string) {}
}
Parser decorators
@ObjectOptions
ajv object validation additional properties
@ObjectOptions({ additionalProperties: true, maxProperties: 1})
class Test {}
@DataProp
class Test {
@DataProp<string>()
data: string
}
array validation require type
definition in items
property
class Test {
@DataProp<AnotherDecoratedObject[]>({ required: true, items: { type: AnotherDecoratedObject }, maxItems: 1 })
data: AnotherDecoratedObject[]
}
swagger props also supported
class Test {
@DataProp<string>({ required: true, example: 'some string' })
data: string
}
@NativeAJV
you can also use native schema ajv validation decorator
class Test {
@NativeAJV<string>({ type: 'string' })
data: string
}
to set required combine with @DataProp
class Test {
@NativeAJV<string>({ type: 'string' })
@DataProp<string>({ required: true })
data: string
}
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago