當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript swagger-tools.initializeMiddleware函數代碼示例

本文整理匯總了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;未經允許,請勿轉載。