-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from edwinhern/feat/SwaggerUI
Feat/swagger UI
- Loading branch information
Showing
12 changed files
with
213 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { OpenApiGeneratorV3, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi'; | ||
|
||
import { healthCheckRegistry } from '@modules/healthCheck/healthCheckRegistery'; | ||
import { userRegistry } from '@modules/user/userRegistery'; | ||
|
||
export function generateOpenAPIDocument() { | ||
const registry = new OpenAPIRegistry([healthCheckRegistry, userRegistry]); | ||
const generator = new OpenApiGeneratorV3(registry.definitions); | ||
|
||
return generator.generateDocument({ | ||
openapi: '3.0.0', | ||
info: { | ||
version: '1.0.0', | ||
title: 'Swagger API', | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { z } from 'zod'; | ||
|
||
export const commonValidations = { | ||
id: z.string().regex(/^\d+$/, 'ID must be a number').transform(Number), | ||
id: z | ||
.string() | ||
.refine((data) => !isNaN(Number(data)), 'ID must be a numeric value') | ||
.transform(Number) | ||
.refine((num) => num > 0, 'ID must be a positive number'), | ||
// ... other common validations | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { z } from 'zod'; | ||
|
||
import { ServiceResponseSchema } from '@common/models/serviceResponse'; | ||
|
||
const HealthCheckResponseSchema = ServiceResponseSchema(z.null()); | ||
const healthCheckRegistry = new OpenAPIRegistry(); | ||
|
||
// Define and register health check API Route | ||
healthCheckRegistry.registerPath({ | ||
method: 'get', | ||
path: '/health-check', | ||
tags: ['Health Check'], | ||
description: 'Health check endpoint', | ||
summary: 'Health Check', | ||
operationId: 'getHealthCheck', | ||
responses: { | ||
[StatusCodes.OK]: { | ||
description: 'Service is healthy', | ||
content: { | ||
'application/json': { | ||
schema: HealthCheckResponseSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export { healthCheckRegistry }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'; | ||
import { z } from 'zod'; | ||
|
||
import { commonValidations } from '@common/utils/commonValidation'; | ||
|
||
extendZodWithOpenApi(z); | ||
|
||
export type User = z.infer<typeof UserSchema>; | ||
export const UserSchema = z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
email: z.string().email(), | ||
age: z.number(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
}); | ||
export const UserSchema = z | ||
.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
email: z.string().email(), | ||
age: z.number(), | ||
createdAt: z.date(), | ||
updatedAt: z.date(), | ||
}) | ||
.openapi('User'); | ||
|
||
// Schema for 'GET users/:id' endpoint | ||
export const GetUserSchema = z.object({ | ||
params: z.object({ id: commonValidations.id }), | ||
}); | ||
export const GetUserSchema = z | ||
.object({ | ||
params: z.object({ id: commonValidations.id }), | ||
}) | ||
.openapi('GetUser'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { z } from 'zod'; | ||
|
||
import { ServiceResponseSchema } from '@common/models/serviceResponse'; | ||
import { GetUserSchema, UserSchema } from '@modules/user/userModel'; | ||
|
||
const userRegistry = new OpenAPIRegistry(); | ||
|
||
// Register schemas | ||
userRegistry.register('User', UserSchema); | ||
userRegistry.register('GetUser', GetUserSchema); | ||
|
||
// Define and register user API Routes | ||
userRegistry.registerPath({ | ||
method: 'get', | ||
path: '/users', | ||
tags: ['User'], | ||
description: 'Retrieve all users', | ||
summary: 'Get Users', | ||
responses: { | ||
[StatusCodes.OK]: { | ||
description: 'List of users', | ||
content: { | ||
'application/json': { | ||
schema: ServiceResponseSchema(z.array(UserSchema)), | ||
}, | ||
}, | ||
}, | ||
[StatusCodes.NOT_FOUND]: { | ||
description: 'User not found', | ||
}, | ||
[StatusCodes.INTERNAL_SERVER_ERROR]: { | ||
description: 'Internal server error', | ||
}, | ||
}, | ||
}); | ||
|
||
userRegistry.registerPath({ | ||
method: 'get', | ||
path: '/users/{id}', | ||
tags: ['User'], | ||
description: 'Retrieve a single user by ID', | ||
summary: 'Get User by ID', | ||
request: { | ||
params: GetUserSchema.shape.params, | ||
}, | ||
responses: { | ||
[StatusCodes.OK]: { | ||
description: 'User data', | ||
content: { | ||
'application/json': { | ||
schema: ServiceResponseSchema(UserSchema), | ||
}, | ||
}, | ||
}, | ||
[StatusCodes.NOT_FOUND]: { | ||
description: 'User not found', | ||
}, | ||
[StatusCodes.INTERNAL_SERVER_ERROR]: { | ||
description: 'Internal server error', | ||
}, | ||
[StatusCodes.BAD_REQUEST]: { | ||
description: 'Bad request', | ||
}, | ||
}, | ||
}); | ||
|
||
export { userRegistry }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters