lapeeh 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +14 -0
- package/LICENSE +21 -0
- package/bin/index.js +934 -0
- package/doc/en/ARCHITECTURE_GUIDE.md +79 -0
- package/doc/en/CHANGELOG.md +203 -0
- package/doc/en/CHEATSHEET.md +90 -0
- package/doc/en/CLI.md +111 -0
- package/doc/en/CONTRIBUTING.md +119 -0
- package/doc/en/DEPLOYMENT.md +171 -0
- package/doc/en/FAQ.md +69 -0
- package/doc/en/FEATURES.md +99 -0
- package/doc/en/GETTING_STARTED.md +84 -0
- package/doc/en/INTRODUCTION.md +62 -0
- package/doc/en/PACKAGES.md +63 -0
- package/doc/en/PERFORMANCE.md +98 -0
- package/doc/en/ROADMAP.md +104 -0
- package/doc/en/SECURITY.md +95 -0
- package/doc/en/STRUCTURE.md +79 -0
- package/doc/en/TUTORIAL.md +145 -0
- package/doc/id/ARCHITECTURE_GUIDE.md +76 -0
- package/doc/id/CHANGELOG.md +203 -0
- package/doc/id/CHEATSHEET.md +90 -0
- package/doc/id/CLI.md +139 -0
- package/doc/id/CONTRIBUTING.md +119 -0
- package/doc/id/DEPLOYMENT.md +171 -0
- package/doc/id/FAQ.md +69 -0
- package/doc/id/FEATURES.md +169 -0
- package/doc/id/GETTING_STARTED.md +91 -0
- package/doc/id/INTRODUCTION.md +62 -0
- package/doc/id/PACKAGES.md +63 -0
- package/doc/id/PERFORMANCE.md +100 -0
- package/doc/id/ROADMAP.md +107 -0
- package/doc/id/SECURITY.md +94 -0
- package/doc/id/STRUCTURE.md +79 -0
- package/doc/id/TUTORIAL.md +145 -0
- package/docker-compose.yml +24 -0
- package/ecosystem.config.js +17 -0
- package/eslint.config.mjs +26 -0
- package/gitignore.template +30 -0
- package/lib/bootstrap.ts +210 -0
- package/lib/core/realtime.ts +34 -0
- package/lib/core/redis.ts +139 -0
- package/lib/core/serializer.ts +63 -0
- package/lib/core/server.ts +70 -0
- package/lib/core/store.ts +116 -0
- package/lib/middleware/auth.ts +63 -0
- package/lib/middleware/error.ts +50 -0
- package/lib/middleware/multipart.ts +13 -0
- package/lib/middleware/rateLimit.ts +14 -0
- package/lib/middleware/requestLogger.ts +27 -0
- package/lib/middleware/visitor.ts +178 -0
- package/lib/utils/logger.ts +100 -0
- package/lib/utils/pagination.ts +56 -0
- package/lib/utils/response.ts +88 -0
- package/lib/utils/validator.ts +394 -0
- package/nodemon.json +6 -0
- package/package.json +126 -0
- package/readme.md +357 -0
- package/scripts/check-update.js +92 -0
- package/scripts/config-clear.js +45 -0
- package/scripts/generate-jwt-secret.js +38 -0
- package/scripts/init-project.js +84 -0
- package/scripts/make-module.js +89 -0
- package/scripts/release.js +494 -0
- package/scripts/seed-json.js +158 -0
- package/scripts/verify-rbac-functional.js +187 -0
- package/src/config/app.ts +9 -0
- package/src/config/cors.ts +5 -0
- package/src/modules/Auth/auth.controller.ts +519 -0
- package/src/modules/Rbac/rbac.controller.ts +533 -0
- package/src/routes/auth.ts +74 -0
- package/src/routes/index.ts +7 -0
- package/src/routes/rbac.ts +42 -0
- package/storage/logs/.gitkeep +0 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +30 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Roadmap & Future Requests (Rencana Pengembangan)
|
|
2
|
+
|
|
3
|
+
Dokumen ini berisi daftar fitur yang direncanakan untuk membuat **lapeeh Framework** setara dengan framework backend enterprise lainnya (seperti NestJS, Laravel, atau Django).
|
|
4
|
+
|
|
5
|
+
Ini adalah **Undangan Terbuka** bagi para kontributor! Jika Anda tertarik mengerjakan salah satu fitur di bawah ini, silakan buat Issue/PR.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 🏗️ Core & Architecture
|
|
10
|
+
|
|
11
|
+
### 1. Dependency Injection (DI) Container
|
|
12
|
+
|
|
13
|
+
- **Tujuan**: Membuat kode lebih testable dan modular.
|
|
14
|
+
- **Konsep**: Saat ini lapeeh menggunakan pendekatan import langsung. Implementasi DI sederhana (seperti `InversifyJS` atau custom container) akan memudahkan unit testing dan decoupling.
|
|
15
|
+
- **Inspirasi**: NestJS Providers, Angular DI.
|
|
16
|
+
|
|
17
|
+
### 2. Event Emitter (Pub/Sub)
|
|
18
|
+
|
|
19
|
+
- **Tujuan**: Decoupling logic antar modul.
|
|
20
|
+
- **Contoh**: Saat user register -> Emit event `UserRegistered` -> Listener `SendWelcomeEmail` & `CreateWallet` berjalan terpisah.
|
|
21
|
+
- **Status**: Belum ada (bisa pakai `eventemitter3`).
|
|
22
|
+
|
|
23
|
+
### 3. API Documentation Generator (Swagger/OpenAPI)
|
|
24
|
+
|
|
25
|
+
- **Tujuan**: Auto-generate dokumentasi API interaktif.
|
|
26
|
+
- **Rencana**: Membaca file route dan validator schema, lalu men-generate spesifikasi Swagger UI secara otomatis di `/api/docs`.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 🛠️ Fitur Enterprise (The "Missing" Parts)
|
|
31
|
+
|
|
32
|
+
### 4. Job Queues & Background Workers
|
|
33
|
+
|
|
34
|
+
- **Tujuan**: Memproses tugas berat di background agar tidak memblokir HTTP request.
|
|
35
|
+
- **Teknologi**: Integrasi dengan **BullMQ** (Redis).
|
|
36
|
+
- **Fitur**:
|
|
37
|
+
- Decorators/Helpers untuk mendefinisikan Job.
|
|
38
|
+
- Dashboard monitoring job (seperti Horizon di Laravel).
|
|
39
|
+
- Retry mechanism & Failed job handling.
|
|
40
|
+
|
|
41
|
+
### 5. Task Scheduling (Cron Jobs)
|
|
42
|
+
|
|
43
|
+
- **Tujuan**: Menjalankan tugas berkala (misal: "Kirim rekap email setiap jam 12 malam").
|
|
44
|
+
- **Rencana**: Wrapper di atas `node-cron` yang terintegrasi dengan struktur framework.
|
|
45
|
+
- **CLI**: `npm run schedule:run`.
|
|
46
|
+
|
|
47
|
+
### 6. Storage Abstraction Layer
|
|
48
|
+
|
|
49
|
+
- **Tujuan**: Satu interface untuk upload file ke berbagai provider (Local, AWS S3, Google Cloud Storage, MinIO).
|
|
50
|
+
- **Konsep**:
|
|
51
|
+
```typescript
|
|
52
|
+
// Tidak peduli drivernya apa, kodenya sama:
|
|
53
|
+
await Storage.disk("s3").put("avatars/1.jpg", fileBuffer);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 7. Mailer Service
|
|
57
|
+
|
|
58
|
+
- **Tujuan**: Standarisasi pengiriman email.
|
|
59
|
+
- **Fitur**:
|
|
60
|
+
- Support SMTP, Mailgun, SES.
|
|
61
|
+
- Support Template Engine (Handlebars/EJS) untuk body email.
|
|
62
|
+
- Queue integration (kirim email di background).
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## 🧪 Testing & Quality Assurance
|
|
67
|
+
|
|
68
|
+
### 8. Native Testing Suite
|
|
69
|
+
|
|
70
|
+
- **Tujuan**: Memudahkan user menulis test.
|
|
71
|
+
- **Rencana**:
|
|
72
|
+
- Integrasi **Vitest** atau **Jest** pre-configured.
|
|
73
|
+
- Helper untuk HTTP Testing (supertest wrapper).
|
|
74
|
+
- Helper untuk Database Transaction (rollback setelah test selesai).
|
|
75
|
+
- Command: `npm run test`, `npm run make:test`.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## 🌐 Globalization & Security
|
|
80
|
+
|
|
81
|
+
### 9. Localization (i18n)
|
|
82
|
+
|
|
83
|
+
- **Tujuan**: Mendukung respon API dalam berbagai bahasa.
|
|
84
|
+
- **Fitur**: Mendeteksi header `Accept-Language` dan mengembalikan pesan error/success sesuai bahasa user.
|
|
85
|
+
|
|
86
|
+
### 10. API Versioning
|
|
87
|
+
|
|
88
|
+
- **Tujuan**: Mendukung multiple versi API (v1, v2) tanpa merusak klien lama.
|
|
89
|
+
- **Rencana**: Routing strategy berbasis URL prefix (`/api/v1/...`) atau Header.
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 💻 CLI Enhancements
|
|
94
|
+
|
|
95
|
+
### 11. Interactive CLI (TUI)
|
|
96
|
+
|
|
97
|
+
- **Tujuan**: Membuat CLI lebih user friendly.
|
|
98
|
+
- **Ide**: Saat menjalankan `npx lapeeh`, muncul menu interaktif untuk memilih fitur yang mau diinstall (pilih DB, pilih mau pakai Redis atau tidak, dll).
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Tertarik Berkontribusi?
|
|
103
|
+
|
|
104
|
+
Pilih salah satu topik di atas, buat Issue di GitHub dengan judul `[Proposal] Nama Fitur`, dan mari kita diskusikan cara implementasinya! 🚀
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Security Best Practices (Panduan Keamanan)
|
|
2
|
+
|
|
3
|
+
Keamanan bukan fitur tambahan, melainkan prioritas utama. lapeeh Framework sudah dikonfigurasi dengan standar keamanan yang baik (Secure by Default), namun Anda tetap perlu memahami cara menjaga aplikasi tetap aman.
|
|
4
|
+
|
|
5
|
+
## 1. Perlindungan Bawaan (Built-in Protection)
|
|
6
|
+
|
|
7
|
+
Secara default, lapeeh sudah mengaktifkan:
|
|
8
|
+
|
|
9
|
+
### Helmet (HTTP Headers)
|
|
10
|
+
|
|
11
|
+
Mengamankan aplikasi dari serangan umum web vulnerabilities dengan mengatur HTTP headers yang tepat.
|
|
12
|
+
|
|
13
|
+
- **XSS Filter**: Aktif.
|
|
14
|
+
- **Frameguard**: Mencegah Clickjacking.
|
|
15
|
+
- **HSTS**: Memaksa browser menggunakan HTTPS (di production).
|
|
16
|
+
|
|
17
|
+
### Rate Limiting
|
|
18
|
+
|
|
19
|
+
Mencegah serangan _Brute Force_ dan _DDoS_ sederhana.
|
|
20
|
+
|
|
21
|
+
- **Default**: 100 request per 15 menit per IP.
|
|
22
|
+
- **Lokasi Config**: `src/middleware/rateLimit.ts`.
|
|
23
|
+
|
|
24
|
+
### CORS (Cross-Origin Resource Sharing)
|
|
25
|
+
|
|
26
|
+
Mengontrol domain mana yang boleh mengakses API Anda.
|
|
27
|
+
|
|
28
|
+
- **Lokasi Config**: `src/core/server.ts`.
|
|
29
|
+
- **Saran**: Di production, ubah `origin: "*"` menjadi domain frontend spesifik Anda (misal `origin: "https://frontend-anda.com"`).
|
|
30
|
+
|
|
31
|
+
## 2. Praktik Terbaik Developer
|
|
32
|
+
|
|
33
|
+
Framework tidak bisa melindungi dari kode yang buruk. Berikut hal yang WAJIB Anda lakukan:
|
|
34
|
+
|
|
35
|
+
### a. Validasi Input (Wajib!)
|
|
36
|
+
|
|
37
|
+
Jangan pernah mempercayai input user. Selalu gunakan `Validator`.
|
|
38
|
+
|
|
39
|
+
**❌ Insecure (Raw Query):**
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
// Danger of SQL Injection if using manual raw query without sanitization
|
|
43
|
+
const user = await db.query(
|
|
44
|
+
`SELECT * FROM users WHERE email = '${req.body.email}'`
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**✅ Secure (Validator + Parameterized Query):**
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const valid = await Validator.make(req.body, { email: "required|email" });
|
|
52
|
+
// Use parameterized query or your preferred ORM/Query Builder
|
|
53
|
+
const user = await db.users.findOne({
|
|
54
|
+
where: { email: valid.validated().email },
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### b. Manajemen Password
|
|
59
|
+
|
|
60
|
+
- **Hashing**: Jangan pernah simpan password plain text. Gunakan `bcryptjs` (sudah terintegrasi di AuthController).
|
|
61
|
+
- **Strength**: Validasi password minimal 8 karakter.
|
|
62
|
+
```typescript
|
|
63
|
+
password: "required|string|min:8";
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### c. JWT & Session
|
|
67
|
+
|
|
68
|
+
- **Secret Key**: Pastikan `JWT_SECRET` di `.env` adalah string acak yang panjang (32+ karakter).
|
|
69
|
+
- **Expiration**: Token akses (`ACCESS_TOKEN_EXPIRES_IN`) sebaiknya pendek (misal 15 menit - 1 jam). Gunakan Refresh Token (fitur roadmap) untuk sesi panjang.
|
|
70
|
+
|
|
71
|
+
### d. Error Handling
|
|
72
|
+
|
|
73
|
+
Jangan pernah mengekspos detail error sistem (stack trace) ke user di Production.
|
|
74
|
+
|
|
75
|
+
**❌ Buruk:**
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
res.status(500).json({ error: err.message, stack: err.stack }); // Hacker bisa lihat struktur folder server
|
|
79
|
+
**✅ Aman (lapeeh Default):**
|
|
80
|
+
lapeeh sudah menangani ini. Di mode `production`, error internal hanya akan menampilkan "Internal Server Error" tanpa detail sensitif.
|
|
81
|
+
**✅ Aman (Lapeeh Default):**
|
|
82
|
+
Lapeeh sudah menangani ini. Di mode `production`, error internal hanya akan menampilkan "Internal Server Error" tanpa detail sensitif.
|
|
83
|
+
|
|
84
|
+
## 3. Checklist Sebelum Deploy
|
|
85
|
+
|
|
86
|
+
- [ ] Pastikan `NODE_ENV=production` di server.
|
|
87
|
+
- [ ] Ganti `JWT_SECRET` default dengan yang baru (`npm run generate:jwt`).
|
|
88
|
+
- [ ] Batasi CORS origin hanya ke domain frontend Anda.
|
|
89
|
+
- [ ] Pastikan database tidak bisa diakses publik (gunakan firewall/private IP).
|
|
90
|
+
- [ ] Gunakan HTTPS (SSL/TLS). API tanpa HTTPS sangat mudah disadap.
|
|
91
|
+
|
|
92
|
+
## 4. Melaporkan Celah Keamanan
|
|
93
|
+
|
|
94
|
+
Jika Anda menemukan celah keamanan di framework ini, mohon **JANGAN** buat Issue publik. Kirim email langsung ke maintainer (lihat `package.json`) agar bisa diperbaiki (patched) sebelum diexploitasi orang jahat.
|
|
95
|
+
```
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# 🏗️ Project Structure
|
|
2
|
+
|
|
3
|
+
lapeeh Framework is designed to be **intuitive**. We separate your application code (_User Space_) from the framework engine (_Core_), so you can focus on building features without being distracted by system complexity.
|
|
4
|
+
|
|
5
|
+
## 🗺️ Navigation Map
|
|
6
|
+
|
|
7
|
+
Here is a mental map to understand lapeeh folders:
|
|
8
|
+
|
|
9
|
+
| Folder | Icon | Function | Status |
|
|
10
|
+
| :------------- | :--: | :---------------------------------------------------------------------- | :-------------- |
|
|
11
|
+
| **`src/`** | 🏠 | **YOUR APP CODE**. You work here every day. | **Must Edit** |
|
|
12
|
+
| **`scripts/`** | 🤖 | **Automation Assistants**. Scripts for release, module generation, etc. | **Can Edit** |
|
|
13
|
+
| **`bin/`** | 🚀 | **CLI Tools**. Entry point for `npx lapeeh` commands. | **Rarely Edit** |
|
|
14
|
+
| **`lib/`** | ⚙️ | **Framework Engine**. Core server & database logic. | **DO NOT Edit** |
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 1. 🏠 Folder `src/` (User Space)
|
|
19
|
+
|
|
20
|
+
This is your "home". 99% of new feature code will be written here.
|
|
21
|
+
|
|
22
|
+
### `src/modules/` (Modular Architecture)
|
|
23
|
+
|
|
24
|
+
lapeeh uses a **Modular** approach. One feature = One folder.
|
|
25
|
+
|
|
26
|
+
- **Example**: `src/modules/Auth/` folder contains all login/register logic.
|
|
27
|
+
- **Benefit**: Code is neat, easy to find, and easy to delete if the feature is no longer used.
|
|
28
|
+
|
|
29
|
+
### `src/routes/`
|
|
30
|
+
|
|
31
|
+
Your API gateway.
|
|
32
|
+
|
|
33
|
+
- Defines URLs (e.g., `/api/users`).
|
|
34
|
+
- Connects URLs to **Controllers**.
|
|
35
|
+
|
|
36
|
+
### `src/config/`
|
|
37
|
+
|
|
38
|
+
Application settings center.
|
|
39
|
+
|
|
40
|
+
- `app.ts`: Global configuration.
|
|
41
|
+
- `cors.ts`: Domain access security.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## 2. 🤖 Folder `scripts/` (Robot Assistants)
|
|
46
|
+
|
|
47
|
+
Don't do boring things manually! lapeeh provides robots here:
|
|
48
|
+
|
|
49
|
+
- **`release.js`**: **Super Script** for releasing new versions.
|
|
50
|
+
- ✨ Automatic version bump (package.json).
|
|
51
|
+
- 📝 **Auto-Blog**: Automatically creates release articles from Git Commit history.
|
|
52
|
+
- 🔄 **Auto-Sync**: Synchronizes documentation to the website.
|
|
53
|
+
- 🚀 Push to Git & Publish to NPM in one command.
|
|
54
|
+
- **`make-module.js`**: Generator to create new module folder structures instantly.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 3. 🚀 Folder `bin/` (CLI & Update)
|
|
59
|
+
|
|
60
|
+
This folder handles `npx lapeeh` terminal commands.
|
|
61
|
+
|
|
62
|
+
- Provides **Auto-Update Check** feature when you run `npm run dev`.
|
|
63
|
+
- Handles `upgrade` command to sync your project with the latest lapeeh version without breaking your code.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## 4. ⚙️ Folder `lib/` (The Core)
|
|
68
|
+
|
|
69
|
+
lapeeh's "Engine Room". Contains Express setup, Database connection, Logger, and basic Middleware.
|
|
70
|
+
|
|
71
|
+
> ⚠️ **Warning**: Changing the contents of this folder can make the application difficult to update to the next lapeeh version.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## 📄 Other Important Files
|
|
76
|
+
|
|
77
|
+
- **`.env`**: **Secret Keys**. Store DB passwords and API Keys here. (Do not upload to Git!)
|
|
78
|
+
- **`package.json`**: List of your project's library "shopping list".
|
|
79
|
+
- **`ecosystem.config.js`**: Ready to deploy? This file configures how the application runs on a production server (VPS) using PM2.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Tutorial: Building a Library API
|
|
2
|
+
|
|
3
|
+
In this tutorial, we will build a simple "Book Management" feature using lapeeh Framework's core features:
|
|
4
|
+
|
|
5
|
+
1. **CLI** for code generation.
|
|
6
|
+
2. **Validator** for input validation.
|
|
7
|
+
3. **Fast Serialization** for high-performance responses.
|
|
8
|
+
|
|
9
|
+
> **Note**: This tutorial uses an in-memory array for data storage to keep things simple. lapeeh v3.0.0 is database-agnostic, so you can easily swap this with Prisma, TypeORM, or any other DB library.
|
|
10
|
+
|
|
11
|
+
## Step 1: Generate Module (Controller & Route)
|
|
12
|
+
|
|
13
|
+
We will create a controller and route for our Book feature.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm run make:module Book
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The framework will generate:
|
|
20
|
+
|
|
21
|
+
- `src/controllers/bookController.ts`
|
|
22
|
+
- `src/routes/book.ts`
|
|
23
|
+
|
|
24
|
+
## Step 2: Implement Controller
|
|
25
|
+
|
|
26
|
+
Open `src/controllers/bookController.ts` and let's implement **Create** and **List** features.
|
|
27
|
+
|
|
28
|
+
### Setup Import & Data Store
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Request, Response } from "express";
|
|
32
|
+
import { sendFastSuccess, sendError } from "@/utils/response";
|
|
33
|
+
import { Validator } from "@/utils/validator";
|
|
34
|
+
import { getSerializer, createResponseSchema } from "@/core/serializer";
|
|
35
|
+
|
|
36
|
+
// Simple in-memory store
|
|
37
|
+
interface Book {
|
|
38
|
+
id: string;
|
|
39
|
+
title: string;
|
|
40
|
+
author: string;
|
|
41
|
+
isbn: string;
|
|
42
|
+
stock: number;
|
|
43
|
+
}
|
|
44
|
+
const books: Book[] = [];
|
|
45
|
+
|
|
46
|
+
// 1. Define Output Schema (for Fastify Serialization)
|
|
47
|
+
const bookSchema = {
|
|
48
|
+
type: "object",
|
|
49
|
+
properties: {
|
|
50
|
+
id: { type: "string" },
|
|
51
|
+
title: { type: "string" },
|
|
52
|
+
author: { type: "string" },
|
|
53
|
+
isbn: { type: "string" },
|
|
54
|
+
stock: { type: "number" },
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// 2. Create Serializer
|
|
59
|
+
const bookDetailSerializer = getSerializer(
|
|
60
|
+
"book-detail",
|
|
61
|
+
createResponseSchema(bookSchema)
|
|
62
|
+
);
|
|
63
|
+
const bookListSerializer = getSerializer(
|
|
64
|
+
"book-list",
|
|
65
|
+
createResponseSchema({
|
|
66
|
+
type: "array",
|
|
67
|
+
items: bookSchema,
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Implement Create (with Validation)
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
export async function createBook(req: Request, res: Response) {
|
|
76
|
+
// 1. Validate Input
|
|
77
|
+
const validator = await Validator.make(req.body, {
|
|
78
|
+
title: "required|string|min:3",
|
|
79
|
+
author: "required|string",
|
|
80
|
+
isbn: "required|string",
|
|
81
|
+
stock: "required|number|min:1",
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (validator.fails()) {
|
|
85
|
+
return sendError(res, 400, "Validation Error", validator.errors());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 2. Save Data (In-Memory)
|
|
89
|
+
const newBook: Book = {
|
|
90
|
+
id: Date.now().toString(),
|
|
91
|
+
...validator.validated(),
|
|
92
|
+
};
|
|
93
|
+
books.push(newBook);
|
|
94
|
+
|
|
95
|
+
// 3. Send Response (Serialized)
|
|
96
|
+
return sendFastSuccess(res, bookDetailSerializer(newBook), 201);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function listBooks(req: Request, res: Response) {
|
|
100
|
+
// Return all books
|
|
101
|
+
return sendFastSuccess(res, bookListSerializer(books));
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Step 3: Register Route
|
|
106
|
+
|
|
107
|
+
Open `src/routes/book.ts`. The CLI has already generated the basic structure. We just need to connect it to our controller functions.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { Router } from "express";
|
|
111
|
+
import { createBook, listBooks } from "../controllers/bookController";
|
|
112
|
+
|
|
113
|
+
const router = Router();
|
|
114
|
+
|
|
115
|
+
router.post("/", createBook);
|
|
116
|
+
router.get("/", listBooks);
|
|
117
|
+
|
|
118
|
+
export default router;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Step 4: Test Your API
|
|
122
|
+
|
|
123
|
+
Run the server:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npm run dev
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Test with curl or Postman:
|
|
130
|
+
|
|
131
|
+
**Create Book:**
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
curl -X POST http://localhost:8000/api/book \
|
|
135
|
+
-H "Content-Type: application/json" \
|
|
136
|
+
-d '{"title":"lapeeh Guide", "author":"Team", "isbn":"12345", "stock":10}'
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**List Books:**
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
curl http://localhost:8000/api/book
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Congratulations! You have built a fast, validated API without getting bogged down in database configuration.
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Panduan Arsitektur: Menuju "Framework as a Dependency" (Next.js Style)
|
|
2
|
+
|
|
3
|
+
Saat ini, lapeeh menggunakan pendekatan **Boilerplate** (seperti Laravel), di mana pengguna mendapatkan seluruh kode sumber (`src/`) dan bertanggung jawab atas `express`, `driver database`, dll.
|
|
4
|
+
|
|
5
|
+
Untuk mengubahnya menjadi seperti **Next.js** (di mana pengguna hanya menginstall `lapeeh` dan `package.json` mereka bersih), kita perlu mengubah arsitektur menjadi **Library**.
|
|
6
|
+
|
|
7
|
+
## 1. Perbedaan Utama
|
|
8
|
+
|
|
9
|
+
| Fitur | Boilerplate (lapeeh Saat Ini) | Library (Next.js Style) |
|
|
10
|
+
| :--------------- | :----------------------------------------- | :----------------------------------- |
|
|
11
|
+
| **Instalasi** | `git clone` / `npx create-lapeeh` | `npm install lapeeh` |
|
|
12
|
+
| **package.json** | Banyak dependency (`express`, `cors`, dll) | Sedikit (`lapeeh`, `react`) |
|
|
13
|
+
| **Scripts** | Panjang (`nodemon src/index.ts`) | Pendek (`lapeeh dev`) |
|
|
14
|
+
| **Core Code** | Terbuka di `src/core/` | Tersembunyi di `node_modules/lapeeh` |
|
|
15
|
+
| **Update** | Susah (harus merge manual) | Mudah (`npm update lapeeh`) |
|
|
16
|
+
|
|
17
|
+
## 2. Langkah Implementasi
|
|
18
|
+
|
|
19
|
+
Saya telah memulai langkah pertama dengan menambahkan **CLI Runner** di `bin/index.js`.
|
|
20
|
+
|
|
21
|
+
### A. Update CLI (`bin/index.js`) ✅ (Sudah Dilakukan)
|
|
22
|
+
|
|
23
|
+
Saya sudah menambahkan command `dev`, `start`, dan `build` ke dalam CLI lapeeh. Ini memungkinkan pengguna menjalankan server tanpa tahu perintah aslinya.
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// Contoh penggunaan nanti:
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "lapeeh dev",
|
|
29
|
+
"build": "lapeeh build",
|
|
30
|
+
"start": "lapeeh start"
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### B. Struktur Project Pengguna (Target)
|
|
35
|
+
|
|
36
|
+
Nantinya, project pengguna lapeeh hanya akan berisi file bisnis mereka:
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
my-app/
|
|
40
|
+
├── src/
|
|
41
|
+
│ ├── controllers/
|
|
42
|
+
│ ├── routes/
|
|
43
|
+
│ └── models/
|
|
44
|
+
├── lapeeh.config.ts <-- Konfigurasi framework (pengganti edit core)
|
|
45
|
+
└── package.json
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Dan `package.json` mereka akan terlihat seperti ini:
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"lapeeh": "^2.0.0"
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@lapeeh/lapeeh": "^2.0.0"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"dev": "lapeeh dev",
|
|
58
|
+
"build": "lapeeh build",
|
|
59
|
+
"start": "lapeeh start"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### C. Apa yang Harus Dilakukan Selanjutnya?
|
|
65
|
+
|
|
66
|
+
1. **Publish Package**: Anda perlu mempublish folder framework ini ke NPM (atau private registry).
|
|
67
|
+
- Pastikan `express`, `cors`, `helmet`, dll ada di `dependencies` (bukan `devDependencies`).
|
|
68
|
+
2. **Abstraksi `src/index.ts`**:
|
|
69
|
+
- Saat ini `src/index.ts` adalah entry point yang diedit user.
|
|
70
|
+
- Ubah agar `lapeeh dev` menjalankan server internal yang **mengimpor** routes/controller user secara dinamis (seperti Next.js pages router).
|
|
71
|
+
3. **Config Loader**:
|
|
72
|
+
- Buat sistem pembacaan `lapeeh.config.ts` untuk mengatur Port, Database URL, dll tanpa mengedit kode core.
|
|
73
|
+
|
|
74
|
+
## 3. Kesimpulan
|
|
75
|
+
|
|
76
|
+
Perubahan yang saya lakukan di `bin/index.js` adalah fondasi untuk CLI style. Untuk mencapai "Clean package.json" sepenuhnya, Anda harus memisahkan **Framework Core** (repo ini) dengan **User Project** (repo baru yang menginstall framework ini).
|