Skip to content

Commit

Permalink
Merge pull request #484 from andrechristikan/development
Browse files Browse the repository at this point in the history
update doc, and swagger options
  • Loading branch information
andrechristikan committed Oct 18, 2023
2 parents 8b67045 + 9e24252 commit 3733c19
Show file tree
Hide file tree
Showing 30 changed files with 172 additions and 142 deletions.
4 changes: 4 additions & 0 deletions docs/database/databae_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ For remove all data do
yarn rollback
```

# API Key Test
api key: `2ihKDneb9jQGgidAOqfO`
api key secret: `ZLCtDd2rh3TAyVhfAeo3JOPvWfAsTp0Oq6rHl69D`

# User Test

1. Super Admin
Expand Down
4 changes: 3 additions & 1 deletion nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": ["@nestjs/swagger"],
"plugins": [
"@nestjs/swagger"
],
"assets": [
{
"include": "languages/**/*",
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"rimraf": "^5.0.5",
"rotating-file-stream": "^3.1.1",
"rxjs": "^7.8.1",
"swagger-themes": "^1.2.30",
"ua-parser-js": "^1.0.36",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^4.7.1",
Expand Down
29 changes: 1 addition & 28 deletions src/app/controllers/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Controller, Get, VERSION_NEUTRAL } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ApiTags } from '@nestjs/swagger';
import { AppHelloApiKeyDoc, AppHelloDoc } from 'src/app/docs/app.doc';
import { AppHelloDoc } from 'src/app/docs/app.doc';
import { AppHelloSerialization } from 'src/app/serializations/app.hello.serialization';
import { ApiKeyPublicProtected } from 'src/common/api-key/decorators/api-key.decorator';
import { HelperDateService } from 'src/common/helper/services/helper.date.service';
import { RequestUserAgent } from 'src/common/request/decorators/request.decorator';
import { Response } from 'src/common/response/decorators/response.decorator';
Expand Down Expand Up @@ -47,30 +46,4 @@ export class AppController {
},
};
}

@AppHelloApiKeyDoc()
@Response('app.hello', { serialization: AppHelloSerialization })
@ApiKeyPublicProtected()
@Get('/hello/api-key')
async helloApiKey(
@RequestUserAgent() userAgent: IResult
): Promise<IResponse> {
const newDate = this.helperDateService.create();

return {
_metadata: {
customProperty: {
messageProperties: {
serviceName: this.serviceName,
},
},
},
data: {
userAgent,
date: newDate,
format: this.helperDateService.format(newDate),
timestamp: this.helperDateService.timestamp(newDate),
},
};
}
}
22 changes: 2 additions & 20 deletions src/app/docs/app.doc.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import { applyDecorators } from '@nestjs/common';
import { AppHelloSerialization } from 'src/app/serializations/app.hello.serialization';
import {
Doc,
DocAuth,
DocGuard,
DocResponse,
} from 'src/common/doc/decorators/doc.decorator';
import { Doc, DocResponse } from 'src/common/doc/decorators/doc.decorator';

export function AppHelloDoc(): MethodDecorator {
return applyDecorators(
Doc({
operation: 'hello',
operation: 'hello test api',
}),
DocResponse<AppHelloSerialization>('app.hello', {
serialization: AppHelloSerialization,
})
);
}

export function AppHelloApiKeyDoc(): MethodDecorator {
return applyDecorators(
Doc({
operation: 'hello',
}),
DocAuth({ apiKey: true }),
DocGuard({ timestamp: true, userAgent: true }),
DocResponse<AppHelloSerialization>('app.helloApiKey', {
serialization: AppHelloSerialization,
})
);
}
18 changes: 9 additions & 9 deletions src/common/api-key/docs/api-key.admin.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ResponseIdSerialization } from 'src/common/response/serializations/resp

export function ApiKeyAdminListDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'get list of api keys' }),
DocRequest({
queries: ApiKeyDocQueryIsActive,
}),
Expand All @@ -43,7 +43,7 @@ export function ApiKeyAdminListDoc(): MethodDecorator {

export function ApiKeyAdminGetDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'get detail an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand All @@ -67,7 +67,7 @@ export function ApiKeyAdminGetDoc(): MethodDecorator {

export function ApiKeyAdminCreateDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'create an api key' }),
DocAuth({
jwtAccessToken: true,
}),
Expand All @@ -85,7 +85,7 @@ export function ApiKeyAdminCreateDoc(): MethodDecorator {

export function ApiKeyAdminActiveDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'make api key be active' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand Down Expand Up @@ -120,7 +120,7 @@ export function ApiKeyAdminActiveDoc(): MethodDecorator {

export function ApiKeyAdminInactiveDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'make api key be inactive' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand Down Expand Up @@ -155,7 +155,7 @@ export function ApiKeyAdminInactiveDoc(): MethodDecorator {

export function ApiKeyAdminResetDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'reset secret an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand Down Expand Up @@ -192,7 +192,7 @@ export function ApiKeyAdminResetDoc(): MethodDecorator {

export function ApiKeyAdminUpdateDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'update data an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
bodyType: ENUM_DOC_REQUEST_BODY_TYPE.JSON,
Expand Down Expand Up @@ -231,7 +231,7 @@ export function ApiKeyAdminUpdateDoc(): MethodDecorator {

export function ApiKeyAdminUpdateDateDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'update date of api key' }),
DocRequest({
params: ApiKeyDocParamsId,
bodyType: ENUM_DOC_REQUEST_BODY_TYPE.JSON,
Expand Down Expand Up @@ -270,7 +270,7 @@ export function ApiKeyAdminUpdateDateDoc(): MethodDecorator {

export function ApiKeyAdminDeleteDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.apiKey' }),
Doc({ summary: 'delete an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand Down
16 changes: 8 additions & 8 deletions src/common/api-key/docs/api-key.user.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ResponseIdSerialization } from 'src/common/response/serializations/resp

export function ApiKeyUserListDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'get list of api keys' }),
DocRequest({
queries: ApiKeyDocQueryIsActive,
}),
Expand All @@ -35,7 +35,7 @@ export function ApiKeyUserListDoc(): MethodDecorator {

export function ApiKeyUserGetDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'get detail an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand All @@ -51,7 +51,7 @@ export function ApiKeyUserGetDoc(): MethodDecorator {

export function ApiKeyUserCreateDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'create an api key' }),
DocAuth({
jwtAccessToken: true,
}),
Expand All @@ -65,7 +65,7 @@ export function ApiKeyUserCreateDoc(): MethodDecorator {

export function ApiKeyUserActiveDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'make api key be active' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand All @@ -79,7 +79,7 @@ export function ApiKeyUserActiveDoc(): MethodDecorator {

export function ApiKeyUserInactiveDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'make api key be inactive' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand All @@ -93,7 +93,7 @@ export function ApiKeyUserInactiveDoc(): MethodDecorator {

export function ApiKeyUserResetDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'reset secret an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand All @@ -109,7 +109,7 @@ export function ApiKeyUserResetDoc(): MethodDecorator {

export function ApiKeyUserUpdateDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'update data an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand All @@ -125,7 +125,7 @@ export function ApiKeyUserUpdateDoc(): MethodDecorator {

export function ApiKeyUserDeleteDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.user.apiKey' }),
Doc({ summary: 'delete an api key' }),
DocRequest({
params: ApiKeyDocParamsId,
}),
Expand Down
11 changes: 10 additions & 1 deletion src/common/doc/decorators/doc.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ export function Doc(options?: IDocOptions): MethodDecorator {

return applyDecorators(
ApiOperation({
summary: options?.operation,
summary: options?.summary,
deprecated: options?.deprecated,
description: options?.description,
operationId: options?.operation,
}),
ApiHeaders([
{
Expand Down Expand Up @@ -411,6 +412,14 @@ export function DocAuth(options?: IDocAuthOptions) {
});
}

if (options?.google) {
docs.push(ApiBearerAuth('google'));
oneOfUnauthorized.push({
messagePath: 'auth.error.googleSSO',
statusCode: ENUM_AUTH_STATUS_CODE_ERROR.AUTH_GOOGLE_SSO_ERROR,
});
}

if (options?.apiKey) {
docs.push(ApiSecurity('apiKey'));
oneOfUnauthorized.push(
Expand Down
2 changes: 2 additions & 0 deletions src/common/doc/interfaces/doc.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ENUM_DOC_REQUEST_BODY_TYPE } from 'src/common/doc/constants/doc.enum.co
import { ENUM_FILE_EXCEL_MIME } from 'src/common/file/constants/file.enum.constant';

export interface IDocOptions {
summary?: string;
operation?: string;
deprecated?: boolean;
description?: string;
Expand All @@ -24,6 +25,7 @@ export interface IDocAuthOptions {
jwtAccessToken?: boolean;
jwtRefreshToken?: boolean;
apiKey?: boolean;
google?: boolean;
}

export interface IDocRequestOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/common/message/docs/message.public.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MessageLanguageSerialization } from 'src/common/message/serializations/

export function MessagePublicLanguageDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.public.message' }),
Doc({ summary: 'get all language of app' }),
DocResponse<MessageLanguageSerialization>('apiKey.languages', {
serialization: MessageLanguageSerialization,
})
Expand Down
2 changes: 1 addition & 1 deletion src/common/setting/docs/setting.admin.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ENUM_SETTING_STATUS_CODE_ERROR } from 'src/common/setting/constants/set

export function SettingAdminUpdateDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.admin.setting' }),
Doc({ summary: 'update a setting' }),
DocRequest({
params: SettingDocParamsId,
}),
Expand Down
4 changes: 2 additions & 2 deletions src/common/setting/docs/setting.public.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { SettingListSerialization } from 'src/common/setting/serializations/sett
export function SettingPublicListDoc(): MethodDecorator {
return applyDecorators(
Doc({
operation: 'common.public.setting',
summary: 'get list of settings',
}),
DocResponsePaging<SettingListSerialization>('setting.list', {
serialization: SettingListSerialization,
Expand All @@ -25,7 +25,7 @@ export function SettingPublicListDoc(): MethodDecorator {

export function SettingPublicGetDoc(): MethodDecorator {
return applyDecorators(
Doc({ operation: 'common.public.setting' }),
Doc({ summary: 'get detail a setting' }),
DocRequest({
params: SettingDocParamsId,
}),
Expand Down
6 changes: 6 additions & 0 deletions src/health/controllers/health.public.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MongooseHealthIndicator,
} from '@nestjs/terminus';
import { Connection } from 'mongoose';
import { ApiKeyPublicProtected } from 'src/common/api-key/decorators/api-key.decorator';
import { DatabaseConnection } from 'src/common/database/decorators/database.decorator';
import { Response } from 'src/common/response/decorators/response.decorator';
import { IResponse } from 'src/common/response/interfaces/response.interface';
Expand All @@ -33,6 +34,7 @@ export class HealthPublicController {
@HealthCheckDoc()
@Response('health.check', { serialization: HealthSerialization })
@HealthCheck()
@ApiKeyPublicProtected()
@Get('/aws')
async checkAws(): Promise<IResponse> {
const data = await this.health.check([
Expand All @@ -47,6 +49,7 @@ export class HealthPublicController {
@HealthCheckDoc()
@Response('health.check', { serialization: HealthSerialization })
@HealthCheck()
@ApiKeyPublicProtected()
@Get('/database')
async checkDatabase(): Promise<IResponse> {
const data = await this.health.check([
Expand All @@ -64,6 +67,7 @@ export class HealthPublicController {
@HealthCheckDoc()
@Response('health.check', { serialization: HealthSerialization })
@HealthCheck()
@ApiKeyPublicProtected()
@Get('/memory-heap')
async checkMemoryHeap(): Promise<IResponse> {
const data = await this.health.check([
Expand All @@ -82,6 +86,7 @@ export class HealthPublicController {
@HealthCheckDoc()
@Response('health.check', { serialization: HealthSerialization })
@HealthCheck()
@ApiKeyPublicProtected()
@Get('/memory-rss')
async checkMemoryRss(): Promise<IResponse> {
const data = await this.health.check([
Expand All @@ -100,6 +105,7 @@ export class HealthPublicController {
@HealthCheckDoc()
@Response('health.check', { serialization: HealthSerialization })
@HealthCheck()
@ApiKeyPublicProtected()
@Get('/storage')
async checkStorage(): Promise<IResponse> {
const data = await this.health.check([
Expand Down
2 changes: 1 addition & 1 deletion src/health/docs/health.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { HealthSerialization } from 'src/health/serializations/health.serializat
export function HealthCheckDoc(): MethodDecorator {
return applyDecorators(
Doc({
operation: 'health',
summary: 'health check api',
}),
DocAuth({
jwtAccessToken: true,
Expand Down
Loading

0 comments on commit 3733c19

Please sign in to comment.