All Projects → JBlaak → Fitted

JBlaak / Fitted

Simplifying http requests using ES decorators

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Fitted

Restclient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮
Stars: ✭ 675 (+385.61%)
Mutual labels:  ajax, http-requests
Webapiclientgen
Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
Stars: ✭ 134 (-3.6%)
Mutual labels:  ajax
Ajax Hook
🔱 Intercepting browser's AJAX requests which made by XMLHttpRequest.
Stars: ✭ 1,900 (+1266.91%)
Mutual labels:  ajax
Typescript Rest Swagger
Swagger tools for typescript-rest
Stars: ✭ 129 (-7.19%)
Mutual labels:  decorators
Sente
Realtime web comms for Clojure/Script
Stars: ✭ 1,626 (+1069.78%)
Mutual labels:  ajax
Frequest
FRequest - A fast, lightweight and opensource desktop application to make HTTP(s) requests
Stars: ✭ 130 (-6.47%)
Mutual labels:  http-requests
Vue2 Bootstrap Table
A sortable and searchable table, as a Vue2 component, using bootstrap styling.
Stars: ✭ 120 (-13.67%)
Mutual labels:  ajax
Laravel 5.3 And Vue Js 2.0 Social Network
Social network built with Laravel 5.3 and Vue js 2.0
Stars: ✭ 137 (-1.44%)
Mutual labels:  ajax
Traits Decorator
Traits with decorators
Stars: ✭ 133 (-4.32%)
Mutual labels:  decorators
Ayanami
🍭 A better way to react with state
Stars: ✭ 129 (-7.19%)
Mutual labels:  decorators
Agent orange
Parse and process User Agents like a secret one
Stars: ✭ 127 (-8.63%)
Mutual labels:  http-requests
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+1296.4%)
Mutual labels:  decorators
Chucker
🔎 An HTTP inspector for Android & OkHTTP (like Charles but on device)
Stars: ✭ 2,169 (+1460.43%)
Mutual labels:  http-requests
Python And Oop
Object-Oriented Programming concepts in Python
Stars: ✭ 123 (-11.51%)
Mutual labels:  decorators
Reflection
Lightweight (3K) ES Module implementation of reflect-metadata
Stars: ✭ 136 (-2.16%)
Mutual labels:  decorators
Yii2fullcalendar
JQuery Fullcalendar Yii2 Extension
Stars: ✭ 120 (-13.67%)
Mutual labels:  ajax
Ntoastnotify
Asp.Net Core abstraction for server side rendered toast notifications using toast.js or noty.js. Supports AJAX calls as well.
Stars: ✭ 127 (-8.63%)
Mutual labels:  ajax
Klik Socialmediawebsite
Complete PHP-based Login/Registration system, Profile system, Chat room, Forum system and Blog/Polls/Event Management System.
Stars: ✭ 129 (-7.19%)
Mutual labels:  ajax
Jquery Confirm
A multipurpose plugin for alert, confirm & dialog, with extended features.
Stars: ✭ 1,776 (+1177.7%)
Mutual labels:  ajax
Dotnetdesk
Asp.Net Example web application showing the capabilities of ASP.NET Core 2 MVC, EF (Entity Framework), Web API, Bootstrap, jQuery, datatables, adminlte template and many more. Web app created as helpdesk or ticket support portal.
Stars: ✭ 136 (-2.16%)
Mutual labels:  ajax

Fitted

Build Status

Use ECMAScript decorators (currently a Stage 2 proposal) to execute HTTP requests and manage processing of responses, an easy and readable way of managing how data flows through the networking layer of your application.

Example

Two main parts, the method decorators which will actually do the request, and the class decorators which will allow you to handle the way responses from the server are transformed and handled.

The simplest example is just a fetch of data from a JSON endpoint:

import {get} from 'fitted';

class HackerNews {
    @get('https://hacker-news.firebaseio.com/v0/topstories.json')
    topstories (request, response) {
      return request({}, response);
    }
}

And fetch:

const hackerNews = new HackerNews();
const topstories = await hackerNews.topstories();
console.log(topstories);

Usage

Basic request

Using the get decorator you can trigger a GET request:

import {get} from 'fitted';

class HackerNews {
    @get('https://hacker-news.firebaseio.com/v0/topstories.json')
    topstories (request, response) {
      return request({}, response);
    }
}

Merging params

To merge params with the url we use url-template, which uses curly brackets to encapsulate a to be merged variable.

import {get} from 'fitted';

class HackerNews {
      @get('https://hacker-news.firebaseio.com/v0/item/{id}.json')
      item (id, request, response) {
          return request({
                template: {
                    id: 123
                 }
          }, response);
      }
}

Base url

Most of the time your endpoints will share the same base url, so Fitted allows you to set a base url which will be prefixed to all paths set in your method decorators.

import {base, get} from 'fitted';

@base('https://hacker-news.firebaseio.com/v0/')
class HackerNews {
      @get('item/{id}.json')
      item (id, request, response) {
          return request({
                template: {
                    id: 123
                 }
          }, response);
      }
}

Sending data

To add data to your request for post, put and destroy requests and specifying a query string for your get request you add a data object to your request definition.

import {put} from 'fitted';

class HackerNews {
      @put('https://hacker-news.firebaseio.com/v0/item/{id}.json')
      item (id, name, request, response) {
          return request({
                template: {
                    id: 123
                },
                data: {
                   name: name
                }
          }, response);
      }
}

Request decorating

Often all endpoints will have the same request requirements, requiring, for example, all to have some header set. For this the @request decorator can be used. It will get the config of each request passed before handing it over to the driver.

import {get, request} from 'fitted';

const myRequestHandler = config => {
    config.headers = {
        'accept': 'application/json' 
    }
    
    return config;
}

@request(myRequestHandler)
class HackerNews {
    @get('item/{id}.json')
    item (id, request, response) {
      return request({
            template: {
                id: id
            }
        }, response);
    }
}

Response handling

When the server responds with a Content-Type header containing application/json Fitted will automatically feed it to the JSON.parse function so that the resulting Promise will output the corresponding object.

Any other Content-Type will result in an Error being thrown and require you to implement your own handler.

Custom response processor

When your endpoint returns something that requires some pre-processing you can define a processor for all endpoints in your api definition. This consists of a function that receives the response from the server and passes the parsed data to the response object.

import {get, processor} from 'fitted';

const myProcessor = response => {
    const data = JSON.parse(response.getBody());
    response.setBody(data);
    
    return data;
}

@processor(myProcessor)
class HackerNews {
    @get('item/{id}.json')
    item (id, request, response) {
      return request({
            template: {
                id: id
            }
        }, response);
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].