Skip to content

Commit

Permalink
Deleted redundant search parameters in API URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Aug 5, 2022
1 parent 279e801 commit 0c47bae
Show file tree
Hide file tree
Showing 41 changed files with 68 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .changeset/chilly-chefs-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moralisweb3/api-utils': patch
'@moralisweb3/evm-api': patch
'@moralisweb3/sol-api': patch
---

Deleted redundant search parameters in API URLs.
1 change: 1 addition & 0 deletions packages/apiUtils/src/resolvers/Endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Endpoint<ApiParams, Params, ApiResult, AdaptedResult, JSONResul
resultToJson: (result: AdaptedResult) => JSONResult;
parseParams: (params: Params) => ApiParams;
method?: EndpointMethod;
urlParams?: readonly (keyof Params)[];
bodyParams?: readonly (keyof Params)[];
bodyType?: EndpointBodyType;
name: string;
Expand Down
22 changes: 12 additions & 10 deletions packages/apiUtils/src/resolvers/EndpointParamsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import { Endpoint, EndpointBodyType } from './Endpoint';
export class EndpointParamsReader<ApiParams, Params, ApiResult, AdaptedResult, JSONResult> {
public constructor(private readonly endpoint: Endpoint<ApiParams, Params, ApiResult, AdaptedResult, JSONResult>) {}

protected isBodyParam = (param: string) => {
private isBodyParam(param: string): boolean {
if (this.endpoint.method === 'get') {
return false;
}
if (!this.endpoint.bodyParams || this.endpoint.bodyParams.length === 0) {
return false;
}
// @ts-ignore TODO: fix the param string cast from keyof
return this.endpoint.bodyParams.includes(param);
};
return this.endpoint.bodyParams.includes(param as keyof Params);
}

private isUrlParam(param: string): boolean {
return !!this.endpoint.urlParams && this.endpoint.urlParams.includes(param as keyof Params);
}

public getSearchParams(params: ApiParams) {
return Object.keys(params).reduce((result, key) => {
// @ts-ignore TODO: fix the ApiParams type, as it should extend object/record
if (!params[key] || this.isBodyParam(key)) {
const paramKey = key as keyof ApiParams;

if (!params[paramKey] || this.isBodyParam(key) || this.isUrlParam(key)) {
return result;
}

// @ts-ignore TODO: fix the ApiParams type, as it should extend object/record
return { ...result, [key]: params[key] };
return { ...result, [key]: params[paramKey] };
}, {});
}

Expand All @@ -36,8 +39,7 @@ export class EndpointParamsReader<ApiParams, Params, ApiResult, AdaptedResult, J
// @ts-ignore TODO: fix the ApiParams type, as it should extend object/record
return { ...result, [key]: params[key] };
}
// @ts-ignore TODO: fix the ApiParams type, as it should extend object/record
return params[key];
return result;
}, {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('PaginatedEndpointResolver', () => {
data: eventRawResult,
});
} else {
return Promise.reject('Invalid url');
return Promise.reject(`Invalid url: ${options.url}`);
}
});
});
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/controllers/RequestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class RequestController {

private constructor(private readonly logger: LoggerController) {}

public async request<Data, Response>(config: AxiosRequestConfig<Data>): Promise<Response> {
private async request<Data, Response>(config: AxiosRequestConfig<Data>): Promise<Response> {
this.logger.verbose('[RequestController] request started', {
url: config.url,
method: config.method,
Expand Down Expand Up @@ -72,7 +72,7 @@ export class RequestController {
}
}

public makeError(error: unknown): MoralisCoreError {
private makeError(error: unknown): MoralisCoreError {
if (axios.isAxiosError(error)) {
const axiosError = error as AxiosError;
return new MoralisCoreError({
Expand All @@ -95,50 +95,50 @@ export class RequestController {
});
}

public post<Response, Params extends Record<string, string>, Body extends Record<string, unknown>>(
public post<Response, SearchParams extends Record<string, string>, Body extends Record<string, unknown>>(
url: string,
params?: Params,
searchParams?: SearchParams,
body?: Body,
options?: RequestOptions,
abortSignal?: AbortController['signal'],
): Promise<Response> {
return this.request<Body, Response>({
url,
params: searchParams,
method: 'POST',
data: body,
params,
headers: options?.headers,
signal: abortSignal,
});
}

public put<Response, Params extends Record<string, string>, Body extends Record<string, unknown>>(
public put<Response, SearchParams extends Record<string, string>, Body extends Record<string, unknown>>(
url: string,
params?: Params,
searchParams?: SearchParams,
body?: Body,
options?: RequestOptions,
abortSignal?: AbortController['signal'],
): Promise<Response> {
return this.request<Body, Response>({
url,
params: searchParams,
method: 'PUT',
data: body,
params,
headers: options?.headers,
signal: abortSignal,
});
}

public async get<Response, Params extends Record<string, string>>(
public async get<Response, SearchParams extends Record<string, string>>(
url: string,
params?: Params,
searchParams?: SearchParams,
options?: RequestOptions,
abortSignal?: AbortController['signal'],
): Promise<Response> {
return this.request<unknown, Response>({
url,
params: searchParams,
method: 'GET',
params,
headers: options?.headers,
signal: abortSignal,
});
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/account/getNFTTransfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTTransfers = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getNFTTransfers',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/nft/transfers`,
apiToResult: (data: ApiResult) =>
data.result?.map((transfer) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/account/getNFTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTs = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getNFTs',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/nft`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((nft) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTsForContract = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getNFTsForContract',
urlParams: ['address', 'tokenAddress'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/nft/${params.tokenAddress}`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((nft) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/account/getNativeBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>> {
export const getNativeBalance = createEndpointFactory((core) =>
createEndpoint({
name: 'getNativeBalance',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/balance`,
apiToResult: (data: ApiResult) => ({
balance: EvmNative.create(data.balance, 'wei'),
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/account/getTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>> {
export const getTokenBalances = createEndpointFactory((core) =>
createEndpoint({
name: 'getTokenBalances',
urlParams: ['address'],
getUrl: (params: ApiParams) => `${BASE_URL}/${params.address}/erc20`,
apiToResult: (data: ApiResult, params: ApiParams) =>
data.map((token) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/account/getTokenTransfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getTokenTransfers = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getTokenTransfers',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/erc20/transfers`,
apiToResult: (data: ApiResult) =>
data.result?.map((transfer) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/account/getTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getTransactions = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getTransactions',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((transaction) =>
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/defi/getPairAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'token0_addre
export const getPairAddress = createEndpointFactory((core) =>
createEndpoint({
name: 'getPairAddress',
urlParams: ['token0Address', 'token1Address'],
getUrl: (params: Params) => `${BASE_URL}/${params.token0Address}/${params.token1Address}/pairAddress`,
apiToResult: (data: ApiResult, params: Params) => ({
// ApiResult types generated all come as undefined which should not be the case TODO:
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/defi/getPairReserves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ApiResult = {
export const getPairReserves = createEndpointFactory((core) =>
createEndpoint({
name: 'getPairReserves',
urlParams: ['pairAddress'],
getUrl: (params: Params) => `${BASE_URL}/${params.pairAddress}/reserves`,
apiToResult: (data: ApiResult) => data,
resultToJson: (data) => data,
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/native/getBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const apiToResult = (core: MoralisCore, apiData: ApiResult, params: Params) => {
export const getBlock = createEndpointFactory((core) =>
createEndpoint({
name: 'getBlock',
urlParams: ['blockNumberOrHash'],
getUrl: (params: Params) => `${BASE_URL}/block/${params.blockNumberOrHash}`,
apiToResult: (result: ApiResult, params: Params) => {
return apiToResult(core, result, params);
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/native/getContractEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getContractEvents = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getContractEvents',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/events`,
// TODO: remove PaginatedResponse when api squad make swagger update
apiToResult: (data: PaginatedResult<ApiResult>) =>
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/native/getLogsByAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getLogsByAddress = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getLogsByAddress',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/logs`,
apiToResult: (data: ApiResult) =>
data.result?.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTTransfersByBlock = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getNFTTransfersByBlock',
urlParams: ['blockNumberOrHash'],
getUrl: (params: Params) => `${BASE_URL}/block/${params.blockNumberOrHash}/nft/transfers`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((transfer) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/native/getTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain'>> {
export const getTransaction = createEndpointFactory((core) =>
createEndpoint({
name: 'getTransaction',
urlParams: ['transactionHash'],
getUrl: (params: Params) => `${BASE_URL}/transaction/${params.transactionHash}`,
apiToResult: (data: ApiResult, params: Params) => {
const transactionReciept = EvmTransactionReceipt.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const runContractFunction = createEndpointFactory((core) =>
createEndpoint({
name: 'runContractFunction',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/${params.address}/function`,
apiToResult: (data: ApiResult) => {
return data;
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/resolve/resolveAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'address'>> {
export const resolveAddress = createEndpointFactory((core) =>
createEndpoint({
name: 'resolveAddress',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/resolve/${params.address}/reverse`,
apiToResult: (data: ApiResult) => data,
resultToJson: (data) => data,
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/resolve/resolveDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const resolveDomain = createEndpointFactory((core) =>
createEndpoint({
name: 'resolveDomain',
urlParams: ['domain'],
getUrl: (params: ApiParams) => `${BASE_URL}/resolve/${params.domain}`,
apiToResult: (data: ApiResult) => ({
address: EvmAddress.create(data.address, core),
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getAllTokenIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface ApiResult extends Omit<GeneratedApiResult, 'result'> {
export const getAllTokenIds = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getAllTokenIds',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((nft) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getContractNFTTransfers = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getContractNFTTransfers',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/transfers`,
apiToResult: (data: ApiResult) =>
data.result?.map((transfer) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getNFTLowestPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTLowestPrice = createEndpointFactory((core) =>
createEndpoint({
name: 'getNFTLowestPrice',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/lowestprice`,
apiToResult: (data: ApiResult) => ({
...toCamelCase(data),
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getNFTMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Params {
export const getNFTMetadata = createEndpointFactory((core) =>
createEndpoint({
name: 'getNFTMetadata',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/metadata`,
apiToResult: (data: ApiResult) => ({
...toCamelCase(data),
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getNFTOwners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTOwners = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getNFTOwners',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/owners`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((nft) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getNFTTrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getNFTTrades = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getNFTTrades',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/trades`,
apiToResult: (data: ApiResult) =>
data.result?.map((trade) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getTokenAddressTransfers = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getTokenAddressTransfers',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/erc20/${params.address}/transfers`,
apiToResult: (data: ApiResult) =>
data.result?.map((transfer) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getTokenAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'owner_addres
export const getTokenAllowance = createEndpointFactory((core) =>
createEndpoint({
name: 'getTokenAllowance',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/erc20/${params.address}/allowance`,
apiToResult: (data: ApiResult) => ({
allowance: BigNumber.create(data.allowance),
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getTokenIdMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ApiResult extends GeneratedApiResult {
export const getTokenIdMetadata = createEndpointFactory((core) =>
createEndpoint({
name: 'getTokenIdMetadata',
urlParams: ['address', 'tokenId'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/${params.tokenId}`,
apiToResult: (data: ApiResult, params: Params) => ({
token: new EvmNFT({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getTokenIdOwners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getTokenIdOwners = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getTokenIdOwners',
urlParams: ['address', 'tokenId'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/${params.tokenId}/owners`,
apiToResult: (data: ApiResult, params: Params) =>
data.result?.map((nft) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/getTokenPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>> {
export const getTokenPrice = createEndpointFactory((core) =>
createEndpoint({
name: 'getTokenPrice',
urlParams: ['address'],
getUrl: (params: ApiParams) => `${BASE_URL}/erc20/${params.address}/price`,
apiToResult: (data: ApiResult) => ({
...toCamelCase(data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ApiResult = operations[operation]['responses']['200']['content']['applicati
export const getWalletTokenIdTransfers = createPaginatedEndpointFactory((core) =>
createPaginatedEndpoint({
name: 'getWalletTokenIdTransfers',
urlParams: ['address', 'tokenId'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/${params.tokenId}/transfers`,
apiToResult: (data: ApiResult) =>
data.result?.map((transfer) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/reSyncMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>> {
export const reSyncMetadata = createEndpointFactory((core) =>
createEndpoint({
name: 'reSyncMetadata',
urlParams: ['address', 'tokenId'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/${params.tokenId}/metadata/resync`,
apiToResult: (data: ApiResult) => ({
...data,
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/resolvers/token/syncNFTContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>> {
export const syncNFTContract = createEndpointFactory((core) =>
createEndpoint({
name: 'syncNFTContract',
urlParams: ['address'],
getUrl: (params: Params) => `${BASE_URL}/nft/${params.address}/sync`,
apiToResult: (data: ApiResult) => ({
success: true,
Expand Down
Loading

0 comments on commit 0c47bae

Please sign in to comment.