1
apicheck.ts
bRNS98 edited this page 2024-10-11 22:59:37 +02:00

Documentation for apicheck.ts

Welcome to the documentation for the apicheck.ts file! This document will guide you through the purpose and functionality of this TypeScript API handler.

Index

  1. Introduction
  2. File Overview
  3. Functionality
  4. Code Walkthrough
  5. Response Codes and Messages
  6. Usage
  7. Summary

Introduction

The apicheck.ts file is a simple serverless function that acts as an API endpoint in a Next.js application. It is designed to handle HTTP requests and provide a basic response, primarily used to check if an API is functioning correctly. 🚀

File Overview

  • Filename: apicheck.ts
  • Location: Typically located in the pages/api directory of a Next.js project.
  • Purpose: Handle HTTP POST requests and ensure the API is operational.

Functionality

The primary function of this file is to:

  • Respond to HTTP POST requests with a success message indicating the API is operational.
  • Reject non-POST HTTP methods with an appropriate error message.

This ensures that only POST requests are processed, maintaining the integrity and expected behavior of the API.

Code Walkthrough

Let's break down the code into its components:

import type { NextApiRequest, NextApiResponse } from 'next';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
    if (req.method === 'POST') {
        res.status(200).json({ message: 'API is working!' });
    } else {
        res.status(405).json({ message: 'Method not allowed' });
    }
}

Imports

  • NextApiRequest and NextApiResponse: These are TypeScript types imported from the next package. They define the structure of the request and response objects used in the handler function.

Exported Function: handler

  • This is the default exported function that will be used by the Next.js API route.

Logic

  1. Check Request Method:

    • The function checks if the req.method is 'POST'.
  2. Response for POST Request:

    • If the method is 'POST', it sends a JSON response with a status code of 200 and a message: 'API is working!'.
  3. Response for Non-POST Requests:

    • If the method is anything other than 'POST', it sends a JSON response with a status code of 405 (Method Not Allowed) and a message: 'Method not allowed'.

Response Codes and Messages

HTTP Method Status Code Message
POST 200 API is working!
Others 405 Method not allowed

Usage

How to Use

  1. Setup your Next.js Project:

    • Ensure you have a Next.js project setup.
  2. Place the File:

    • Save the apicheck.ts file in your project's pages/api directory.
  3. Send a POST Request:

    • Use a tool like Postman or a simple fetch request from a client-side application to send a POST request to /api/apicheck.
  4. Observe the Response:

    • You should receive a response: { "message": "API is working!" }.

Example POST Request

fetch('/api/apicheck', {
  method: 'POST'
})
  .then(response => response.json())
  .then(data => console.log(data.message)); // Output: API is working!