当前正在检出https://www.npmjs.com/package/swagger-client。这些文档让我有些困惑。
我打算打一个www.mysite.com/topic/:topicName
这样的网址
我尝试了一些类似的事情:
const authHeaders = {
'X-Api-Key': 'mykey',
Authorization: `Bearer mytoken`,
};
const topic = await client.apis.Topic.getTopic({
headers: authHeaders,
parameters: {
topicName: 'myName'
}
});
我似乎找不到正确的方法来发送标头,并从招摇的文档中填写变量以形成URL。我看过一些示例,其中标头是第一个参数,所以我也尝试过
const topic = await client.apis.Topic.getTopic(authHeaders, {
parameters: {
topicName: 'myName'
}
});
当我在文档中查找单词标题时,他们只是在谈论设置初始客户端。我认为让它每次都发送auth标头是另一个不错的解决方案,因此我正在寻找两种方式(因为根据标头,两种方法都有意义)。
答案 0 :(得分:0)
使用swagger-client与常规的http客户端有些不同,因为它完全遵循您的swagger文件,因此您必须提供您的swagger规范描述的参数。 对于swagger client v3,此格式对我有效:
Swagger({
url: "www.example.org/swagger.json",
authorization: {
// This must be described in your swagger file, if not, will be ignored.
// See https://swagger.io/docs/specification/authentication/bearer-authentication/
bearerAuth: {type: 'apiKey', 'in': 'header', name: 'Authorization'}
}
}).then(client => {
client.apis.Topic.getTopic({
// The parameters need to be described in the swagger file, or will be ignored.
// Also make sure that the getTopic operation describes that uses the bearerAuth
// Or swagger-client will not send the Auth credentials.
topicName: "myName"
}).then(response => {
// Do magic.
})
});
您的getTopic操作的摇摇欲坠部分应与此类似:
{
"/rest/3/topic/{topicName}": {
"tags": ["topic"]
"operationId": "getTopic"
"get": {
"parameters": [
{
"name": "topicName", "in": "path", "required": true, "type": "string"
}
],
"security": { "basicAuth": "global" }
}
}
}