Server
Quick Start

Quick Start

feTS Server is a library that allows you create a fully type-safe REST API.

If the consumer is a JavaScript environment, we highly recommend using feTS Client which is fully type-safe HTTP client that uses OpenAPI to generate the client.

Installation

Terminal
yarn add fets
yarn add uWebSockets.js@uNetworking/uWebSockets.js#v20.30.0

Write your first route

Let's write a simple route that returns a greeting message.

greetings-router.ts
// We recommend you to use `Response` from `fets`, because the native one isn't type-safe
// Let's assume that we are using Node.js
// We recommend µWebSockets.js instead of node:http for a better performance
import { createRouter, Response } from 'fets'
import { App } from 'uWebSockets.js'
 
// Create a router
const router = createRouter()
  // `.route` method should be in chain after `createRouter`
  .route({
    path: '/greetings',
    method: 'GET',
    // Define the response schema
    schemas: {
      responses: {
        // The status code
        200: {
          type: 'object',
          properties: {
            message: {
              type: 'string'
            }
          },
          required: ['message'],
          additionalProperties: false
        }
      }
      // Schemas should always have `as const` modifier
    } as const,
    handler() {
      // If you don't specify the status code, it will be 200
      return Response.json({
        message: 'Hello from feTS!'
      })
    }
  })
 
App()
  .any('/*', router)
  .listen(3000, () => {
    console.info(`Server is listening on http://localhost:3000`)
  })
💡

If you still want to use node:http, see node:http integration recipe

💡

You can play with your API with Swagger UI (opens in a new tab) that is available at http://localhost:3000/docs (opens in a new tab)

.route API

It uses .route() method to add routes to the router with the following parameters;

  • method: The HTTP method of the request. This is optional and it handles all methods if it's not given.
  • path: The URL pattern that the request should match. The url pattern is a string that follows the URLPattern (opens in a new tab) standard. You can learn more about URL patterns here (opens in a new tab).
  • schemas: An object that contains the schemas for the request and response. This brings a type-safety and validation to your requests and responses. You can learn more about schemas here.
  • handler: The function that will be called when the request matches the given method and path. This function can be either synchronous or asynchronous. If it's asynchronous, it should return a Promise that resolves to a Response object.

Router gives you an extended version of the regular Request object that has the following properties:

  • request.params: An object that contains the parameters that are given in the url pattern.
  • request.query: An object that contains the query parameters that are given in the url.

You can learn more about the original Request object here (opens in a new tab).