Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'routing-controllers-openapi' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
const routingControllersOptions = {
controllers: [UsersController],
routePrefix: '/api'
}
const app: Express = createExpressServer(routingControllersOptions)
// Parse class-validator classes into JSON Schema:
const metadatas = (getFromContainer(MetadataStorage) as any).validationMetadatas
const schemas = validationMetadatasToSchemas(metadatas, {
refPointerPrefix: '#/components/schemas/'
})
// Parse routing-controllers classes into OpenAPI spec:
const storage = getMetadataArgsStorage()
const spec = routingControllersToSpec(storage, routingControllersOptions, {
components: {
schemas,
securitySchemes: {
basicAuth: {
scheme: 'basic',
type: 'http'
}
}
},
info: {
description: 'Generated with `routing-controllers-openapi`',
title: 'A sample API',
version: '1.0.0'
}
})
class CreateUserBody {
@IsString()
name: string
@IsOptional()
@MaxLength(20, { each: true })
hobbies: string[]
}
@OpenAPI({
security: [{ basicAuth: [] }]
})
@JsonController('/users')
export class UsersController {
@Get('/')
@OpenAPI({ summary: 'Return a list of users' })
getAll() {
return [
{ id: 1, name: 'First user!', hobbies: [] },
{ id: 2, name: 'Second user!', hobbies: ['fishing', 'cycling'] }
]
}
@Get('/:id')
@OpenAPI({ summary: 'Return a single user' })
getOne(@Param('id') id: number) {
return { name: 'User #' + id }
}
@Post('/')
@OpenAPI({ summary: 'Create a new user' })
createUser(@Body({ validate: true }) body: CreateUserBody) {
Param,
Post,
Put
} from 'routing-controllers'
import { OpenAPI } from 'routing-controllers-openapi'
class CreateUserBody {
@IsString()
name: string
@IsOptional()
@MaxLength(20, { each: true })
hobbies: string[]
}
@OpenAPI({
security: [{ basicAuth: [] }]
})
@JsonController('/users')
export class UsersController {
@Get('/')
@OpenAPI({ summary: 'Return a list of users' })
getAll() {
return [
{ id: 1, name: 'First user!', hobbies: [] },
{ id: 2, name: 'Second user!', hobbies: ['fishing', 'cycling'] }
]
}
@Get('/:id')
@OpenAPI({ summary: 'Return a single user' })
getOne(@Param('id') id: number) {
@OpenAPI({
security: [{ basicAuth: [] }]
})
@JsonController('/users')
export class UsersController {
@Get('/')
@OpenAPI({ summary: 'Return a list of users' })
getAll() {
return [
{ id: 1, name: 'First user!', hobbies: [] },
{ id: 2, name: 'Second user!', hobbies: ['fishing', 'cycling'] }
]
}
@Get('/:id')
@OpenAPI({ summary: 'Return a single user' })
getOne(@Param('id') id: number) {
return { name: 'User #' + id }
}
@Post('/')
@OpenAPI({ summary: 'Create a new user' })
createUser(@Body({ validate: true }) body: CreateUserBody) {
return { ...body, id: 3 }
}
@Put('/')
createManyUsers(@Body({ type: CreateUserBody }) body: CreateUserBody[]) {
return {}
}
}
@OpenAPI({ summary: 'Return a list of users' })
getAll() {
return [
{ id: 1, name: 'First user!', hobbies: [] },
{ id: 2, name: 'Second user!', hobbies: ['fishing', 'cycling'] }
]
}
@Get('/:id')
@OpenAPI({ summary: 'Return a single user' })
getOne(@Param('id') id: number) {
return { name: 'User #' + id }
}
@Post('/')
@OpenAPI({ summary: 'Create a new user' })
createUser(@Body({ validate: true }) body: CreateUserBody) {
return { ...body, id: 3 }
}
@Put('/')
createManyUsers(@Body({ type: CreateUserBody }) body: CreateUserBody[]) {
return {}
}
}