nextjs-api-typescript

A plug-and-play Next.js API application with built-in logging and error handling—all the stuff I wish I had from the start.

MIT License

Stars
5
Committers
1

NextJS API typescript

A plug-and-play Next.js application template designed for seamless API handling. Just drop in your business logic and start building robust APIs in minutes.

Built with

Features

Streamlined Logging

This project utilizes Pino logger for streamlined logging, enabling you to easily trace logs throughout the application.

Each inbound request is assigned a unique request ID for precise tracking.

Uniform Error Handling

You can throw errors anywhere in your implementation using the APIError.

These errors are consistently caught and returned as HTTP responses.

Example:

const user = await db.user.find({ id: '1' });

if (!user) {
  throw new ApiError({
    type: 'NotFoundError',
    message: 'User not found', // optional
  });
}

The above example will return Http Status 404 with body { message: 'User not found'}.

Code Prettier

Running yarn prettier will format your code to a consistent style, ensuring a uniform codebase.

Code Validation

Running yarn validate will validate the entire codebase, helping to maintain code quality and consistency.

Example Implementations

This project includes an example implementation for handling a specific API route, demonstrating how to manage HTTP requests within your application:

API Route

  • Method: POST
  • Path: /api/example-route
  • Description: Handles incoming POST requests with custom logic.

Getting Started

Step 1: Local Development

To start working with this project locally, follow these steps:

  1. Fork this repository: Create your own fork of this repository on GitHub.
  2. Clone the repository: Clone your forked repository to your local machine.
  3. Navigate to the project directory: Move into the directory where the project was cloned.
  4. Install dependencies: Run yarn install to install all required dependencies.
  5. Start the development server: Run yarn dev to start the development server.

Step 2: Deploy Your Application

Once your business logic is implemented, deploy your application using your preferred hosting service (e.g., Vercel, Netlify, Heroku).

Step 3: Celebrate

License

Licensed under MIT.

Links

Looking for more exciting projects and insights?

👇👇

Connect with me on 𝕏 @renenielsendk for updates, tips, and more awesome content!

#nextjs, #nextjs-api, #api, #template