강동현

타입 리팩토링

1 import { RoomDescription, RoomInfo } from "./dataType"; 1 import { RoomDescription, RoomInfo } from "./dataType";
2 +import { keys } from "ts-transformer-keys";
2 3
3 // 서버로 들어오는 메세지 타입을 정의합니다. 4 // 서버로 들어오는 메세지 타입을 정의합니다.
4 // 'result' 속성은 서버 요청 결과에만 포함되는 특별한 속성입니다. 5 // 'result' 속성은 서버 요청 결과에만 포함되는 특별한 속성입니다.
5 -export interface ServerInboundMessageMap { 6 +interface ServerInboundMessageMap {
6 // 로그인을 시도합니다. 7 // 로그인을 시도합니다.
7 login: { 8 login: {
8 username: string; 9 username: string;
...@@ -47,7 +48,7 @@ export interface ServerInboundMessageMap { ...@@ -47,7 +48,7 @@ export interface ServerInboundMessageMap {
47 } 48 }
48 49
49 // 서버에서 나가는 메세지 타입을 정의합니다. 50 // 서버에서 나가는 메세지 타입을 정의합니다.
50 -export interface ServerOutboundMessageMap { 51 +interface ServerOutboundMessageMap {
51 // 방에 접속 중인 유저 목록이 업데이트 되었습니다. 52 // 방에 접속 중인 유저 목록이 업데이트 되었습니다.
52 updateRoomUser: { 53 updateRoomUser: {
53 state: "added" | "updated" | "removed"; 54 state: "added" | "updated" | "removed";
...@@ -121,10 +122,16 @@ export interface ServerOutboundMessageMap { ...@@ -121,10 +122,16 @@ export interface ServerOutboundMessageMap {
121 }; 122 };
122 } 123 }
123 124
124 -export type ServerInboundMessage<Key extends keyof ServerInboundMessageMap> = 125 +export type ServerInboundMessageKey = keyof ServerInboundMessageMap;
125 - Omit<ServerInboundMessageMap[Key], "result">;
126 126
127 -export interface ServerResponse<Key extends keyof ServerInboundMessageMap> { 127 +export const ServerInboundMessageKeyArray = keys<ServerInboundMessageMap>();
128 +
129 +export type ServerInboundMessage<Key extends ServerInboundMessageKey> = Omit<
130 + ServerInboundMessageMap[Key],
131 + "result"
132 +>;
133 +
134 +export interface ServerResponse<Key extends ServerInboundMessageKey> {
128 ok: boolean; 135 ok: boolean;
129 reason?: string; 136 reason?: string;
130 result?: "result" extends keyof ServerInboundMessageMap[Key] 137 result?: "result" extends keyof ServerInboundMessageMap[Key]
...@@ -134,3 +141,7 @@ export interface ServerResponse<Key extends keyof ServerInboundMessageMap> { ...@@ -134,3 +141,7 @@ export interface ServerResponse<Key extends keyof ServerInboundMessageMap> {
134 141
135 export type ServerOutboundMessage<Key extends keyof ServerOutboundMessageMap> = 142 export type ServerOutboundMessage<Key extends keyof ServerOutboundMessageMap> =
136 ServerOutboundMessageMap[Key]; 143 ServerOutboundMessageMap[Key];
144 +
145 +export type ServerOutboundMessageKey = keyof ServerOutboundMessageMap;
146 +
147 +export const ServerOutboundMessageKeyArray = keys<ServerOutboundMessageMap>();
......
1 +{
2 + "name": "common",
3 + "version": "1.0.0",
4 + "main": "index.js",
5 + "license": "MIT",
6 + "dependencies": {
7 + "ts-transformer-keys": "^0.4.3"
8 + }
9 +}
1 +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2 +# yarn lockfile v1
3 +
4 +
5 +ts-transformer-keys@^0.4.3:
6 + version "0.4.3"
7 + resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
8 + integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
1 import { Socket } from "socket.io"; 1 import { Socket } from "socket.io";
2 -import { ServerOutboundMessage, ServerOutboundMessageMap } from "../../common"; 2 +import { ServerOutboundMessage, ServerOutboundMessageKey } from "../../common";
3 import { MessageHandlerChain } from "../message/MessageHandlerChain"; 3 import { MessageHandlerChain } from "../message/MessageHandlerChain";
4 import { Room } from "../room/Room"; 4 import { Room } from "../room/Room";
5 import { User } from "../user/User"; 5 import { User } from "../user/User";
...@@ -16,7 +16,7 @@ export class Connection { ...@@ -16,7 +16,7 @@ export class Connection {
16 this.messageHandlerChain = new MessageHandlerChain(this); 16 this.messageHandlerChain = new MessageHandlerChain(this);
17 } 17 }
18 18
19 - public send<T extends keyof ServerOutboundMessageMap>( 19 + public send<T extends ServerOutboundMessageKey>(
20 type: T, 20 type: T,
21 message: ServerOutboundMessage<T> 21 message: ServerOutboundMessage<T>
22 ) { 22 ) {
......
1 import { Connection } from "../connection/Connection"; 1 import { Connection } from "../connection/Connection";
2 import { 2 import {
3 ServerInboundMessage, 3 ServerInboundMessage,
4 - ServerInboundMessageMap, 4 + ServerInboundMessageKey,
5 ServerResponse, 5 ServerResponse,
6 } from "../../common/index"; 6 } from "../../common/index";
7 import { User } from "../user/User"; 7 import { User } from "../user/User";
8 8
9 type UserHandlerMap = { 9 type UserHandlerMap = {
10 - [Key in keyof ServerInboundMessageMap]?: ( 10 + [Key in ServerInboundMessageKey]?: (
11 user: User, 11 user: User,
12 message: ServerInboundMessage<Key> 12 message: ServerInboundMessage<Key>
13 ) => ServerResponse<Key>; 13 ) => ServerResponse<Key>;
...@@ -21,7 +21,7 @@ export class MessageHandler { ...@@ -21,7 +21,7 @@ export class MessageHandler {
21 } 21 }
22 22
23 public handle( 23 public handle(
24 - type: keyof ServerInboundMessageMap, 24 + type: ServerInboundMessageKey,
25 user: User, 25 user: User,
26 message: any, 26 message: any,
27 callback: Function 27 callback: Function
......
1 import { Connection } from "../connection/Connection"; 1 import { Connection } from "../connection/Connection";
2 import { 2 import {
3 ServerInboundMessage, 3 ServerInboundMessage,
4 - ServerInboundMessageMap, 4 + ServerInboundMessageKey,
5 + ServerOutboundMessageKeyArray,
5 ServerResponse, 6 ServerResponse,
6 } from "../../common/index"; 7 } from "../../common/index";
7 -import { keys } from "ts-transformer-keys";
8 import { User } from "../user/User"; 8 import { User } from "../user/User";
9 9
10 export class MessageHandlerChain { 10 export class MessageHandlerChain {
...@@ -24,8 +24,8 @@ export class MessageHandlerChain { ...@@ -24,8 +24,8 @@ export class MessageHandlerChain {
24 } 24 }
25 ); 25 );
26 26
27 - for (const key in keys<ServerInboundMessageMap>()) { 27 + for (const key in ServerOutboundMessageKeyArray) {
28 - const type = key as keyof ServerInboundMessageMap; 28 + const type = key as ServerInboundMessageKey;
29 this.connection.socket.on(key, (message: any, callback: Function) => { 29 this.connection.socket.on(key, (message: any, callback: Function) => {
30 // Game > Room > User 순으로 전달 30 // Game > Room > User 순으로 전달
31 if ( 31 if (
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 "socket.io": "^4.1.2", 16 "socket.io": "^4.1.2",
17 "socket.io-client": "^4.1.2", 17 "socket.io-client": "^4.1.2",
18 "ts-node": "^9.1.1", 18 "ts-node": "^9.1.1",
19 - "ts-transformer-keys": "^0.4.3",
20 "typescript": "^4.2.4", 19 "typescript": "^4.2.4",
21 "uuid": "^8.3.2" 20 "uuid": "^8.3.2"
22 }, 21 },
......
...@@ -6,7 +6,7 @@ import { MessageHandler } from "../message/MessageHandler"; ...@@ -6,7 +6,7 @@ import { MessageHandler } from "../message/MessageHandler";
6 import { 6 import {
7 ServerInboundMessage, 7 ServerInboundMessage,
8 ServerOutboundMessage, 8 ServerOutboundMessage,
9 - ServerOutboundMessageMap, 9 + ServerOutboundMessageKey,
10 } from "../../common"; 10 } from "../../common";
11 import { RoomDescription, RoomInfo, UserData } from "../../common/dataType"; 11 import { RoomDescription, RoomInfo, UserData } from "../../common/dataType";
12 12
...@@ -86,7 +86,7 @@ export class Room { ...@@ -86,7 +86,7 @@ export class Room {
86 }; 86 };
87 } 87 }
88 88
89 - public broadcast<T extends keyof ServerOutboundMessageMap>( 89 + public broadcast<T extends ServerOutboundMessageKey>(
90 type: T, 90 type: T,
91 message: ServerOutboundMessage<T>, 91 message: ServerOutboundMessage<T>,
92 except?: User 92 except?: User
......
...@@ -1657,11 +1657,6 @@ ts-node@^9.1.1: ...@@ -1657,11 +1657,6 @@ ts-node@^9.1.1:
1657 source-map-support "^0.5.17" 1657 source-map-support "^0.5.17"
1658 yn "3.1.1" 1658 yn "3.1.1"
1659 1659
1660 -ts-transformer-keys@^0.4.3:
1661 - version "0.4.3"
1662 - resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
1663 - integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
1664 -
1665 type-detect@^4.0.0, type-detect@^4.0.5: 1660 type-detect@^4.0.0, type-detect@^4.0.5:
1666 version "4.0.8" 1661 version "4.0.8"
1667 resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" 1662 resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
......