In JS, we normally pass in credentials in as it's being created but Swagger's documentation only shows how to do this after the client object has been created.
Asked
Active
Viewed 1,724 times
1 Answers
4
Yes, just create an authorizations object with key names that align to the names in the securityDefinitions
and pass it to the constructor. Assuming a securityDefinition of:
"securityDefinitions" : {
"sec_def_entry" : {
"type" : "apiKey",
"name" : "entry_name",
"in" : "header"
}
}
The code would look something like the following:
SwaggerClient = require('swagger-client'); //node
SwaggerClient = window.SwaggerClient; //browser
var auths = {
sec_def_entry : new SwaggerClient.ApiKeyAuthorization("entry_name", "special-key","header")
};
var client = new SwaggerClient({
"url": 'https://example.com/swagger.json',
authorizations: auths
})
Note, however, that this will cause Swagger to pass the credential information when retrieving the spec from the server. This can cause issues if you have basic authorization turned on as basic-auth triggers an options preflight (costing an additional round-trip and requires setting the server to respond with a 200 respons to all options requests) and disallows wildcard origin in CORS.

Indolering
- 3,058
- 30
- 45
-
1Not really related, but your answer helped me to map actual arguments and swagger's definition: https://github.com/swagger-api/swagger-js/pull/911 – maxkoryukov Dec 24 '16 at 10:40