From 16efcb943dd0eb9879fbf9e3b3e18eba9deeabe0 Mon Sep 17 00:00:00 2001 From: Alex Brick Date: Tue, 13 Jun 2017 11:50:09 +0200 Subject: [PATCH] Adding types for swagger-tools https://github.com/apigee-127/swagger-tools --- types/swagger-tools/index.d.ts | 78 ++++++++++++++++++++++ types/swagger-tools/swagger-tools-tests.ts | 77 +++++++++++++++++++++ types/swagger-tools/tsconfig.json | 22 ++++++ types/swagger-tools/tslint.json | 1 + 4 files changed, 178 insertions(+) create mode 100644 types/swagger-tools/index.d.ts create mode 100644 types/swagger-tools/swagger-tools-tests.ts create mode 100644 types/swagger-tools/tsconfig.json create mode 100644 types/swagger-tools/tslint.json diff --git a/types/swagger-tools/index.d.ts b/types/swagger-tools/index.d.ts new file mode 100644 index 0000000000..340d962ba0 --- /dev/null +++ b/types/swagger-tools/index.d.ts @@ -0,0 +1,78 @@ +// Type definitions for swagger-tools 0.10 +// Project: https://github.com/apigee-127/swagger-tools +// Definitions by: Alex Brick +// 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; diff --git a/types/swagger-tools/swagger-tools-tests.ts b/types/swagger-tools/swagger-tools-tests.ts new file mode 100644 index 0000000000..e39670be7c --- /dev/null +++ b/types/swagger-tools/swagger-tools-tests.ts @@ -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); + }); +}); diff --git a/types/swagger-tools/tsconfig.json b/types/swagger-tools/tsconfig.json new file mode 100644 index 0000000000..a926cbf792 --- /dev/null +++ b/types/swagger-tools/tsconfig.json @@ -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" + ] +} diff --git a/types/swagger-tools/tslint.json b/types/swagger-tools/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/swagger-tools/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }