Skip to content

Commit

Permalink
Merge pull request #124 from mnfst/feature/api-doc
Browse files Browse the repository at this point in the history
Display API Documentation (Swagger UI)
  • Loading branch information
brunobuddy authored Jul 2, 2024
2 parents fd24568 + 3598d57 commit 9ad64d8
Show file tree
Hide file tree
Showing 22 changed files with 2,567 additions and 15 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"metas",
"mnfst",
"nestjs",
"openapi",
"typeorm",
"uniqid"
],
Expand Down
6 changes: 6 additions & 0 deletions packages/core/manifest/e2e/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { INestApplication } from '@nestjs/common'
import supertest from 'supertest'
import { load } from 'js-yaml'
import fs from 'fs'
import { SwaggerModule } from '@nestjs/swagger'
import { OpenApiService } from '../src/open-api/services/open-api.service'

let app: INestApplication

Expand Down Expand Up @@ -35,6 +37,10 @@ beforeAll(async () => {
// Store request object in global scope to use in tests.
global.request = supertest(app.getHttpServer())

// Set the SwaggerModule to serve the OpenAPI doc.
const openApiService = app.get(OpenApiService)
SwaggerModule.setup('api', app, openApiService.generateOpenApiObject())

await app.init()
})

Expand Down
8 changes: 8 additions & 0 deletions packages/core/manifest/e2e/tests/open-api.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('Open API (e2e)', () => {
it('GET /api', async () => {
const response = await global.request.get('/api')

expect(response.status).toBe(200)
expect(response.text).toContain('<html lang="en">')
})
})
62 changes: 62 additions & 0 deletions packages/core/manifest/package-lock.json

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

1 change: 1 addition & 0 deletions packages/core/manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.3",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/swagger": "^7.3.1",
"@nestjs/typeorm": "^10.0.2",
"ajv": "^8.12.0",
"better-sqlite3": "^9.6.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/core/manifest/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ManifestModule } from './manifest/manifest.module'
import { SeedModule } from './seed/seed.module'
import { HealthModule } from './health/health.module'
import { BetterSqlite3ConnectionOptions } from 'typeorm/driver/better-sqlite3/BetterSqlite3ConnectionOptions'
import { OpenApiModule } from './open-api/open-api.module'

@Module({
imports: [
Expand Down Expand Up @@ -44,7 +45,8 @@ import { BetterSqlite3ConnectionOptions } from 'typeorm/driver/better-sqlite3/Be
CrudModule,
AuthModule,
LoggerModule,
HealthModule
HealthModule,
OpenApiModule
]
})
export class AppModule {
Expand All @@ -57,8 +59,9 @@ export class AppModule {
private async init() {
const isSeed: boolean = process.argv[1].includes('seed')
const isTest: boolean = process.env.NODE_ENV === 'test'
const isProduction: boolean = process.env.NODE_ENV === 'production'

if (!isSeed && !isTest) {
if (!isSeed && !isTest && !isProduction) {
this.loggerService.initMessage()
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/manifest/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { AuthenticableEntity } from '@mnfst/types'
import { Request } from 'express'
import { AuthService } from './auth.service'
import { SignupAuthenticableEntityDto } from './dtos/signup-authenticable-entity.dto'
import { ApiTags } from '@nestjs/swagger'

@ApiTags('Auth')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsEmail, IsNotEmpty } from 'class-validator'

export class SignupAuthenticableEntityDto {
@ApiProperty({
description: 'The email of the user',
example: 'admin@manifest.build'
})
@IsNotEmpty()
@IsEmail()
public email: string

@ApiProperty({
description: 'The password of the user',
example: 'admin'
})
@IsNotEmpty()
public password: string
}
5 changes: 3 additions & 2 deletions packages/core/manifest/src/config/paths.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default (): { paths: { admin: string } } => {
export default (): { paths: { admin: string; database: string } } => {
return {
paths: {
admin: `${process.cwd()}/node_modules/@mnfst/admin/dist`
admin: `${process.cwd()}/node_modules/@mnfst/admin/dist`,
database: `${process.cwd()}/manifest/backend.yml`
}
}
}
4 changes: 0 additions & 4 deletions packages/core/manifest/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Manifest.
export const MANIFEST_FOLDER_NAME = 'manifest'
export const MANIFEST_FILE_NAME = 'backend.yml'

// Default values.
export const DEFAULT_PORT = 1111
export const DEFAULT_RESULTS_PER_PAGE = 20
Expand Down
12 changes: 10 additions & 2 deletions packages/core/manifest/src/logger/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ export class LoggerService {

console.log()

console.log(chalk.blue('Manifest backend successfully started! '))
console.log()

console.log(
chalk.blue(
'🎉 Manifest successfully started! See your admin panel: ',
'🖥️ Admin Panel: ',
chalk.underline.blue(`http://localhost:${port}`)
)
)

console.log(
chalk.blue(
'📚 API Doc: ',
chalk.underline.blue(`http://localhost:${port}/api`)
)
)
console.log()
}
}
Loading

0 comments on commit 9ad64d8

Please sign in to comment.