You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+28-2
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Simple API Router
2
2
3
3
## Overview
4
-
**Simple API Router** is a lightweight npm package that simplifies setting up Express APIs. It automatically detects all API routes in a defined directory and binds them to an Express app.
4
+
**Simple API Router** is a lightweight npm package that simplifies setting up Express APIs. It automatically detects all API routes in a defined directory and binds them to an Express app. Additionally, it provides an API client generator for frontend integration and automatic API documentation generation.
5
5
6
6
## Installation
7
7
@@ -37,7 +37,7 @@ app.listen(PORT, () => {
37
37
The package allows easy creation of a sample project with a predefined structure:
38
38
39
39
```bash
40
-
npx n-sar my-api-project
40
+
npx n-sar create my-api-project
41
41
```
42
42
43
43
This creates a new folder `my-api-project` with the following structure:
@@ -59,6 +59,32 @@ npm install
59
59
node server.js
60
60
```
61
61
62
+
### 3. Generate an API Client for Frontend Integration
63
+
64
+
With `n-sar`, you can automatically generate a frontend API client that connects to your backend (localhost or your server domain/IP):
65
+
66
+
```bash
67
+
npx n-sar export-routes http://localhost:3000
68
+
```
69
+
70
+
This creates an `apiClient.js` file with functions for each detected route, making API calls simple:
console.log(`API client generated at ${outputPath}`);
145
+
}
146
+
147
+
functiongenerateDocs(){
148
+
constapiDir=path.join(process.cwd(),'api');
149
+
if(!fs.existsSync(apiDir)){
150
+
console.error('Error: API directory does not exist.');
151
+
process.exit(1);
152
+
}
153
+
154
+
constroutes=scanRoutes(apiDir);
155
+
letdocContent='# API Documentation\n\n## Endpoints\n';
156
+
157
+
routes.forEach(route=>{
158
+
docContent+=`### \`${route}\`\n- **GET**: Fetch data from ${route}\n- **POST**: Send data to ${route}\n- **PUT**: Update data at ${route}\n- **DELETE**: Remove data at ${route}\n\n`;
0 commit comments