Panduan Pembuatan RESTful API dengan NestJS

Sep 9, 2024

Catatan Kuliah: Pembuatan RESTful API Menggunakan NestJS

Pengenalan

  • Nama pembicara: Eko Kurniawan
  • Pekerjaan: Technical Architect di e-commerce terbesar di Indonesia
  • Pengalaman: 13 tahun di bidang programming
  • Konten terkait: Programmer Zaman Now (website & YouTube)

Persiapan Sebelum Kelas

  • Pastikan telah mengikuti kelas sebelumnya:
    • Javascript
    • Node.js
    • TypeScript
    • NestJS

Kasus Studi: RESTful API

Tujuan

  • Membangun RESTful API untuk Contact Management

Fitur Utama

  1. User Management

    • Registrasi
    • Login
    • Update User
    • Get User
    • Logout
  2. Contact Management

    • Create Contact
    • Update Contact
    • Get Contact
    • Search Contact
    • Remove Contact
  3. Address Management

    • Create Address
    • Update Address
    • Get Address
    • List Addresses
    • Remove Address

Data User

  • Data yang diperlukan:
    • username (wajib)
    • password (wajib)
    • name (wajib)

API User Management

  • Registrasi API: POST /api/users
  • Login API: POST /api/users/login
  • Update User API: PATCH /api/users/current
  • Get User API: GET /api/users/current
  • Logout API: DELETE /api/users/current

Data Kontak

  • Data yang diperlukan:
    • firstName (wajib)
    • lastName (opsional)
    • email (opsional)
    • phone (opsional)

API Contact Management

  • Create Contact API: POST /api/contacts
  • Get Contact API: GET /api/contacts/{id}
  • Update Contact API: PUT /api/contacts/{id}
  • Remove Contact API: DELETE /api/contacts/{id}
  • Search Contact API: GET /api/contacts/search

Data Alamat

  • Data yang diperlukan:
    • street (opsional)
    • city (opsional)
    • province (opsional)
    • country (wajib)
    • postalCode (wajib)

API Address Management

  • Create Address API: POST /api/contacts/{contactId}/addresses
  • Get Address API: GET /api/contacts/{contactId}/addresses/{id}
  • Update Address API: PUT /api/contacts/{contactId}/addresses/{id}
  • Remove Address API: DELETE /api/contacts/{contactId}/addresses/{id}
  • List Addresses API: GET /api/contacts/{contactId}/addresses

Pembuatan Project

  1. Buat project baru dengan perintah: nest new belajar-nestjs-restful-api
  2. Install dependencies:
    • Zod untuk validasi
    • Prisma untuk database
    • Winston untuk logging
    • Bcrypt untuk hashing password
    • UUID untuk session management
    • Config module untuk layanan konfigurasi

Skema Database Menggunakan Prisma

  • Buat model untuk User, Contact, dan Address

Model User

  • atribut: username, password, name, token (opsional)

Model Contact

  • atribut: firstName, lastName, email, phone, username (foreign key)

Model Address

  • atribut: street, city, province, country, postalCode, contactId (foreign key)

API Specification

User API

  1. Register User

    • Request: POST /api/users { username, password, name }
    • Response: { data: { username, name, token (optional) } }
  2. Login User

    • Request: POST /api/users/login { username, password }
    • Response: { data: { username, name, token } }
  3. Get User

    • Request: GET /api/users/current
    • Response: { data: { username, name } }
  4. Update User

    • Request: PATCH /api/users/current { name (optional), password (optional) }
    • Response: { data: { username, name } }
  5. Logout User

    • Request: DELETE /api/users/current
    • Response: { data: true }

Contact API

  1. Create Contact

    • Request: POST /api/contacts { firstName, lastName (optional), email (optional), phone (optional) }
    • Response: { data: { id, firstName, lastName, email, phone } }
  2. Get Contact

    • Request: GET /api/contacts/{id}
    • Response: { data: { id, firstName, lastName, email, phone } }
  3. Update Contact

    • Request: PUT /api/contacts/{id} { firstName (optional), lastName (optional), email (optional), phone (optional) }
    • Response: { data: { id, firstName, lastName, email, phone } }
  4. Remove Contact

    • Request: DELETE /api/contacts/{id}
    • Response: { data: true }
  5. Search Contact

    • Request: GET /api/contacts/search { query params: name, email, phone, page, size }
    • Response: { data: [{ id, firstName, lastName, email, phone }], paging: { currentPage, totalPages } }

Address API

  1. Create Address

    • Request: POST /api/contacts/{contactId}/addresses { street, city, province, country (required), postalCode (required) }
    • Response: { data: { id, street, city, province, country, postalCode } }
  2. Get Address

    • Request: GET /api/contacts/{contactId}/addresses/{id}
    • Response: { data: { id, street, city, province, country, postalCode } }
  3. Update Address

    • Request: PUT /api/contacts/{contactId}/addresses/{id} { street, city, province, country, postalCode }
    • Response: { data: { id, street, city, province, country, postalCode } }
  4. Remove Address

    • Request: DELETE /api/contacts/{contactId}/addresses/{id}
    • Response: { data: true }
  5. List Addresses

    • Request: GET /api/contacts/{contactId}/addresses
    • Response: { data: [{ id, street, city, province, country, postalCode }] }

Penyelesaian Proyek

  1. Jalankan unit test untuk memastikan tidak ada masalah.
  2. Compile TypeScript menjadi JavaScript dengan npm run build.
  3. Jalankan aplikasi dalam mode produksi dengan npm run start:prod.
  4. Lakukan tes manual menggunakan HTTP client (Postman, Insomnia, dll).

Penutupan

  • Materi tentang RESTful API menggunakan NestJS selesai.
  • Diskusikan di forum jika ada pertanyaan.
  • Terus belajar dan eksplorasi fitur baru di NestJS.