github.com/ethereum-optimism/optimism@v1.7.2/packages/common-ts/src/base-service/options.ts (about)

     1  import { ValidatorSpec, Spec } from 'envalid'
     2  
     3  import { LogLevel } from '../common/logger'
     4  import { validators } from './validators'
     5  
     6  /**
     7   * Options for a service.
     8   */
     9  export type Options = {
    10    [key: string]: any
    11  }
    12  
    13  /**
    14   * Specification for options.
    15   */
    16  export type OptionsSpec<TOptions extends Options> = {
    17    [P in keyof Required<TOptions>]: {
    18      validator: (spec?: Spec<TOptions[P]>) => ValidatorSpec<TOptions[P]>
    19      desc: string
    20      default?: TOptions[P]
    21      public?: boolean
    22    }
    23  }
    24  
    25  /**
    26   * Standard options shared by all services.
    27   */
    28  export type StandardOptions = {
    29    loopIntervalMs?: number
    30    port?: number
    31    hostname?: string
    32    logLevel?: LogLevel
    33    useEnv?: boolean
    34    useArgv?: boolean
    35  }
    36  
    37  /**
    38   * Specification for standard options.
    39   */
    40  export const stdOptionsSpec: OptionsSpec<StandardOptions> = {
    41    loopIntervalMs: {
    42      validator: validators.num,
    43      desc: 'Loop interval in milliseconds, only applies if service is set to loop',
    44      default: 0,
    45      public: true,
    46    },
    47    port: {
    48      validator: validators.num,
    49      desc: 'Port for the app server',
    50      default: 7300,
    51      public: true,
    52    },
    53    hostname: {
    54      validator: validators.str,
    55      desc: 'Hostname for the app server',
    56      default: '0.0.0.0',
    57      public: true,
    58    },
    59    logLevel: {
    60      validator: validators.logLevel,
    61      desc: 'Log level',
    62      default: 'debug',
    63      public: true,
    64    },
    65    useEnv: {
    66      validator: validators.bool,
    67      desc: 'For programmatic use, whether to use environment variables',
    68      default: true,
    69      public: true,
    70    },
    71    useArgv: {
    72      validator: validators.bool,
    73      desc: 'For programmatic use, whether to use command line arguments',
    74      default: true,
    75      public: true,
    76    },
    77  }
    78  
    79  /**
    80   * Gets the list of public option names from an options specification.
    81   *
    82   * @param optionsSpec Options specification.
    83   * @returns List of public option names.
    84   */
    85  export const getPublicOptions = (
    86    optionsSpec: OptionsSpec<Options>
    87  ): string[] => {
    88    return Object.keys(optionsSpec).filter((key) => {
    89      return optionsSpec[key].public
    90    })
    91  }