Qeres - The easiest and most flexible way to create APIs

Ben Timor

Backend Engineer

Qeres - The easiest and most flexible way to create APIs

What is Qeres?

Qeres is a new way to create web-APIs, similarly to REST or GraphQL. Qeres was created with 3 main goals:
Flexibility
Easy to use / Beginner-friendly
Fast
Lets just see an example of Qeres use. Here's the request we're sending to the server:
And here's the response:
Basically, We're just telling the server how to name our data and what data we need. So if we send a request of "privacy": "privacyTermsOfUse()", we tell the server that we want it to send us the return value of privacyTermsOfUse() and name it "privacy".

Installation

How to use Qeres?

Data Methods

Qeres is method-based. Everything we can request from our API, has to be created as a method.
Additionally, Every method has to be created inside a class (The method can be static). It's because we're going to need to decorate the method, and JavaScript won't allow us to decorate functions outside of a class.
Now lets say we want to allow the user to request our terms of use. We'll create a class with a method which returns our terms of use, Like this:
This is called a root method, Because it can be accessed directly through our API. And it's a data method, which tells us it can't be used to access another methods (Next title we're going to see the opposite example!)
Request example:
Response example:
Note: In Qeres, we tell the server how to name the value we want to get, and tell it what function it has to run to get this value. In our example, we told the server "Hey! Get the return value of termsOfUse() and name it 'tos' in the response"

Path Methods

Sometimes we want to return an object from a method, And let the user use its methods.
For example lets say we have a "Math" class which allows the user to use math methods on two numbers. Here's our class:
Note: We don't have @Qeres.data on our multipication method, so the user is going to get an error if he try to access it.
Now, lets create a new method in our RootAPI class which returns the Math object:
Note2: Right now if you don't change your parameters values (as explained at The Qeres object part) you can only pass string parameters directly to the function.
But, You can pass object paremters by setting them in Query Variables (as explained at Query Variables part) or pass any other type which's returned from Qeres data methods by passing it into query variable.
Request example:
Response example:
Note: With path methods, instead of telling the server how to name them, we tell the server what methods we want to get from them. It runs recursively so we can have a method inside a method inside a method...

Combined Methods

A method can be both path and data typed. It allows you both to get it's return value and to access its methods.
All we have to do to create a method like this, Is to add both @Qeres.data and @Qeres.path. This is the example with our math method;

Accessing Objects

Sometimes you get an object from the API and you don't want the full object. In this case, you can specify what you want to get. For example, lets say we have this method, which returns text, status and moreInfo:
If we call this method, this is how our Qeres response gonna look like:
But now we want only the text and hello. Because honestly, Who cares about statuses and the world? We can do it like this:
And then we're gonna get this reponse:

Query Variables

To reduce the amount of requests to the server as much as possible, You can store variables in the query itself. This way you won't have to get information from the server and then send it again for another method. For example, Lets say we have those hello and world methods:
We want the server to return us "HELLO WORLD". Instead of requesting the world method and then sending another request for the hello method, we can do it like this:
And then the response is going to be:
Additionally, We can just set custom variables (without calling method) by putting $ before the variable name. Those variables can be both object typed and string typed, like this:
Sometimes, You'd even like to pass variables for the FE to use. In this case you can do it by using handleRequest second parameter, Like this:
Now you can access $apple variable in your query.
Note: You have to create the variable you want to use before you use it.

Custom Errors

If you want to throw an error and send it to the client, You have to create a custom error by extending QeresError object. You can do it like this:
And then you can use it inside a Qeres method like this:

The Qeres object

In Qeres, everything happens inside its object. It's done like this because we want to allow you to use multiple Qeres objects for different API endpoints when needed.
When we create the Qeres object, we have to tell Qeres what root methods we have. This is how it looks:
Now, Qeres is going to allow the user to access only "termsOfUse" and "math" methods. Unless you use a path method (like math), And than it allows you to access the object's functions.
Additionally, You can parse the parameters values who enter into Qeres before passing them into the function by passing a second parameter to the Qeres constructor.
For example, If I want to convert every string that starts with + to number, I can do it like this:
And then the request is going to look like this:
Then sum is going to be equal to 3 even if add wouldn't convert its parameters to number.

Handling Requests

Once you have the qeres object, it's easy to handle requests. All you have to do is something like this:
Note: "req.body" must be a JSON object. If you use express, you may have to add bodyparse for this to be possible (Something like app.use(bodyParser.json());)
Partner With Ben
View Services

More Projects by Ben