From af44a703464ce58ee6c57978c6500ad817991ae3 Mon Sep 17 00:00:00 2001 From: Jagdeep Singh Date: Thu, 23 May 2019 21:26:50 -0700 Subject: [PATCH 1/5] swagger docs --- README.md | 5 +- docs/config/swagger.json | 156 +++++++++++++++++++++++++++++++++++++++ src/api/v1.js | 5 ++ 3 files changed, 163 insertions(+), 3 deletions(-) create mode 100755 docs/config/swagger.json diff --git a/README.md b/README.md index 51bedd6..809fd47 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,14 @@ ### Author: Jagdeep Singh ### Links and Resources -* [submission PR](https://github.com/401-advanced-javascript-js/lab-09-api-server/pull/1) +* [submission PR](https://github.com/401-advanced-javascript-js/lab-09-api-server/pull/2) * [travis](https://www.travis-ci.com/401-advanced-javascript-js/lab-09-api-server) #### Documentation * [api docs](http://xyz.com) (API servers) -* [jsdoc](http://xyz.com) (Server assignments) -* [styleguide](http://xyz.com) (React assignments) +* [jsdoc](http://xyz.com) (https://401-advanced-javascript-js.github.io/lab-09-api-server/) ### Modules #### `modulename.js` diff --git a/docs/config/swagger.json b/docs/config/swagger.json new file mode 100755 index 0000000..2e95e42 --- /dev/null +++ b/docs/config/swagger.json @@ -0,0 +1,156 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "API Server", + "description": "The worlds funnest little API", + "version": "1.0.0" + }, + "servers": [ + { + "url": "http://localhost:3000/" + } + ], + "paths": { + "/categories": { + "get": { + "description": "Get all categories", + "responses": { + "200": { + "description": "Successfully got list of categories.", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/CategoryList" + } + } + } + } + } + }, + "post": { + "description": "Add a new category", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewCategory" + } + } + } + }, + "responses": { + "200": { + "description": "Successfully added category and returned the added category", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/Category" + } + } + } + } + } + } + }, + "/categories/{id}": { + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer" + } + } + ], + "get": { + "description": "Get One Category", + "responses": { + "200": { + "description": "Returned one category", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/Category" + } + } + } + } + } + }, + "delete": { + "description": "Delete One Category", + "responses": { + "200": { + "description": "Deleted one category", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/Delete" + } + } + } + } + } + }, + "put": { + "description": "Update a category", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Category" + } + } + } + }, + "responses": { + "200": { + "description": "Returned the updated category", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/Category" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "CategoryList": { + "type": "array", + "example": [ + { + "id": 1, + "Category 1": null + }, + { + "id": 2, + "Category 2": null + } + ] + }, + "NewCategory": { + "type": "object", + "example": { + "name": "Category Name" + } + }, + "Category": { + "type": "object", + "example": { + "id": 2, + "name": "Category Name" + } + }, + "Delete": { + "type": "object", + "example": {} + } + } + } +} \ No newline at end of file diff --git a/src/api/v1.js b/src/api/v1.js index 51c9e3a..c30ba63 100644 --- a/src/api/v1.js +++ b/src/api/v1.js @@ -10,6 +10,9 @@ const cwd = process.cwd(); const express = require('express'); +const swagger = require('swagger-ui-express'); +const swaggerDocs = require('../../docs/config/swagger.json'); + // Figures out which model to use const modelFinder = require(`${cwd}/src/middleware/model-finder.js`); @@ -20,6 +23,8 @@ const router = express.Router(); // Evaluate the model, dynamically router.param('model', modelFinder); +router.use('/api/v1/api-docs', swagger.serve, swagger.setup(swaggerDocs)); + // API Routes router.get('/api/v1/:model', handleGetAll); router.post('/api/v1/:model', handlePost); From 4be9c6756c6bd9c91ee636f6fff5535952e040db Mon Sep 17 00:00:00 2001 From: Jagdeep Singh Date: Thu, 23 May 2019 21:44:38 -0700 Subject: [PATCH 2/5] minor change --- docs/config/swagger.json | 2 +- index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config/swagger.json b/docs/config/swagger.json index 2e95e42..84a02f3 100755 --- a/docs/config/swagger.json +++ b/docs/config/swagger.json @@ -2,7 +2,7 @@ "openapi": "3.0.1", "info": { "title": "API Server", - "description": "The worlds funnest little API", + "description": "A Simple API", "version": "1.0.0" }, "servers": [ diff --git a/index.js b/index.js index 44ae550..621186b 100644 --- a/index.js +++ b/index.js @@ -10,4 +10,4 @@ const mongooseOptions = { mongoose.connect(process.env.MONGODB_URI, mongooseOptions); -require('./src/app.js').start(process.env.PORT); +require('./src/app.js').start(process.env.PORT || 3000); From 976f91f62223d867a58cc5a1c7399392731e79ef Mon Sep 17 00:00:00 2001 From: Jagdeep Singh Date: Thu, 23 May 2019 21:54:50 -0700 Subject: [PATCH 3/5] updated readme --- README.md | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 809fd47..a664d7d 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,8 @@ * [front-end](http://xyz.com) (when applicable) --> #### Documentation -* [api docs](http://xyz.com) (API servers) -* [jsdoc](http://xyz.com) (https://401-advanced-javascript-js.github.io/lab-09-api-server/) - -### Modules -#### `modulename.js` -##### Exported Values and Methods - -###### `foo(thing) -> string` -Usage Notes or examples - -###### `bar(array) -> array` -Usage Notes or examples +* [api docs](http://api-server-js.herokuapp.com/api/v1/api-docs/) +* [jsdoc](https://401-advanced-javascript-js.github.io/lab-09-api-server/) ### Setup #### `.env` requirements @@ -32,18 +22,30 @@ Usage Notes or examples #### Running the app * `npm start` -* Endpoint: `/foo/bar/` - * Returns a JSON object with abc in it. -* Endpoint: `/bing/zing/` - * Returns a JSON object with xyz in it. + + Endpoint: `/api/v1/categories` + * returns all categories + + Endpoint: `/api/v1/categories/:id` + * returns category with passed id + + Endpoint: `/api/v1/players` + * returns all players + + Endpoint: `/api/v1/players/:id` + * returns player with passed id + + Endpoint: `/api/v1/teams` + * returns all teams + + Endpoint: `/api/v1/teams/:id` + * returns team with passed id + #### Tests * How do you run tests? `npm test` -* What assertions were made? -* What assertions need to be / should be made? - #### UML ![UML of Data Flow](./assets/uml.jpeg) \ No newline at end of file From d228a07fd5ddc33c78bfee7fcfd3fb12cd8b60a6 Mon Sep 17 00:00:00 2001 From: Jagdeep Singh Date: Thu, 23 May 2019 21:57:38 -0700 Subject: [PATCH 4/5] updated url for api docs --- README.md | 4 ++-- docs/config/swagger.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a664d7d..29b0cdd 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ ### Links and Resources * [submission PR](https://github.com/401-advanced-javascript-js/lab-09-api-server/pull/2) * [travis](https://www.travis-ci.com/401-advanced-javascript-js/lab-09-api-server) - +* [back-end](http://api-server-js.herokuapp.com/api/v1/) + #### Documentation * [api docs](http://api-server-js.herokuapp.com/api/v1/api-docs/) diff --git a/docs/config/swagger.json b/docs/config/swagger.json index 84a02f3..d19f268 100755 --- a/docs/config/swagger.json +++ b/docs/config/swagger.json @@ -7,7 +7,7 @@ }, "servers": [ { - "url": "http://localhost:3000/" + "url": "http://api-server-js.herokuapp.com/api/v1/" } ], "paths": { From 54f200be9ab4abdbb64100d53cbc7c53614ef7ab Mon Sep 17 00:00:00 2001 From: Jagdeep Singh Date: Sat, 25 May 2019 18:14:05 -0700 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 29b0cdd..3c3f4b7 100644 --- a/README.md +++ b/README.md @@ -48,4 +48,4 @@ `npm test` #### UML -![UML of Data Flow](./assets/uml.jpeg) \ No newline at end of file +![UML of Data Flow](./assets/UML.jpeg)