Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GMS-1127] chore: Export api types from blockchain data package #971

Merged
merged 15 commits into from
Oct 12, 2023
Merged
18 changes: 12 additions & 6 deletions packages/blockchain-data/sample-app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Environment, ImmutableConfiguration } from '@imtbl/config';
import {
BlockchainData,
BlockchainDataModuleConfiguration,
ChainsApi,
} from '@imtbl/blockchain-data';
import { PageLayout } from '@/components/PageLayout';
import { capitalizeFirstLetter } from '@/utils';
Expand Down Expand Up @@ -405,7 +406,9 @@ const endpointDomains = {
};

export default function Home() {
const [response, setResponse] = useState('');
const [response, setResponse] = useState<ChainsApi.ListChainsResult | null>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

example usage

null,
);

useEffect(() => {
async function getData() {
Expand All @@ -425,8 +428,9 @@ export default function Home() {
const client = new BlockchainData(config);

try {
const response = await client.listChains();
setResponse(response.result);
const request: ChainsApi.ListChainsRequest = {};
const response = await client.listChains(request);
setResponse(response);
} catch (error) {
console.error(error);
}
Expand Down Expand Up @@ -466,9 +470,11 @@ export default function Home() {
})}
</div>
<div className="flex-1 bg-gray w-full h-full">
<pre className="p-4 bg-neutral-800/50 rounded-lg">
{JSON.stringify(response, null, 2)}
</pre>
{response !== null && (
<pre className="p-4 bg-neutral-800/50 rounded-lg">
{JSON.stringify(response.result, null, 2)}
</pre>
)}
</div>
</div>
</PageLayout>
Expand Down
18 changes: 17 additions & 1 deletion packages/blockchain-data/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { mr } from '@imtbl/generated-clients';
import {
mr,
ActivitiesApi,
ChainsApi,
CollectionsApi,
MetadataApi,
NFTOwnersApi,
NFTsApi,
TokensApi,
} from '@imtbl/generated-clients';
import { APIError } from './types/errors';
import { BlockchainData } from './blockchain-data';
import { BlockchainDataModuleConfiguration } from './config';

type ActivityType = mr.ActivityType;

export {
ActivitiesApi,
ChainsApi,
CollectionsApi,
MetadataApi,
NFTOwnersApi,
NFTsApi,
TokensApi,
APIError,
BlockchainData,
BlockchainDataModuleConfiguration,
Expand Down
12 changes: 6 additions & 6 deletions packages/internal/generated-clients/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ generate-imx-openapi: get-imx-openapi generate-imx-api-client
.PHONY: generate-mr-openapi
generate-mr-openapi: get-mr-openapi generate-mr-api-client

# Note: We generate clients with sandbox spec only as Prod and Sandbox are maintained to be same.
# Note: We generate clients with sandbox spec only as Prod and Sandbox are maintained to be same.
#. Prod url is passed in via config to talk to prod API.
.PHONY: get-imx-openapi
get-imx-openapi:
Expand All @@ -28,15 +28,15 @@ generate-imx-api-client:
docker run --rm -v $(shell pwd):/app openapitools/openapi-generator-cli:v6.2.1 generate \
-i ./app/src/imx-openapi.json \
-g typescript-axios \
--additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true,withSeparateModelsAndApi=true,modelPackage=models,apiPackage=domain,useSingleRequestParameter=true \
-o /app/src/imx
-o /app/src/imx \
-c /app/imx.config.json

.PHONY: generate-mr-api-client
generate-mr-api-client:
rm -rf src/multi-rollup && \
mkdir src/multi-rollup && \
docker run --rm -v $(shell pwd):/app openapitools/openapi-generator-cli:v6.2.1 generate \
docker run --rm -v $(shell pwd):/app openapitools/openapi-generator-cli:v7.0.1 generate \
-i ./app/src/mr-openapi.json \
-g typescript-axios \
--additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true,withSeparateModelsAndApi=true,modelPackage=models,apiPackage=domain,useSingleRequestParameter=true \
-o /app/src/multi-rollup
-o /app/src/multi-rollup \
-c /app/mr.config.json
9 changes: 9 additions & 0 deletions packages/internal/generated-clients/imx.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"supportsES6": true,
"npmVersion": "6.9.0",
"typescriptThreePlus": true,
"withSeparateModelsAndApi": true,
"modelPackage": "models",
"apiPackage": "domain",
"useSingleRequestParameter": true
}
16 changes: 16 additions & 0 deletions packages/internal/generated-clients/mr.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"supportsES6": true,
"npmVersion": "6.9.0",
"typescriptThreePlus": true,
"withSeparateModelsAndApi": true,
"modelPackage": "models",
"apiPackage": "domain",
"useSingleRequestParameter": true,
"templateDir": "app/src/templates",
"files": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here is where the custom generators are allowed to be configured, we are doing this for mr only at the moment, hence the seperate config files

"api-types.mustache": {
"templateType": "API",
"destinationFilename": "-types.ts"
}
}
}
11 changes: 11 additions & 0 deletions packages/internal/generated-clients/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
export * as imx from './imx';
export * as mr from './multi-rollup';

export * as ActivitiesApi from './multi-rollup/domain/activities-api-types';
Copy link
Contributor Author

@allan-almeida allan-almeida Oct 11, 2023

Choose a reason for hiding this comment

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

