Skip to content

Commit 660ed60

Browse files
committed
PIX-31: add openapi description of the api, add swagger-ui-express to see the definition of the api on the endpoint /api-docs
1 parent 6060f34 commit 660ed60

5 files changed

Lines changed: 105 additions & 4 deletions

File tree

server/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
"express": "^4.21.2",
1616
"jsonwebtoken": "^9.0.3",
1717
"mongoose": "^8.9.5",
18-
"socket.io": "^4.8.1"
18+
"socket.io": "^4.8.1",
19+
"swagger-ui-express": "^5.0.1",
20+
"yamljs": "^0.3.0"
1921
},
2022
"devDependencies": {
2123
"@types/cors": "^2.8.17",
2224
"@types/express": "^4.17.21",
2325
"@types/jsonwebtoken": "^9.0.7",
2426
"@types/node": "^20.10.6",
27+
"@types/swagger-ui-express": "^4.1.8",
28+
"@types/yamljs": "^0.2.34",
2529
"nodemon": "^3.1.9",
2630
"ts-node": "^10.9.2",
2731
"tsx": "^4.7.0",
@@ -33,4 +37,4 @@
3337
"keywords": [],
3438
"author": "",
3539
"license": "ISC"
36-
}
40+
}

server/pixie-api.yaml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Pixie API
4+
version: 1.0.0
5+
description: API for Pixie application, providing user authentication and management.
6+
servers:
7+
- url: http://localhost:3000/api
8+
description: Local server
9+
paths:
10+
/login:
11+
post:
12+
summary: User login
13+
description: Authenticates a user or registers a new one if not found.
14+
tags:
15+
- Auth
16+
requestBody:
17+
required: true
18+
content:
19+
application/json:
20+
schema:
21+
type: object
22+
properties:
23+
username:
24+
type: string
25+
example: "playerOne"
26+
required:
27+
- username
28+
responses:
29+
'200':
30+
description: Successful login
31+
content:
32+
application/json:
33+
schema:
34+
$ref: '#/components/schemas/LoginResponse'
35+
'400':
36+
description: Bad Request (Missing username)
37+
/users:
38+
get:
39+
summary: Get all users
40+
description: Retrieve a list of all users. Requires authentication.
41+
tags:
42+
- Users
43+
security:
44+
- bearerAuth: []
45+
responses:
46+
'200':
47+
description: List of users
48+
content:
49+
application/json:
50+
schema:
51+
type: array
52+
items:
53+
$ref: '#/components/schemas/UserSummary'
54+
'401':
55+
description: Unauthorized (Missing token)
56+
'403':
57+
description: Forbidden (Invalid token)
58+
components:
59+
securitySchemes:
60+
bearerAuth:
61+
type: http
62+
scheme: bearer
63+
bearerFormat: JWT
64+
schemas:
65+
UserSummary:
66+
type: object
67+
properties:
68+
username:
69+
type: string
70+
example: "playerOne"
71+
isAdmin:
72+
type: boolean
73+
example: false
74+
_id:
75+
type: string
76+
example: "60d0fe4f5311236168a109ca"
77+
LoginResponse:
78+
type: object
79+
properties:
80+
username:
81+
type: string
82+
example: "playerOne"
83+
id:
84+
type: string
85+
example: "60d0fe4f5311236168a109ca"
86+
token:
87+
type: string
88+
description: JWT token for authentication
89+
isNewUser:
90+
type: boolean
91+
description: True if a new account was created

server/src/db/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import mongoose from "mongoose"
2-
import { seedUsers } from "./seed"
2+
import { seedUsers } from "./seed.js"
33

44
const MONGO_URI = process.env.MONGO_URI || "mongodb://localhost:27017/pixie"
55

server/src/db/seed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { User } from "../models/User"
1+
import { User } from "../models/User.js"
22

33
export const seedUsers = async (): Promise<void> => {
44
const users = [

server/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ import { errorHandler } from "./middlewares/errorMiddleware.js";
3333
// API Routes (REST)
3434
app.use("/api", router);
3535

36+
// Swagger UI
37+
import swaggerUi from "swagger-ui-express";
38+
import YAML from "yamljs";
39+
const swaggerDocument = YAML.load("./pixie-api.yaml");
40+
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
41+
3642
// Global Error Handler
3743
app.use(errorHandler);
3844

0 commit comments

Comments
 (0)