Skip to content

yasaricli/tatsy

Repository files navigation

A simple interface for creating REST APIs
Website | Wiki

Getting started

Install with yarn:

yarn add tatsy

or with npm:

npm install tatsy

and add a script to your package.json like this:

"scripts": {
  "start": "tatsy --start",
  "build": "tatsy --build",
},

and then just run yarn start and go to http://localhost:3000/api

Defining Collection Routes

One of the most common uses for a REST API is exposing a set of operations on your collections. All available REST endpoints can be generated for a Mongo Collection using Tatsy.Collection(name, options).

// articles.js | Given a URL "/articles/5"
Tatsy.Collection('articles', {
  schema: {
    title: String,
    author: String
  }
})

/api/<collection>

  • Operations on the entire collection
  • GET and POST

/api/<collection>/:id

  • Operations on a single entity within the collection
  • GET, PUT, PATCH and DELETE

Defining Custom Routes

Routes are defined using Tatsy.Route(). A route consists of a path and a set of endpoints defined at that path.

In this example we have a parameter named _id. If we navigate to the /posts/5 URL in our browser, inside of the GET endpoint function we can get the actual value of the _id from (_id) { console.log(_id) }. In this case _id => 5.

// posts.js | Given a URL "/posts/5"
Tatsy.Route({
  endpoints: {
    get: {
      authRequired: true, // default false
      action() {
        const { _id } = this.urlProps;
        
        return {
          _id
        }
      }
    }
  }
});

Defining Endpoints

The last parameter of Tatsy.Route is an object with properties corresponding to the supported HTTP methods. At least one method must have an endpoint defined on it. The following endpoints can be defined in Tatsy:

  • getAll
  • get
  • post
  • put
  • delete

Packages

This repository is a monorepo that we manage using Lerna. That means that we actually publish several packages to npm from the same codebase, including:

Package Version Description
tatsy npm The core of tatsy
tatsy-http npm Http Server for Express.js
tatsy-collection npm Mongodb collection use mongoose
tatsy-logger npm Log using chalk
tatsy-config npm Config package
tatsy-watcher npm package to watch all javascript files

Developer Resources

Interested in helping or contributing to Tatsy? These resources will help:

License

Tatsy is open source software licensed as MIT.

About

A simple interface for creating REST APIs

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors