Skip to content

Commit

Permalink
refactor(exchanges): improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
weyoss committed Jan 2, 2024
1 parent d9195e4 commit 97665fe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 64 deletions.
6 changes: 2 additions & 4 deletions src/lib/exchange/_from-json.ts
Expand Up @@ -7,15 +7,13 @@
* in the root directory of this source tree.
*/

import { EExchangeType, TExchangeSerialized } from '../../../types';
import { EExchangeType, TExchange, TExchangeSerialized } from '../../../types';
import { ExchangeInvalidDataError } from './errors';
import { ExchangeFanOut } from './exchange-fan-out';
import { ExchangeTopic } from './exchange-topic';
import { ExchangeDirect } from './exchange-direct';

export function _fromJSON(
json: Partial<TExchangeSerialized>,
): ExchangeFanOut | ExchangeTopic | ExchangeDirect {
export function _fromJSON(json: Partial<TExchangeSerialized>): TExchange {
if (!json.bindingParams || json.type === undefined)
throw new ExchangeInvalidDataError();
if (json.type === EExchangeType.FANOUT) {
Expand Down
35 changes: 35 additions & 0 deletions types/exchange/common.ts
@@ -0,0 +1,35 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

import { ExchangeDirect } from '../../src/lib/exchange/exchange-direct';
import { ExchangeTopic } from '../../src/lib/exchange/exchange-topic';
import { ExchangeFanOut } from '../../src/lib/exchange/exchange-fan-out';
import { IQueueParams } from '../queue';

export enum EExchangeType {
DIRECT,
FANOUT,
TOPIC,
}

export type TTopicParams = {
topic: string;
ns: string;
};

export type TExchangeDirectBindingParams = IQueueParams | string;
export type TExchangeTopicBindingParams = TTopicParams | string;
export type TExchangeFanOutBindingParams = string;

export type TExchange = ExchangeDirect | ExchangeTopic | ExchangeFanOut;

export type TExchangeSerialized =
| ReturnType<ExchangeDirect['toJSON']>
| ReturnType<ExchangeTopic['toJSON']>
| ReturnType<ExchangeFanOut['toJSON']>;
67 changes: 7 additions & 60 deletions types/exchange/exchange.ts
Expand Up @@ -9,30 +9,7 @@

import { IQueueParams } from '../queue';
import { ICallback } from 'redis-smq-common';

export type TTopicParams = {
topic: string;
ns: string;
};

export enum EExchangeType {
DIRECT,
FANOUT,
TOPIC,
}

export type TExchangeDirectBindingParams = IQueueParams | string;

export type TExchangeFanOutBindingParams = string;

export type TExchangeTopicBindingParams = TTopicParams | string;

export interface IExchange<BindingParams, ExchangeType extends EExchangeType>
extends IExchangeSerialized<BindingParams, ExchangeType> {
toJSON(): IExchangeSerialized<BindingParams, ExchangeType>;
getQueues(cb: ICallback<IQueueParams[]>): void;
getBindingParams(): BindingParams;
}
import { EExchangeType } from './common';

export interface IExchangeSerialized<
BindingParams,
Expand All @@ -43,39 +20,9 @@ export interface IExchangeSerialized<
readonly exchangeTag: string;
}

export type TExchangeDirectSerialized = IExchangeSerialized<
TExchangeDirectBindingParams,
EExchangeType.DIRECT
>;

export type TExchangeTopicSerialized = IExchangeSerialized<
TExchangeTopicBindingParams,
EExchangeType.TOPIC
>;

export type TExchangeFanOutSerialized = IExchangeSerialized<
TExchangeFanOutBindingParams,
EExchangeType.FANOUT
>;

export type TExchangeDirect = IExchange<
TExchangeDirectBindingParams,
EExchangeType.DIRECT
>;

export type TExchangeTopic = IExchange<
TExchangeTopicBindingParams,
EExchangeType.TOPIC
>;

export type TExchangeFanOut = IExchange<
TExchangeFanOutBindingParams,
EExchangeType.FANOUT
>;

export type TExchange = TExchangeDirect | TExchangeTopic | TExchangeFanOut;

export type TExchangeSerialized =
| TExchangeDirectSerialized
| TExchangeTopicSerialized
| TExchangeFanOutSerialized;
export interface IExchange<BindingParams, ExchangeType extends EExchangeType>
extends IExchangeSerialized<BindingParams, ExchangeType> {
toJSON(): IExchangeSerialized<BindingParams, ExchangeType>;
getQueues(cb: ICallback<IQueueParams[]>): void;
getBindingParams(): BindingParams;
}
1 change: 1 addition & 0 deletions types/exchange/index.ts
Expand Up @@ -8,3 +8,4 @@
*/

export * from './exchange';
export * from './common';

0 comments on commit 97665fe

Please sign in to comment.