Add typings for swagger-ui-express package

This commit is contained in:
d.rogozhny@gmail.com
2018-06-23 23:49:12 +03:00
parent e6b8a914b6
commit abffdc1c91
4 changed files with 177 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
// Type definitions for swagger-ui-express 3.0
// Project: https://github.com/scottie1984/swagger-ui-express
// Definitions by: Dmitry Rogozhny <https://github.com/dmitryrogozhny>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import { RequestHandler } from "express";
import { ServeStaticOptions } from "serve-static";
declare namespace SwaggerUiExpress {
type JsonObject = { [key: string]: any; };
type SwaggerUiOptions = { [key: string]: any; };
type SwaggerOptions = { [key: string]: any; };
/**
* Creates a middleware function that returns the pre-generated html file for the Swagger UI page.
*
* @param {(JsonObject | null)} [swaggerDoc] json object with the API schema.
* @param {(SwaggerUiOptions | false | null)} [opts] swagger-ui-express options.
* @param {SwaggerOptions} [options] custom swagger options.
* @param {(string | false | null)} [customCss] string with a custom css to embed into the page.
* @param {(string | false | null)} [customfavIcon] link to a custom favicon.
* @param {(string | false | null)} [swaggerUrl] Url of the swagger API schema, can be specified instead of the swaggerDoc.
* @param {(string | false | null)} [customeSiteTitle] custom title for a page
* @returns {RequestHandler} an express middleware function that returns the generated html page.
*/
function setup(swaggerDoc?: JsonObject | null,
opts?: SwaggerUiOptions | false | null,
options?: SwaggerOptions,
customCss?: string | false | null,
customfavIcon?: string | false | null,
swaggerUrl?: string | false | null,
customeSiteTitle?: string | false | null): RequestHandler;
/**
* Returns handlers for serving Swagger UI files.
* This includes custom initialization js file and static files of Swagger UI.
*
* @returns {Array<RequestHandler>} Express handlers that process requests and return files for Swagger UI.
*/
function serve(): Array<RequestHandler>;
/**
* Returns handlers for serving Swagger UI files.
* This includes custom initialization js file and static files of Swagger UI.
* Additional options are passed to the express.static middleware.
*
* @param {ServeStaticOptions} options options object that is passed to the express.static middleware.
* @returns {Array<RequestHandler>} Express handlers that process requests and return files for Swagger UI.
*/
function serveWithOptions(options: ServeStaticOptions): Array<RequestHandler>;
/**
* Generates the custom html page for the UI API.
*
* @param {(JsonObject | null)} [swaggerDoc] json object with the API schema.
* @param {(SwaggerUiOptions | false | null)} [opts] swagger-ui-express options.
* @param {SwaggerOptions} [options] custom swagger options.
* @param {(string | false | null)} [customCss] string with a custom css to embed into the page.
* @param {(string | false | null)} [customfavIcon] link to a custom favicon.
* @param {(string | false | null)} [swaggerUrl] Url of the swagger API schema, can be specified instead of the swaggerDoc.
* @param {(string | false | null)} [customeSiteTitle] custom title for a page
* @returns {string} the generated html page.
*/
function generateHTML(swaggerDoc?: JsonObject | null,
opts?: SwaggerUiOptions | false | null,
options?: SwaggerOptions,
customCss?: string | false | null,
customfavIcon?: string | false | null,
swaggerUrl?: string | false | null,
customeSiteTitle?: string | false | null): string;
/**
* Returns handlers for serving Swagger UI files.
* This includes custom initialization js file and static files of Swagger UI.
* Additional options object is passed to Swagger UI.
*
* @param {JsonObject} [swaggerDoc] json object with the Swagger API schema.
* @param {SwaggerUiOptions} [opts] options to pass to Swagger UI.
* @returns {Array<RequestHandler>} Express handlers that process requests and return files for Swagger UI.
*/
function serveFiles(swaggerDoc?: JsonObject, opts?: SwaggerUiOptions): Array<RequestHandler>;
}
export default SwaggerUiExpress;
@@ -0,0 +1,63 @@
/**
* Tests are taken from the module's repository at https://github.com/scottie1984/swagger-ui-express/blob/master/test/testapp/app.js
*/
import swaggerUi from "swagger-ui-express";
const express = require('express');
const app = express();
const swaggerDocument = require('./swagger.json');
const swaggerDocumentSplit = require('./swagger-split.json');
const options = {
validatorUrl: null,
oauth: {
clientId: "your-client-id1",
clientSecret: "your-client-secret-if-required1",
realm: "your-realms1",
appName: "your-app-name1",
scopeSeparator: ",",
additionalQueryStringParams: {}
},
docExpansion: 'full',
};
app.use('/api-docs', swaggerUi.serve);
app.get('/api-docs', swaggerUi.setup(swaggerDocument, false, options, '.swagger-ui .topbar { background-color: red }'));
app.use('/api-docs-from-url', swaggerUi.serve);
app.get('/api-docs-from-url', swaggerUi.setup(null, false, options, '.swagger-ui .topbar { background-color: red }', null, '/swagger.json'));
const swaggerUiOpts = {
explorer: false,
swaggerOptions: options,
customCss: '.swagger-ui .topbar { background-color: blue }'
};
app.use('/api-docs-using-object', swaggerUi.serve);
app.get('/api-docs-using-object', swaggerUi.setup(swaggerDocument, swaggerUiOpts));
const swaggerUiOpts2 = {
explorer: false,
swaggerOptions: options,
customCss: '.swagger-ui .topbar { background-color: pink }',
swaggerUrl: '/swagger.json',
customJs: '/my-custom.js',
operationsSorter: 'alpha'
};
app.use('/api-docs-from-url-using-object', swaggerUi.serve);
app.get('/api-docs-from-url-using-object', swaggerUi.setup(null, swaggerUiOpts2));
app.use('/api-docs-with-null', swaggerUi.serve);
app.get('/api-docs-with-null', swaggerUi.setup(swaggerDocument, null, options, '.swagger-ui .topbar { background-color: orange }'));
app.use('/api-docs-split', swaggerUi.serve);
app.get('/api-docs-split', swaggerUi.setup(swaggerDocumentSplit, null, options, '.swagger-ui .topbar { background-color: orange }'));
app.use('/api-docs-with-opts/', swaggerUi.serveWithOptions({ redirect: false }));
app.get('/api-docs-with-opts/', swaggerUi.setup(swaggerDocumentSplit, null, options, '.swagger-ui .topbar { background-color: orange }'));
const swaggerHtml = swaggerUi.generateHTML(swaggerDocument, swaggerUiOpts);
app.use('/api-docs-html1', swaggerUi.serveFiles(swaggerDocument, swaggerUiOpts));
+22
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-ui-express-tests.ts"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"no-var-requires": false
}
}