Skip to content

Commit

Permalink
feat(Core): added error handling to message client
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Nov 29, 2023
1 parent dc05cfe commit e20eff6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { Message } from './message.interface';
export { MessageResponse } from './message-response.type';
export { MessageClient } from './message-client';
export { MessageHost } from './message-host';
export * from './selectors';
7 changes: 5 additions & 2 deletions src/message-response.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Message } from './message.interface';

type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;

type UncompletedMessageResponse<T = any> = Omit<Message<T>, 'path'> & { completed: false; };
export type UncompletedMessageResponse<T = any> = Omit<Message<T>, 'path'> & { completed: false; };
type CompletedMessageResponse = Omit<Message, 'body' | 'path'> & { completed: true; };

export type MessageResponse<T = any> = UncompletedMessageResponse<T> | CompletedMessageResponse;
export type ErrorMessageResponse = {error: any};
export type SuccessfulMessageResponse<T = any> = UncompletedMessageResponse<T> | CompletedMessageResponse;

export type MessageResponse<T = any> = SuccessfulMessageResponse<T> | ErrorMessageResponse;
12 changes: 10 additions & 2 deletions src/request.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { filter, pluck, takeWhile } from 'rxjs/operators';
import { filter, map, takeWhile } from 'rxjs/operators';

import { MessageClient } from './message-client';
import { GET_NEW_ID, RESPONSES$, SEND } from './selectors';
import { ErrorMessageResponse, SuccessfulMessageResponse, UncompletedMessageResponse } from './message-response.type';

export function Request(path: string): MethodDecorator {
return function(target: object, key: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> {
Expand All @@ -11,9 +12,16 @@ export function Request(path: string): MethodDecorator {
this[SEND]({body: message, id: messageId, path});

return this[RESPONSES$].pipe(
map((data) => {
const {error} = data as ErrorMessageResponse;

if (error) throw error;

return data as SuccessfulMessageResponse;
}),
filter(({id}) => id === messageId),
takeWhile(({completed}) => !completed),
pluck('body'),
map(({body}: UncompletedMessageResponse) => body),
);
};

Expand Down

0 comments on commit e20eff6

Please sign in to comment.