Skip to content

Commit

Permalink
v1.1.3: Adding docs and toMap export option (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
djfdyuruiry authored Jan 22, 2020
1 parent e175faa commit f310d49
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 48 deletions.
118 changes: 118 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# swagger2-to-json #

A simple interface for converting Swagger v2 JSON spec requests/responses to JSON samples.

Features:

- Import Swagger Spec direct from URL, JSON file, raw JSON string and JavaScript object

- Export request/response sample JSON to string and file

NPM Package: https://www.npmjs.com/package/swagger2-to-json
GitHub: https://github.com/djfdyuruiry/swagger2-to-json

This package is part of a collection of three Swagger v2 converters I have created:

- swagger2-to-object:
[NPM](https://www.npmjs.com/package/swagger2-to-object) | [GitHub](https://github.com/djfdyuruiry/swagger2-to-object)
- swagger2-postman-generator:
[NPM](https://www.npmjs.com/package/swagger2-postman-generator) | [GitHub](https://github.com/djfdyuruiry/swagger2-postman-generator)

---

## Install ##

``` shell
npm install swagger2-to-json
```

---


## Usage ##

This NPM module returns a single object which is used to access a chain of different functions. Import the module like so:

``` javascript
var Swagger2ToJson = require("swagger2-to-json");

Swagger2ToJson
.convertSwagger()
// do more stuff...
```

**Import Swagger JSON File**

``` javascript
Swagger2ToJson
.convertSwagger()
.fromFile("swagger.json")
```

**Import Swagger JSON String**

``` javascript
Swagger2ToJson
.convertSwagger()
.fromJson('{"swagger":"2.0",...')
```

**Import Swagger JavaScript Object**

``` javascript
var swaggerSpec = getSwaggerSpecFromSomewhere(); // example

Swagger2ToJson
.convertSwagger()
.fromSpec(swaggerSpec)
```

---

**Output Object**

After importing a Swagger spec, the output object will have the following structure:

``` javascript
{
requestsAndResponses,
requests,
responses
}
```

Each of the keys can be called as a function so you can chain the data for export; see below.

---

**Export to Map**

This will create a map where each request/response schema path is a key.

``` javascript
var requestResponsesMap = Swagger2ToJson
.convertSwagger()
.fromUrl("http://petstore.swagger.io/v2/swagger.json")
.requestsAndResponses()
.toMap()
```

**Export to JSON**

``` javascript
var collectionJson = Swagger2ToJson
.convertSwagger()
.fromUrl("http://petstore.swagger.io/v2/swagger.json")
.requestsAndResponses()
.toJson()
```

**Export to JSON File**

``` javascript
Swagger2ToJson
.convertSwagger()
.fromFile("swagger.json")
.responses()
.toJsonFile("responses.json")
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger2-to-json",
"version": "1.1.2",
"version": "1.1.3",
"description": "Use Swagger v2 JSON Collections to generate sample JSON for requests and responses",
"main": "swagger2-to-json.js",
"repository": {
Expand Down
21 changes: 12 additions & 9 deletions swagger2-to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ const toJson = (obj, opts) =>
/* module function chain */
function convertSwagger (swaggerSpec, convertSwaggerOptions) {
var objGenerator = Swagger2Object.generateObjects().for();
var requestsAndResponsesJson = () => toJson(objGenerator.specSchemas(swaggerSpec, convertSwaggerOptions));
var requestsJson = () => toJson(objGenerator.specRequests(swaggerSpec, convertSwaggerOptions));
var responsesJson = () => toJson(objGenerator.specResponses(swaggerSpec, convertSwaggerOptions));
var requestsAndResponses = () => objGenerator.specSchemas(swaggerSpec, convertSwaggerOptions);
var requests = () => objGenerator.specRequests(swaggerSpec, convertSwaggerOptions);
var responses = () => objGenerator.specResponses(swaggerSpec, convertSwaggerOptions);

return {
requestsAndResponses: () => ({
toJson: (options) => requestsAndResponsesJson(),
toJsonFile: (filename, options) => fs.writeFileSync(filename, requestsAndResponsesJson())
toMap: () => requestsAndResponses(),
toJson: () => toJson(requestsAndResponses()),
toJsonFile: filename => fs.writeFileSync(filename, toJson(requestsAndResponses()))
}),
requests: () => ({
toJson: (options) => requestsJson(),
toJsonFile: (filename, options) => fs.writeFileSync(filename, requestsJson())
toMap: () => requests(),
toJson: () => toJson(requests()),
toJsonFile: filename => fs.writeFileSync(filename, toJson(requests()))
}),
responses: () => ({
toJson: (options) => responsesJson(),
toJsonFile: (filename, options) => fs.writeFileSync(filename, responsesJson())
toMap: () => responses(),
toJson: () => toJson(responses()),
toJsonFile: filename => fs.writeFileSync(filename, toJson(responses()))
}),
}
}
Expand Down
76 changes: 38 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
"@types/node" "*"

"@types/node@*":
version "12.0.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.2.tgz#3452a24edf9fea138b48fad4a0a028a683da1e40"
integrity sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA==
version "13.1.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.1.8.tgz#1d590429fe8187a02707720ecf38a6fe46ce294b"
integrity sha512-6XzyyNM9EKQW4HKuzbo/CkOIjn/evtCmsU+MUM1xDfJ+3/rNjBttM1NgN7AOQvN6tP1Sl1D1PIKMreTArnxM9A==

"@types/node@^10.0.3":
version "10.14.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.7.tgz#1854f0a9aa8d2cd6818d607b3d091346c6730362"
integrity sha512-on4MmIDgHXiuJDELPk1NFaKVUxxCFr37tm8E9yN6rAiF5Pzp/9bBfBHkoexqRiY+hk/Z04EJU9kKEb59YqJ82A==
version "10.17.13"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.13.tgz#ccebcdb990bd6139cd16e84c39dc2fb1023ca90c"
integrity sha512-pMCcqU2zT4TjqYFrWtYHKal7Sl30Ims6ulZ4UFXxI4xbtQqK/qqKwkDoBFCfooRqqmRu9vY3xaJRwxSh673aYg==

"@types/node@^8.0.0":
version "8.10.48"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.48.tgz#e385073561643a9ba6199a1985ffc03530f90781"
integrity sha512-c35YEBTkL4rzXY2ucpSKy+UYHjUBIIkuJbWYbsGIrKLEWU5dgJMmLkkIb3qeC3O3Tpb1ZQCwecscvJTDjDjkRw==
version "8.10.59"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.59.tgz#9e34261f30183f9777017a13d185dfac6b899e04"
integrity sha512-8RkBivJrDCyPpBXhVZcjh7cQxVBSmRk9QM7hOketZzp6Tg79c0N8kkpAIito9bnJ3HCVCHVYz+KHTEbfQNfeVQ==

"@types/qs@^6.2.31":
version "6.5.3"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.5.3.tgz#1c3b71b091eaeaf5924538006b7f70603ce63d38"
integrity sha512-Jugo5V/1bS0fRhy2z8+cUAHEyWOATaz4rbyLVvcFs7+dXp5HfwpEwzF1Q11bB10ApUqHf+yTauxI0UXQDwGrbA==
version "6.9.0"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.0.tgz#2a5fa918786d07d3725726f7f650527e1cfeaffd"
integrity sha512-c4zji5CjWv1tJxIZkz1oUtGcdOlsH3aza28Nqmm+uNDWBRHoMsjooBEN4czZp1V3iXPihE/VRUOBqg+4Xq0W4g==

asap@~2.0.6:
version "2.0.6"
Expand Down Expand Up @@ -84,9 +84,9 @@ delayed-stream@~1.0.0:
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

form-data@^2.2.0:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
integrity sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==
version "2.5.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.5.1.tgz#f2cbec57b5e59e23716e128fe44d4e5dd23895f4"
integrity sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.6"
Expand Down Expand Up @@ -115,36 +115,36 @@ http-response-object@^3.0.1:
"@types/node" "^10.0.3"

inherits@^2.0.3, inherits@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=
version "2.0.4"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=

mime-db@1.40.0:
version "1.40.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.40.0.tgz#a65057e998db090f732a68f6c276d387d4126c32"
integrity sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==
mime-db@1.43.0:
version "1.43.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.43.0.tgz#0a12e0502650e473d735535050e7c8f4eb4fae58"
integrity sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ==

mime-types@^2.1.12:
version "2.1.24"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.24.tgz#b6f8d0b3e951efb77dedeca194cff6d16f676f81"
integrity sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==
version "2.1.26"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.26.tgz#9c921fc09b7e149a65dfdc0da4d20997200b0a06"
integrity sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==
dependencies:
mime-db "1.40.0"
mime-db "1.43.0"

parse-cache-control@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parse-cache-control/-/parse-cache-control-1.0.1.tgz#8eeab3e54fa56920fe16ba38f77fa21aacc2d74e"
integrity sha1-juqz5U+laSD+Fro493+iGqzC104=

process-nextick-args@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"
integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==
version "2.0.1"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

promise@^8.0.0:
version "8.0.3"
Expand All @@ -154,14 +154,14 @@ promise@^8.0.0:
asap "~2.0.6"

qs@^6.4.0:
version "6.7.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==
version "6.9.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.1.tgz#20082c65cb78223635ab1a9eaca8875a29bf8ec9"
integrity sha512-Cxm7/SS/y/Z3MHWSxXb8lIFqgqBowP5JMlTUFyJN88y0SGQhVmZnqFK/PeuMX9LzUyWsqqhNxIyg0jlzq946yA==

readable-stream@^2.2.2:
version "2.3.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf"
integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==
version "2.3.7"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57"
integrity sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.3"
Expand Down Expand Up @@ -198,9 +198,9 @@ sync-request@^6.1.0:
then-request "^6.0.0"

sync-rpc@^1.2.1:
version "1.3.4"
resolved "https://registry.yarnpkg.com/sync-rpc/-/sync-rpc-1.3.4.tgz#24bcbdb2ffcb98f23690c15b304660085cdd206c"
integrity sha512-Iug+t1ICVFenUcTnDu8WXFnT+k8IVoLKGh8VA3eXUtl2Rt9SjKX3YEv33OenABqpTPL9QEaHv1+CNn2LK8vMow==
version "1.3.6"
resolved "https://registry.yarnpkg.com/sync-rpc/-/sync-rpc-1.3.6.tgz#b2e8b2550a12ccbc71df8644810529deb68665a7"
integrity sha512-J8jTXuZzRlvU7HemDgHi3pGnh/rkoqR/OZSjhTyyZrEkkYQbk7Z33AXp37mkPfPpfdOuj7Ex3H/TJM1z48uPQw==
dependencies:
get-port "^3.1.0"

Expand Down

0 comments on commit f310d49

Please sign in to comment.