Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Getting started with redux-json-api

This guide describes use of v1 of Redux-Json-Api library. See this awesome update from [@dvidsilva](https://github.com/dvidsilva/redux-json-api-demo) for v2 guide.
# Getting started with redux-json-api v2

## Introduction
I have a need to pull [json api](http://jsonapi.org) formatted data from an API server. The [Redux Json Api package](https://github.com/dixieio/redux-json-api) is listed as one of the JS client implementations, so it seems like a good place to start in solving that need.
Expand Down Expand Up @@ -78,15 +76,25 @@ const store = createStore(reducer, applyMiddleware(thunk))
#### Configuring the API server options
We add the API server hostname, path and any required authentication token strings to out newly created store. In the example code, I've included soe dummy JSON and we'll serve that from the webpack-dev server as well, so we're stil lusing localhost.


The configuration on version 1 used to be:

``` js
store.dispatch(setEndpointHost('http://localhost:8080'));
store.dispatch(setEndpointPath('/api/v1'));
```

If the API server you're playing with requires some authentication token you will need to include that here as well.
On version 2, all these `setEndpointHost, setEndpointPath, setAccessToken, setHeaders`
are replaced with `setAxiosConfig`. Every configuration available in Axios can be set here,
for a full list of options see the [Axios documentation](https://github.com/axios/axios#request-config).

``` js
store.dispatch(setAccessToken('SECRET-TOKEN-HERE'));
```js
store.dispatch(setAxiosConfig({
baseURL: 'http://localhost:8080/api/v1',
headers: {
'Authorization': 'bearer' + Math.random(),
}
}));
```

#### Mapping state to props
Expand Down
10 changes: 7 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import { createStore, applyMiddleware, combineReducers } from 'redux'
import { connect } from 'react-redux';
import thunk from 'redux-thunk';
import { reducer as api, readEndpoint } from 'redux-json-api';
import { setEndpointHost, setEndpointPath, setAccessToken, setHeaders } from 'redux-json-api';
import { setAxiosConfig } from 'redux-json-api';

const reducer = combineReducers({
api
});

const store = createStore(reducer, applyMiddleware(thunk))

store.dispatch(setEndpointHost('http://localhost:8080'));
store.dispatch(setEndpointPath('/api/v1'))
store.dispatch(setAxiosConfig({
baseURL: 'http://localhost:8080/api/v1',
headers: {
'Authorization': 'bearer' + Math.random(),
}
}));

class PostList extends React.Component {
componentWillMount() {
Expand Down
Loading