1.0.21 • Published 2 years ago

@fncdev/route-composer v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

NameDescription
containerFilesglob path to the modules for awilix di container: asClass , asValue

routeComposerInstance returned methods and properties

NameDescription
renderRoutesfastify plugin
containerawilix 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
}
1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.11

3 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.12

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago