Skip to content

Commit

Permalink
Move Server to using built-in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed May 22, 2023
1 parent 48a1289 commit f00d4e9
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/account_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { ServerApi } from "./server_api";
* @class AccountCallBuilder
* @extends CallBuilder
* @constructor
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class AccountCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.AccountRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl);
this.segment("accounts");
}
Expand Down
4 changes: 2 additions & 2 deletions src/assets_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { ServerApi } from "./server_api";
* @class AssetsCallBuilder
* @constructor
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class AssetsCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.AssetRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl);
this.segment("assets");
}
Expand Down
3 changes: 1 addition & 2 deletions src/call_builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import URI from "urijs";
import URITemplate from "urijs/src/URITemplate";

import { BadRequestError, NetworkError, NotFoundError } from "./errors";
Expand Down Expand Up @@ -45,7 +44,7 @@ export class CallBuilder<

public filter: string[][];

constructor(serverUrl: URL | URI, neighborRoot: string = "") {
constructor(serverUrl: URL | string, neighborRoot: string = "") {
this.url = new URL(serverUrl.toString());

this.filter = [];
Expand Down
4 changes: 2 additions & 2 deletions src/claimable_balances_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { ServerApi } from "./server_api";
* @class ClaimableBalanceCallBuilder
* @constructor
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {URL|string} serverUrl Horizon server URL.
*/
export class ClaimableBalanceCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.ClaimableBalanceRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL|string) {
super(serverUrl);
this.segment("claimable_balances");
}
Expand Down
4 changes: 2 additions & 2 deletions src/effect_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { ServerApi } from "./server_api";
* @extends CallBuilder
* @see [All Effects](https://developers.stellar.org/api/resources/effects/)
* @constructor
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class EffectCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.EffectRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl, "effects");
this.segment("effects");
}
Expand Down
2 changes: 1 addition & 1 deletion src/friendbot_builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CallBuilder } from "./call_builder";

