Skip to content

Commit

Permalink
🚚 Move cli/assertion in a separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Aug 21, 2024
1 parent 1b47870 commit ea9e45c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 130 deletions.
44 changes: 20 additions & 24 deletions example/simple.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { arbitrumSepolia, optimismSepolia, polygonAmoy } from "viem/chains";
import {
type Config,
buildAlchemyUpstream,
buildEnvioUpstream,
buildErpcConfig,
buildEvmNetworks,
buildProject,
envVariable,
Expand Down Expand Up @@ -35,31 +35,27 @@ const networks = buildEvmNetworks({
});

/* -------------------------------------------------------------------------- */
/* 2. Create your config */
/* 2. Create config and export it */
/* -------------------------------------------------------------------------- */

const config: Config = {
logLevel: "warn",
database: {
evmJsonRpcCache: {
driver: "postgresql",
postgresql: {
connectionUri: envVariable("ERPC_DATABASE_URL"),
table: "rpc_cache",
export default buildErpcConfig({
config: {
logLevel: "warn",
database: {
evmJsonRpcCache: {
driver: "postgresql",
postgresql: {
connectionUri: envVariable("ERPC_DATABASE_URL"),
table: "rpc_cache",
},
},
},
projects: [
buildProject({
id: "simple-erpc",
networks,
upstreams: [alchemyUpstream, envioUpstream],
}),
],
},
projects: [
buildProject({
id: "simple-erpc",
networks,
upstreams: [alchemyUpstream, envioUpstream],
}),
],
};

/* -------------------------------------------------------------------------- */
/* 3. Write your config */
/* -------------------------------------------------------------------------- */

export default config;
});
105 changes: 105 additions & 0 deletions src/cli/utils/assertion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type { Config } from "../../../generatedTypes/erpcTypes";
import { checkRateLimitsDuplication, checkRateLimitsReference } from "./rateLimits";

type ConfigCheckResult = {
errors: ConfigError[];
hasFatalError: boolean;
// Some global info
stats: {
projects: number;
rateLimiters: number;
};
};

export type ConfigError = {
projects?: string;
field: string;
message: string;
level?: "error" | "warning";
};

/**
* Check the config validity
*/
export function checkConfigValidity(config: Config): ConfigCheckResult {
// All the error we will face
const errors: ConfigError[] = [];
const stats: ConfigCheckResult["stats"] = {
projects: 0,
rateLimiters: 0,
};

// Perform a few basics type checks
if (typeof config !== "object") {
errors.push({
field: "root",
message: "Config must be an object",
level: "error",
});
return { errors, hasFatalError: true, stats };
}

// Ensure the config has the required fields
if (
!config.projects ||
!config.logLevel ||
!Array.isArray(config.projects)
) {
errors.push({
field: "projects",
message: "Config must have a `projects` field",
level: "error",
});
errors.push({
field: "logLevel",
message: "Config must have a `logLevel` field",
level: "error",
});
return { errors, hasFatalError: true, stats };
}

// Update our stats object
stats.projects = config.projects.length;
stats.rateLimiters = config.rateLimiters?.budgets?.length ?? 0;

// Check for projects duplication
errors.push(...checkProjectsDuplication(config));

// Check rate limits id duplication
const { errors: rateLimitsErrors, ids: rateLimitsIds } =
checkRateLimitsDuplication(config);
errors.push(...rateLimitsErrors);

// Check for rate limits reference
errors.push(...checkRateLimitsReference({ config, rateLimitsIds }));

return {
errors,
hasFatalError: errors.some((error) => error.level === "error"),
stats,
};
}

/**
* Check that the config doesn't contain duplicated rate limits id
*/
function checkProjectsDuplication(config: Config): ConfigError[] {
const errors: ConfigError[] = [];
const projects = config.projects;

const ids = new Set<string>();
for (const project of projects) {
if (!project) continue;
if (ids.has(project.id)) {
errors.push({
field: `project.${project.id}`,
message: `Duplicated project id: ${project.id}`,
level: "error",
});
continue;
}
ids.add(project.id);
}

return errors;
}
Original file line number Diff line number Diff line change
@@ -1,112 +1,10 @@
import type { Config } from "../../generatedTypes/erpcTypes";

type ConfigCheckResult = {
errors: ConfigError[];
hasFatalError: boolean;
// Some global info
stats: {
projects: number;
rateLimiters: number;
};
};

export type ConfigError = {
projects?: string;
field: string;
message: string;
level?: "error" | "warning";
};

/**
* Check the config validity
*/
export function checkConfigValidity(config: Config): ConfigCheckResult {
// All the error we will face
const errors: ConfigError[] = [];
const stats: ConfigCheckResult["stats"] = {
projects: 0,
rateLimiters: 0,
};

// Perform a few basics type checks
if (typeof config !== "object") {
errors.push({
field: "root",
message: "Config must be an object",
level: "error",
});
return { errors, hasFatalError: true, stats };
}

// Ensure the config has the required fields
if (
!config.projects ||
!config.logLevel ||
!Array.isArray(config.projects)
) {
errors.push({
field: "projects",
message: "Config must have a `projects` field",
level: "error",
});
errors.push({
field: "logLevel",
message: "Config must have a `logLevel` field",
level: "error",
});
return { errors, hasFatalError: true, stats };
}

// Update our stats object
stats.projects = config.projects.length;
stats.rateLimiters = config.rateLimiters?.budgets?.length ?? 0;

// Check for projects duplication
errors.push(...checkProjectsDuplication(config));

// Check rate limits id duplication
const { errors: rateLimitsErrors, ids: rateLimitsIds } =
checkRateLimitsDuplication(config);
errors.push(...rateLimitsErrors);

// Check for rate limits reference
errors.push(...checkRateLimitsReference({ config, rateLimitsIds }));

return {
errors,
hasFatalError: errors.some((error) => error.level === "error"),
stats,
};
}

/**
* Check that the config doesn't contain duplicated rate limits id
*/
function checkProjectsDuplication(config: Config): ConfigError[] {
const errors: ConfigError[] = [];
const projects = config.projects;

const ids = new Set<string>();
for (const project of projects) {
if (!project) continue;
if (ids.has(project.id)) {
errors.push({
field: `project.${project.id}`,
message: `Duplicated project id: ${project.id}`,
level: "error",
});
continue;
}
ids.add(project.id);
}

return errors;
}
import type { ConfigError } from ".";
import type { Config } from "../../../generatedTypes/erpcTypes";

/**
* Check that the config doesn't contain duplicated rate limits id
*/
function checkRateLimitsDuplication(config: Config): {
export function checkRateLimitsDuplication(config: Config): {
errors: ConfigError[];
ids: Set<string>;
} {
Expand All @@ -133,7 +31,7 @@ function checkRateLimitsDuplication(config: Config): {
/**
* Ensure that every rate limits referenced in the projects are defined
*/
function checkRateLimitsReference({
export function checkRateLimitsReference({
config,
rateLimitsIds,
}: { config: Config; rateLimitsIds: Set<string> }): ConfigError[] {
Expand Down
8 changes: 8 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Config } from "./generatedTypes/erpcTypes";

/**
* Build the globel erpc config
*/
export function buildErpcConfig({ config }: { config: Config }) {
return config;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export {
} from "./auth";
export * from "./generatedTypes/erpcTypes";
export { bundlersMethods } from "./types/rpc";
export { buildErpcConfig } from "./config";
export type { RpcMethodWithRegex, RpcMethodSplitted } from "./types/rpc";

0 comments on commit ea9e45c

Please sign in to comment.