Skip to content

Commit

Permalink
response adapter types. (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz authored Nov 22, 2022
1 parent 5a99a63 commit 117e627
Show file tree
Hide file tree
Showing 51 changed files with 408 additions and 159 deletions.
11 changes: 4 additions & 7 deletions packages/apiUtils/scripts/generate-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ for (const groupName of uniqueGroupNames) {
for (const operation of package.operations.filter((o) => o.groupName === groupName)) {
const operationVarName = `${operation.name}Operation`;
const requestClassName = `${capitalizeFirst(operation.name)}Request`;
const responseClassName = `${capitalizeFirst(operation.name)}Response`;
const jsonResponseClassName = `${capitalizeFirst(operation.name)}JSONResponse`;
const responseClassName = `${capitalizeFirst(operation.name)}ResponseAdapter`;

const omitRequest = !operation?.urlPathParamNames && !operation.urlSearchParamNames && !operation.bodyParamNames;

Expand All @@ -41,15 +40,15 @@ for (const groupName of uniqueGroupNames) {
switch (determineOperationType(operation)) {
case 'nonNullable':
resolverClassName = 'OperationResolver';
returnType = `ResponseAdapter<${responseClassName}, ${jsonResponseClassName}>`;
returnType = responseClassName;
break;
case 'nullable':
resolverClassName = 'NullableOperationResolver';
returnType = `ResponseAdapter<${responseClassName}, ${jsonResponseClassName}> | null`;
returnType = `${responseClassName} | null`;
break;
case 'paginated':
resolverClassName = 'PaginatedOperationResolver';
returnType = `PaginatedResponseAdapter<${responseClassName}, ${jsonResponseClassName}['result']>`;
returnType = responseClassName;
break;
}

Expand All @@ -58,9 +57,7 @@ for (const groupName of uniqueGroupNames) {
sourcePackageImports.add(requestClassName);
}
sourcePackageImports.add(responseClassName);
sourcePackageImports.add(jsonResponseClassName);
apiUtilsPackageImports.add(resolverClassName);
corePackageImports.add(returnType.split('<')[0]);

bodyOutput += ` ${operation.name}: (${
omitRequest ? '' : `request: ${requestClassName}`
Expand Down
13 changes: 4 additions & 9 deletions packages/client/apiUtils/scripts/generate-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const { determineOperationType } = require('@moralisweb3/common-core');

const uniqueGroupNames = new Set(package.operations.map((o) => o.groupName));
const sourcePackageImports = new Set();
const corePackageImports = new Set();
let bodyOutput = ``;

for (const groupName of uniqueGroupNames) {
Expand All @@ -30,31 +29,28 @@ for (const groupName of uniqueGroupNames) {
for (const operation of package.operations.filter((o) => o.groupName === groupName)) {
const operationVarName = `${operation.name}Operation`;
const requestClassName = `${capitalizeFirst(operation.name)}Request`;
const responseClassName = `${capitalizeFirst(operation.name)}Response`;
const jsonResponseClassName = `${capitalizeFirst(operation.name)}JSONResponse`;
const responseClassName = `${capitalizeFirst(operation.name)}ResponseAdapter`;

let methodName;
let returnType;
switch (determineOperationType(operation)) {
case 'nonNullable':
methodName = 'handle';
returnType = `ResponseAdapter<${responseClassName}, ${jsonResponseClassName}>`;
returnType = responseClassName;
break;
case 'nullable':
methodName = 'handleNullable';
returnType = `ResponseAdapter<${responseClassName}, ${jsonResponseClassName}> | null`;
returnType = `${responseClassName} | null`;
break;
case 'paginated':
methodName = 'handlePaginated';
returnType = `PaginatedResponseAdapter<${responseClassName}, ${jsonResponseClassName}['result']>`;
returnType = responseClassName;
break;
}

sourcePackageImports.add(operationVarName);
sourcePackageImports.add(requestClassName);
sourcePackageImports.add(responseClassName);
sourcePackageImports.add(jsonResponseClassName);
corePackageImports.add(returnType.split('<')[0]);

bodyOutput += ` ${operation.name}: (request: ${requestClassName}): Promise<${returnType}> => {
return this.requestHandler.${methodName}(request, ${operationVarName});
Expand All @@ -72,7 +68,6 @@ const output = `
import { ${[...sourcePackageImports].join(', ')} } from '${sourcePackageName}';
import { ClientRequestHandler } from '@moralisweb3/client-api-utils';
import { ${[...corePackageImports].join(', ')} } from '@moralisweb3/common-core';
export class ${outputClassName} {
public constructor(
Expand Down
81 changes: 40 additions & 41 deletions packages/client/evmApi/src/generated/ClientEvmApi.ts

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions packages/client/solApi/src/generated/ClientSolApi.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@

// CAUTION: This file is automatically generated. Do not edit it manually!

import { getBalanceOperation, GetBalanceRequest, GetBalanceResponse, GetBalanceJSONResponse, getNFTsOperation, GetNFTsRequest, GetNFTsResponse, GetNFTsJSONResponse, getPortfolioOperation, GetPortfolioRequest, GetPortfolioResponse, GetPortfolioJSONResponse, getSPLOperation, GetSPLRequest, GetSPLResponse, GetSPLJSONResponse, getNFTMetadataOperation, GetNFTMetadataRequest, GetNFTMetadataResponse, GetNFTMetadataJSONResponse, getTokenPriceOperation, GetTokenPriceRequest, GetTokenPriceResponse, GetTokenPriceJSONResponse } from '@moralisweb3/common-sol-utils';
import { getBalanceOperation, GetBalanceRequest, GetBalanceResponseAdapter, getNFTsOperation, GetNFTsRequest, GetNFTsResponseAdapter, getPortfolioOperation, GetPortfolioRequest, GetPortfolioResponseAdapter, getSPLOperation, GetSPLRequest, GetSPLResponseAdapter, getNFTMetadataOperation, GetNFTMetadataRequest, GetNFTMetadataResponseAdapter, getTokenPriceOperation, GetTokenPriceRequest, GetTokenPriceResponseAdapter } from '@moralisweb3/common-sol-utils';
import { ClientRequestHandler } from '@moralisweb3/client-api-utils';
import { ResponseAdapter } from '@moralisweb3/common-core';

export class ClientSolApi {
public constructor(
private readonly requestHandler: ClientRequestHandler) {
}

public readonly account = {
getBalance: (request: GetBalanceRequest): Promise<ResponseAdapter<GetBalanceResponse, GetBalanceJSONResponse>> => {
getBalance: (request: GetBalanceRequest): Promise<GetBalanceResponseAdapter> => {
return this.requestHandler.handle(request, getBalanceOperation);
},
getNFTs: (request: GetNFTsRequest): Promise<ResponseAdapter<GetNFTsResponse, GetNFTsJSONResponse>> => {
getNFTs: (request: GetNFTsRequest): Promise<GetNFTsResponseAdapter> => {
return this.requestHandler.handle(request, getNFTsOperation);
},
getPortfolio: (request: GetPortfolioRequest): Promise<ResponseAdapter<GetPortfolioResponse, GetPortfolioJSONResponse>> => {
getPortfolio: (request: GetPortfolioRequest): Promise<GetPortfolioResponseAdapter> => {
return this.requestHandler.handle(request, getPortfolioOperation);
},
getSPL: (request: GetSPLRequest): Promise<ResponseAdapter<GetSPLResponse, GetSPLJSONResponse>> => {
getSPL: (request: GetSPLRequest): Promise<GetSPLResponseAdapter> => {
return this.requestHandler.handle(request, getSPLOperation);
},

};

public readonly nft = {
getNFTMetadata: (request: GetNFTMetadataRequest): Promise<ResponseAdapter<GetNFTMetadataResponse, GetNFTMetadataJSONResponse>> => {
getNFTMetadata: (request: GetNFTMetadataRequest): Promise<GetNFTMetadataResponseAdapter> => {
return this.requestHandler.handle(request, getNFTMetadataOperation);
},

};

public readonly token = {
getTokenPrice: (request: GetTokenPriceRequest): Promise<ResponseAdapter<GetTokenPriceResponse, GetTokenPriceJSONResponse>> => {
getTokenPrice: (request: GetTokenPriceRequest): Promise<GetTokenPriceResponseAdapter> => {
return this.requestHandler.handle(request, getTokenPriceOperation);
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, maybe } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, maybe, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNative } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +24,9 @@ export type GetNativeBalanceJSONResponse = SuccessResponse;

export type GetNativeBalanceResponse = ReturnType<typeof deserializeResponse>;

export interface GetNativeBalanceResponseAdapter
extends ResponseAdapter<GetNativeBalanceResponse, GetNativeBalanceJSONResponse> {}

export const getNativeBalanceOperation: Operation<
GetNativeBalanceRequest,
GetNativeBalanceJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, toCamelCase } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, toCamelCase, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmBlock, EvmChain, EvmChainish, EvmTransaction, EvmTransactionLog, LogTopic } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -23,6 +23,8 @@ export type GetBlockJSONResponse = SuccessResponse;

export type GetBlockResponse = ReturnType<typeof deserializeResponse>;

export interface GetBlockResponseAdapter extends ResponseAdapter<GetBlockResponse, GetBlockJSONResponse> {}

export const getBlockOperation: Operation<
GetBlockRequest,
GetBlockJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, DateInput } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, DateInput, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +24,9 @@ export type GetDateToBlockJSONResponse = SuccessResponse;

export type GetDateToBlockResponse = ReturnType<typeof deserializeResponse>;

export interface GetDateToBlockResponseAdapter
extends ResponseAdapter<GetDateToBlockResponse, GetDateToBlockJSONResponse> {}

export const getDateToBlockOperation: Operation<
GetDateToBlockRequest,
GetDateToBlockJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, DateInput } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, DateInput, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, Erc20Token } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand Down Expand Up @@ -27,6 +27,9 @@ export type GetPairAddressJSONResponse = SuccessResponse;

export type GetPairAddressResponse = ReturnType<typeof deserializeResponse>;

export interface GetPairAddressResponseAdapter
extends ResponseAdapter<GetPairAddressResponse, GetPairAddressJSONResponse> {}

export const getPairAddressOperation: Operation<
GetPairAddressRequest,
GetPairAddressJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, DateInput } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, DateInput, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -25,6 +25,9 @@ export type GetPairReservesJSONResponse = SuccessResponse;

export type GetPairReservesResponse = ReturnType<typeof deserializeResponse>;

export interface GetPairReservesResponseAdapter
extends ResponseAdapter<GetPairReservesResponse, GetPairReservesJSONResponse> {}

export const getPairReservesOperation: Operation<
GetPairReservesRequest,
GetPairReservesJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Core, Camelize, PaginatedOperation, maybe, DateInput } from '@moralisweb3/common-core';
import {
Core,
Camelize,
PaginatedOperation,
maybe,
DateInput,
PaginatedResponseAdapter,
} from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmEvent } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand Down Expand Up @@ -28,6 +35,9 @@ export type GetContractEventsJSONResponse = SuccessResponse;

export type GetContractEventsResponse = ReturnType<typeof deserializeResponse>;

export interface GetContractEventsResponseAdapter
extends PaginatedResponseAdapter<GetContractEventsResponse, GetContractEventsJSONResponse['result']> {}

export const getContractEventsOperation: PaginatedOperation<
GetContractEventsRequest,
GetContractEventsJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Core, Camelize, PaginatedOperation, maybe, DateInput, toCamelCase } from '@moralisweb3/common-core';
import {
Core,
Camelize,
PaginatedOperation,
maybe,
DateInput,
toCamelCase,
PaginatedResponseAdapter,
} from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmTransactionLog, LogTopic } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand Down Expand Up @@ -27,6 +35,9 @@ export type GetContractLogsJSONResponse = SuccessResponse;

export type GetContractLogsResponse = ReturnType<typeof deserializeResponse>;

export interface GetContractLogsResponseAdapter
extends PaginatedResponseAdapter<GetContractLogsResponse, GetContractLogsJSONResponse['result']> {}

export const getContractLogsOperation: PaginatedOperation<
GetContractLogsRequest,
GetContractLogsJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Operation } from '@moralisweb3/common-core';
import { Operation, ResponseAdapter } from '@moralisweb3/common-core';

import { operations } from '../openapi';

Expand All @@ -22,6 +22,8 @@ export type UploadFolderJSONResponse = SuccessResponse;

export type UploadFolderResponse = ReturnType<typeof deserializeResponse>;

export interface UploadFolderResponseAdapter extends ResponseAdapter<UploadFolderResponse, UploadFolderJSONResponse> {}

export const uploadFolderOperation: Operation<
UploadFolderRequest,
UploadFolderJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Core, Camelize, PaginatedOperation, toCamelCase, maybe } from '@moralisweb3/common-core';
import {
Core,
Camelize,
PaginatedOperation,
toCamelCase,
maybe,
PaginatedResponseAdapter,
} from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNft } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +31,9 @@ export type GetContractNFTsJSONResponse = SuccessResponse;

export type GetContractNFTsResponse = ReturnType<typeof deserializeResponse>;

export interface GetContractNFTsResponseAdapter
extends PaginatedResponseAdapter<GetContractNFTsResponse, GetContractNFTsJSONResponse['result']> {}

export const getContractNFTsOperation: PaginatedOperation<
GetContractNFTsRequest,
GetContractNFTsJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, toCamelCase, maybe } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, toCamelCase, maybe, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNftMetadata } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +24,9 @@ export type GetNFTContractMetadataJSONResponse = SuccessResponse;

export type GetNFTContractMetadataResponse = ReturnType<typeof deserializeResponse>;

export interface GetNFTContractMetadataResponseAdapter
extends ResponseAdapter<GetNFTContractMetadataResponse, GetNFTContractMetadataJSONResponse> {}

export const getNFTContractMetadataOperation: Operation<
GetNFTContractMetadataRequest,
GetNFTContractMetadataJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Core, Camelize, PaginatedOperation, toCamelCase, maybe } from '@moralisweb3/common-core';
import {
Core,
Camelize,
PaginatedOperation,
toCamelCase,
maybe,
PaginatedResponseAdapter,
} from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNftTransfer, EvmNative } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +31,9 @@ export type GetNFTContractTransfersJSONResponse = SuccessResponse;

export type GetNFTContractTransfersResponse = ReturnType<typeof deserializeResponse>;

export interface GetNFTContractTransfersResponseAdapter
extends PaginatedResponseAdapter<GetNFTContractTransfersResponse, GetNFTContractTransfersJSONResponse['result']> {}

export const getNFTContractTransfersOperation: PaginatedOperation<
GetNFTContractTransfersRequest,
GetNFTContractTransfersJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, toCamelCase, maybe } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, toCamelCase, maybe, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNftTrade, EvmNative } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +24,9 @@ export type GetNFTLowestPriceJSONResponse = SuccessResponse;

export type GetNFTLowestPriceResponse = ReturnType<typeof deserializeResponse>;

export interface GetNFTLowestPriceResponseAdapter
extends ResponseAdapter<GetNFTLowestPriceResponse, GetNFTLowestPriceJSONResponse> {}

export const getNFTLowestPriceOperation: Operation<
GetNFTLowestPriceRequest,
GetNFTLowestPriceJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core, Camelize, Operation, toCamelCase } from '@moralisweb3/common-core';
import { Core, Camelize, Operation, toCamelCase, ResponseAdapter } from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNft } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +24,9 @@ export type GetNFTMetadataJSONResponse = SuccessResponse;

export type GetNFTMetadataResponse = ReturnType<typeof deserializeResponse>;

export interface GetNFTMetadataResponseAdapter
extends ResponseAdapter<GetNFTMetadataResponse, GetNFTMetadataJSONResponse> {}

export const getNFTMetadataOperation: Operation<
GetNFTMetadataRequest,
GetNFTMetadataJSONRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Core, Camelize, PaginatedOperation, toCamelCase, maybe } from '@moralisweb3/common-core';
import {
Core,
Camelize,
PaginatedOperation,
toCamelCase,
maybe,
PaginatedResponseAdapter,
} from '@moralisweb3/common-core';
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, EvmNft } from '../../dataTypes';
import { EvmChainResolver } from '../../EvmChainResolver';
import { operations } from '../openapi';
Expand All @@ -24,6 +31,9 @@ export type GetNFTOwnersJSONResponse = SuccessResponse;

export type GetNFTOwnersResponse = ReturnType<typeof deserializeResponse>;

export interface GetNFTOwnersResponseAdapter
extends PaginatedResponseAdapter<GetNFTOwnersResponse, GetNFTOwnersJSONResponse['result']> {}

export const getNFTOwnersOperation: PaginatedOperation<
GetNFTOwnersRequest,
GetNFTOwnersJSONRequest,
Expand Down
Loading

1 comment on commit 117e627

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage

Title Lines Statements Branches Functions
api-utils Coverage: 24%
25.68% (47/183) 19.56% (9/46) 22.85% (8/35)
auth Coverage: 94%
95.38% (124/130) 81.81% (18/22) 95.45% (42/44)
evm-api Coverage: 97%
97.46% (77/79) 66.66% (6/9) 95.74% (45/47)
common-evm-utils Coverage: 64%
65% (938/1443) 19.93% (123/617) 35.89% (201/560)
sol-api Coverage: 96%
96.66% (29/30) 66.66% (6/9) 91.66% (11/12)
common-sol-utils Coverage: 74%
73.77% (135/183) 60% (12/20) 65.67% (44/67)
common-streams-utils Coverage: 95%
95.6% (674/705) 97.93% (190/194) 100% (244/244)
streams Coverage: 87%
87.64% (525/599) 68.26% (71/104) 84.52% (142/168)

Please sign in to comment.