angular_migration/logs/phase2_api_service_migration.log

32 lines
2.1 KiB
Plaintext
Raw Normal View History

# Log: API Service Migration
**Task**: Migrate Angular's `ApiRequestService` to a reusable React service using `axios`.
**Analysis of `api-request.service.ts`**:
- Uses `appConfig` to get `baseApiPath`. This path includes the protocol, host, and a trailing slash (e.g., `http://localhost:3000/`).
- Handles setting `Content-Type: application/json` headers.
- Has commented-out logic to add an `Authorization` token from `userInfoService`.
- Provides wrappers for GET, POST, PUT, DELETE methods.
- Includes a special method `postFormData` for `multipart/form-data` uploads.
- Basic error handling in `handleError`.
**Analysis of `helper.ts` and other services**:
- Some services (`forgotpass.service`, `user-info.service`) import a `baseUrl` from `helper.ts`.
- `helper.ts` gets its value from `environment.backendUrl`.
- The provided `environment.ts` lacks `backendUrl`, but has `apiUrl: 'http://localhost:3000'`.
- **Assumption**: `backendUrl` and `apiUrl` should be treated as the same value.
- Services using `baseUrl` manually add a `/` separator, e.g., `${baseUrl}/api/...`. This is consistent with `baseApiPath` from `appConfig` which already includes the trailing slash.
**Migration Plan**:
1. Create `migration2react/src/services/apiService.ts`.
2. Use `axios.create()` to set up a default instance with `baseURL` derived from our new `apiConfig.ts`.
3. Implement an `axios` request interceptor to attach the Authorization token. This will replicate the logic from `userInfoService` and `apiRequestService` to get the token from `sessionStorage`.
4. Implement an `axios` response interceptor for global error handling, improving upon the `handleError` method.
5. Export the configured `axios` instance as the default module export.
6. Create and export a separate `postFormData` function for handling file uploads, as its headers and configuration differ.
**Result**:
- A single, configured `axios` instance (`apiService`) that can be imported and used for all standard API calls.
- A separate utility function (`postFormData`) for specific multipart requests.
- This centralizes API configuration and handling in a standard React pattern.