Getting Started

Getting Started

This page explains how to begin using the Tarin library after completing the installation process.

To fully understand how Tarin works, it's essential to familiarize yourself with its core components. Tarin consists of the following five key components:

Tarin App

The Tarin component is responsible for creating the HTTP server. You will use it to initialize your Tarin application and set up your server.

Example:

import { Tarin } from "tarin";
 
// Create Tarin App
const tarinApp = new Tarin();
 
// Create HTTP Server
tarinApp.createServer();
 
// Define Endpoints
...
 
// Listen on Port 3000
tarinApp.listen(3000);

Once the server is created, you can proceed to define your API endpoints.

Endpoint

An Endpoint defines the API route, including its input, output, and the logic to handle requests. You can define an endpoint using the endpoint utility provided by Tarin. This utility supports the following HTTP methods:

  • GET
  • POST
  • DELETE
  • PATCH
  • HEAD
  • OPTIONS
  • PUT
  • TRACE

Each endpoint has:

  • Input: The data expected in the request (e.g., query, body, params).
  • Output: The format of the response data.
  • handleLogic: A callback that processes the request and returns a response.

For more details on input and output, refer to the Input & Output section, and for handling logic, refer to the Logic Handling section.

Example:

import { endpoint, SchemaValidator } from "tarin";
 
// Create Tarin App & HTTP Server code
...
 
// Define Endpoint
const myEndpoint = endpoint.get("/")
    .input({ query: SchemaValidator.object({ username: SchemaValidator.string() }) })
    .output({ body: SchemaValidator.object({ message: SchemaValidator.string() }) })
    .handleLogic(input => Result.success({ body: { message: `Hello ${input.query.username}` }}));
 
// Add Endpoint to Tarin App
tarinApp.addEndpoint(myEndpoint);

You can also use addEndpoints to register multiple endpoints at once if preferred.

RequestHandler

The RequestHandler component is used internally to manage requests and responses. It automatically validates input and output types, executes the handleLogic callback, and ensures the proper handling of requests. You don't need to worry about configuring it manually.

SchemaValidator

The SchemaValidator is responsible for validating and parsing input and output data. As seen in the previous Endpoint example, it is used to define and validate the schema for both input and output. For a deeper understanding, visit the Schema Validator page.

OpenAPIInterpreter

The OpenAPIInterpreter component is used internally to convert endpoint definitions into an OpenAPI specification document. You don't need to configure or interact with this directly. It works automatically behind the scenes to generate the OpenAPI documentation for your API.