Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'swagger-express-ts' 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.
import { Schema } from 'mongoose';
import { ApiModel, ApiModelProperty } from 'swagger-express-ts';
import mongoose from '../config/database';
export interface IUser {
_id?: string;
email: string;
password: string;
}
@ApiModel({
description: 'User Model',
name: 'User'
})
export class UserModel implements IUser {
/* tslint:disable */
public _id?: string;
/* tslint:enable */
@ApiModelProperty({
description: 'email of user',
example: ['email@test.com'],
required: true,
})
public email: string;
@ApiModelProperty({
description: 'password of user',
})
export class UserController {
private router: express.Router;
constructor() {
this.router = express.Router();
this.router.get('/', this.getUsers);
this.router.post('/', this.createUser);
}
public getRouter(): express.Router {
return this.router;
}
@ApiOperationGet({
description: 'Get user list',
responses: {
200: {
model: 'User',
type: SwaggerDefinitionConstant.Response.Type.ARRAY,
},
},
/* security: { - use if route is protected
apiKeyHeader: [],
}, */
summary: 'Get users list',
})
private async getUsers(
request: express.Request,
response: express.Response
): Promise {
import * as express from 'express';
import 'reflect-metadata';
import { ApiOperationGet, ApiOperationPost, ApiPath, SwaggerDefinitionConstant, } from 'swagger-express-ts';
import UserModel from '../models/user.model';
@ApiPath({
description: 'User Controller',
name: 'User Controller',
path: '/api/users',
// security: { apiKeyHeader: [] }, - use if route is protected
})
export class UserController {
private router: express.Router;
constructor() {
this.router = express.Router();
this.router.get('/', this.getUsers);
this.router.post('/', this.createUser);
}
public getRouter(): express.Router {
public config(): void {
// use json form parser middlware
this.app.use(bodyParser.json());
// use query string parser middlware
this.app.use(
bodyParser.urlencoded({
extended: false,
}),
);
this.app.use(swagger.express(
{
definition: {
externalDocs: {
url: 'Typescript Seed URL'
},
info: {
title: 'Typescript Seed API',
version: '1.0'
}
}
}
));
}
},
},
/* security: { - use if route is protected
apiKeyHeader: [],
}, */
summary: 'Get users list',
})
private async getUsers(
request: express.Request,
response: express.Response
): Promise {
const users = await UserModel.find({}).exec();
response.status(200).json(users);
}
@ApiOperationPost({
description: 'Post user object',
parameters: {
body: {
description: 'New user',
model: 'User',
required: true,
},
},
responses: {
200: {
model: 'User',
},
400: { description: 'Parameters fail' },
},
/* security: { - use if route is protected
apiKeyHeader: [],
constructor() {
this.router = express.Router();
this.router.get('/', this.getUsers);
this.router.post('/', this.createUser);
}
public getRouter(): express.Router {
return this.router;
}
@ApiOperationGet({
description: 'Get user list',
responses: {
200: {
model: 'User',
type: SwaggerDefinitionConstant.Response.Type.ARRAY,
},
},
/* security: { - use if route is protected
apiKeyHeader: [],
}, */
summary: 'Get users list',
})
private async getUsers(
request: express.Request,
response: express.Response
): Promise {
const users = await UserModel.find({}).exec();
response.status(200).json(users);
}
@ApiOperationPost({
import {
ApiModel,
ApiModelProperty,
SwaggerDefinitionConstant,
} from 'swagger-express-ts';
@ApiModel({
description: 'Description Constructor.',
name: 'Constructor',
})
export class ConstructorModel {
@ApiModelProperty({
description: 'Id of Constructor',
required: true,
})
public id: string;
@ApiModelProperty({
description: 'Name of Constructor',
required: true,
itemType: SwaggerDefinitionConstant.Model.Property.Type.STRING,
})
public name: string[];
import { ApiModel, ApiModelProperty } from 'swagger-express-ts';
import { ConstructorModel } from '../constructors/constructor.model';
@ApiModel({
description: 'Car description',
name: 'Car',
})
export class CarModel {
@ApiModelProperty({
description: 'Id of car',
required: true,
example: ['123456789', '12345'],
})
public id: string;
@ApiModelProperty({
description: '',
required: true,
})
public name: string;
ApiOperationPut,
} from 'swagger-express-ts';
import { CarsService } from './cars.service';
import { CarModel } from './car.model';
@ApiPath({
path: '/cars',
name: 'Cars',
security: { apiKeyHeader: [] },
})
@controller('/cars')
@injectable()
export class CarsController implements interfaces.Controller {
constructor(@inject(CarsService.name) private carsService: CarsService) {}
@ApiOperationGet({
description: 'Get cars objects list',
summary: 'Get cars list',
responses: {
200: {
type: SwaggerDefinitionConstant.Response.Type.ARRAY,
model: 'Car',
},
},
security: {
apiKeyHeader: [],
},
})
@httpGet('/')
public getCars(
request: express.Request,
response: express.Response,
@ApiPath({
name: 'Versions',
path: '/versions/{id}',
})
@controller('/versions/:id')
@injectable()
export class VersionController implements interfaces.Controller {
public static TARGET_NAME: string = 'VersionController';
constructor(
@inject(VersionsService.TARGET_NAME)
private versionsService: VersionsService
) {}
@ApiOperationGet({
description: 'Get version object',
parameters: {
path: {
id: {
required: true,
type: SwaggerDefinitionConstant.Parameter.Type.STRING,
},
},
},
responses: {
200: {
model: 'Version',
},
400: {},
},
})