deckardcain
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -7,2 +7,8 @@ 'use strict'; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _jsYaml = require('js-yaml'); | ||
var _jsYaml2 = _interopRequireDefault(_jsYaml); | ||
var API_BLUEPRINT_HEADER = /^[\uFEFF]?(((VERSION:( |\t)2)|(FORMAT:( |\t)(X-)?1A))([\n\r]{1,2}|$))/i; | ||
@@ -21,23 +27,45 @@ var API_BLUEPRINT_RESPONSE = /\+\s+Response\s+\d{3}/i; | ||
if (source.match(API_BLUEPRINT_HEADER)) { | ||
// I spotted 'FORMAT: 1A' header. This is a clear clue that | ||
// the file is API Blueprint. | ||
return 'text/vnd.apiblueprint'; | ||
} | ||
var identifiedType = null; | ||
if (source.match(LEGACY_BLUEPRINT_TITLE)) { | ||
// I spotted '--- Sample Title ---' title. This looks like | ||
// we are dealing with the legacy Apiary Blueprint. | ||
return 'text/vnd.legacyblueprint'; | ||
} | ||
try { | ||
var json = JSON.parse(source); | ||
if (source.match(API_BLUEPRINT_RESPONSE)) { | ||
// I can not see the '--- Sample Title ---' and at the same time | ||
// there is something like '+ Response 200' in the document, which is | ||
// pretty distinctive for API Blueprint. | ||
return 'text/vnd.apiblueprint'; | ||
// It looks like the source is a valid JSON file. I suspect it to | ||
// be a Swagger file. | ||
if (json.swagger) { | ||
// Indeed, we are dealing with Swagger file! | ||
identifiedType = 'application/swagger+json'; | ||
} | ||
} catch (jsonException) { | ||
// Well ok, it's not a JSON...Maybe we're dealing with YAML file? | ||
try { | ||
var yaml = _jsYaml2['default'].safeLoad(source); | ||
if (yaml.swagger) { | ||
// Indeed, we are dealing with Swagger file! | ||
identifiedType = 'application/swagger+yaml'; | ||
} | ||
} catch (yamlException) { | ||
// Well ok I am sorry, it's not a Swaggerish | ||
} | ||
} finally { | ||
if (identifiedType === null) { | ||
if (source.match(API_BLUEPRINT_HEADER)) { | ||
// I spotted 'FORMAT: 1A' header, which gives us a clear clue that | ||
// the file is API Blueprint. | ||
identifiedType = 'text/vnd.apiblueprint'; | ||
} else if (source.match(LEGACY_BLUEPRINT_TITLE)) { | ||
// I spotted '--- Sample Title ---' title. This looks like | ||
// we are dealing with the legacy Apiary Blueprint. | ||
identifiedType = 'text/vnd.legacyblueprint'; | ||
} else if (source.match(API_BLUEPRINT_RESPONSE)) { | ||
// I can not see the '--- Sample Title ---' and at the same time | ||
// there is something like '+ Response 200' in the document, which is | ||
// pretty distinctive for API Blueprint. | ||
identifiedType = 'text/vnd.apiblueprint'; | ||
} | ||
} | ||
return identifiedType; | ||
} | ||
// I do not know. Very strange item! | ||
return null; | ||
} | ||
@@ -44,0 +72,0 @@ |
{ | ||
"name": "deckardcain", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Identifies (media) type of API description files", | ||
@@ -36,4 +36,4 @@ "main": "lib/deckardcain", | ||
"chai": "^3.2.0", | ||
"dedent": "^0.6.0", | ||
"eslint": "^1.2.1", | ||
"eslint-config-airbnb": "0.0.8", | ||
"karma": "^0.13.3", | ||
@@ -45,5 +45,8 @@ "karma-browserify": "^4.2.1", | ||
"karma-mocha-reporter": "^1.0.4", | ||
"eslint-config-airbnb": "^1.0.0", | ||
"mocha": "^2.2.1" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"js-yaml": "^3.4.3" | ||
} | ||
} |
@@ -15,2 +15,3 @@ # Deckard Cain | ||
- Legacy Apiary Blueprint (predecessor of API Blueprint) - `text/vnd.legacyblueprint` | ||
- Swagger - `application/swagger+json` (according to [swagger-api/swagger-spec#110](https://github.com/swagger-api/swagger-spec/issues/110)) and `application/swagger+yaml` | ||
@@ -17,0 +18,0 @@ ## Install |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
9271
58
81
1
14
8
1
+ Addedjs-yaml@^3.4.3
+ Addedargparse@1.0.10(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedsprintf-js@1.0.3(transitive)