export class FriendbotBuilder extends CallBuilder<any> {
constructor(serverUrl: URI, address: string) {
constructor(serverUrl: URL | string, address: string) {
super(serverUrl);
this.segment("friendbot");
this.url.searchParams.set("addr", address);
Expand Down
4 changes: 2 additions & 2 deletions src/ledger_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { ServerApi } from "./server_api";
* @constructor
* @class LedgerCallBuilder
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class LedgerCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.LedgerRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl);
this.segment("ledgers");
}
Expand Down
4 changes: 2 additions & 2 deletions src/liquidity_pool_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { ServerApi } from "./server_api";
* @class LiquidityPoolCallBuilder
* @extends CallBuilder
* @constructor
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class LiquidityPoolCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.LiquidityPoolRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl);
this.segment("liquidity_pools");
}
Expand Down
4 changes: 2 additions & 2 deletions src/offer_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { ServerApi } from "./server_api";
* @class OfferCallBuilder
* @constructor
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class OfferCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.OfferRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl, "offers");
this.segment("offers");
}
Expand Down
4 changes: 2 additions & 2 deletions src/operation_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { ServerApi } from "./server_api";
* @class OperationCallBuilder
* @constructor
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class OperationCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.OperationRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl, "operations");
this.segment("operations");
}
Expand Down
4 changes: 2 additions & 2 deletions src/orderbook_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { ServerApi } from "./server_api";
*
* Do not create this object directly, use {@link Server#orderbook}.
* @see [Orderbook Details](https://developers.stellar.org/api/aggregations/order-books/)
* @param {string} serverUrl serverUrl Horizon server URL.
* @param {string|URL} serverUrl serverUrl Horizon server URL.
* @param {Asset} selling Asset being sold
* @param {Asset} buying Asset being bought
*/
export class OrderbookCallBuilder extends CallBuilder<ServerApi.OrderbookRecord> {
constructor(serverUrl: URI, selling: Asset, buying: Asset) {
constructor(serverUrl: URL | string, selling: Asset, buying: Asset) {
super(serverUrl);
this.segment("order_book");
if (!selling.isNative()) {
Expand Down
4 changes: 2 additions & 2 deletions src/path_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ServerApi } from "./server_api";
* Do not create this object directly, use {@link Server#paths}.
* @see [Find Payment Paths](https://developers.stellar.org/api/aggregations/paths/)
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
* @param {string} source The sender's account ID. Any returned path must use a source that the sender can hold.
* @param {string} destination The destination account ID that any returned path should use.
* @param {Asset} destinationAsset The destination asset.
Expand All @@ -30,7 +30,7 @@ export class PathCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.PaymentPathRecord>
> {
constructor(
serverUrl: URI,
serverUrl: URL | string,
source: string,
destination: string,
destinationAsset: Asset,
Expand Down
4 changes: 2 additions & 2 deletions src/payment_call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { ServerApi } from "./server_api";
* @see [All Payments](https://developers.stellar.org/api/resources/payments/)
* @constructor
* @extends CallBuilder
* @param {string} serverUrl Horizon server URL.
* @param {string|URL} serverUrl Horizon server URL.
*/
export class PaymentCallBuilder extends CallBuilder<
ServerApi.CollectionPage<ServerApi.PaymentOperationRecord>
> {
constructor(serverUrl: URI) {
constructor(serverUrl: URL | string) {
super(serverUrl, "payments");
this.segment("payments");
}
Expand Down
45 changes: 22 additions & 23 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Transaction,
xdr,
} from "stellar-base";
import URI from "urijs";

import { CallBuilder } from "./call_builder";
import { Config } from "./config";
Expand Down Expand Up @@ -69,10 +68,10 @@ export class Server {
/**
* Horizon Server URL (ex. `https://horizon-testnet.stellar.org`).
*/
public readonly serverURL: URI;
public readonly serverURL: URL;

constructor(serverURL: string | URL, opts: Server.Options = {}) {
this.serverURL = URI(serverURL.toString());
this.serverURL = new URL(serverURL.toString());

const allowHttp = opts.allowHttp ?? Config.isAllowHttp();
const customHeaders: Record<string, string> = {};
Expand All @@ -96,7 +95,7 @@ export class Server {
});
}

if (this.serverURL.protocol() !== "https" && !allowHttp) {
if (this.serverURL.protocol !== "https:" && !allowHttp) {
throw new Error("Cannot connect to insecure horizon server");
}
}
Expand Down Expand Up @@ -136,7 +135,7 @@ export class Server {
_isRetry: boolean = false,
): Promise<Server.Timebounds> {
// HorizonAxiosClient instead of this.ledgers so we can get at them headers
const currentTime = getCurrentServerTime(this.serverURL.hostname());
const currentTime = getCurrentServerTime(this.serverURL.hostname);

if (currentTime) {
return {
Expand All @@ -155,7 +154,7 @@ export class Server {

// otherwise, retry (by calling the root endpoint)
// toString automatically adds the trailing slash
await HorizonAxiosClient.get(URI(this.serverURL as any).toString());
await HorizonAxiosClient.get(this.serverURL.toString());
return await this.fetchTimebounds(seconds, true);
}

Expand All @@ -178,7 +177,7 @@ export class Server {
*/
public async feeStats(): Promise<Horizon.FeeStatsResponse> {
const cb = new CallBuilder<Horizon.FeeStatsResponse>(
URI(this.serverURL as any),
this.serverURL,
);
cb.filter.push(["fee_stats"]);
return cb.call();
Expand Down Expand Up @@ -506,28 +505,28 @@ export class Server {
* @returns {AccountCallBuilder} New {@link AccountCallBuilder} object configured by a current Horizon server configuration.
*/
public accounts(): AccountCallBuilder {
return new AccountCallBuilder(URI(this.serverURL as any));
return new AccountCallBuilder(this.serverURL);
}

/**
* @returns {ClaimableBalanceCallBuilder} New {@link ClaimableBalanceCallBuilder} object configured by a current Horizon server configuration.
*/
public claimableBalances(): ClaimableBalanceCallBuilder {
return new ClaimableBalanceCallBuilder(URI(this.serverURL as any));
return new ClaimableBalanceCallBuilder(this.serverURL);
}

/**
* @returns {LedgerCallBuilder} New {@link LedgerCallBuilder} object configured by a current Horizon server configuration.
*/
public ledgers(): LedgerCallBuilder {
return new LedgerCallBuilder(URI(this.serverURL as any));
return new LedgerCallBuilder(this.serverURL);
}

/**
* @returns {TransactionCallBuilder} New {@link TransactionCallBuilder} object configured by a current Horizon server configuration.
*/
public transactions(): TransactionCallBuilder {
return new TransactionCallBuilder(URI(this.serverURL as any));
return new TransactionCallBuilder(this.serverURL);
}

/**
Expand All @@ -545,7 +544,7 @@ export class Server {
* @returns {OfferCallBuilder} New {@link OfferCallBuilder} object
*/
public offers(): OfferCallBuilder {
return new OfferCallBuilder(URI(this.serverURL as any));
return new OfferCallBuilder(this.serverURL);
}

/**
Expand All @@ -555,7 +554,7 @@ export class Server {
*/
public orderbook(selling: Asset, buying: Asset): OrderbookCallBuilder {
return new OrderbookCallBuilder(
URI(this.serverURL as any),
this.serverURL,
selling,
buying,
);
Expand All @@ -566,22 +565,22 @@ export class Server {
* @returns {TradesCallBuilder} New {@link TradesCallBuilder} object configured by a current Horizon server configuration.
*/
public trades(): TradesCallBuilder {
return new TradesCallBuilder(URI(this.serverURL as any));
return new TradesCallBuilder(this.serverURL);
}

/**
* @returns {OperationCallBuilder} New {@link OperationCallBuilder} object configured by a current Horizon server configuration.
*/
public operations(): OperationCallBuilder {
return new OperationCallBuilder(URI(this.serverURL as any));
return new OperationCallBuilder(this.serverURL);
}

/**
* @returns {LiquidityPoolCallBuilder} New {@link LiquidityPoolCallBuilder}
* object configured to the current Horizon server settings.
*/
public liquidityPools(): LiquidityPoolCallBuilder {
return new LiquidityPoolCallBuilder(URI(this.serverURL));
return new LiquidityPoolCallBuilder(this.serverURL);
}

/**
Expand Down Expand Up @@ -616,7 +615,7 @@ export class Server {
destinationAmount: string,
): PathCallBuilder {
return new StrictReceivePathCallBuilder(
URI(this.serverURL as any),
this.serverURL,
source,
destinationAsset,
destinationAmount,
Expand Down Expand Up @@ -644,7 +643,7 @@ export class Server {
destination: string | Asset[],
): PathCallBuilder {
return new StrictSendPathCallBuilder(
URI(this.serverURL as any),
this.serverURL,
sourceAsset,
sourceAmount,
destination,
Expand All @@ -656,15 +655,15 @@ export class Server {
* Horizon server configuration.
*/
public payments(): PaymentCallBuilder {
return new PaymentCallBuilder(URI(this.serverURL as any) as any);
return new PaymentCallBuilder(this.serverURL as any);
}

/**
* @returns {EffectCallBuilder} New {@link EffectCallBuilder} instance configured with the current
* Horizon server configuration
*/
public effects(): EffectCallBuilder {
return new EffectCallBuilder(URI(this.serverURL as any) as any);
return new EffectCallBuilder(this.serverURL as any);
}

/**
Expand All @@ -674,7 +673,7 @@ export class Server {
* @private
*/
public friendbot(address: string): FriendbotBuilder {
return new FriendbotBuilder(URI(this.serverURL as any), address);
return new FriendbotBuilder(this.serverURL, address);
}

/**
Expand All @@ -683,7 +682,7 @@ export class Server {
* @returns {AssetsCallBuilder} New AssetsCallBuilder instance
*/
public assets(): AssetsCallBuilder {
return new AssetsCallBuilder(URI(this.serverURL as any));
return new AssetsCallBuilder(this.serverURL);
}

/**
Expand Down Expand Up @@ -723,7 +722,7 @@ export class Server {
offset: number,
): TradeAggregationCallBuilder {
return new TradeAggregationCallBuilder(
URI(this.serverURL as any),
this.serverURL,
base,
counter,
start_time,
Expand Down
Loading

0 comments on commit f00d4e9

Please sign in to comment.