Skip to content

Commit ea1ee34

Browse files
committed
Add CommandResultDone default generic type
Fix destructed result into CommandResult. Add request util for unwrapping result.
1 parent a3ec2f9 commit ea1ee34

7 files changed

+39
-14
lines changed

src/request/index.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,28 @@ export enum KnownDataStatusCode {
5252

5353
export type DataStatusCode = KnownDataStatusCode | number;
5454

55-
export interface DefaultReq {
55+
export interface ResponseState {
5656

57-
[key: string]: unknown;
57+
status: DataStatusCode;
5858

5959
}
6060

61-
export interface DefaultRes {
61+
export interface DefaultReq {
6262

63-
status: DataStatusCode;
6463
[key: string]: unknown;
6564

6665
}
6766

67+
export interface DefaultRes extends DefaultReq, ResponseState {
68+
69+
}
70+
6871
/**
6972
* Wrapped request response.
7073
*/
71-
interface RootCommandResult {
74+
interface RootCommandResult extends ResponseState {
7275

7376
readonly success: boolean;
74-
readonly status: DataStatusCode;
7577

7678
}
7779

@@ -94,6 +96,6 @@ interface CommandResultDoneVoid extends RootCommandResult {
9496

9597
}
9698

97-
export type CommandResultDone<T> = (T extends void ? CommandResultDoneVoid : CommandResultDoneValue<T>);
99+
export type CommandResultDone<T = void> = (T extends void ? CommandResultDoneVoid : CommandResultDoneValue<T>);
98100
export type CommandResult<T = void> = CommandResultFailed | CommandResultDone<T>;
99101
export type AsyncCommandResult<T = void> = Promise<CommandResult<T>>;

src/talk/openlink/talk-open-channel-data-session.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class TalkOpenChannelDataSession implements OpenChannelSession {
4343
return this._store;
4444
}
4545

46-
async markRead(chat: ChatLogged): Promise<{ success: boolean, status: number }> {
46+
async markRead(chat: ChatLogged): AsyncCommandResult {
4747
const res = await this._channelSession.markRead(chat);
4848

4949
if (res.success) {
@@ -160,7 +160,7 @@ export class TalkOpenChannelDataSession implements OpenChannelSession {
160160
chat: ChatLoggedType,
161161
type: RelayEventType,
162162
count: number,
163-
): Promise<{ status: number, success: boolean }> {
163+
): AsyncCommandResult {
164164
return this._channelSession.createEvent(chat, type, count);
165165
}
166166

@@ -214,7 +214,7 @@ export class TalkOpenChannelDataSession implements OpenChannelSession {
214214
return this._channelSession.blockUser(user);
215215
}
216216

217-
react(flag: boolean): Promise<{ status: number, success: boolean }> {
217+
react(flag: boolean): AsyncCommandResult {
218218
return this._channelSession.react(flag);
219219
}
220220

src/talk/openlink/talk-open-channel-session.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class TalkOpenChannelSession implements OpenChannelSession {
143143
return this._linkSession.removeKicked(this._channel, { ...user, kickedChannelId: this._channel.channelId });
144144
}
145145

146-
react(flag: boolean): Promise<{ status: number, success: boolean }> {
146+
react(flag: boolean): AsyncCommandResult {
147147
return this._linkSession.react(this._channel, flag);
148148
}
149149

@@ -179,7 +179,7 @@ export class TalkOpenChannelSession implements OpenChannelSession {
179179
chat: ChatLoggedType,
180180
type: RelayEventType,
181181
count: number,
182-
): Promise<{ status: number, success: boolean }> {
182+
): AsyncCommandResult {
183183
const res = await this._session.request(
184184
'RELAYEVENT',
185185
{

src/talk/openlink/talk-open-channel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class TalkOpenChannel
150150
return this._channelSession.forwardChat(chat, noSeen);
151151
}
152152

153-
deleteChat(chat: ChatLogged): Promise<{ success: boolean, status: number }> {
153+
deleteChat(chat: ChatLogged): AsyncCommandResult {
154154
return this._channelSession.deleteChat(chat);
155155
}
156156

src/talk/openlink/talk-open-link-session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class TalkOpenLinkSession implements OpenLinkSession {
128128
return { status: res.status, success: res.status === KnownDataStatusCode.SUCCESS };
129129
}
130130

131-
async react(link: OpenLinkComponent, flag: boolean): Promise<{ status: number, success: boolean }> {
131+
async react(link: OpenLinkComponent, flag: boolean): AsyncCommandResult {
132132
const res = await this._session.request<JoinInfoRes>(
133133
'REACT',
134134
{

src/util/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
export * from './chained-iterator';
88
export * from './json-util';
99
export * from './device';
10+
export * from './request';

src/util/request.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Created on Sun Jul 04 2021
3+
*
4+
* Copyright (c) storycraft. Licensed under the MIT Licence.
5+
*/
6+
7+
import { CommandResult, CommandResultDone } from "../request";
8+
9+
/**
10+
* Unwrap CommandResult and convert failed case into error.
11+
*
12+
* @template T
13+
* @param {CommandResult<T>} commandRes CommandResult to unwrap
14+
* @return {T} Unwrapped result
15+
*/
16+
export function unwrapResult<T>(commandRes: CommandResult<T>): CommandResultDone<T> {
17+
if (!commandRes.success) {
18+
throw new Error(`Request failed with status: ${commandRes.status}`);
19+
}
20+
21+
return commandRes;
22+
}

0 commit comments

Comments
 (0)