Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async findById(ctx: Context): Promise {
const result = await this.service.findById(ctx.request.params.id);
if (result) {
return new HttpResponseCreated(result);
}
return new HttpResponseNotFound();
}
@Patch('/:id')
@ApiParameter({
in: 'path',
name: 'id',
required: true,
schema: { type: 'integer' }
})
@ApiRequestBody((c: BaseController) => ({
content: {
'application/json': {
schema: c.schema
}
},
required: true
}))
async update(ctx: Context): Promise {
const result = await this.service.update(ctx.request.params.id, ctx.request.body);
if (result) {
return new HttpResponseCreated(result);
}
return new HttpResponseNotFound();
}
@Delete('/:id')
schema: { $ref: '#/components/schemas/User' }
}
},
description: 'Created user object',
required: true
})
@ApiResponse('default', { description: 'successful operation', content: {} })
createUser() {}
@Post('/createWithArray')
@ApiOperation({
operationId: 'createUsersWithArrayInput',
responses: {},
summary: 'Creates list of users with given input array'
})
@ApiRequestBody({
content: {
'*/*': {
schema: {
items: { $ref: '#/components/schemas/User' },
type: 'array',
}
}
},
description: 'List of user object',
required: true
})
@ApiResponse('default', { description: 'successful operation', content: {} })
createUsersWithArrayInput() {}
@Post('/createWithList')
@ApiOperation({
wrapped: true
}
}
},
required: [ 'name', 'photoUrls' ],
type: 'object',
xml: { name: 'Pet' }
})
class PetController {
@Put()
@ApiOperation({
operationId: 'updatePet',
responses: {},
summary: 'Update an existing pet',
})
@ApiRequestBody({
content: {
'application/json': {
schema: { $ref: '#/components/schemas/Pet' }
},
'application/xml': {
schema: { $ref: '#/components/schemas/Pet' }
},
},
description: 'Pet object that needs to be added to the store',
required: true
})
@ApiResponse(400, { description: 'Invalid ID supplied', content: {} })
@ApiResponse(404, { description: 'Pet not found', content: {} })
@ApiResponse(405, { description: 'Validation exception', content: {} })
@ApiSecurityRequirement({
petstore_auth: ['write:pets', 'read:pets']
})
@ApiResponse(400, { description: 'Invalid ID supplied', content: {} })
@ApiResponse(404, { description: 'Pet not found', content: {} })
@ApiResponse(405, { description: 'Validation exception', content: {} })
@ApiSecurityRequirement({
petstore_auth: ['write:pets', 'read:pets']
})
updatePet() { }
@Post()
@ApiOperation({
operationId: 'addPet',
responses: {},
summary: 'Add a new pet to the store'
})
@ApiRequestBody({
content: {
'application/json': {
schema: { $ref: '#/components/schemas/Pet' }
},
'application/xml': {
schema: { $ref: '#/components/schemas/Pet' }
},
},
description: 'Pet object that needs to be added to the store',
required: true,
})
@ApiResponse(405, { description: 'Invalid input', content: {} })
@ApiSecurityRequirement({
petstore_auth: ['write:pets', 'read:pets']
})
addPet() { }
}
}
},
description: 'List of user object',
required: true
})
@ApiResponse('default', { description: 'successful operation', content: {} })
createUsersWithArrayInput() {}
@Post('/createWithList')
@ApiOperation({
operationId: 'createUsersWithListInput',
responses: {},
summary: 'Creates list of users with given input array'
})
@ApiRequestBody({
content: {
'*/*': {
schema: {
items: { $ref: '#/components/schemas/User' },
type: 'array'
}
}
},
description: 'List of user object',
required: true
})
@ApiResponse('default', { description: 'successful operation', content: {} })
createUsersWithListInput() {}
@Get('/login')
@ApiOperation({
}
},
description: 'successful operation'
})
@ApiSecurityRequirement({
api_key: []
})
getInventory() { }
@Post('/order')
@ApiOperation({
operationId: 'placeOrder',
responses: {},
summary: 'Place an order for a pet'
})
@ApiRequestBody({
content: {
'*/*': {
schema: { $ref: '#/components/schemas/Order' }
}
},
description: 'order placed for purchasing the pet',
required: true
})
@ApiResponse(200, {
content: {
'application/json': {
schema: { $ref: '#/components/schemas/Order' }
},
'application/xml': {
schema: { $ref: '#/components/schemas/Order' }
},
@ApiOperation({
operationId: 'updatePetWithForm',
responses: {},
summary: 'Updates a pet in the store with form data'
})
@ApiParameter({
description: 'ID of pet that needs to be updated',
in: 'path',
name: 'petId',
required: true,
schema: {
format: 'int64',
type: 'integer',
}
})
@ApiRequestBody({
content: {
'application/x-www-form-urlencoded': {
schema: {
properties: {
name: { type: 'string', description: 'Updated name of the pet' },
status: { type: 'string', description: 'Updated status of the pet' },
}
}
}
}
})
@ApiResponse(405, { description: 'Invalid input', content: {} })
@ApiSecurityRequirement({
petstore_auth: ['write:pets', 'read:pets']
})
updatePetWithForm() { }
@Put('/:username')
@ApiOperation({
description: 'This can only be done by the logged in user.',
operationId: 'updateUser',
responses: {},
summary: 'Updated user'
})
@ApiParameter({
description: 'name that need to be updated',
in: 'path',
name: 'username',
required: true,
schema: { type: 'string' }
})
@ApiRequestBody({
content: {
'*/*': {
schema: { $ref: '#/components/schemas/User' }
}
},
description: 'Updated user object',
required: true
})
@ApiResponse(400, { description: 'Invalid user supplied', content: {} })
@ApiResponse(404, { description: 'User not found', content: {} })
updateUser() {}
@Delete('/:username')
@ApiOperation({
description: 'This can only be done by the logged in user.',
operationId: 'deleteUser',
deleteById(id: any);
}
abstract class BaseController {
abstract service: CRUDService;
entity: Class;
schema: IApiSchema;
constructor(entity: Class, schema: IApiSchema) {
this.entity = entity;
this.schema = schema;
}
@Post('/')
@ValidateBody((c: BaseController) => c.schema)
@ApiRequestBody((c: BaseController) => ({
content: {
'application/json': {
schema: c.schema
}
},
required: true
}))
async create(ctx: Context): Promise {
const result = await this.service.create(ctx.request.body);
return new HttpResponseCreated(result);
}
@Get('/')
async findAll(): Promise {
const repository = await getRepository(this.entity);
const result = await repository.find();
}
@Post('/:id')
postById() {
return new HttpResponseMethodNotAllowed();
}
@Patch('/')
patch() {
return new HttpResponseMethodNotAllowed();
}
@Patch('/:id')
@ValidateParams({ properties: { id: { type: 'number' } }, type: 'object' })
@ValidateBody({ ...productSchema, required: [] })
@ApiRequestBody({
content: {
'application/json': {
schema: { ...productSchema, required: [] }
}
},
required: true
})
async patchById(ctx: Context) {
const product = await getRepository(Product).findOne(ctx.request.params.id);
if (!product) {
return new HttpResponseNotFound();
}
Object.assign(product, ctx.request.body);