本文整理汇总了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());
});
}
示例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();
});
});
示例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());
});
}
示例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);
});
});
示例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',
//.........这里部分代码省略.........
示例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');