Adding types for swagger-tools

https://github.com/apigee-127/swagger-tools
This commit is contained in:
Alex Brick
2017-06-13 11:50:09 +02:00
parent 9340ad9a47
commit 16efcb943d
4 changed files with 178 additions and 0 deletions

78
types/swagger-tools/index.d.ts vendored Normal file
View File

@@ -0,0 +1,78 @@
// Type definitions for swagger-tools 0.10
// Project: https://github.com/apigee-127/swagger-tools
// Definitions by: Alex Brick <https://github.com/bricka>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { NextHandleFunction } from 'connect';
import { IncomingMessage } from 'http';
export interface SwaggerRouterOptionsControllers {
[handlerName: string]: NextHandleFunction;
}
export interface SwaggerRouterOptions {
controllers?: SwaggerRouterOptionsControllers | string | string[];
useStubs?: boolean;
}
export interface SwaggerSecurityError {
code?: string;
message?: string;
state?: string;
statusCode?: number;
}
export type SwaggerSecurityCallback = (err?: SwaggerSecurityError) => void;
export type SwaggerSecurityHandler = (request: IncomingMessage, securityDefinition: any, scopes: string | string[], callback: SwaggerSecurityCallback) => void;
export interface SwaggerSecurityOptions {
[securityDefinitionName: string]: SwaggerSecurityHandler;
}
export interface SwaggerUi12ApiDeclarations {
[path: string]: any;
}
export interface SwaggerUiOptions {
apiDocs?: string;
apiDocsPrefix?: string;
swaggerUi?: string;
swaggerUiDir?: string;
swaggerUiPrefix?: string;
}
export interface SwaggerValidatorOptions {
validateResponse?: boolean;
}
export interface Middleware {
swaggerMetadata(): NextHandleFunction;
swaggerRouter(options?: SwaggerRouterOptions): NextHandleFunction;
swaggerSecurity(options?: SwaggerSecurityOptions): NextHandleFunction;
swaggerValidator(options?: SwaggerValidatorOptions): NextHandleFunction;
}
export interface Middleware12 extends Middleware {
swaggerUi(apiDeclarations: SwaggerUi12ApiDeclarations, options?: SwaggerUiOptions): NextHandleFunction;
}
export interface Middleware20 extends Middleware {
swaggerUi(options: SwaggerUiOptions): NextHandleFunction;
}
export type InitializeMiddlewareCallback12 = (middleware: Middleware12) => void;
export type InitializeMiddlewareCallback20 = (middleware: Middleware20) => void;
export interface Resource {
resourcePath: string;
}
export function initializeMiddleware(swaggerObject: any, callback: InitializeMiddlewareCallback20): void;
export function initializeMiddleware(
swaggerObject: any,
resources: Resource[],
callback: InitializeMiddlewareCallback12
): void;

View File

@@ -0,0 +1,77 @@
// 2.0 examples from https://github.com/apigee-127/swagger-tools/blob/master/examples/2.0/index.js
import * as connect from 'connect';
import { createServer } from 'http';
import * as swaggerTools from 'swagger-tools';
const app = connect();
const serverPort = 3000;
// swaggerRouter configuration
const options = {
controllers: './controllers',
useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode)
};
const swaggerUiOptions = {
apiDocs: 'apiDocs',
swaggerUi: 'swaggerUi',
};
// 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));
// 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');
// The Swagger API Declaration Documents (require them, build them programmatically, fetch them from a URL, ...)
const apiDeclarations = [
// tslint:disable-next-line no-var-requires
require('./api/weather.json')
];
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(apiDoc12, apiDeclarations, 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({
'/weather': apiDeclarations[0]
}));
// Start the server
createServer(app).listen(serverPort, () => {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
});
});

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"swagger-tools-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }