swagger-snippet
Advanced tools
Comparing version 0.5.1 to 0.6.0
{ | ||
"name": "swagger-snippet", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Generates code snippets for given Swagger / Open API Specification files", | ||
@@ -13,3 +13,3 @@ "repository": { | ||
"Swagger", | ||
"Open API Specification" | ||
"OpenAPI Specification" | ||
], | ||
@@ -21,13 +21,14 @@ "author": "Erik Wittern", | ||
"test": "node test/test.js | tap-spec", | ||
"build": "browserify index.js | uglifyjs -c > swaggersnippet.min.js" | ||
"build": "browserify -g uglifyify ./index.js > ./dist/swaggersnippet.min.js" | ||
}, | ||
"dependencies": { | ||
"httpsnippet": "^1.16.5" | ||
"httpsnippet": "^1.16.7", | ||
"openapi-sampler": "^1.0.0-beta.14" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^13.1.0", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.6.0", | ||
"uglifyify": "^3.0.3" | ||
"browserify": "^16.2.3", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.10.1", | ||
"uglifyify": "^5.0.1" | ||
} | ||
} |
# Swagger Snippet | ||
**Generates code snippets for given Swagger / Open API specification files.** | ||
This package takes as input a Swagger 2.0 / Open API specification. It translates the specification into an [HTTP Archive 1.2 request object](http://www.softwareishard.com/blog/har-12-spec/#request). It uses the [HTTP Snippet](https://github.com/Mashape/httpsnippet) library to generate code snippets for every API endpoint (URL path + HTTP method) defined in the specification in various languages & tools (`cURL`, `Node`, `Python`, `Ruby`, `Java`, `Go`, `C#`...). | ||
This package takes as input a Swagger 2.0 / OpenAPI v3.0.x specification. It translates the specification into an [HTTP Archive 1.2 request object](http://www.softwareishard.com/blog/har-12-spec/#request). It uses the [HTTP Snippet](https://github.com/Mashape/httpsnippet) library to generate code snippets for every API endpoint (URL path + HTTP method) defined in the specification in various languages & tools (`cURL`, `Node`, `Python`, `Ruby`, `Java`, `Go`, `C#`...). | ||
## Installation | ||
npm i --save swagger-snippet | ||
```bash | ||
npm i swagger-snippet | ||
``` | ||
## Build Swagger Snippet (for use in browser) | ||
Clone the Swagger Snippet repository. Install required dependencies: | ||
npm i | ||
```bash | ||
npm i | ||
``` | ||
Build a minified version of Swagger Snippet (`swaggersnippet.min.js`): | ||
npm run build | ||
```bash | ||
npm run build | ||
``` | ||
@@ -25,14 +30,14 @@ ## Usage | ||
```javascript | ||
let SwaggerSnippet = require('swagger-snippet') | ||
const SwaggerSnippet = require('swagger-snippet') | ||
// define input: | ||
var swagger = ... // a Swagger / Open API specification | ||
var targets = ['node_unirest', 'c'] // array of targets for code snippets. See list below... | ||
const swagger = ... // a Swagger / Open API specification | ||
const targets = ['node_unirest', 'c'] // array of targets for code snippets. See list below... | ||
try { | ||
// either, get snippets for ALL endpoints: | ||
var results = SwaggerSnippet.getSwaggerSnippets(swagger, targets) // results is now array of snippets, see "Output" below. | ||
const results = SwaggerSnippet.getSwaggerSnippets(swagger, targets) // results is now array of snippets, see "Output" below. | ||
// ...or, get snippets for a single endpoint: | ||
var results2 = SwaggerSnippet.getEndpointSnippets(swagger, '/users/{user-id}/relationship', 'get', targets) | ||
const results2 = SwaggerSnippet.getEndpointSnippets(swagger, '/users/{user-id}/relationship', 'get', targets) | ||
} catch (err) { | ||
@@ -47,3 +52,5 @@ // do something with potential errors... | ||
<script type="text/javascript" src="path/to/swaggersnippet.min.js"></script> | ||
```html | ||
<script type="text/javascript" src="path/to/swaggersnippet.min.js"></script> | ||
``` | ||
@@ -60,3 +67,3 @@ Use Swagger Snippet, which now defines the global variable `SwaggerSnippet`. | ||
``` | ||
```json | ||
[ | ||
@@ -63,0 +70,0 @@ ... |
@@ -21,3 +21,3 @@ /** | ||
*/ | ||
var Instantiator = require('./schema-instantiator.js') | ||
var OpenAPISampler = require('openapi-sampler') | ||
@@ -75,59 +75,25 @@ /** | ||
typeof param.schema !== 'undefined') { | ||
var schema | ||
if (typeof param.schema['$ref'] === 'undefined') { | ||
schema = param.schema | ||
} else if (/^http/.test(param.schema['$ref'])) { | ||
// can't resolve this for now... | ||
} else { | ||
var ref = param.schema['$ref'].split('/').slice(-1)[0] | ||
schema = getResolvedSchema(swagger, swagger.definitions[ref]) | ||
} | ||
return { | ||
mimeType: 'application/json', | ||
text: JSON.stringify(Instantiator.instantiate(schema)) | ||
} | ||
try { | ||
const sample = OpenAPISampler.sample(param.schema, {skipReadOnly: true}, swagger) | ||
return { | ||
mimeType: 'application/json', | ||
text: JSON.stringify(sample) | ||
} | ||
} catch (err) { | ||
console.log(err) | ||
return null | ||
} | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
/** | ||
* Get a complete JSON schema from Swagger, where all references ($ref) are | ||
* resolved. $ref appear: | ||
* - as properties | ||
* - as items | ||
* | ||
* @param {[type]} swagger [description] | ||
* @param {[type]} schema [description] | ||
* @param {[type]} ref [description] | ||
* @return {[type]} [description] | ||
*/ | ||
var getResolvedSchema = function (swagger, schema) { | ||
if (schema.type === 'object') { | ||
if (typeof schema.properties !== 'undefined') { | ||
for (var propKey in schema.properties) { | ||
var prop = schema.properties[propKey] | ||
if (typeof prop['$ref'] === 'string' && | ||
!/^http/.test(prop['$ref'])) { | ||
var ref = prop['$ref'].split('/').slice(-1)[0] | ||
schema.properties[propKey] = swagger.definitions[ref] | ||
if (swagger.paths[path][method].requestBody && swagger.paths[path][method].requestBody.content && | ||
swagger.paths[path][method].requestBody.content['application/json'] && | ||
swagger.paths[path][method].requestBody.content['application/json'].schema) { | ||
const sample = OpenAPISampler.sample(swagger.paths[path][method].requestBody.content['application/json'].schema, {skipReadOnly: true}, swagger) | ||
return { | ||
mimeType: 'application/json', | ||
text: JSON.stringify(sample) | ||
} | ||
getResolvedSchema(swagger, schema.properties[propKey]) | ||
} | ||
} | ||
} else if (schema.type === 'array') { | ||
if (typeof schema.items !== 'undefined') { | ||
for (var itemKey in schema.items) { | ||
if (itemKey === '$ref' && | ||
!/^http/.test(schema.items[itemKey])) { | ||
var ref2 = schema.items['$ref'].split('/').slice(-1)[0] | ||
schema.items = swagger.definitions[ref2] | ||
} | ||
getResolvedSchema(swagger, schema.items) | ||
} | ||
} | ||
} | ||
return schema | ||
return null | ||
} | ||
@@ -142,2 +108,4 @@ | ||
var getBaseUrl = function (swagger) { | ||
if (swagger.servers) | ||
return swagger.servers[0].url; | ||
var baseUrl = '' | ||
@@ -189,3 +157,3 @@ if (typeof swagger.schemes !== 'undefined') { | ||
? (typeof param.default === 'undefined' | ||
? ('SOME_' + param.type.toUpperCase() + '_VALUE') | ||
? ('SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE') | ||
: param.default + '') | ||
@@ -237,2 +205,12 @@ : (values[param.name] + '') /* adding a empty string to convert to string */ | ||
// v3 'content-type' header: | ||
if (pathObj.requestBody && pathObj.requestBody.content) { | ||
for (const type3 of Object.keys(pathObj.requestBody.content)) { | ||
headers.push({ | ||
name: 'content-type', | ||
value: type3 | ||
}); | ||
} | ||
} | ||
// headers defined in path object: | ||
@@ -245,3 +223,3 @@ if (typeof pathObj.parameters !== 'undefined') { | ||
name: param.name, | ||
value: 'SOME_' + param.type.toUpperCase() + '_VALUE' | ||
value: 'SOME_' + (param.type||param.schema.type).toUpperCase() + '_VALUE' | ||
}) | ||
@@ -259,3 +237,6 @@ } | ||
var secScheme = Object.keys(pathObj.security[l])[0] | ||
var authType = swagger.securityDefinitions[secScheme].type.toLowerCase() | ||
var secDefinition = swagger.securityDefinitions ? | ||
swagger.securityDefinitions[secScheme] : | ||
swagger.components.securitySchemes[secScheme]; | ||
var authType = secDefinition.type.toLowerCase(); | ||
switch (authType) { | ||
@@ -266,4 +247,4 @@ case 'basic': | ||
case 'apikey': | ||
if (swagger.securityDefinitions[secScheme].in === 'query') { | ||
apiKeyAuthDef = secScheme | ||
if (secDefinition.in === 'header') { | ||
apiKeyAuthDef = secDefinition | ||
} | ||
@@ -286,3 +267,3 @@ break | ||
if (swagger.securityDefinitions[overallSecScheme].in === 'query') { | ||
apiKeyAuthDef = overallSecScheme | ||
apiKeyAuthDef = swagger.securityDefinitions[overallSecScheme] | ||
} | ||
@@ -304,3 +285,3 @@ break | ||
headers.push({ | ||
name: swagger.securityDefinitions[apiKeyAuthDef].name, | ||
name: apiKeyAuthDef.name, | ||
value: 'REPLACE_KEY_VALUE' | ||
@@ -346,3 +327,3 @@ }) | ||
} catch (e) { | ||
return null | ||
console.log(e); | ||
} | ||
@@ -349,0 +330,0 @@ } |
@@ -11,2 +11,4 @@ 'use strict' | ||
var IBMSwagger = require('./ibm_watson_alchemy_data_news_api.json') | ||
var PetStoreSwagger = require('./petstore_swagger.json') | ||
var PetstoreOas = require('./petstore_oas.json') | ||
@@ -53,2 +55,9 @@ test('Getting snippets should not result in error or undefined', function (t) { | ||
test('Getting snippets from OpenAPI 3.0.x shoudl work', function (t) { | ||
t.plan(1) | ||
// checks the 'Pages' schema... | ||
var result = SwaggerSnippet.getEndpointSnippets(PetstoreOas, '/pets/{id}', 'get', ['node_request']) | ||
t.notEqual(result, undefined) | ||
}) | ||
test('Testing optionally provided parameter values', function (t) { | ||
@@ -92,1 +101,11 @@ t.plan(2) | ||
}) | ||
test('Resolve samples from nested examples', function (t) { | ||
var result = SwaggerSnippet.getEndpointSnippets(PetStoreSwagger, '/user', 'post', ['node_request']) | ||
var snippet = result.snippets[0].content | ||
t.true(/username.*John78\'/.test(snippet)) | ||
t.true(/email.*john.smith@example.com\'/.test(snippet)) | ||
t.true(/phone.*\+1\-202\-555\-0192/.test(snippet)) | ||
t.true(/password.*drowssaP123/.test(snippet)) | ||
t.end() | ||
}) |
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
1190793
40568
115
2
+ Added@types/json-schema@7.0.15(transitive)
+ Addedforeach@2.0.6(transitive)
+ Addedjson-pointer@0.6.2(transitive)
+ Addedopenapi-sampler@1.5.1(transitive)
Updatedhttpsnippet@^1.16.7