-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move environment validation to common (#13)
Uses common functions to validate environment variables in the client and the worker, reducing code duplication. The validation result is renamed to "config" instead of "env", since "env" is just the source of the data, but did not properly describe its usage.
- Loading branch information
Showing
4 changed files
with
71 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,23 @@ | ||
export interface Env { | ||
import { validatePositiveInteger, validateString, validateUrl } from '@meyfa/ddns-common' | ||
|
||
export interface Config { | ||
DDNS_URL: URL | ||
DDNS_SECRET: string | ||
DDNS_UPDATE_INTERVAL: number | ||
DDNS_REQUEST_TIMEOUT: number | ||
} | ||
|
||
export function validateEnvironment(env: NodeJS.ProcessEnv): Env { | ||
const result = { | ||
export function validateEnvironment(env: NodeJS.ProcessEnv): Config { | ||
const config = { | ||
DDNS_URL: validateUrl(env, 'DDNS_URL'), | ||
DDNS_SECRET: validateString(env, 'DDNS_SECRET'), | ||
DDNS_UPDATE_INTERVAL: validatePositiveInteger(env, 'DDNS_UPDATE_INTERVAL'), | ||
DDNS_REQUEST_TIMEOUT: validatePositiveInteger(env, 'DDNS_REQUEST_TIMEOUT') | ||
} | ||
|
||
if (result.DDNS_UPDATE_INTERVAL < result.DDNS_REQUEST_TIMEOUT) { | ||
if (config.DDNS_UPDATE_INTERVAL < config.DDNS_REQUEST_TIMEOUT) { | ||
throw new Error('DDNS_UPDATE_INTERVAL must be greater than or equal to DDNS_REQUEST_TIMEOUT') | ||
} | ||
|
||
return result | ||
} | ||
|
||
function validateUrl(env: NodeJS.ProcessEnv, key: string): URL { | ||
const input = env[key] | ||
if (input == null || input === '' || !URL.canParse(input)) { | ||
throw new Error(`${key} must be a valid URL`) | ||
} | ||
return new URL(input) | ||
} | ||
|
||
function validateString(env: NodeJS.ProcessEnv, key: string): string { | ||
const input = env[key] | ||
if (input == null || input === '') { | ||
throw new Error(`${key} is required`) | ||
} | ||
return input | ||
} | ||
|
||
function validatePositiveInteger(env: NodeJS.ProcessEnv, key: string): number { | ||
const input = env[key] | ||
const error = `${key} must be a positive integer` | ||
if (input == null || input === '' || !/^\d+$/.test(input)) { | ||
throw new Error(error) | ||
} | ||
const value = Number.parseInt(input, 10) | ||
if (value <= 0) { | ||
throw new Error(error) | ||
} | ||
return value | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters