Skip to content

Commit f7a2e53

Browse files
author
Andrea Cosentino
committed
docs: update documentations
1 parent 72fa40a commit f7a2e53

4 files changed

Lines changed: 929 additions & 641 deletions

File tree

README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# vite-plugin-universal-api
88

9-
### Seamless Mock APIs, Accelerate Your Development Journey
9+
### Accelerate Your Development Journey with Mock APIs without changing your client code.
1010

1111
[![npm version](https://img.shields.io/npm/v/%40ndriadev/vite-plugin-universal-api?color=orange&style=for-the-badge)](https://www.npmjs.com/package/%40ndriadev/vite-plugin-universal-api)
1212
![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40ndriadev%2Fvite-plugin-universal-api?style=for-the-badge&label=SIZE&color=yellow)
@@ -42,6 +42,10 @@
4242
- [REST API Handlers](#rest-api-handlers)
4343
- [WebSocket Handlers](#websocket-handlers)
4444
- [File-System Based API](#file-system-based-api)
45+
- [How it works](#how-it-works)
46+
- [Vite proxy](#vite-proxy)
47+
- [When to use what](#when-to-use-what)
48+
- [Environment variables](#environment-variables)
4549
- [Usage Examples](#-usage-examples)
4650
- [File-Based Mocking](#file-based-mocking)
4751
- [Custom REST Handlers](#custom-rest-handlers)
@@ -608,7 +612,102 @@ interface WebSocketHandler {
608612
}
609613
```
610614

611-
---
615+
## ⚙️ How it works
616+
617+
**`vite-plugin-universal-api`** only affects development. In production, your application performs real HTTP requests.
618+
In a real application, you typically want to:
619+
620+
- use local APIs during development
621+
- call real APIs in production
622+
623+
You can achieve this with a simple base URL configuration:
624+
625+
```ts
626+
// api.ts
627+
export const API_BASE_URL = import.meta.env.PROD
628+
? 'https://api.example.com'
629+
: '/api'
630+
631+
632+
// usage.ts
633+
import {API_BASE_URL }from'./api'
634+
635+
export async function getUsers() {
636+
const res = await fetch(`${API_BASE_URL}/users`)
637+
return res.json()
638+
}
639+
```
640+
641+
- **In development**
642+
- **`/api/users`** is intercepted by **`vite-plugin-universal-api`**
643+
- you can return mocked or locally defined responses
644+
- **In production**
645+
- requests go directly to **`https://api.example.com/users`**
646+
647+
This means you can develop without a backend and switch to real APIs without changing your code.
648+
649+
### Vite proxy
650+
Instead of defining local API endpoints, you can forward requests to a real backend during development using Vite's proxy.
651+
652+
```ts
653+
// vite.config.ts
654+
export default {
655+
server: {
656+
proxy: {
657+
'/api': {
658+
target: 'https://api.example.com',
659+
changeOrigin: true,
660+
rewrite: (path) => path.replace(/^\/api/, '')
661+
}
662+
}
663+
}
664+
}
665+
```
666+
667+
You can then call your API as usual:
668+
669+
```ts
670+
// in your file
671+
...
672+
fetch('/api/users')
673+
```
674+
675+
### When to use what
676+
677+
- **`vite-plugin-universal-api`** → mock or local APIs
678+
- Vite proxy → real backend during development
679+
680+
Both approaches work with the same client code.
681+
682+
### Environment variables
683+
684+
For more flexibility, use .env files:
685+
686+
```bash
687+
# .env.development
688+
VITE_API_BASE_URL=/api
689+
690+
# .env.production
691+
VITE_API_BASE_URL=https://api.example.com
692+
```
693+
694+
```ts
695+
// api.ts
696+
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL
697+
698+
// services/users.ts
699+
import {API_BASE_URL }from'../api'
700+
701+
export async function getUsers() {
702+
const res = await fetch(`${API_BASE_URL}/users`)
703+
return res.json()
704+
}
705+
```
706+
707+
### Result
708+
709+
- **`Development`** → mocked response from local files
710+
- **`Production`** → real API calls
612711

613712
## 💡 Usage Examples
614713

docs/examples/index.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,71 @@ universalApi({
8686
})
8787
```
8888

89+
## Development strategies
90+
91+
### 1. Local APIs (vite-plugin-universal-api)
92+
93+
- fully mocked
94+
- no backend required
95+
96+
```ts
97+
// api/users.handler.ts
98+
export default {
99+
pattern: '/users/{id}',
100+
method: 'GET',
101+
handle: async (req, res) => {
102+
const user = await db.findUser(req.params.id)
103+
res.writeHead(200, { 'Content-Type': 'application/json' })
104+
res.end(JSON.stringify(user))
105+
}
106+
}
107+
```
108+
109+
### 2. Real backend via Vite proxy
110+
111+
- no mocks
112+
- real API during development
113+
114+
```ts
115+
// vite.config.ts
116+
export default {
117+
server: {
118+
proxy: {
119+
'/api': {
120+
target:'https://api.example.com',
121+
changeOrigin:true,
122+
rewrite: (path) =>path.replace(/^\/api/,'')
123+
}
124+
}
125+
}
126+
}
127+
```
128+
129+
### Shared client code
130+
131+
```ts
132+
fetch('/api/users')
133+
```
134+
135+
Both approaches work without changing your application code. You can switch between mocked APIs and a real backend without changing your client code.
136+
137+
### Optional: Environment variables
138+
139+
For more flexibility, use **`.env`** files:
140+
141+
```bash
142+
# .env.development
143+
VITE_API_BASE_URL=/api
144+
145+
# .env.production
146+
VITE_API_BASE_URL=https://api.example.com
147+
```
148+
149+
```ts
150+
// api.ts
151+
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL
152+
```
153+
89154
## Browse All Examples
90155

91156
Click on any example below to see the complete implementation:

docs/guide/getting-started.md

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,26 @@ Add some mock data:
114114
]
115115
```
116116

117-
### 3. Start Development Server
117+
This file will handle requests to:
118+
119+
```ts
120+
GET /api/users
121+
```
122+
123+
### 3. Call the API from your app
124+
```ts
125+
// services/users.ts
126+
export async function getUsers() {
127+
const res = await fetch('/api/users')
128+
129+
if (!res.ok) {
130+
throw new Error('Failed to fetch users')
131+
}
132+
133+
return res.json()
134+
}
135+
```
136+
### 4. Start Development Server
118137

119138
```bash
120139
npm run dev
@@ -124,9 +143,10 @@ npm run dev
124143

125144
Your mock data is now available:
126145

127-
```bash
146+
```ts
128147
# Fetch all users
129-
curl http://localhost:5173/api/users
148+
...
149+
const result = await getUsers();
130150

131151
# Returns:
132152
# [
@@ -135,6 +155,101 @@ curl http://localhost:5173/api/users
135155
# ]
136156
```
137157

158+
## 🌐 Using real APIs in production
159+
160+
In a real application, you usually want:
161+
162+
- **mocked endpoints during development**
163+
- **real APIs in production**
164+
165+
You can achieve this with a simple configuration.
166+
167+
### Centralize your API base URL
168+
169+
```ts
170+
// api.ts
171+
export const API_BASE_URL=import.meta.env.PROD
172+
? 'https://api.example.com'
173+
: '/api'
174+
```
175+
176+
### Use it in your services
177+
178+
```ts
179+
// services/users.ts
180+
import {API_BASE_URL }from'../api'
181+
182+
export async function getUsers() {
183+
const res = await fetch(`${API_BASE_URL}/users`)
184+
185+
if (!res.ok) {
186+
throw new Error('Failed to fetch users')
187+
}
188+
189+
return res.json()
190+
}
191+
```
192+
193+
### How it works
194+
195+
- **Development**
196+
- **`/api/users`** is handled by **`vite-plugin-universal-api`**
197+
- responses come from your local files (e.g. **`api/users.get.ts`**)
198+
- **Production**
199+
- requests go to **`https://api.example.com/users`**
200+
- your app behaves like a standard client
201+
202+
### **::: tip**
203+
204+
**`vite-plugin-universal-api`** only affects development.
205+
206+
In production, your application performs real HTTP requests.
207+
208+
:::
209+
210+
### 🧪 Optional: Environment variables
211+
212+
For more flexibility, use **`.env`** files:
213+
214+
```bash
215+
# .env.development
216+
VITE_API_BASE_URL=/api
217+
218+
# .env.production
219+
VITE_API_BASE_URL=https://api.example.com
220+
```
221+
222+
```ts
223+
// api.ts
224+
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL
225+
```
226+
227+
### 🔧 Alternative: Vite proxy
228+
229+
If you prefer not to define local endpoints, you can proxy requests:
230+
231+
```ts
232+
// vite.config.ts
233+
export default {
234+
server: {
235+
proxy: {
236+
'/api': {
237+
target:'https://api.example.com',
238+
changeOrigin:true,
239+
rewrite: (path) =>path.replace(/^\/api/,'')
240+
}
241+
}
242+
}
243+
}
244+
```
245+
246+
### 🧠 Summary
247+
248+
- define API routes as files during development
249+
- call them using **`fetch('/api/...')`**
250+
- switch to real APIs in production with a base URL
251+
- no changes needed in your business logic
252+
138253
## What's Next?
139254

140255
Now that you have a basic setup running, explore more features:

0 commit comments

Comments
 (0)