Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying Swagger - Open-source code tool. #44

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"@fastify/jwt": "^8.0.0",
"@fastify/multipart": "^8.1.0",
"@fastify/static": "^6.12.0",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^2.1.0",
"@prisma/client": "5.8.1",
"@types/bcryptjs": "^2.4.6",
"@types/express": "^4.17.21",
Expand Down
43 changes: 43 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fastify from 'fastify'
import { userRoutes } from './controller/user/routes'
import { env } from './env'
import { ZodError } from 'zod'
Expand All @@ -8,8 +7,14 @@ import cors from '@fastify/cors'
import { logMiddleware } from './controller/middlewares/logMiddleware'

import { projectRoutes } from './controller/project/routes'
import fastify from 'fastify'

import { setupSwagger } from './swagger'

export const app = fastify()

setupSwagger(app)

app.register(cors, {
origin: [env.FRONTEND_URL],
})
Expand Down
9 changes: 6 additions & 3 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import fastifyStatic from '@fastify/static'
import { getProjectsByTags } from './getProjectsByTags'
import { editProject } from './editProjectById'
import { deleteProjectById } from './deleteProjectById'
import getProjectByUserIdSchema from './swagger/getProjectsByUserIdSwagger.json'
import getProjectByIdSchema from './swagger/getProjectByIDSwagger.json'
import createProjectSwagger from './swagger/createProjectSwagger.json'

export async function projectRoutes(app: FastifyInstance) {
app.register(FastifyMultipart, {
Expand All @@ -24,11 +27,11 @@ export async function projectRoutes(app: FastifyInstance) {
})

app.post('/projects/tags', getProjectsByTags)
app.get('/projects/:userId', getProjectsByUserId)
app.get('/project/:projectId', getProjectsById)
app.get('/projects/:userId', getProjectByUserIdSchema, getProjectsByUserId)
app.get('/project/:projectId', getProjectByIdSchema, getProjectsById)

app.post('/project/:projectId/photo', addImageProject)
app.post('/user/:userId/project', createProject)
app.post('/user/:userId/project', createProjectSwagger, createProject)

app.put('/project/:projectId/edit', editProject)
app.delete('/project/:projectId', deleteProjectById)
Expand Down
70 changes: 70 additions & 0 deletions src/controller/project/swagger/createProjectSwagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"schema": {
"tags": [
"Project"
],
"summary": "Create Project",
"description": "Create a project based on the user ID.",
"operationId": "createProject",
"body": {
"title": "object",
"properties": {
"title": {
"type": "string"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"link": {
"type": "string"
},
"description": {
"type": "string"
}
},
"required": [
"title",
"tags",
"link",
"description"
],
"type": "object"
},
"params": {
"type": "object",
"properties": {
"userId": {
"type": "string",
"format": "uuid"
}
},
"required": [
"userId"
],
"type": "object"
},
"response": {
"201": {
"description": "Project created successfully",
"type": "object",
"properties": {
"project": {
"type": "object"
}
}
},
"409": {
"description": "User was not Found !",
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
47 changes: 47 additions & 0 deletions src/controller/project/swagger/getProjectByIDSwagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"schema": {
"tags": [
"Project"
],
"summary": "Get project by ID",
"description": "Retrieves project information based on the provided project ID.",
"operationId": "getProjectById",
"parameters": [
{
"name": "projectId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid"
},
"description": "The ID of the project to retrieve."
}
],
"responses": {
"200": {
"description": "Project found and returned successfully.",
"content": {
"application/json": {
"example": {
"project": {
"id": "example_uuid",
"name": "Example Project"
}
}
}
}
},
"404": {
"description": "Project not found.",
"content": {
"application/json": {
"example": {
"error": "Project not found."
}
}
}
}
}
}
}
51 changes: 51 additions & 0 deletions src/controller/project/swagger/getProjectsByUserIdSwagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"schema": {
"tags": [
"Project"
],
"summary": "Get projects by user ID",
"operationId": "getProjectsByUserId",
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid"
},
"description": "The ID of the user to retrieve projects for."
}
],
"responses": {
"200": {
"description": "Projects found and returned successfully.",
"content": {
"application/json": {
"example": {
"projects": [
{
"id": "example_uuid_1",
"name": "Project 1"
},
{
"id": "example_uuid_2",
"name": "Project 2"
}
]
}
}
}
},
"404": {
"description": "User not Found !",
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
42 changes: 42 additions & 0 deletions src/controller/user/Swagger/getUserByIdSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"schema": {
"tags": [
"User"
],
"summary": "Get user by ID",
"description": "Retrieves user information based on the provided user ID.",
"operationId": "getUserById",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid"
},
"description": "The ID of the user to retrieve."
}
],
"responses": {
"200": {
"description": "User found and returned successfully.",
"type": "object",
"properties": {
"message": {
"description": "User found and returned successfully"
}
}
},
"404": {
"description": "User not found.",
"type": "object",
"properties": {
"message": {
"description": "User not found."
}
}
}
}
}
}
55 changes: 55 additions & 0 deletions src/controller/user/Swagger/registerUserSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"schema": {
"tags": [
"User"
],
"summary": "Register a new user",
"description": "Registers a new user with the provided information.",
"operationId": "registerUser",
"body": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"surname": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"password": {
"type": "string",
"minLength": 6
}
},
"required": [
"name",
"surname",
"email",
"password"
]
},
"response": {
"201": {
"description": "User successfully registered",
"type": "object",
"properties": {
"message": {
"description": "No body returned for response"
}
}
},
"409": {
"description": "E-mail already exists.",
"type": "object",
"properties": {
"message": {
"description": "E-mail already exists."
}
}
}
}
}
}
Loading
Loading