Skip to content

Utilities

Utility modules live in apps/frontend/src/utils/.


request.ts — HTTP Client

The global Axios instance used for all API calls throughout the application.

ts
import request from '@/utils/request'

const response = await request({
  url: '/api/v1/unit/',
  method: 'get',
})

Configuration

SettingValue
baseURLVITE_API_BASE_URL env var or https://app.intecoglogistech.com
timeout30000 ms (30 seconds)
withCredentialstrue — always sends the userAccessToken cookie

Request Interceptor

Passes the config through unchanged. Errors are re-thrown.

Response Interceptor

On 401 Unauthorized: automatically redirects to /signin (unless already on the signin page). This prevents infinite redirect loops.

ts
// Simplified interceptor logic
if (error.response?.status === 401) {
  if (window.location.hash !== '#/signin') {
    window.location.hash = '#/signin'
  }
}

Usage Pattern

All views make direct request({...}) calls:

ts
const response = await request({
  url: '/api/v1/unit/',
  method: 'post',
  data: { name: 'Cold Room A', min_temperature: 2, max_temperature: 8 }
})

notifications.ts — Toast Notifications

A thin wrapper around ElNotification for consistent toast messages.

ts
import { showSuccessNotification, showFailureNotification } from '@/utils/notifications'

showSuccessNotification(title, message, options?)

ts
showSuccessNotification('Unit Created', 'Cold Room A has been created successfully')
  • Type: success
  • Position: bottom-right
  • Duration: 4500ms (default)

showFailureNotification(title, message, options?)

ts
showFailureNotification('Error', 'Failed to fetch units. Please try again.')
  • Type: error
  • Position: bottom-right
  • Duration: 4500ms (default)

Both functions accept an optional third options argument to override any ElNotification options.


env.ts — Environment Variables

Typed helpers for accessing Vite environment variables:

ts
import { env } from '@/utils/env'

env.apiBaseUrl  // → import.meta.env.VITE_API_BASE_URL
env.appEnv      // → import.meta.env.VITE_APP_ENV
env.isDev       // → env.appEnv === 'development'
env.isProd      // → env.appEnv === 'production'

Use this instead of reading import.meta.env directly to get TypeScript type safety and a single access point.


index.ts — CSS & Theme Utilities

ts
import { setProperty, mix } from '@/utils'

setProperty(prop, val, dom?)

Sets a CSS custom property on a DOM element (defaults to document.documentElement):

ts
setProperty('--el-color-primary', '#409EFF')

Used by themeStore to apply dynamic theme colors.

mix(color1, color2, weight)

Blends two hex colors by weight (0–100). Used internally by themeStore to generate the lighter/darker Element Plus color variants (e.g. --el-color-primary-light-3):

ts
const lightVariant = mix('#409EFF', '#ffffff', 30) // 30% white blend

api/index.ts — API Helpers

Thin fetch wrappers used in a few contexts:

ts
fetchData(url)       // GET any URL
fetchUserData()      // GET ./mock/user.json  (development mock)
fetchRoleData()      // GET ./mock/role.json  (development mock)

Most API calls in the app do not go through this module — they use request({...}) directly from @/utils/request. This module is mainly used for fetching mock data during development.

Intecog Logistech IoT Monitoring Platform