diff --git a/apicheck.ts.md b/apicheck.ts.md new file mode 100644 index 0000000..bf3892c --- /dev/null +++ b/apicheck.ts.md @@ -0,0 +1,101 @@ +# 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](#introduction) +2. [File Overview](#file-overview) +3. [Functionality](#functionality) +4. [Code Walkthrough](#code-walkthrough) +5. [Response Codes and Messages](#response-codes-and-messages) +6. [Usage](#usage) +7. [Summary](#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: + +```typescript +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 + +```javascript +fetch('/api/apicheck', { + method: 'POST' +}) + .then(response => response.json()) + .then(data => console.log(data.message)); // Output: API is working! +``` +