Skip to content

Commit 2a612ea

Browse files
B4nanclaude
andcommitted
feat: support multiple env vars per config field, export coerceNumber and z
Allow `envVar` to accept `string | string[]` so the Apify SDK can map ACTOR_/APIFY_/CRAWLEE_ prefixed env vars to a single config field. Also export `coerceNumber` and `z` for SDK reuse. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 713c642 commit 2a612ea

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

packages/core/src/configuration.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import { serviceLocator } from './service_locator.js';
1212

1313
export interface ConfigField<T extends z.ZodType = z.ZodType> {
1414
schema: T;
15-
envVar?: string;
15+
envVar?: string | string[];
1616
}
1717

18-
export function field<T extends z.ZodType>(schema: T, envVar?: string): ConfigField<T> {
18+
export function field<T extends z.ZodType>(schema: T, envVar?: string | string[]): ConfigField<T> {
1919
return { schema, envVar };
2020
}
2121

@@ -29,7 +29,7 @@ export const coerceBoolean = z.preprocess((val) => {
2929
return val;
3030
}, z.boolean());
3131

32-
const coerceNumber = z.preprocess((val) => {
32+
export const coerceNumber = z.preprocess((val) => {
3333
if (typeof val === 'string') return Number(val);
3434
return val;
3535
}, z.number());
@@ -211,9 +211,12 @@ export class Configuration {
211211

212212
// 2. Environment variables
213213
if (fieldDef.envVar) {
214-
const envValue = process.env[fieldDef.envVar];
215-
if (envValue != null && envValue !== '') {
216-
return fieldDef.schema.parse(envValue);
214+
const envVars = Array.isArray(fieldDef.envVar) ? fieldDef.envVar : [fieldDef.envVar];
215+
for (const envVar of envVars) {
216+
const envValue = process.env[envVar];
217+
if (envValue != null && envValue !== '') {
218+
return fieldDef.schema.parse(envValue);
219+
}
217220
}
218221
}
219222

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export * from './cookie_utils.js';
1717
export * from './recoverable_state.js';
1818
export { PseudoUrl } from '@apify/pseudo_url';
1919
export { Dictionary, Awaitable, Constructor, StorageClient, Cookie, QueueOperationInfo } from '@crawlee/types';
20+
export { z } from 'zod';

0 commit comments

Comments
 (0)