Skip to content

Commit

Permalink
✨ Update types + optional server and metrics fields
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Aug 27, 2024
1 parent 5bbb35f commit 756d261
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-dots-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@konfeature/erpc-config-generator": patch
---

Optional `server` and `metrics` field in the root config object + generation info
5 changes: 5 additions & 0 deletions .changeset/serious-hornets-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@konfeature/erpc-config-generator": patch
---

Update erpc types for erpc `v0.0.18`
20 changes: 20 additions & 0 deletions src/cli/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ export async function generateCmd({ print, parameters }: GluegunToolbox) {
print.info("Config:");
print.info(` - Projects: ${stats.projects}`);
print.info(` - Rate limiters: ${stats.rateLimiters}`);
if (stats.server) {
print.info(
` - Server IPv4: ${stats.server.hostV4}:${stats.server.port}`
);
print.info(
` - Server IPv6: ${stats.server.hostV6}:${stats.server.port}`
);
} else {
print.info(" - Server: Will use default from eRPC config");
}
if (stats.metrics) {
print.info(
` - Metrics IPv4: ${stats.metrics.hostV4}:${stats.metrics.port}`
);
print.info(
` - Metrics IPv6: ${stats.metrics.hostV6}:${stats.metrics.port}`
);
} else {
print.info(" - Metrics: Not enable or not set in the config");
}

// Write the erpc config
spinner.text = "Writing eRPC config file";
Expand Down
24 changes: 24 additions & 0 deletions src/cli/utils/assertion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ type ConfigCheckResult = {
stats: {
projects: number;
rateLimiters: number;
server?: {
port: number;
hostV4: string;
hostV6: string;
};
metrics?: {
port: number;
hostV4: string;
hostV6: string;
};
};
};

Expand Down Expand Up @@ -64,6 +74,20 @@ export function checkConfigValidity(config: Config): ConfigCheckResult {
// Update our stats object
stats.projects = config.projects.length;
stats.rateLimiters = config.rateLimiters?.budgets?.length ?? 0;
stats.server = config.server
? {
port: config.server.httpPort,
hostV4: config.server.httpHostV4,
hostV6: config.server.httpHostV6,
}
: undefined;
stats.metrics = config.metrics?.enabled
? {
port: config.metrics.port,
hostV4: config.metrics.hostV4,
hostV6: config.metrics.hostV6,
}
: undefined;

// Check for projects duplication
errors.push(...checkProjectsDuplication(config));
Expand Down
41 changes: 38 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
import type { Config } from "./generatedTypes/erpcTypes";
import type {
Config,
MetricsConfig,
ServerConfig,
} from "./generatedTypes/erpcTypes";
import type { OptionalField } from "./types/utils";

type ConfigWithOptionalServer = Omit<Config, "server" | "metrics"> & {
server?: OptionalField<
ServerConfig,
"httpPort" | "httpHostV4" | "httpHostV6"
>;
metrics?: OptionalField<MetricsConfig, "port" | "hostV4" | "hostV6">;
};

// Default config for metrics and server
const defaultServerConfig = {
httpPort: 4000,
httpHostV4: "0.0.0.0",
httpHostV6: "[::]",
};
const defaultMetricsCofnig = {
port: 4001,
hostV4: "0.0.0.0",
hostV6: "[::]",
};

/**
* Build the globel erpc config
*/
export function buildErpcConfig({ config }: { config: Config }) {
return config;
export function buildErpcConfig({
config,
}: { config: ConfigWithOptionalServer }) {
return {
...config,
server: config?.server
? { ...defaultServerConfig, ...config.server }
: undefined,
metrics: config?.metrics
? { ...defaultMetricsCofnig, ...config.metrics }
: undefined,
};
}
8 changes: 6 additions & 2 deletions src/generatedTypes/erpcTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface Config {
admin?: AdminConfig;
}
export interface ServerConfig {
httpHost: string;
httpHostV4: string;
httpHostV6: string;
httpPort: number /* int */;
maxTimeout: string;
}
Expand Down Expand Up @@ -216,7 +217,8 @@ export interface NetworkStrategyConfig {
}
export interface MetricsConfig {
enabled: boolean;
host: string;
hostV4: string;
hostV6: string;
port: number /* int */;
}

Expand All @@ -243,6 +245,8 @@ export type Network = any;
export type UpstreamType = string;
export const UpstreamTypeEvm: UpstreamType = "evm";
export const UpstreamTypeEvmAlchemy: UpstreamType = "evm+alchemy";
export const UpstreamTypeEvmDrpc: UpstreamType = "evm+drpc";
export const UpstreamTypeEvmBlastapi: UpstreamType = "evm+blastapi";
export const UpstreamTypeEvmEnvio: UpstreamType = "evm+envio";
export const UpstreamTypeEvmPimlico: UpstreamType = "evm+pimlico";
export const UpstreamTypeEvmThirdweb: UpstreamType = "evm+thirdweb";
Expand Down

0 comments on commit 756d261

Please sign in to comment.