From 4f1a04090f26f8e8565c516921d5d3332b6a4649 Mon Sep 17 00:00:00 2001 From: Janek Rahrt Date: Tue, 30 Nov 2021 17:01:56 +0100 Subject: [PATCH] fix: use urljoin --- package-lock.json | 24 +++++++++++++++++------- package.json | 4 +++- src/provider/default.ts | 39 ++++++++++++++++++++++++--------------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index deca23409..32b00df2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,8 @@ "hash.js": "^1.1.7", "json-bigint": "^1.0.0", "minimalistic-assert": "^1.0.1", - "pako": "^2.0.4" + "pako": "^2.0.4", + "url-join": "^4.0.1" }, "devDependencies": { "@babel/preset-env": "^7.15.8", @@ -33,6 +34,7 @@ "@types/json-bigint": "^1.0.1", "@types/minimalistic-assert": "^1.0.1", "@types/pako": "^1.0.2", + "@types/url-join": "^4.0.1", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "eslint": "^7.32.0", @@ -3231,6 +3233,12 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, + "node_modules/@types/url-join": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@types/url-join/-/url-join-4.0.1.tgz", + "integrity": "sha512-wDXw9LEEUHyV+7UWy7U315nrJGJ7p1BzaCxDpEoLr789Dk1WDVMMlf3iBfbG2F8NdWnYyFbtTxUn2ZNbm1Q4LQ==", + "dev": true + }, "node_modules/@types/yargs": { "version": "16.0.4", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", @@ -13587,9 +13595,7 @@ "node_modules/url-join": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz", - "integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==", - "dev": true, - "peer": true + "integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==" }, "node_modules/util-deprecate": { "version": "1.0.2", @@ -16318,6 +16324,12 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, + "@types/url-join": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@types/url-join/-/url-join-4.0.1.tgz", + "integrity": "sha512-wDXw9LEEUHyV+7UWy7U315nrJGJ7p1BzaCxDpEoLr789Dk1WDVMMlf3iBfbG2F8NdWnYyFbtTxUn2ZNbm1Q4LQ==", + "dev": true + }, "@types/yargs": { "version": "16.0.4", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", @@ -24104,9 +24116,7 @@ "url-join": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz", - "integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==", - "dev": true, - "peer": true + "integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==" }, "util-deprecate": { "version": "1.0.2", diff --git a/package.json b/package.json index c185ffa91..2fb9d4d9a 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@types/json-bigint": "^1.0.1", "@types/minimalistic-assert": "^1.0.1", "@types/pako": "^1.0.2", + "@types/url-join": "^4.0.1", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "eslint": "^7.32.0", @@ -65,7 +66,8 @@ "hash.js": "^1.1.7", "json-bigint": "^1.0.0", "minimalistic-assert": "^1.0.1", - "pako": "^2.0.4" + "pako": "^2.0.4", + "url-join": "^4.0.1" }, "lint-staged": { "*.ts": "eslint --cache --fix", diff --git a/src/provider/default.ts b/src/provider/default.ts index 624228d75..fdaed459a 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import urljoin from 'url-join'; import { AddTransactionResponse, @@ -49,15 +50,15 @@ export class Provider implements ProviderInterface { ? optionsOrProvider.baseUrl : Provider.getNetworkFromName(optionsOrProvider.network); this.baseUrl = baseUrl; - this.feederGatewayUrl = `${baseUrl}/feeder_gateway`; - this.gatewayUrl = `${baseUrl}/gateway`; + this.feederGatewayUrl = urljoin(baseUrl, 'feeder_gateway'); + this.gatewayUrl = urljoin(baseUrl, 'gateway'); } } protected static getNetworkFromName(name: NetworkName) { switch (name) { case 'mainnet-alpha': - return 'http://alpha-mainnet.starknet.io/'; + return 'https://alpha-mainnet.starknet.io'; case 'georli-alpha': default: return 'https://alpha4.starknet.io'; @@ -72,7 +73,7 @@ export class Provider implements ProviderInterface { */ public async getContractAddresses(): Promise { const { data } = await axios.get( - `${this.feederGatewayUrl}/get_contract_addresses` + urljoin(this.feederGatewayUrl, 'get_contract_addresses') ); return data; } @@ -91,7 +92,7 @@ export class Provider implements ProviderInterface { blockId?: number ): Promise { const { data } = await axios.post( - `${this.feederGatewayUrl}/call_contract?blockId=${blockId ?? 'null'}`, + urljoin(this.feederGatewayUrl, 'call_contract', `?blockId=${blockId ?? 'null'}`), { signature: [], calldata: [], @@ -111,7 +112,7 @@ export class Provider implements ProviderInterface { */ public async getBlock(blockId?: number): Promise { const { data } = await axios.get( - `${this.feederGatewayUrl}/get_block?blockId=${blockId ?? 'null'}` + urljoin(this.feederGatewayUrl, 'get_block', `?blockId=${blockId ?? 'null'}`) ); return data; } @@ -127,9 +128,11 @@ export class Provider implements ProviderInterface { */ public async getCode(contractAddress: string, blockId?: number): Promise { const { data } = await axios.get( - `${this.feederGatewayUrl}/get_code?contractAddress=${contractAddress}&blockId=${ - blockId ?? 'null' - }` + urljoin( + this.feederGatewayUrl, + 'get_code', + `?contractAddress=${contractAddress}&blockId=${blockId ?? 'null'}` + ) ); return data; } @@ -151,9 +154,11 @@ export class Provider implements ProviderInterface { blockId?: number ): Promise { const { data } = await axios.get( - `${ - this.feederGatewayUrl - }/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}` + urljoin( + this.feederGatewayUrl, + 'get_storage_at', + `?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}` + ) ); return data; } @@ -169,7 +174,11 @@ export class Provider implements ProviderInterface { public async getTransactionStatus(txHash: BigNumberish): Promise { const txHashBn = toBN(txHash); const { data } = await axios.get( - `${this.feederGatewayUrl}/get_transaction_status?transactionHash=${toHex(txHashBn)}` + urljoin( + this.feederGatewayUrl, + 'get_transaction_status', + `?transactionHash=${toHex(txHashBn)}` + ) ); return data; } @@ -185,7 +194,7 @@ export class Provider implements ProviderInterface { public async getTransaction(txHash: BigNumberish): Promise { const txHashBn = toBN(txHash); const { data } = await axios.get( - `${this.feederGatewayUrl}/get_transaction?transactionHash=${toHex(txHashBn)}` + urljoin(this.feederGatewayUrl, 'get_transaction', `?transactionHash=${toHex(txHashBn)}`) ); return data; } @@ -203,7 +212,7 @@ export class Provider implements ProviderInterface { const contract_address_salt = tx.type === 'DEPLOY' && toHex(toBN(tx.contract_address_salt)); const { data } = await axios.post( - `${this.gatewayUrl}/add_transaction`, + urljoin(this.gatewayUrl, 'add_transaction'), stringify({ ...tx, // the tx can contain BigInts, so we use our own `stringify` ...(Array.isArray(signature) && { signature }), // not needed on deploy tx