I experimented with auto-generating a combined namespace for all these types, but it proved to be a hinderance later on when trying to re-export selected api types from the public blockchain data package. For now we are exporting all the api domains, and choosing to re-export only relevant ones from blockchain data (not passport or orders api)

export * as ChainsApi from './multi-rollup/domain/chains-api-types';
export * as CollectionsApi from './multi-rollup/domain/collections-api-types';
export * as MetadataApi from './multi-rollup/domain/metadata-api-types';
export * as NFTOwnersApi from './multi-rollup/domain/nft-owners-api-types';
export * as NFTsApi from './multi-rollup/domain/nfts-api-types';
export * as OrdersApi from './multi-rollup/domain/orders-api-types';
export * as PassportApi from './multi-rollup/domain/passport-api-types';
export * as TokensApi from './multi-rollup/domain/tokens-api-types';

export { ImxApiClients } from './imx-api-clients';
export { MultiRollupApiClients } from './mr-api-clients';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ api.ts
base.ts
common.ts
configuration.ts
domain/activities-api-types.ts
domain/activities-api.ts
domain/chains-api-types.ts
domain/chains-api.ts
domain/collections-api-types.ts
domain/collections-api.ts
domain/metadata-api-types.ts
domain/metadata-api.ts
domain/nft-owners-api-types.ts
domain/nft-owners-api.ts
domain/nfts-api-types.ts
domain/nfts-api.ts
domain/orders-api-types.ts
domain/orders-api.ts
domain/passport-api-types.ts
domain/passport-api.ts
domain/tokens-api-types.ts
domain/tokens-api.ts
git_push.sh
index.ts
Expand All @@ -24,19 +33,12 @@ models/activity-nft.ts
models/activity-token.ts
models/activity-type.ts
models/activity.ts
models/apierror400-all-of.ts
models/apierror400.ts
models/apierror401-all-of.ts
models/apierror401.ts
models/apierror403-all-of.ts
models/apierror403.ts
models/apierror404-all-of.ts
models/apierror404.ts
models/apierror429-all-of.ts
models/apierror429.ts
models/apierror500-all-of.ts
models/apierror500.ts
models/apierror501-all-of.ts
models/apierror501.ts
models/basic-apierror.ts
models/blockchain-metadata.ts
Expand All @@ -45,7 +47,6 @@ models/cancel-orders-request-body.ts
models/cancel-orders-result-data.ts
models/cancel-orders-result.ts
models/cancelled-order-status.ts
models/chain-with-details-all-of.ts
models/chain-with-details.ts
models/chain.ts
models/collection-contract-type.ts
Expand Down Expand Up @@ -107,10 +108,8 @@ models/protocol-data.ts
models/refresh-collection-metadata-request.ts
models/refresh-collection-metadata-result.ts
models/refresh-metadata-by-id.ts
models/refresh-metadata-by-idall-of.ts
models/refresh-metadata-by-idrequest.ts
models/refresh-metadata-by-token-id.ts
models/refresh-metadata-by-token-idall-of.ts
models/refresh-nftmetadata-by-token-idrequest.ts
models/refreshable-nftattributes.ts
models/sale-fee.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.2.1
7.0.1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was required to upgrade the generator library version in order to resolve compile issues with the generated types. I went through the consuming packages of the generated clients internal package in the SDK and checked they all still compiled

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
*/


import { Configuration } from "./configuration";
import type { Configuration } from './configuration';
// Some imports not used depending on template conditions
// @ts-ignore
import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig } from 'axios';
import type { AxiosPromise, AxiosInstance, AxiosRequestConfig } from 'axios';
import globalAxios from 'axios';

export const BASE_PATH = "https://api.sandbox.immutable.com".replace(/\/+$/, "");

Expand Down Expand Up @@ -64,8 +65,8 @@ export class BaseAPI {
* @extends {Error}
*/
export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
super(msg);
this.name = "RequiredError"
}
}
10 changes: 6 additions & 4 deletions packages/internal/generated-clients/src/multi-rollup/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
*/


import { Configuration } from "./configuration";
import { RequiredError, RequestArgs } from "./base";
import { AxiosInstance, AxiosResponse } from 'axios';
import type { Configuration } from "./configuration";
import type { RequestArgs } from "./base";
import type { AxiosInstance, AxiosResponse } from 'axios';
import { RequiredError } from "./base";

/**
*
Expand Down Expand Up @@ -84,6 +85,7 @@ export const setOAuthToObject = async function (object: any, name: string, scope
}

function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: any, key: string = ""): void {
if (parameter == null) return;
if (typeof parameter === "object") {
if (Array.isArray(parameter)) {
(parameter as any[]).forEach(item => setFlattenedQueryParams(urlSearchParams, item, key));
Expand Down Expand Up @@ -142,7 +144,7 @@ export const toPathString = function (url: URL) {
*/
export const createRequestFunction = function (axiosArgs: RequestArgs, globalAxios: AxiosInstance, BASE_PATH: string, configuration?: Configuration) {
return <T = unknown, R = AxiosResponse<T>>(axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {...axiosArgs.options, url: (configuration?.basePath || basePath) + axiosArgs.url};
const axiosRequestArgs = {...axiosArgs.options, url: (configuration?.basePath || axios.defaults.baseURL || basePath) + axiosArgs.url};
return axios.request<T, R>(axiosRequestArgs);
};
}
Loading
Loading