Skip to content

Commit cec0681

Browse files
Merge pull request #20 from roydekleijn/swagger
added Swagger to the api
2 parents 914be03 + 004e0af commit cec0681

File tree

10 files changed

+910
-20
lines changed

10 files changed

+910
-20
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ yarn dev
3737
```
3838

3939
visit `http://localhost:3000/`
40+
41+
### Open Swagger UI
42+
43+
visit `http://localhost:3000/swagger`

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"next": "10.0.1",
2323
"next-compose-plugins": "^2.2.1",
2424
"next-optimized-images": "^2.6.2",
25+
"next-swagger-doc": "^0.1.9",
2526
"postcss-import": "^13.0.0",
2627
"postcss-preset-env": "^6.7.0",
2728
"prop-types": "15.7.2",
@@ -34,6 +35,7 @@
3435
"redux-thunk": "^2.3.0",
3536
"sass": "^1.29.0",
3637
"store2": "^2.12.0",
38+
"swagger-ui-react": "^3.52.3",
3739
"tailwindcss": "^1.9.6",
3840
"webpack": "^4.0.0"
3941
}

pages/api/checkout.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import { isValidUser } from '../../src/constants/users';
22

3+
/**
4+
* @swagger
5+
* /api/checkout:
6+
* get:
7+
* description: Validate if the logged in user is allowed to checkout products.
8+
* parameters:
9+
* - in: query
10+
* name: userName
11+
* required: false
12+
* description: Name of the signed in user.
13+
* responses:
14+
* 200:
15+
* description: userName is valid.
16+
* 422:
17+
* description: userName is invalid.
18+
*/
319
export default (req, res) => {
420
const userName = req.body['userName'];
521
if (isValidUser(userName)) {
622
res.statusCode = 200;
723
res.json({});
8-
} else {
24+
} else {
925
res.statusCode = 422;
1026
res.json({ });
1127
}

pages/api/doc.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { withSwagger } from 'next-swagger-doc';
2+
3+
const swaggerHandler = withSwagger({
4+
openApiVersion: '3.0.0',
5+
title: 'BstackDemo API',
6+
version: '1.0.0',
7+
apiFolder: 'pages/api',
8+
});
9+
export default swaggerHandler();

pages/api/offers.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import offersData from '../../src/constants/offers.json';
22

3+
/**
4+
* @swagger
5+
* /api/offers:
6+
* get:
7+
* description: Returns all available offers on products.
8+
* parameters:
9+
* - in: query
10+
* name: userName
11+
* required: false
12+
* description: Name of the signed in user.
13+
* - in: query
14+
* name: latitude
15+
* required: false
16+
* description: latitude of the location.
17+
* - in: query
18+
* name: longitude
19+
* required: false
20+
* description: longitude of the location.
21+
* responses:
22+
* 200:
23+
* description: List of all offers for a specific location.
24+
* 404:
25+
* description: City name could not be determined.
26+
*/
327
export default (req, res) => {
428
const userName = req.query['userName'];
529
const latitude = parseInt(req.query['latitude'], 10);
@@ -35,7 +59,7 @@ export default (req, res) => {
3559
const hasMatchingLon = longitude >= cityCoords.lon - 3 && longitude <= cityCoords.lon + 3;
3660
if(hasMatchingLat && hasMatchingLon) {
3761
city = cityName;
38-
}
62+
}
3963
});
4064

4165
if (city) {

pages/api/orders.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import ordersData from '../../src/constants/orders.json';
22

3+
/**
4+
* @swagger
5+
* /api/orders:
6+
* get:
7+
* description: Returns all orders associated with the logged in user.
8+
* parameters:
9+
* - in: query
10+
* name: userName
11+
* required: false
12+
* description: Name of the signed in user. If the user name is set to existing_orders_user, the response returns all orders.
13+
* responses:
14+
* 200:
15+
* description: List of all orders.
16+
* 404:
17+
* description: No orders found
18+
*/
319
export default (req, res) => {
420
const userName = req.query['userName'];
521

pages/api/products.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import productsData from '../../src/constants/products.json';
22

3+
/**
4+
* @swagger
5+
* /api/products:
6+
* get:
7+
* description: Returns all products
8+
* parameters:
9+
* - in: query
10+
* name: userName
11+
* required: false
12+
* description: Name of the signed in user. If the user name is set to fav_user , the response returns the first 5 products marked as favorite.
13+
* responses:
14+
* 200:
15+
* description: List of products
16+
*/
317
export default (req, res) => {
418
const userName = req.query['userName'];
519

pages/api/signin.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import { isValidUser, isValidPassword, isLockedUser } from '../../src/constants/users';
22

3+
/**
4+
* @swagger
5+
* /api/signin:
6+
* post:
7+
* description: Authenticate and sign in the user.
8+
* requestBody:
9+
* description: Sign in credentials that you want to authenticate.
10+
* required: true
11+
* content:
12+
* application/json:
13+
* schema:
14+
* type: object
15+
* properties:
16+
* userName:
17+
* type: string
18+
* password:
19+
* type: string
20+
* responses:
21+
* 200:
22+
* description: Successful signin
23+
* 422:
24+
* description: User credentials cannot be processed. Check if the credentials are incorrect or if your account is locked.
25+
*/
326
export default (req, res) => {
427
const userName = req.body['userName'];
528
const password = req.body['password'];
@@ -19,7 +42,7 @@ export default (req, res) => {
1942
if (isLockedUser(userName)) {
2043
errorMessage = 'Your account has been locked.'
2144
}
22-
45+
2346
res.statusCode = 422;
2447
res.json({ errorMessage });
2548
}

pages/swagger/index.jsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Head from 'next/head';
2+
3+
import SwaggerUI from 'swagger-ui-react';
4+
import 'swagger-ui-react/swagger-ui.css';
5+
6+
const Swagger = () => {
7+
8+
return (
9+
<div>
10+
<Head>
11+
<title>Bstack Demo API</title>
12+
<meta name="description" content="Generated by create next app" />
13+
<link rel="icon" href="/favicon.ico" />
14+
</Head>
15+
<SwaggerUI url="/api/doc" />
16+
</div>
17+
);
18+
};
19+
20+
export default Swagger;

0 commit comments

Comments
 (0)