diff --git a/server/package.json b/server/package.json index ab4d2f8..9bc0f55 100644 --- a/server/package.json +++ b/server/package.json @@ -15,13 +15,17 @@ "express": "^4.21.2", "jsonwebtoken": "^9.0.3", "mongoose": "^8.9.5", - "socket.io": "^4.8.1" + "socket.io": "^4.8.1", + "swagger-ui-express": "^5.0.1", + "yamljs": "^0.3.0" }, "devDependencies": { "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/jsonwebtoken": "^9.0.7", "@types/node": "^20.10.6", + "@types/swagger-ui-express": "^4.1.8", + "@types/yamljs": "^0.2.34", "nodemon": "^3.1.9", "ts-node": "^10.9.2", "tsx": "^4.7.0", @@ -33,4 +37,4 @@ "keywords": [], "author": "", "license": "ISC" -} \ No newline at end of file +} diff --git a/server/pixie-api.yaml b/server/pixie-api.yaml new file mode 100644 index 0000000..fb09b69 --- /dev/null +++ b/server/pixie-api.yaml @@ -0,0 +1,91 @@ +openapi: 3.1.0 +info: + title: Pixie API + version: 1.0.0 + description: API for Pixie application, providing user authentication and management. +servers: + - url: http://localhost:3000/api + description: Local server +paths: + /login: + post: + summary: User login + description: Authenticates a user or registers a new one if not found. + tags: + - Auth + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + username: + type: string + example: "playerOne" + required: + - username + responses: + '200': + description: Successful login + content: + application/json: + schema: + $ref: '#/components/schemas/LoginResponse' + '400': + description: Bad Request (Missing username) + /users: + get: + summary: Get all users + description: Retrieve a list of all users. Requires authentication. + tags: + - Users + security: + - bearerAuth: [] + responses: + '200': + description: List of users + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/UserSummary' + '401': + description: Unauthorized (Missing token) + '403': + description: Forbidden (Invalid token) +components: + securitySchemes: + bearerAuth: + type: http + scheme: bearer + bearerFormat: JWT + schemas: + UserSummary: + type: object + properties: + username: + type: string + example: "playerOne" + isAdmin: + type: boolean + example: false + _id: + type: string + example: "60d0fe4f5311236168a109ca" + LoginResponse: + type: object + properties: + username: + type: string + example: "playerOne" + id: + type: string + example: "60d0fe4f5311236168a109ca" + token: + type: string + description: JWT token for authentication + isNewUser: + type: boolean + description: True if a new account was created diff --git a/server/src/db/connect.ts b/server/src/db/connect.ts index 4cbad30..f447615 100644 --- a/server/src/db/connect.ts +++ b/server/src/db/connect.ts @@ -1,5 +1,5 @@ import mongoose from "mongoose" -import { seedUsers } from "./seed" +import { seedUsers } from "./seed.js" const MONGO_URI = process.env.MONGO_URI || "mongodb://localhost:27017/pixie" diff --git a/server/src/db/seed.ts b/server/src/db/seed.ts index 22accd5..1c44110 100644 --- a/server/src/db/seed.ts +++ b/server/src/db/seed.ts @@ -1,4 +1,4 @@ -import { User } from "../models/User" +import { User } from "../models/User.js" export const seedUsers = async (): Promise => { const users = [ diff --git a/server/src/index.ts b/server/src/index.ts index bc04f12..33ec6f2 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -33,6 +33,12 @@ import { errorHandler } from "./middlewares/errorMiddleware.js"; // API Routes (REST) app.use("/api", router); +// Swagger UI +import swaggerUi from "swagger-ui-express"; +import YAML from "yamljs"; +const swaggerDocument = YAML.load("./pixie-api.yaml"); +app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); + // Global Error Handler app.use(errorHandler);