Skip to main content

API Constants

API_VERSION

Current API version number.
API_VERSION
string
Value: "v1"

API_BASE_URL

Base URL for API endpoints.
API_BASE_URL
string
Value: process.env.NEXT_PUBLIC_API_URL || "https://api.example.com"

API_TIMEOUT

Default timeout for API requests in milliseconds.
API_TIMEOUT
number
Value: 30000 (30 seconds)

HTTP_STATUS

HTTP status code constants.
HTTP_STATUS
object

Example

import { HTTP_STATUS } from '@/constants';

if (response.status === HTTP_STATUS.OK) {
  // Handle success
} else if (response.status === HTTP_STATUS.UNAUTHORIZED) {
  // Redirect to login
}

Authentication Constants

AUTH_TOKEN_KEY

LocalStorage key for authentication token.
AUTH_TOKEN_KEY
string
Value: "auth_token"

REFRESH_TOKEN_KEY

LocalStorage key for refresh token.
REFRESH_TOKEN_KEY
string
Value: "refresh_token"

TOKEN_EXPIRY_KEY

LocalStorage key for token expiry timestamp.
TOKEN_EXPIRY_KEY
string
Value: "token_expiry"

SESSION_TIMEOUT

Session timeout duration in milliseconds.
SESSION_TIMEOUT
number
Value: 3600000 (1 hour)

REFRESH_THRESHOLD

Time before expiry to refresh token (milliseconds).
REFRESH_THRESHOLD
number
Value: 300000 (5 minutes)

Route Constants

ROUTES

Application route paths.
ROUTES
object

Example

import { ROUTES } from '@/constants';
import { useRouter } from 'next/router';

function LoginButton() {
  const router = useRouter();
  
  const handleLogin = () => {
    router.push(ROUTES.LOGIN);
  };
  
  return <button onClick={handleLogin}>Login</button>;
}

UI Constants

BREAKPOINTS

Responsive design breakpoints in pixels.
BREAKPOINTS
object

THEME_COLORS

Application color palette.
THEME_COLORS
object

Z_INDEX

Z-index layering constants.
Z_INDEX
object

Example

import { BREAKPOINTS, THEME_COLORS, Z_INDEX } from '@/constants';
import styled from 'styled-components';

const Modal = styled.div`
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: ${Z_INDEX.MODAL};
  background-color: ${THEME_COLORS.PRIMARY};
  
  @media (min-width: ${BREAKPOINTS.MD}px) {
    max-width: 600px;
    margin: 0 auto;
  }
`;

Validation Constants

VALIDATION_RULES

Common validation rules and limits.
VALIDATION_RULES
object

REGEX_PATTERNS

Common regular expression patterns.
REGEX_PATTERNS
object

Example

import { VALIDATION_RULES, REGEX_PATTERNS } from '@/constants';

function validateUsername(username: string): boolean {
  return (
    username.length >= VALIDATION_RULES.USERNAME_MIN_LENGTH &&
    username.length <= VALIDATION_RULES.USERNAME_MAX_LENGTH &&
    REGEX_PATTERNS.USERNAME.test(username)
  );
}

function validateEmail(email: string): boolean {
  return (
    email.length <= VALIDATION_RULES.EMAIL_MAX_LENGTH &&
    REGEX_PATTERNS.EMAIL.test(email)
  );
}

Pagination Constants

DEFAULT_PAGE_SIZE

Default number of items per page.
DEFAULT_PAGE_SIZE
number
Value: 20

MAX_PAGE_SIZE

Maximum allowed items per page.
MAX_PAGE_SIZE
number
Value: 100

PAGE_SIZE_OPTIONS

Available page size options for user selection.
PAGE_SIZE_OPTIONS
number[]
Value: [10, 20, 50, 100]

File Upload Constants

MAX_FILE_SIZE

Maximum file upload size in bytes.
MAX_FILE_SIZE
number
Value: 5242880 (5 MB)

ALLOWED_FILE_TYPES

Allowed file MIME types for upload.
ALLOWED_FILE_TYPES
object

Example

import { MAX_FILE_SIZE, ALLOWED_FILE_TYPES } from '@/constants';

function validateFile(file: File): { valid: boolean; error?: string } {
  if (file.size > MAX_FILE_SIZE) {
    return { valid: false, error: 'File too large (max 5MB)' };
  }
  
  const allAllowedTypes = [
    ...ALLOWED_FILE_TYPES.IMAGES,
    ...ALLOWED_FILE_TYPES.DOCUMENTS
  ];
  
  if (!allAllowedTypes.includes(file.type)) {
    return { valid: false, error: 'File type not allowed' };
  }
  
  return { valid: true };
}

Date & Time Constants

DATE_FORMATS

Common date format strings.
DATE_FORMATS
object

TIME_ZONES

Common time zone identifiers.
TIME_ZONES
object

Error Messages

ERROR_MESSAGES

Standardized error messages.
ERROR_MESSAGES
object

Example

import { ERROR_MESSAGES } from '@/constants';

function LoginForm() {
  const [errors, setErrors] = useState<Record<string, string>>({});
  
  const validate = (email: string, password: string) => {
    const newErrors: Record<string, string> = {};
    
    if (!email) {
      newErrors.email = ERROR_MESSAGES.REQUIRED_FIELD;
    } else if (!email.includes('@')) {
      newErrors.email = ERROR_MESSAGES.INVALID_EMAIL;
    }
    
    if (!password) {
      newErrors.password = ERROR_MESSAGES.REQUIRED_FIELD;
    }
    
    setErrors(newErrors);
    return Object.keys(newErrors).length === 0;
  };
  
  // ...
}

Build docs developers (and LLMs) love