0.0.29 • Published 6 years ago

ajax-wrapper v0.0.29

Weekly downloads
86
License
-
Repository
-
Last release
6 years ago

ajax-wrapper

A simple clone of rxjs-ajax-wrapper, using the fetch API.

Version npm download

Simple to use and simple to setup wrapper for fetch. Allows you to define your distant resources api and call them on the fly.

How to use

Step 1: Define your api routes.

const apiDefs = {
  getAllFilms: {
    url: 'https://ghibliapi.herokuapp.com/films',
    method: 'GET',
    responseType: 'json',
  },
  getSingleFilm: {
    url: 'https://ghibliapi.herokuapp.com/films/:id',
    method: 'GET',
    responseType: 'json',
  },
  postFilm: {
    url: 'https://ghibliapi.herokuapp.com/films',
    method: 'POST',
    responseType: 'json',
  },
};

You can check all the available request params here The returned data in the Promise resolve is already formatted following the responseType of the request definition.

Step 2: Initialize the wrapper

import { AjaxWrapper } from 'ajax-wrapper';

const api = new AjaxWrapper(apiDefs);

Step 3: Call the routes

api.routes.getAllFilms()

Return value

Return a Promise. Checkout Fetch API for more details.

Options

Functions

FunctionExplanationArgumentsReturn ValueExample
combineWrappers()Combine multiples wrappers.({wrapperKey: wrapper, ...})The combined wrappers, with each wrapper routes in the respective wrapper object.combineWrappers({authWrapper, filmWrapper});

Methods

MethodExplanationArgumentsExample
addRequestMiddlewares()Define a function that returns arguments to append to the request header.([{name: middlewareName: handler: middlewareFunc}], middlewareFuncParams)api.addRequestMiddlewares([name: 'token', handler: (store) => ({Authorization: store.getState().token})], store);
addErrorMiddlewares()Define a function that returns functions to call when an error occurs.([{name: middlewareName: handler: middlewareFunc}], middlewareFuncParams)api.addErrorMiddlewares([name: '404Middleware', handler: (request) => { if (request.status === 404) dispatch(somtething()) }]);

Ignore middleware on specific route.

Simple, just an set an array ignoreMiddlewares containing the names of the middlewares you wish to ignore. Works for both errorMiddlewares and requestMiddlewares.

Example :

const apiDefs = {
  specialRoute: {
    url: 'https://ghibliapi.herokuapp.com/films',
    method: 'GET',
    responseType: 'json',
    ignoreMiddlewares: [
      '404',
      'tokenMiddleware',
    ]
  },
};

Todo ideas

  • Update/delete middleware.
0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago