Skip to content

Commit 185b3df

Browse files
committed
fix(cli): add env validation to Blockscout, IPFS, and Minio codegen
- Replace non-null assertions with runtime environment variable validation - Add proper error messages for missing required configuration - Improve generated code safety for Blockscout UI endpoint - Enhance IPFS API endpoint validation - Strengthen Minio client configuration with access key validation
1 parent 29eec47 commit 185b3df

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

sdk/cli/src/commands/codegen/codegen-blockscout.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { rm, writeFile } from "node:fs/promises";
22
import { basename, resolve } from "node:path";
3-
import { writeTemplate } from "@/commands/codegen/utils/write-template";
4-
import { getApplicationOrPersonalAccessToken } from "@/utils/get-app-or-personal-token";
53
import { generateSchema } from "@gql.tada/cli-utils";
64
import { projectRoot } from "@settlemint/sdk-utils/filesystem";
75
import { appendHeaders, graphqlFetchWithRetry } from "@settlemint/sdk-utils/http";
86
import { installDependencies, isPackageInstalled } from "@settlemint/sdk-utils/package-manager";
97
import { note } from "@settlemint/sdk-utils/terminal";
108
import { type DotEnv, LOCAL_INSTANCE, STANDALONE_INSTANCE } from "@settlemint/sdk-utils/validation";
9+
import { writeTemplate } from "@/commands/codegen/utils/write-template";
10+
import { getApplicationOrPersonalAccessToken } from "@/utils/get-app-or-personal-token";
1111

1212
const PACKAGE_NAME = "@settlemint/sdk-blockscout";
1313

@@ -167,6 +167,18 @@ import { createLogger, requestLogger, type LogLevel } from '@settlemint/sdk-util
167167
168168
const logger = createLogger({ level: process.env.SETTLEMINT_LOG_LEVEL as LogLevel });
169169
170+
// Validate required environment variables
171+
const blockscoutEndpoint = process.env.SETTLEMINT_BLOCKSCOUT_ENDPOINT;
172+
const blockscoutUiEndpointVar = process.env.SETTLEMINT_BLOCKSCOUT_UI_ENDPOINT;
173+
174+
if (!blockscoutEndpoint) {
175+
throw new Error('SETTLEMINT_BLOCKSCOUT_ENDPOINT environment variable is required');
176+
}
177+
178+
if (!blockscoutUiEndpointVar) {
179+
throw new Error('SETTLEMINT_BLOCKSCOUT_UI_ENDPOINT environment variable is required');
180+
}
181+
170182
export const { client: blockscoutClient, graphql: blockscoutGraphql } = createBlockscoutClient<{
171183
introspection: introspection;
172184
disableMasking: true;
@@ -189,13 +201,13 @@ export const { client: blockscoutClient, graphql: blockscoutGraphql } = createBl
189201
Wei: string;
190202
};
191203
}>({
192-
instance: process.env.SETTLEMINT_BLOCKSCOUT_ENDPOINT!,
204+
instance: blockscoutEndpoint,
193205
accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,
194206
}, {
195207
fetch: requestLogger(logger, "blockscout", fetch) as typeof fetch,
196208
});
197209
198-
export const blockscoutUiEndpoint = process.env.SETTLEMINT_BLOCKSCOUT_UI_ENDPOINT!;`;
210+
export const blockscoutUiEndpoint = blockscoutUiEndpointVar;`;
199211

200212
await writeTemplate(template, "/lib/settlemint", "blockscout.ts");
201213

sdk/cli/src/commands/codegen/codegen-ipfs.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { writeTemplate } from "@/commands/codegen/utils/write-template";
21
import { projectRoot } from "@settlemint/sdk-utils/filesystem";
32
import { installDependencies, isPackageInstalled } from "@settlemint/sdk-utils/package-manager";
43
import type { DotEnv } from "@settlemint/sdk-utils/validation";
4+
import { writeTemplate } from "@/commands/codegen/utils/write-template";
55

66
const PACKAGE_NAME = "@settlemint/sdk-ipfs";
77

@@ -17,8 +17,15 @@ export async function codegenIpfs(env: DotEnv) {
1717

1818
const clientTemplate = `import { createServerIpfsClient } from "${PACKAGE_NAME}";
1919
20+
// Validate required environment variables
21+
const ipfsApiEndpoint = process.env.SETTLEMINT_IPFS_API_ENDPOINT;
22+
23+
if (!ipfsApiEndpoint) {
24+
throw new Error('SETTLEMINT_IPFS_API_ENDPOINT environment variable is required');
25+
}
26+
2027
export const { client } = createServerIpfsClient({
21-
instance: process.env.SETTLEMINT_IPFS_API_ENDPOINT!,
28+
instance: ipfsApiEndpoint,
2229
accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,
2330
});`;
2431

sdk/cli/src/commands/codegen/codegen-minio.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { writeTemplate } from "@/commands/codegen/utils/write-template";
21
import { projectRoot } from "@settlemint/sdk-utils/filesystem";
32
import { installDependencies, isPackageInstalled } from "@settlemint/sdk-utils/package-manager";
43
import type { DotEnv } from "@settlemint/sdk-utils/validation";
4+
import { writeTemplate } from "@/commands/codegen/utils/write-template";
55

66
const PACKAGE_NAME = "@settlemint/sdk-minio";
77

@@ -17,10 +17,27 @@ export async function codegenMinio(env: DotEnv) {
1717

1818
const clientTemplate = `import { createServerMinioClient } from "${PACKAGE_NAME}";
1919
20+
// Validate required environment variables
21+
const minioEndpoint = process.env.SETTLEMINT_MINIO_ENDPOINT;
22+
const minioAccessKey = process.env.SETTLEMINT_MINIO_ACCESS_KEY;
23+
const minioSecretKey = process.env.SETTLEMINT_MINIO_SECRET_KEY;
24+
25+
if (!minioEndpoint) {
26+
throw new Error('SETTLEMINT_MINIO_ENDPOINT environment variable is required');
27+
}
28+
29+
if (!minioAccessKey) {
30+
throw new Error('SETTLEMINT_MINIO_ACCESS_KEY environment variable is required');
31+
}
32+
33+
if (!minioSecretKey) {
34+
throw new Error('SETTLEMINT_MINIO_SECRET_KEY environment variable is required');
35+
}
36+
2037
export const { client } = createServerMinioClient({
21-
instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
22-
accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
23-
secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
38+
instance: minioEndpoint,
39+
accessKey: minioAccessKey,
40+
secretKey: minioSecretKey
2441
});`;
2542

2643
await writeTemplate(clientTemplate, "/lib/settlemint", "minio.ts");

0 commit comments

Comments
 (0)