Skip to content

Commit

Permalink
refactor: ♻️ add api version
Browse files Browse the repository at this point in the history
Add the v1 folder to segment modules according to API version.
  • Loading branch information
1001Josias committed Mar 7, 2024
1 parent be43814 commit d5d70b2
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/api-docs/openAPIDocumentGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenApiGeneratorV3, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';

import { healthCheckRegistry } from '@/api/healthCheck/healthCheckRouter';
import { userRegistry } from '@/api/user/userRouter';
import { healthCheckRegistry } from '@/api/v1/healthCheck/healthCheckRouter';
import { userRegistry } from '@/api/v1/user/userRouter';

export function generateOpenAPIDocument() {
const registry = new OpenAPIRegistry([healthCheckRegistry, userRegistry]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { app } from '@/server';

describe('Health Check API endpoints', () => {
it('GET / - success', async () => {
const response = await request(app).get('/health-check');
const response = await request(app).get('/api/v1/health-check');
const result: ServiceResponse = response.body;

expect(response.statusCode).toEqual(StatusCodes.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const healthCheckRouter: Router = (() => {

healthCheckRegistry.registerPath({
method: 'get',
path: '/health-check',
path: '/api/v1/health-check',
tags: ['Health Check'],
responses: createApiResponse(z.null(), 'Success'),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { StatusCodes } from 'http-status-codes';
import request from 'supertest';

import { User } from '@/api/user/userModel';
import { users } from '@/api/user/userRepository';
import { User } from '@/api/v1/user/userModel';
import { users } from '@/api/v1/user/userRepository';
import { ServiceResponse } from '@/common/models/serviceResponse';
import { app } from '@/server';

describe('User API Endpoints', () => {
describe('GET /users', () => {
describe('GET /api/v1/users', () => {
it('should return a list of users', async () => {
// Act
const response = await request(app).get('/users');
const response = await request(app).get('/api/v1/users');
const responseBody: ServiceResponse<User[]> = response.body;

// Assert
Expand All @@ -22,14 +22,14 @@ describe('User API Endpoints', () => {
});
});

describe('GET /users/:id', () => {
describe('GET /api/v1/users/:id', () => {
it('should return a user for a valid ID', async () => {
// Arrange
const testId = 1;
const expectedUser = users.find((user) => user.id === testId) as User;

// Act
const response = await request(app).get(`/users/${testId}`);
const response = await request(app).get(`/api/v1/users/${testId}`);
const responseBody: ServiceResponse<User> = response.body;

// Assert
Expand All @@ -45,7 +45,7 @@ describe('User API Endpoints', () => {
const testId = Number.MAX_SAFE_INTEGER;

// Act
const response = await request(app).get(`/users/${testId}`);
const response = await request(app).get(`/api/v1/users/${testId}`);
const responseBody: ServiceResponse = response.body;

// Assert
Expand All @@ -58,7 +58,7 @@ describe('User API Endpoints', () => {
it('should return a bad request for invalid ID format', async () => {
// Act
const invalidInput = 'abc';
const response = await request(app).get(`/users/${invalidInput}`);
const response = await request(app).get(`/api/v1/users/${invalidInput}`);
const responseBody: ServiceResponse = response.body;

// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { StatusCodes } from 'http-status-codes';
import { Mock } from 'vitest';

import { User } from '@/api/user/userModel';
import { userRepository } from '@/api/user/userRepository';
import { userService } from '@/api/user/userService';
import { User } from '@/api/v1/user/userModel';
import { userRepository } from '@/api/v1/user/userRepository';
import { userService } from '@/api/v1/user/userService';

vi.mock('@/api/user/userRepository');
vi.mock('@/api/v1/user/userRepository');
vi.mock('@/server', () => ({
...vi.importActual('@/server'),
logger: {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from '@/api/user/userModel';
import { User } from '@/api/v1/user/userModel';

export const users: User[] = [
{ id: 1, name: 'Alice', email: 'alice@example.com', age: 42, createdAt: new Date(), updatedAt: new Date() },
Expand Down
8 changes: 4 additions & 4 deletions src/api/user/userRouter.ts → src/api/v1/user/userRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import express, { Request, Response, Router } from 'express';
import { z } from 'zod';

import { GetUserSchema, UserSchema } from '@/api/user/userModel';
import { userService } from '@/api/user/userService';
import { GetUserSchema, UserSchema } from '@/api/v1/user/userModel';
import { userService } from '@/api/v1/user/userService';
import { createApiResponse } from '@/api-docs/openAPIResponseBuilders';
import { handleServiceResponse, validateRequest } from '@/common/utils/httpHandlers';

Expand All @@ -16,7 +16,7 @@ export const userRouter: Router = (() => {

userRegistry.registerPath({
method: 'get',
path: '/users',
path: '/api/v1/users',
tags: ['User'],
responses: createApiResponse(z.array(UserSchema), 'Success'),
});
Expand All @@ -28,7 +28,7 @@ export const userRouter: Router = (() => {

userRegistry.registerPath({
method: 'get',
path: '/users/{id}',
path: '/api/v1/users/{id}',
tags: ['User'],
request: { params: GetUserSchema.shape.params },
responses: createApiResponse(UserSchema, 'Success'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StatusCodes } from 'http-status-codes';

import { User } from '@/api/user/userModel';
import { userRepository } from '@/api/user/userRepository';
import { User } from '@/api/v1/user/userModel';
import { userRepository } from '@/api/v1/user/userRepository';
import { ResponseStatus, ServiceResponse } from '@/common/models/serviceResponse';
import { logger } from '@/server';

Expand Down
8 changes: 4 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import express, { Express } from 'express';
import helmet from 'helmet';
import { pino } from 'pino';

import { healthCheckRouter } from '@/api/healthCheck/healthCheckRouter';
import { userRouter } from '@/api/user/userRouter';
import { healthCheckRouter } from '@/api/v1/healthCheck/healthCheckRouter';
import { userRouter } from '@/api/v1/user/userRouter';
import { openAPIRouter } from '@/api-docs/openAPIRouter';
import errorHandler from '@/common/middleware/errorHandler';
import rateLimiter from '@/common/middleware/rateLimiter';
Expand All @@ -26,8 +26,8 @@ app.use(rateLimiter);
app.use(requestLogger());

// Routes
app.use('/health-check', healthCheckRouter);
app.use('/users', userRouter);
app.use('/api/v1/health-check', healthCheckRouter);
app.use('/api/v1/users', userRouter);

// Swagger UI
app.use(openAPIRouter);
Expand Down

0 comments on commit d5d70b2

Please sign in to comment.