本文整理汇总了TypeScript中swagger-tools.initializeMiddleware函数的典型用法代码示例。如果您正苦于以下问题:TypeScript initializeMiddleware函数的具体用法?TypeScript initializeMiddleware怎么用?TypeScript initializeMiddleware使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了initializeMiddleware函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: setupSwagger
setupSwagger() {
const spath = path.resolve(__dirname, '../../src/modules/pet-store/swagger.yml');
const file = fs.readFileSync(spath, 'utf8');
const spec = yaml.safeLoad(file);
swaggerTools.initializeMiddleware(spec, (middleware) => {
this.app.use(middleware.swaggerMetadata());
this.app.use(middleware.swaggerValidator());
this.app.use(middleware.swaggerUi());
});
}
开发者ID:iamchairs,项目名称:node-microservice-demo,代码行数:11,代码来源:Express.ts
示例2: initializeMiddleware
return new Promise<void>((resolve) => {
initializeMiddleware(this.swagger, (swaggerTools) => {
// logger.debug('Adding swagger middleware');
const swaggerMetadataFunc = swaggerTools.swaggerMetadata();
const swaggerValidatorFunc = swaggerTools.swaggerValidator();
const sm = { jwt: this.jwtHandler, apiKeyHeader: ApiKeyVerification.verifyApiKey(apiKey) };
const swaggerSecurityFunc = swaggerTools.swaggerSecurity(sm);
expressApp.use((req, res, next) => {
swaggerMetadataFunc(req, res, (err) => {
if (err) { return next(err); }
return next();
});
});
expressApp.use(swaggerSecurityFunc);
expressApp.use(swaggerValidatorFunc);
// logger.debug('Adding swagger middleware - Done');
resolve();
});
});
开发者ID:yruan,项目名称:inceptum,代码行数:22,代码来源:SwaggerMetadataMiddleware.ts
示例3: setupSwagger
setupSwagger() {
// resolve the spec
const spath = path.resolve('swagger.yml');
const file = fs.readFileSync(spath, 'utf8');
const spec = yaml.safeLoad(file);
// init jwt middleware
const jwtSecret = config.get('auth.jwt_secret').toString();
let jwt = expressJwt({
secret: new Buffer(jwtSecret, 'base64')
});
// setup middleware swagger middleware in express
swaggerTools.initializeMiddleware(spec, (middleware) => {
this.app.use(middleware.swaggerMetadata());
this.app.use(middleware.swaggerValidator({
validateResponse: true
}));
this.app.use(middleware.swaggerSecurity({
jwt_token: (req, authOrSecDef, scopes, cb) => {
jwt(req, req.res, (err) => {
if (req.userName === undefined) {
return cb(new Error('Access Denied!'));
} else {
logger.info(`${req.userName} authorized`, req.userName);
return cb(null);
}
});
}
}));
this.app.use(middleware.swaggerUi());
});
}
开发者ID:efueger,项目名称:node-microservice-demo,代码行数:36,代码来源:Express.ts
示例4: createServer
swaggerTools.initializeMiddleware(swaggerDoc20, middleware => {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Test passing in the handlers directly
app.use(middleware.swaggerRouter({
controllers: {
foo_bar: (req, res, next) => {
console.log(req.swagger.apiPath);
console.log(req.swagger.operation);
console.log(req.swagger.operationParameters);
console.log(req.swagger.operationPath);
console.log(req.swagger.path);
console.log(req.swagger.security);
console.log(req.swagger.swaggerObject);
console.log(req.swagger.params.location.value);
console.log(req.swagger.params.unit.value);
res.setHeader('Content-Type', 'application/json');
res.end([ 'foo', 0 ]);
},
}
}));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi(swaggerUiOptions));
app.use(middleware.swaggerUi());
// Start the server
createServer(app).listen(serverPort, () => {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
});
});
开发者ID:DenisCarriere,项目名称:DefinitelyTyped,代码行数:40,代码来源:swagger-tools-tests.ts
示例5: createServer
swaggerTools.initializeMiddleware(swaggerDoc20, middleware => {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Test passing in the handlers directly
app.use(middleware.swaggerRouter({
controllers: {
// These tests are based on tests here:
// https://github.com/apigee-127/swagger-tools/blob/0cea535b122265c6d01546e199e2e8fda4c0f5da/test/2.0/test-middleware-swagger-metadata.js#L102-L138
foo_bar: (req, res, next) => {
req.swagger.swaggerVersion = '2.0';
req.swagger.apiPath = '/pets/{id}';
req.swagger.operation = {
security: [
{
oauth2: ["read"]
}
],
tags: [ "Pet Operations" ],
operationId: "getPetById",
summary: "Finds the pet by id",
responses: {
200: {
description: "Pet response",
schema: {
$ref: "#/definitions/Pet"
}
},
default: {
description: "Unexpected error",
schema: {
$ref: "#/definitions/Error"
}
}
},
parameters: [
{
in: 'query',
name: 'mock',
description: 'Mock mode',
required: false,
type: 'boolean'
}
]
};
req.swagger.operationParameters = [
{
path: ['paths', '/pets/{id}', 'get', 'parameters', '0'],
schema: {
in: 'query',
name: 'mock',
description: 'Mock mode',
required: false,
type: 'boolean'
},
},
{
path: ['paths', '/pets/{id}', 'parameters', '0'],
schema: {
name: "id",
in: "path",
description: "ID of pet",
required: true,
type: "integer",
format: "int64"
}
}
];
req.swagger.operationPath = ['paths', '/pets/{id}', 'get'];
req.swagger.security = [
{
oauth2: [ 'read' ]
}
];
req.swagger.params = {
id: {
path: ['paths', '/pets/{id}', 'parameters', '0'],
schema: {
name: "id",
in: "path",
description: "ID of pet",
required: true,
type: "integer",
format: "int64"
},
originalValue: '1',
value: 1
},
mock: {
path: ['paths', '/pets/{id}', 'get', 'parameters', '0'],
schema: {
in: 'query',
//.........这里部分代码省略.........
开发者ID:Dru89,项目名称:DefinitelyTyped,代码行数:101,代码来源:swagger-tools-tests.ts
示例6: require
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
// tslint:disable-next-line no-var-requires
const swaggerDoc20 = require('./api/swagger.json');
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc20, middleware => {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi(swaggerUiOptions));
app.use(middleware.swaggerUi());
// Start the server
createServer(app).listen(serverPort, () => {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
});
});
// 1.2 examples from https://github.com/apigee-127/swagger-tools/blob/master/examples/1.2/index.js
// The Swagger Resource Listing Document (require it, build it programmatically, fetch it from a URL, ...)
// tslint:disable-next-line no-var-requires
const apiDoc12 = require('./api/api-doc.json');
开发者ID:Crevil,项目名称:DefinitelyTyped,代码行数:30,代码来源:swagger-tools-tests.ts
注:本文中的swagger-tools.initializeMiddleware函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论