0.0.6 • Published 8 years ago

@teslagov/rampart-api-request v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

rampart-api-request

This library provides a utility for making Rampart related network requests.

Installing

Install with:

npm i @teslagov/rampart-api-request

Usage

Suppose we're writing a TODO microservice, where TODOs have a name and a description. We would write a todo.api.js that centralizes all network calls related to TODOs, and provides functions to hit the http://api.yourhost.com/todo endpoint:

import { get, post, put, del } from '@teslagov/rampart-api-request'

const getTodo = (todoId) => get(`http://api.yourhost.com/todo/${todoId}`);
const createTodo = (name, description) => post('http://api.yourhost.com/todo', {name, description});

export default {
  createTodo,
  getTodo
}

The functions you pull in all return a Promise.

By default, if you don't provide a JWT, we attempt to pull one from local storage. If you need to provide a custom JWT for testing/development purposes, you may provide one as an optional 2nd arg:

get(`/todo/${todoId}`, 'myJwt');

You can also provide any additional options that would be accepted by the isomorphic-fetch library (which we use under the hood) as an optional 3rd arg:

get(`/todo/${todoId}`, 'myJwt', {headers: {Accept: 'application/xml'}});

Usage with redux-saga

We recommend using redux-saga for your side-effects.

To use it, we would do something like:

import { call, put, take } from 'redux-saga/effects';

import todoApi from './todo.api';
import { GET_TODO_REQUEST, getTodoSuccess, getTodoError } from './todo-get.duck';

export function* watchGetTodo() {
  while (true) {
    const { todoId } = yield take(GET_TODO_REQUEST);

    try {
      const response = yield call(todoApi.getTodo, todoId);
      yield put(getTodoSuccess(response));
    } catch (errorResponse) {
      yield put(getTodoError(errorResponse.errors));
    }
  }
}

Dependencies

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago