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

feat: introduce distinct browser and node clients for packages/api #24

Merged
merged 10 commits into from
Sep 7, 2023
Merged
6 changes: 3 additions & 3 deletions apps/maestro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
"@vercel/postgres": "^0.4.1",
"@web3modal/ethereum": "^2.7.1",
"@web3modal/react": "^2.7.1",
"drizzle-orm": "^0.28.5",
"drizzle-orm": "^0.28.6",
"eslint": "^8.48.0",
"eslint-config-next": "^13.4.19",
"ky": "^0.33.3",
"ky": "^1.0.1",
"logrocket": "^4.0.4",
"logrocket-react": "^5.0.1",
"lucide-react": "^0.265.0",
Expand All @@ -69,7 +69,7 @@
"tailwind-styled-components": "^2.2.0",
"trpc-openapi": "^1.2.0",
"typescript": "^5.2.2",
"viem": "~1.10.3",
"viem": "~1.10.4",
"wagmi": "^1.4.1",
"zod": "^3.22.2"
},
Expand Down
4 changes: 2 additions & 2 deletions apps/maestro/src/services/axelarscan/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAxelarscanClient } from "@axelarjs/api/axelarscan";
import { createAxelarscanBrowserClient } from "@axelarjs/api/axelarscan/browser";

export default createAxelarscanClient({
export default createAxelarscanBrowserClient({
prefixUrl: String(process.env.NEXT_PUBLIC_EXPLORER_API_URL),
});
4 changes: 2 additions & 2 deletions apps/maestro/src/services/gmp/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createGMPClient } from "@axelarjs/api/gmp";
import { createGMPBrowserClient } from "@axelarjs/api/gmp/browser";

export default createGMPClient({
export default createGMPBrowserClient({
prefixUrl: String(process.env.NEXT_PUBLIC_GMP_API_URL),
});
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@
"resolutions": {
"@types/react": "18.2.16",
"vite": "4.4.9"
},
"pnpm": {
"overrides": {
"protobufjs@>=6.10.0 <6.11.4": ">=6.11.4",
"@adobe/css-tools@<4.3.1": ">=4.3.1"
}
}
}
6 changes: 3 additions & 3 deletions packages/api/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
node_modules/
build/
axelarscan/
gmp/
axelar-query/
/axelarscan/
/gmp/
/axelar-query/
*.d.ts
*.js
.turbo/
27 changes: 17 additions & 10 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@
"import": "./build/module/axelar-query/index.js",
"require": "./build/commonjs/axelar-query/index.js"
},
"./gmp/*": {
"import": "./build/module/gmp/*.js",
"require": "./build/commonjs//gmp/*.js"
},
"./axelarscan/*": {
"import": "./build/module/axelarscan/*.js",
"require": "./build/commonjs/axelarscan/*.js"
},
"./axelar-query/*": {
"import": "./build/module/axelar-query/*.js",
"require": "./build/commonjs/axelar-query/*.js"
},
".": {
"import": "./build/module/index.js",
"require": "./build/commonjs/index.js"
}
},
"typesVersions": {
"*": {
"*": [
"./build/module/*"
]
"import": "./build/module/*.js",
"require": "./build/commonjs/*.js"
}
},
"scripts": {
Expand All @@ -54,12 +59,14 @@
"matchers": "link:@testing-library/jest-dom/matchers",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^5.0.1",
"typescript": "^5.2.2",
"vite": "4.4.9",
"vitest": "^0.34.3"
},
"dependencies": {
"ky": "^0.33.3",
"got": "^13.0.0",
"ky": "^1.0.1",
"rambda": "^7.5.0"
}
}
23 changes: 0 additions & 23 deletions packages/api/src/HTTPClient.ts

This file was deleted.

84 changes: 84 additions & 0 deletions packages/api/src/IsomorphicHTTPClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { Got } from "got/dist/source";
import type { KyInstance } from "ky/distribution/types/ky";

/**
* Defines the runtime target for the HTTP client, either "browser" or "node".
*/
export type ClientTarget = "browser" | "node";

/**
* Configuration options for creating an instance of `IsomorphicHTTPClient`.
* The type is a discriminated union based on the target ("browser" or "node").
*/
export type ClientOptions =
| {
/**
* Indicates that the client will run in a browser environment.
*/
target: "browser";
/**
* The underlying HTTP client instance for browser, based on Ky.
*/
instance: KyInstance;
}
| {
/**
* Indicates that the client will run in a Node.js environment.
*/
target: "node";
/**
* The underlying HTTP client instance for Node.js, based on Got.
*/
instance: Got;
};

/**
* Metadata to provide additional information about the client.
*/
export type ClientMeta = {
/**
* The name of the client.
*/
name: string;
/**
* The version of the client.
*/
version: string;
};

/**
* Abstract class that defines an isomorphic HTTP client.
* This can be used both in browser and Node.js environments.
*/
export abstract class IsomorphicHTTPClient {
/**
* The name of the HTTP client.
*/
public name: string;
/**
* The version of the HTTP client.
*/
public version: string;
/**
* The runtime target of the HTTP client, either "browser" or "node".
*/
public target: ClientTarget;

/**
* The internal client instance, either based on Ky for browsers or Got for Node.js.
*/
protected client: KyInstance | Got;

/**
* Constructs a new instance of `IsomorphicHTTPClient`.
*
* @param client - Configuration options for the client instance.
* @param meta - Optional metadata like name and version for the client.
*/
constructor(client: ClientOptions, meta?: ClientMeta) {
this.client = client.instance;
this.target = client.target;
this.name = meta?.name ?? "HTTPClient";
this.version = meta?.version ?? "0.0.0";
}
}
12 changes: 0 additions & 12 deletions packages/api/src/axelar-query/axelar-query.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/api/src/axelar-query/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ky, { type Options } from "ky";

import { AxelarQueryAPIClient } from "./isomorphic";

export const createAxelarQueryBrowserClient = (options: Options) =>
AxelarQueryAPIClient.init({
target: "browser",
instance: ky.extend(options),
});
2 changes: 1 addition & 1 deletion packages/api/src/axelar-query/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./types";
export * from "./axelar-query";
export * from "./isomorphic";
10 changes: 10 additions & 0 deletions packages/api/src/axelar-query/isomorphic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ClientOptions, IsomorphicHTTPClient } from "../IsomorphicHTTPClient";

export class AxelarQueryAPIClient extends IsomorphicHTTPClient {
static init(options: ClientOptions) {
return new AxelarQueryAPIClient(options, {
name: "AxelarQueryAPI",
version: "0.0.1",
});
}
}
9 changes: 9 additions & 0 deletions packages/api/src/axelar-query/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import got, { type Options } from "got/dist/source";

import { AxelarQueryAPIClient } from "./isomorphic";

export const createAxelarQueryNodeClient = (options: Options) =>
AxelarQueryAPIClient.init({
target: "node",
instance: got.extend(options),
});
9 changes: 9 additions & 0 deletions packages/api/src/axelarscan/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ky, { type Options } from "ky";

import { AxelarscanClient } from "./isomorphic";

export const createAxelarscanBrowserClient = (options: Options) =>
AxelarscanClient.init({
target: "browser",
instance: ky.extend(options),
});
4 changes: 3 additions & 1 deletion packages/api/src/axelarscan/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./axelarscan";
export * from "./isomorphic";
export * from "./browser";
export * from "./node";
export * from "./types";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { partition } from "rambda";

import { HTTPClient, Options } from "../HTTPClient";
import { ClientOptions, IsomorphicHTTPClient } from "../IsomorphicHTTPClient";
import {
AxelarAssetPrice,
AxelarScanAsset,
Expand Down Expand Up @@ -29,8 +29,8 @@ export type GetAssetsPriceResponse = AxelarAssetPrice[];

export type GetChainConfigsResponse = (EVMChainConfig | CosmosChainConfig)[];

export class AxelarscanClient extends HTTPClient {
static init(options: Options) {
export class AxelarscanClient extends IsomorphicHTTPClient {
static init(options: ClientOptions) {
return new AxelarscanClient(options, {
name: "AxelarscanClient",
version: "0.0.1",
Expand Down Expand Up @@ -92,5 +92,3 @@ export class AxelarscanClient extends HTTPClient {
};
}
}

export const createAxelarscanClient = AxelarscanClient.init;
9 changes: 9 additions & 0 deletions packages/api/src/axelarscan/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import got, { type Options } from "got/dist/source";

import { AxelarscanClient } from "./isomorphic";

export const createAxelarscanNodeClient = (options: Options) =>
AxelarscanClient.init({
target: "node",
instance: got.extend(options),
});
9 changes: 9 additions & 0 deletions packages/api/src/gmp/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ky, { type Options } from "ky";

import { GMPClient } from "./isomorphic";

export const createGMPBrowserClient = (options: Options) =>
GMPClient.init({
target: "browser",
instance: ky.extend(options),
});
4 changes: 3 additions & 1 deletion packages/api/src/gmp/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./gmp";
export * from "./isomorphic";
export * from "./browser";
export * from "./node";
export * from "./types";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { always } from "rambda";

import { HTTPClient, Options } from "../HTTPClient";
import { ClientOptions, IsomorphicHTTPClient } from "../IsomorphicHTTPClient";
import {
EstimateTimeSpentParams,
GetContractsResponse,
Expand All @@ -20,8 +20,8 @@ import {
SearchGMPResponse,
} from "./types";

export class GMPClient extends HTTPClient {
static init(options: Options) {
export class GMPClient extends IsomorphicHTTPClient {
static init(options: ClientOptions) {
return new GMPClient(options, {
name: "GMPClient",
version: "0.0.1",
Expand Down Expand Up @@ -137,5 +137,3 @@ export class GMPClient extends HTTPClient {
.json<GetContractsResponse>();
}
}

export const createGMPClient = GMPClient.init;
9 changes: 9 additions & 0 deletions packages/api/src/gmp/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import got, { type Options } from "got/dist/source";

import { GMPClient } from "./isomorphic";

export const createGMPNodeClient = (options: Options) =>
GMPClient.init({
target: "node",
instance: got.extend(options),
});
4 changes: 2 additions & 2 deletions packages/cosmos/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# build artifacts
build/
query-client/
signing-client/
/query-client/
/signing-client/
*.js
*.d.ts
9 changes: 7 additions & 2 deletions packages/cosmos/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/bash

pnpm clean
pnpm tsc -m commonjs --outDir build/commonjs
pnpm tsc -m esnext --outDir build/module

# build only files under src
pnpm tsc -p tsconfig.build.json -m commonjs --outDir build/commonjs
pnpm tsc -p tsconfig.build.json -m esnext --outDir build/module

cp -Rf build/commonjs/* .
Loading
Loading