File tree 10 files changed +910
-20
lines changed
10 files changed +910
-20
lines changed Original file line number Diff line number Diff line change @@ -37,3 +37,7 @@ yarn dev
37
37
```
38
38
39
39
visit ` http://localhost:3000/ `
40
+
41
+ ### Open Swagger UI
42
+
43
+ visit ` http://localhost:3000/swagger `
Original file line number Diff line number Diff line change 22
22
"next" : " 10.0.1" ,
23
23
"next-compose-plugins" : " ^2.2.1" ,
24
24
"next-optimized-images" : " ^2.6.2" ,
25
+ "next-swagger-doc" : " ^0.1.9" ,
25
26
"postcss-import" : " ^13.0.0" ,
26
27
"postcss-preset-env" : " ^6.7.0" ,
27
28
"prop-types" : " 15.7.2" ,
34
35
"redux-thunk" : " ^2.3.0" ,
35
36
"sass" : " ^1.29.0" ,
36
37
"store2" : " ^2.12.0" ,
38
+ "swagger-ui-react" : " ^3.52.3" ,
37
39
"tailwindcss" : " ^1.9.6" ,
38
40
"webpack" : " ^4.0.0"
39
41
}
Original file line number Diff line number Diff line change 1
1
import { isValidUser } from '../../src/constants/users' ;
2
2
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
+ */
3
19
export default ( req , res ) => {
4
20
const userName = req . body [ 'userName' ] ;
5
21
if ( isValidUser ( userName ) ) {
6
22
res . statusCode = 200 ;
7
23
res . json ( { } ) ;
8
- } else {
24
+ } else {
9
25
res . statusCode = 422 ;
10
26
res . json ( { } ) ;
11
27
}
Original file line number Diff line number Diff line change
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 ( ) ;
Original file line number Diff line number Diff line change 1
1
import offersData from '../../src/constants/offers.json' ;
2
2
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
+ */
3
27
export default ( req , res ) => {
4
28
const userName = req . query [ 'userName' ] ;
5
29
const latitude = parseInt ( req . query [ 'latitude' ] , 10 ) ;
@@ -35,7 +59,7 @@ export default (req, res) => {
35
59
const hasMatchingLon = longitude >= cityCoords . lon - 3 && longitude <= cityCoords . lon + 3 ;
36
60
if ( hasMatchingLat && hasMatchingLon ) {
37
61
city = cityName ;
38
- }
62
+ }
39
63
} ) ;
40
64
41
65
if ( city ) {
Original file line number Diff line number Diff line change 1
1
import ordersData from '../../src/constants/orders.json' ;
2
2
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
+ */
3
19
export default ( req , res ) => {
4
20
const userName = req . query [ 'userName' ] ;
5
21
Original file line number Diff line number Diff line change 1
1
import productsData from '../../src/constants/products.json' ;
2
2
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
+ */
3
17
export default ( req , res ) => {
4
18
const userName = req . query [ 'userName' ] ;
5
19
Original file line number Diff line number Diff line change 1
1
import { isValidUser , isValidPassword , isLockedUser } from '../../src/constants/users' ;
2
2
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
+ */
3
26
export default ( req , res ) => {
4
27
const userName = req . body [ 'userName' ] ;
5
28
const password = req . body [ 'password' ] ;
@@ -19,7 +42,7 @@ export default (req, res) => {
19
42
if ( isLockedUser ( userName ) ) {
20
43
errorMessage = 'Your account has been locked.'
21
44
}
22
-
45
+
23
46
res . statusCode = 422 ;
24
47
res . json ( { errorMessage } ) ;
25
48
}
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments