Skip to content

Commit

Permalink
config: TimezoneInterceptor를 추가하여 Date 객체를 서울 시간대로 변경 #70
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 13, 2024
1 parent 1c7a30e commit d7dd452
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { BannerModule } from './banner/banner.module';
import { AdminModule } from './admin/admin.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { TimezoneInterceptor } from './config/TimezoneInterceptor';

@Module({
imports: [
Expand All @@ -28,6 +30,12 @@ import { AdminModule } from './admin/admin.module';
AdminModule,
],
controllers: [AppController],
providers: [AppService],
providers: [
AppService,
{
provide: APP_INTERCEPTOR,
useClass: TimezoneInterceptor,
},
],
})
export class AppModule {}
35 changes: 35 additions & 0 deletions backend/src/config/TimezoneInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class TimezoneInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((data) => {
if (data && typeof data === 'object') {
return this.convertDateToSeoulTime(data);
}
return data;
}),
);
}

private convertDateToSeoulTime(data: any): any {
for (const key in data) {
if (data[key] instanceof Date) {
data[key] = data[key].toLocaleString('ko-KR', {
timeZone: 'Asia/Seoul',
});
} else if (typeof data[key] === 'object') {
data[key] = this.convertDateToSeoulTime(data[key]);
}
}
return data;
}
}

0 comments on commit d7dd452

Please sign in to comment.