62 lines
1.6 KiB
Markdown
62 lines
1.6 KiB
Markdown
|
|
# Environment Setup untuk API Services
|
||
|
|
|
||
|
|
## Cara Setup Environment Variables
|
||
|
|
|
||
|
|
Buat file `.env.local` di root project dengan konfigurasi berikut:
|
||
|
|
|
||
|
|
```env
|
||
|
|
# API Endpoints
|
||
|
|
NEXT_PUBLIC_API_BASE_URL=https://jaecookelapagading.com/api
|
||
|
|
NEXT_PUBLIC_LOCAL_API_URL=http://localhost:8800
|
||
|
|
NEXT_PUBLIC_CLIENT_KEY=bb65b1ad-e954-4a1a-b4d0-74df5bb0b640
|
||
|
|
```
|
||
|
|
|
||
|
|
## Penjelasan
|
||
|
|
|
||
|
|
- `NEXT_PUBLIC_API_BASE_URL`: URL base untuk API (production atau development)
|
||
|
|
- `NEXT_PUBLIC_LOCAL_API_URL`: URL untuk API lokal (localhost:8800)
|
||
|
|
- `NEXT_PUBLIC_CLIENT_KEY`: Client key untuk authentikasi API
|
||
|
|
|
||
|
|
## Cara Menggunakan
|
||
|
|
|
||
|
|
### 1. Untuk memanggil localhost:8800
|
||
|
|
|
||
|
|
Ubah `NEXT_PUBLIC_API_BASE_URL` di `.env.local`:
|
||
|
|
|
||
|
|
```env
|
||
|
|
NEXT_PUBLIC_API_BASE_URL=http://localhost:8800
|
||
|
|
```
|
||
|
|
|
||
|
|
### 2. Untuk memanggil API production
|
||
|
|
|
||
|
|
```env
|
||
|
|
NEXT_PUBLIC_API_BASE_URL=https://jaecookelapagading.com/api
|
||
|
|
```
|
||
|
|
|
||
|
|
### 3. Menggunakan service
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
// Tanpa authentication
|
||
|
|
import { getDataFromLocal, postDataToLocal } from '@/service/local-api-service';
|
||
|
|
|
||
|
|
const data = await getDataFromLocal('/endpoint');
|
||
|
|
|
||
|
|
// Dengan authentication (menggunakan Bearer token dari cookies)
|
||
|
|
import { getDataWithAuth, postDataWithAuth } from '@/service/local-api-service';
|
||
|
|
|
||
|
|
const data = await getDataWithAuth('/endpoint');
|
||
|
|
```
|
||
|
|
|
||
|
|
## File yang Sudah Diupdate
|
||
|
|
|
||
|
|
- `service/http-config/axios-base-instance.ts` - Membaca dari env variable
|
||
|
|
- `service/http-config/axios-interceptor-instance.ts` - Membaca dari env variable
|
||
|
|
- `service/local-api-service.ts` - Service helper untuk memanggil API
|
||
|
|
|
||
|
|
## Catatan
|
||
|
|
|
||
|
|
- Pastikan file `.env.local` tidak di-commit ke repository
|
||
|
|
- File `.env.local` sudah ada di `.gitignore`
|
||
|
|
- Restart development server setelah mengubah environment variables
|
||
|
|
|