项目作者: JBlaak

项目描述 :
Simplifying http requests using ES decorators
高级语言: JavaScript
项目地址: git://github.com/JBlaak/Fitted.git
创建时间: 2016-05-28T19:35:11Z
项目社区:https://github.com/JBlaak/Fitted

开源协议:

下载


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:

  1. import {get} from 'fitted';
  2. class HackerNews {
  3. @get('https://hacker-news.firebaseio.com/v0/topstories.json')
  4. topstories (request, response) {
  5. return request({}, response);
  6. }
  7. }

And fetch:

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

Usage

Basic request

Using the get decorator you can trigger a GET request:

  1. import {get} from 'fitted';
  2. class HackerNews {
  3. @get('https://hacker-news.firebaseio.com/v0/topstories.json')
  4. topstories (request, response) {
  5. return request({}, response);
  6. }
  7. }

Merging params

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

  1. import {get} from 'fitted';
  2. class HackerNews {
  3. @get('https://hacker-news.firebaseio.com/v0/item/{id}.json')
  4. item (id, request, response) {
  5. return request({
  6. template: {
  7. id: 123
  8. }
  9. }, response);
  10. }
  11. }

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.

  1. import {base, get} from 'fitted';
  2. @base('https://hacker-news.firebaseio.com/v0/')
  3. class HackerNews {
  4. @get('item/{id}.json')
  5. item (id, request, response) {
  6. return request({
  7. template: {
  8. id: 123
  9. }
  10. }, response);
  11. }
  12. }

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.

  1. import {put} from 'fitted';
  2. class HackerNews {
  3. @put('https://hacker-news.firebaseio.com/v0/item/{id}.json')
  4. item (id, name, request, response) {
  5. return request({
  6. template: {
  7. id: 123
  8. },
  9. data: {
  10. name: name
  11. }
  12. }, response);
  13. }
  14. }

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.

  1. import {get, request} from 'fitted';
  2. const myRequestHandler = config => {
  3. config.headers = {
  4. 'accept': 'application/json'
  5. }
  6. return config;
  7. }
  8. @request(myRequestHandler)
  9. class HackerNews {
  10. @get('item/{id}.json')
  11. item (id, request, response) {
  12. return request({
  13. template: {
  14. id: id
  15. }
  16. }, response);
  17. }
  18. }

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.

  1. import {get, processor} from 'fitted';
  2. const myProcessor = response => {
  3. const data = JSON.parse(response.getBody());
  4. response.setBody(data);
  5. return data;
  6. }
  7. @processor(myProcessor)
  8. class HackerNews {
  9. @get('item/{id}.json')
  10. item (id, request, response) {
  11. return request({
  12. template: {
  13. id: id
  14. }
  15. }, response);
  16. }
  17. }