swagger-client
Advanced tools
Comparing version 2.1.13 to 2.1.14
@@ -46,2 +46,5 @@ 'use strict'; | ||
// favor the object-level authorizations over global | ||
var authz = obj.clientAuthorizations || this.authz; | ||
// Securities could be [ {} ] | ||
@@ -61,3 +64,3 @@ _.each(securities, function (obj, key) { | ||
_.each(this.authz, function (auth, authName) { | ||
_.each(authz, function (auth, authName) { | ||
if(applyAll || _.includes(flattenedSecurities, authName)) { | ||
@@ -64,0 +67,0 @@ var newStatus = auth.apply(obj); |
@@ -46,2 +46,3 @@ 'use strict'; | ||
'help', | ||
'host', | ||
'idFromOp', | ||
@@ -102,3 +103,3 @@ 'info', | ||
this.swaggerObject = {}; | ||
this.deferredClient = Q.defer(); | ||
this.deferredClient = undefined; | ||
@@ -134,2 +135,7 @@ this.clientAuthorizations = new auth.SwaggerAuthorizations(); | ||
if(this.usePromise) { | ||
this.deferredClient = Q.defer(); | ||
} | ||
if (typeof options.success === 'function') { | ||
@@ -255,2 +261,3 @@ this.success = options.success; | ||
this.securityDefinitions = response.securityDefinitions; | ||
this.security = response.security; | ||
this.title = response.title || ''; | ||
@@ -431,2 +438,23 @@ | ||
// sort the apisArray according to the tags | ||
var sortedApis = []; | ||
_.forEach(Object.keys(definedTags), function (tag) { | ||
var _apiToAdd; | ||
var pos; | ||
for(pos in self.apisArray) { | ||
var _api = self.apisArray[pos]; | ||
if(_api && tag === _api.name) { | ||
sortedApis.push(_api); | ||
self.apisArray[pos] = null; | ||
} | ||
} | ||
}); | ||
// add anything left | ||
_.forEach(self.apisArray, function (api) { | ||
if(api) { | ||
sortedApis.push(api); | ||
} | ||
}); | ||
self.apisArray = sortedApis; | ||
_.forEach(response.definitions, function (definitionObj, definition) { | ||
@@ -565,2 +593,19 @@ definitionObj['id'] = definition.toLowerCase(); | ||
SwaggerClient.prototype.setSchemes = function (schemes) { | ||
this.schemes = schemes; | ||
if(schemes && schemes.length > 0) { | ||
if(this.apis) { | ||
_.forEach(this.apis, function (api) { | ||
if (api.operations) { | ||
_.forEach(api.operations, function (operation) { | ||
operation.scheme = schemes[0]; | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
SwaggerClient.prototype.fail = function (message) { | ||
@@ -567,0 +612,0 @@ if (this.usePromise) { |
@@ -132,24 +132,4 @@ 'use strict'; | ||
obj.cache = false; | ||
obj.data = obj.body; | ||
delete obj.useJQuery; | ||
/* | ||
obj.beforeSend = function (xhr) { | ||
var key, results; | ||
if (obj.headers) { | ||
results = []; | ||
for (key in obj.headers) { | ||
if (key.toLowerCase() === 'content-type') { | ||
results.push(obj.contentType = obj.headers[key]); | ||
} else if (key.toLowerCase() === 'accept') { | ||
results.push(obj.accepts = obj.headers[key]); | ||
} else { | ||
results.push(xhr.setRequestHeader(key, obj.headers[key])); | ||
} | ||
} | ||
return results; | ||
} | ||
};*/ | ||
obj.data = obj.body; | ||
delete obj.body; | ||
@@ -156,0 +136,0 @@ |
@@ -64,14 +64,53 @@ 'use strict'; | ||
var definition = spec.definitions[name]; | ||
for (propertyName in definition.properties) { | ||
property = definition.properties[propertyName]; | ||
if(_.isArray(property.allOf)) { | ||
this.processAllOf(root, name, property, resolutionTable, unresolvedRefs, spec); | ||
if(definition['$ref']) { | ||
this.resolveInline(root, spec, definition, resolutionTable, unresolvedRefs, definition); | ||
} | ||
else { | ||
for (propertyName in definition.properties) { | ||
property = definition.properties[propertyName]; | ||
if (_.isArray(property.allOf)) { | ||
this.processAllOf(root, name, property, resolutionTable, unresolvedRefs, spec); | ||
} | ||
else { | ||
this.resolveTo(root, property, resolutionTable, '/definitions'); | ||
} | ||
} | ||
if (definition.allOf) { | ||
this.processAllOf(root, name, definition, resolutionTable, unresolvedRefs, spec); | ||
} | ||
} | ||
} | ||
// shared parameters | ||
spec.parameters = spec.parameters || {}; | ||
for(name in spec.parameters) { | ||
var parameter = spec.parameters[name]; | ||
if (parameter.in === 'body' && parameter.schema) { | ||
if(_.isArray(parameter.schema.allOf)) { | ||
// move to a definition | ||
var modelName = 'inline_model'; | ||
var name = modelName; | ||
var done = false; var counter = 0; | ||
while(!done) { | ||
if(typeof spec.definitions[name] === 'undefined') { | ||
done = true; | ||
break; | ||
} | ||
name = modelName + '_' + counter; | ||
counter ++; | ||
} | ||
spec.definitions[name] = { allOf: parameter.schema.allOf }; | ||
delete parameter.schema.allOf; | ||
parameter.schema.$ref = '#/definitions/' + name; | ||
this.processAllOf(root, name, spec.definitions[name], resolutionTable, unresolvedRefs, spec); | ||
} | ||
else { | ||
this.resolveTo(root, property, resolutionTable, '/definitions'); | ||
this.resolveTo(root, parameter.schema, resolutionTable, location); | ||
} | ||
} | ||
if(definition.allOf) { | ||
this.processAllOf(root, name, definition, resolutionTable, unresolvedRefs, spec); | ||
if (parameter.$ref) { | ||
// parameter reference | ||
this.resolveInline(root, spec, parameter, resolutionTable, unresolvedRefs, parameter.$ref); | ||
} | ||
@@ -169,2 +208,8 @@ } | ||
} | ||
else if('array' === responseObj.schema.type) { | ||
if(responseObj.schema.items && responseObj.schema.items.$ref) { | ||
// response reference | ||
this.resolveInline(root, spec, responseObj.schema.items, resolutionTable, unresolvedRefs, location); | ||
} | ||
} | ||
else { | ||
@@ -597,2 +642,10 @@ this.resolveTo(root, response.schema, resolutionTable, location); | ||
resolutionTable.push({obj: property, resolveAs: 'inline', root: root, key: key, location: location}); | ||
} else if (ref.indexOf('/') === 0 && ref.indexOf('#') === -1) { | ||
location = ref; | ||
var matches = root.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i); | ||
if(matches) { | ||
root = matches[0] + ref.substring(1); | ||
location = ''; | ||
} | ||
resolutionTable.push({obj: property, resolveAs: 'inline', root: root, key: key, location: location}); | ||
} | ||
@@ -599,0 +652,0 @@ else { |
@@ -473,2 +473,8 @@ 'use strict'; | ||
var swaggerRequestHeaders = 'application/json'; | ||
if(opts && opts.swaggerRequestHeaders) { | ||
swaggerRequestHeaders = opts.swaggerRequestHeaders; | ||
} | ||
if(expectedCount === 0) { | ||
@@ -492,3 +498,3 @@ this.finish(callback, swagger); | ||
url: absolutePath, | ||
headers: {accept: 'application/json'}, | ||
headers: { accept: swaggerRequestHeaders }, | ||
on: {}, | ||
@@ -495,0 +501,0 @@ method: 'get' |
@@ -16,3 +16,3 @@ 'use strict'; | ||
this.models = models || {}; | ||
this.name = definition.title || name || 'Inline Model'; | ||
this.name = name || definition.title || 'Inline Model'; | ||
this.modelPropertyMacro = modelPropertyMacro || function (property) { | ||
@@ -19,0 +19,0 @@ return property.default; |
@@ -44,3 +44,3 @@ 'use strict'; | ||
this.schemes = args.schemes || parent.schemes; | ||
this.security = args.security; | ||
this.security = args.security || parent.security; | ||
this.summary = args.summary || ''; | ||
@@ -124,3 +124,5 @@ this.type = null; | ||
if (typeof param['enum'] !== 'undefined') { | ||
var enumValues = param['enum'] || (param.items && param.items['enum']); | ||
if (typeof enumValues !== 'undefined') { | ||
var id; | ||
@@ -132,4 +134,4 @@ | ||
for (id = 0; id < param['enum'].length; id++) { | ||
var value = param['enum'][id]; | ||
for (id = 0; id < enumValues.length; id++) { | ||
var value = enumValues[id]; | ||
var isDefault = (value === param.default || value+'' === param.default); | ||
@@ -738,2 +740,3 @@ | ||
headers: headers, | ||
clientAuthorizations: opts.clientAuthorizations, | ||
on: { | ||
@@ -740,0 +743,0 @@ response: function (response) { |
@@ -11,3 +11,3 @@ { | ||
"description": "swagger-client is a javascript client for use with swaggering APIs.", | ||
"version": "2.1.13", | ||
"version": "2.1.14", | ||
"homepage": "http://swagger.io", | ||
@@ -14,0 +14,0 @@ "repository": { |
@@ -89,3 +89,3 @@ # Swagger JS library | ||
You can use promises too, by passing the `usePromise: true` option: | ||
You can use promises, too, by passing the `usePromise: true` option: | ||
@@ -110,3 +110,3 @@ ```js | ||
Need to pass an API key? Configure one as a query string: | ||
Need to pass an API key? Configure one in your client instance as a query string: | ||
@@ -130,6 +130,6 @@ ```js | ||
authorizations : { | ||
easyapi_basic: new client.PasswordAuthorization('<username>', '<password>'), | ||
someHeaderAuth: new client.ApiKeyAuthorization('<nameOfHeader>', '<value>', 'header'), | ||
someQueryAuth: new client.ApiKeyAuthorization('<nameOfQueryKey>', '<value>', 'query'), | ||
someCookieAuth: new client.CookieAuthorization('<cookie>'), | ||
easyapi_basic: new Swagger.PasswordAuthorization('<username>', '<password>'), | ||
someHeaderAuth: new Swagger.ApiKeyAuthorization('<nameOfHeader>', '<value>', 'header'), | ||
someQueryAuth: new Swagger.ApiKeyAuthorization('<nameOfQueryKey>', '<value>', 'query'), | ||
someCookieAuth: new Swagger.CookieAuthorization('<cookie>'), | ||
} | ||
@@ -139,7 +139,25 @@ }); | ||
Note the authorization nickname, such as `easyapi_basic` in the above example, must match the `security` requirement in the specification (see the [OAI Specification](https://github.com/OAI/OpenAPI-Specification/blob/master/README.md) for details). | ||
You can also pass authorzations on a _per-request_ basis, in the event that you're reusing a `swagger-client` object across multiple connections: | ||
``` | ||
client.pet.addPet({pet: { | ||
name: 'doggie' | ||
}}, { | ||
clientAuthorizations: { | ||
api_key: new Swagger.ApiKeyAuthorization('foo', 'bar', 'header') | ||
} | ||
}) | ||
.then(function(pet) { | ||
console.log(pet.obj); | ||
}); | ||
``` | ||
### Calling an API with swagger + the browser! | ||
Download `browser/swagger-client.js` into your webapp: | ||
Download [`browser/swagger-client.min.js`](https://raw.githubusercontent.com/swagger-api/swagger-js/master/browser/swagger-client.min.js) and place it into your webapp: | ||
```js | ||
```html | ||
<script src='browser/swagger-client.js' type='text/javascript'></script> | ||
@@ -170,2 +188,3 @@ <script type="text/javascript"> | ||
// note: the parameter for `addPet` is named `body` in the example below | ||
client.pet.addPet({body: pet}); | ||
@@ -181,3 +200,3 @@ ``` | ||
### Need XML response? | ||
### Need XML response? (assuming your server can produce it) | ||
```js | ||
@@ -214,2 +233,36 @@ client.pet.getPetById({petId:1}, {responseContentType:"application/xml"}); | ||
### Using your own HTTP client | ||
Don't like [superagent](https://github.com/visionmedia/superagent)? Despise [JQuery](https://github.com/jquery/jquery)? Well, you're in luck. You can plug your own HTTP library easily: | ||
```js | ||
var myHttpClient = { | ||
// implment an execute function | ||
execute: function(obj) { | ||
var httpMethod = obj.method; | ||
var requestHeaders = obj.headers; | ||
var body = obj.body; | ||
var url = obj.url; | ||
// do your thing, and call `obj.on.response` | ||
if(itWorked) { | ||
obj.on.response('horray'); | ||
} | ||
else { | ||
obj.on.error('boo'); | ||
} | ||
} | ||
}; | ||
var client = new SwaggerClient({ | ||
spec: petstoreRaw, | ||
client: myHttpClient, | ||
success: function () { | ||
client.pet.getPetById({petId: 3}, function(data){ | ||
expect(data).toBe('ok'); | ||
done(); | ||
}); | ||
} | ||
}); | ||
``` | ||
### How does it work? | ||
@@ -223,3 +276,3 @@ The swagger javascript client reads the swagger api definition directly from the server. As it does, it constructs a client based on the api definition, which means it is completely dynamic. It even reads the api text descriptions (which are intended for humans!) and provides help if you need it: | ||
The HTTP requests themselves are handled by the excellent [shred](https://github.com/automatthew/shred) library, which has a ton of features itself. But it runs on both node and the browser. | ||
The HTTP requests themselves are handled by the excellent [superagent](https://github.com/visionmedia/superagent) library, which has a ton of features itself. But it runs on both node and the browser. | ||
@@ -230,5 +283,6 @@ | ||
Please [fork the code](https://github.com/swagger-api/swagger-js) and help us improve | ||
swagger-client.js. Send us a pull request to the `master` branch! Tests make merges get accepted more quickly. | ||
Please [fork the code](https://github.com/swagger-api/swagger-js) and help us improve swagger-js. Send us a pull request to the `master` branch! Tests make merges get accepted more quickly. | ||
Note! We _will not_ merge pull requests for features not supported in the OAI Specification! Add an issue there instead! | ||
swagger-js use gulp for Node.js. | ||
@@ -235,0 +289,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1954231
24082
330