const doc = {
info: {
version: "", // by default: '1.0.0'
title: "", // by default: 'REST API'
description: "", // by default: ''
},
host: "", // by default: 'localhost:3000'
basePath: "", // by default: '/'
schemes: [], // by default: ['http']
consumes: [], // by default: ['application/json']
produces: [], // by default: ['application/json']
tags: [
// by default: empty Array
{
name: "Tutorials", // Tag name
description: "", // Tag description
},
{
name: "Login", // Tag name
description: "", // Tag description
},
// { ... }
],
securityDefinitions: {
bearerAuth: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
description: 'Enter JWT Bearer token **_only_** in the format `Bearer <token>`',
},
}, // by default: empty object
security: [
{
bearerAuth: [],
},
],
definitions: {}, // by default: empty object
};
这段代码中,我们首先定义了一个名为 bearerAuth的安全定义,它是一个 API Key 类型的安全定义。我们将该 API Key 命名为 Authorization
并放置在请求头中。
然后,我们使用 security
关键字将安全定义应用到我们的路径上,这样在访问接口 时需要提供 Bearer token。