Skip to content

API Layer

Overview

The app communicates with three external API surfaces:

ClientPackageConfig FilePurpose
Main API@medipal/mp-frontend-apiapp/api.config.tsDevice auth, enrollments, questionnaire submission
Tracker API@medipal/mp-mobile-app-tracker-apiapp/tracker_api.config.tsAnalytics events
Nuxt Layer@medipal/mp-nuxt-api-layervia nuxt.config.ts extendsShared useApi() composable

useTenantApi() — Per-Tenant API Client

This is the primary way to make authenticated API calls.

ts
const api = useTenantApi(tenantId);
const response = await api.get("/some/endpoint");

What It Does

  • Creates a per-tenant Axios instance using the tenant's stored url and access_token
  • Automatically handles token refresh:
    1. On 401, calls refreshToken for that specific tenant
    2. Queues all concurrent requests that arrive during refresh
    3. Retries queued requests with the new token once refresh completes
    4. Uses an isRefreshing flag to prevent multiple simultaneous refresh calls

Do not implement your own token refresh

All API calls must go through useTenantApi(). Implementing token refresh logic elsewhere will cause race conditions and duplicate refresh calls.

Token Storage

Tokens are stored in the tenants SQLite table:

ColumnDescription
access_tokenBearer token attached to every API request
refresh_tokenUsed to obtain a new access token
token_expires_atISO datetime for proactive refresh

Main API Methods

Defined in app/api.config.ts, using @medipal/mp-frontend-api:

MethodPurpose
deviceLoginAuthenticate device with enrollment token
refreshTokenRefresh an expired access token
deviceConsentSubmit device terms acceptance
currentUserFetch current user info
deviceEnrollConsentSubmit enrollment consent
enrollmentUpdateUpdate enrollment status
deviceSyncFull device sync (questionnaires, enrollments, schedules)
questionnaireSubmitSubmit a completed questionnaire response
getInstanceFetch tenant instance details

Nuxt API Layer

The app extends github:medipal/mp-nuxt-api-layer:

ts
// nuxt.config.ts
extends: ['github:medipal/mp-nuxt-api-layer']

This provides the useApi() composable and shared API utilities. It requires GIGET_AUTH (a GitHub Classic PAT) set as an environment variable at dev/build time.

Error Handling

ScenarioBehaviour
401 responseuseTenantApi interceptor refreshes token, retries
Refresh failsNotAuthenticatedEvent emitted → redirect to re-auth
Network error during submissionSubmission saved as pending_submits, retried later

Environment Variables

ini
API_URL=https://api.example.com
TRACKER_API_URL=https://tracker.example.com
TRACKER_API_KEY=your-tracker-key
GIGET_AUTH=ghp_xxxx     # GitHub PAT for fetching mp-nuxt-api-layer