Skip to content

Render process.env['NODE_ENV'] instead of process.env.NODE_ENV #2

@lorisleiva

Description

@lorisleiva

On some clients (depending on TS configs), using process.env.NODE_ENV can cause a TypeScript error that is being resolved with the following global vars:

// Done to appease issue in generated code:
//  error TS4111: Property 'NODE_ENV' comes from an index signature, so it must be accessed with ['NODE_ENV'].

declare namespace NodeJS {
  interface ProcessEnv {
    NODE_ENV: string;
  }
}

Perhaps it is safer to generate as process.env['NODE_ENV'] directly? But first we'd need to investigate if that would cause new issues.

For reference, this is being generated in the error helpers to avoid bundling all the error messages in production environments. For instance:

let tokenWrapErrorMessages: Record<TokenWrapError, string> | undefined;
if (process.env.NODE_ENV !== 'production') {
  tokenWrapErrorMessages = {
    [TOKEN_WRAP_ERROR__BACKPOINTER_MISMATCH]: `Wrapped backpointer account address does not match expected PDA`,
    [TOKEN_WRAP_ERROR__ESCROW_OWNER_MISMATCH]: `Unwrapped escrow token owner is not set to expected PDA`,
    [TOKEN_WRAP_ERROR__INVALID_BACKPOINTER_OWNER]: `Wrapped backpointer account owner is not the expected token wrap program`,
    [TOKEN_WRAP_ERROR__INVALID_WRAPPED_MINT_OWNER]: `Wrapped mint account owner is not the expected token program`,
    [TOKEN_WRAP_ERROR__MINT_AUTHORITY_MISMATCH]: `Wrapped mint authority does not match expected PDA`,
    [TOKEN_WRAP_ERROR__WRAPPED_MINT_MISMATCH]: `Wrapped mint account address does not match expected PDA`,
    [TOKEN_WRAP_ERROR__ZERO_WRAP_AMOUNT]: `Wrap amount should be positive`,
  };
}

export function getTokenWrapErrorMessage(code: TokenWrapError): string {
  if (process.env.NODE_ENV !== 'production') {
    return (tokenWrapErrorMessages as Record<TokenWrapError, string>)[code];
  }

  return 'Error message not available in production bundles.';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions