Skip to content

Commit

Permalink
Fix some stream() types (#269)
Browse files Browse the repository at this point in the history
Passes two type parameters to the CallBuilder now: The types `FetchResponse` and `StreamResponse`, whereas `StreamResponse` defaults to `FetchResponse`.

Fixes #271.
  • Loading branch information
andywer authored and Morley Zhi committed Apr 28, 2019
1 parent 533d1f1 commit 7578d61
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,17 @@ export class Server {
type CallBuilderResponse = Horizon.BaseResponse | Server.CollectionPage;

export namespace Server {
abstract class CallBuilder<T extends CallBuilderResponse> {
abstract class CallBuilder<
FetchResponse extends CallBuilderResponse,
StreamResponse extends CallBuilderResponse = FetchResponse
> {
constructor(serverUrl: string);
call(): Promise<T>;
call(): Promise<FetchResponse>;
cursor(cursor: string): this;
limit(limit: number | string): this;
order(direction: 'asc' | 'desc'): this;
stream(options?: {
onmessage?: (record: T) => void;
onmessage?: (record: StreamResponse) => void;
onerror?: (error: Error) => void;
}): () => void;
}
Expand Down Expand Up @@ -455,14 +458,16 @@ export namespace Server {
}

abstract class AssetsCallBuilder extends CallBuilder<
CollectionPage<AssetRecord>
CollectionPage<AssetRecord>,
AssetRecord
> {
forCode(value: string): this;
forIssuer(value: string): this;
}

abstract class EffectCallBuilder extends CallBuilder<
CollectionPage<EffectRecord>
CollectionPage<EffectRecord>,
EffectRecord
> {
forAccount(accountId: string): this;
forLedger(sequence: string): this;
Expand All @@ -471,15 +476,18 @@ export namespace Server {
}

abstract class LedgerCallBuilder extends CallBuilder<
CollectionPage<LedgerRecord>
CollectionPage<LedgerRecord>,
LedgerRecord
> {}

abstract class OfferCallBuilder extends CallBuilder<
CollectionPage<OfferRecord>
CollectionPage<OfferRecord>,
OfferRecord
> {}

abstract class OperationCallBuilder extends CallBuilder<
CollectionPage<OperationRecord>
CollectionPage<OperationRecord>,
OperationRecord
> {
forAccount(accountId: string): this;
forLedger(sequence: string): this;
Expand All @@ -489,10 +497,12 @@ export namespace Server {
}
abstract class OrderbookCallBuilder extends CallBuilder<OrderbookRecord> {}
abstract class PathCallBuilder extends CallBuilder<
CollectionPage<PaymentPathRecord>
CollectionPage<PaymentPathRecord>,
PaymentPathRecord
> {}
abstract class PaymentCallBuilder extends CallBuilder<
CollectionPage<PaymentOperationRecord>
CollectionPage<PaymentOperationRecord>,
PaymentOperationRecord
> {
forAccount(accountId: string): this;
forLedger(sequence: string): this;
Expand All @@ -504,18 +514,21 @@ export namespace Server {
}

abstract class TradeAggregationCallBuilder extends CallBuilder<
CollectionPage<TradeAggregationRecord>
CollectionPage<TradeAggregationRecord>,
TradeAggregationRecord
> {}
abstract class TradesCallBuilder extends CallBuilder<
CollectionPage<TradeRecord>
CollectionPage<TradeRecord>,
TradeRecord
> {
forAssetPair(base: Asset, counter: Asset): this;
forOffer(offerId: string): this;
forAccount(accountId: string): this;
}

abstract class TransactionCallBuilder extends CallBuilder<
CollectionPage<TransactionRecord>
CollectionPage<TransactionRecord>,
TransactionRecord
> {
transaction(transactionId: string): this;
forAccount(accountId: string): this;
Expand Down

0 comments on commit 7578d61

Please sign in to comment.