Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.3.0 #62

Merged
merged 5 commits into from
May 24, 2017
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ Create a fetch action to be dispatched by the store. Key features:
* Automatically `JSON.stringify` bodies that are objects and automatically `JSON.parse` responses that are `application/json`.
* `next` is a function that's result will be dispatched by the store. It can be an `async` function.
* `retry` is a function that receives the response and needs to resolve to a Boolean. It can be an `async` function.
* `Authorization`, `Content-Type` and `Accept` headers are added automatically (if you want to make a request
without one of these headers, for instance suppressing the Authorization header when calling a
remote service, simply set it to null in the `headers` field of `options`).

```js
const fetch = require('@conveyal/woonerf/fetch')
Expand Down
6 changes: 3 additions & 3 deletions __mocks__/auth0-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Auth0Lock.prototype.getProfile = function (token, callback) {
if (token === 'ok-token') {
callback(null, { profileData: 'blah' })
} else {
callback('error')
callback(new Error('error obtaining Auth0 profile'))
}
}

Auth0Lock.prototype.hide = function () {}

Auth0Lock.prototype.on = function (event, callback) {
callback({ idToken: this.options.fakeAuthenticatedToken })
Auth0Lock.prototype.on = function (event, next) {
next({ idToken: this.options.fakeAuthenticatedToken })
}

Auth0Lock.prototype.show = function () {}
10 changes: 0 additions & 10 deletions __tests__/src/__snapshots__/auth0.js.snap

This file was deleted.

8 changes: 2 additions & 6 deletions __tests__/src/auth0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* globals describe, expect, it */
/* globals describe, it */

import {getLock, setAuth0User} from '../../src/auth0'
import {getLock} from '../../src/auth0'

describe('auth0', () => {
it('getLock should work', () => {
getLock()
})

it('setAuth0User should work', () => {
expect(setAuth0User({ user: 'a user' })).toMatchSnapshot()
})
})
17 changes: 17 additions & 0 deletions __tests__/src/reducers/__snapshots__/user.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`client > reducers > user should handle default state 1`] = `Object {}`;

exports[`client > reducers > user should handle log out 1`] = `Object {}`;

exports[`client > reducers > user should handle set auth0 user 1`] = `
Object {
"id": 1,
}
`;

exports[`client > reducers > user should handle set id token 1`] = `
Object {
"idToken": "abcd",
}
`;
34 changes: 34 additions & 0 deletions __tests__/src/reducers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* globals describe, expect, it */

import {handleActions} from 'redux-actions'

describe('client > reducers > user', () => {
window.localStorage = {
getItem: () => JSON.stringify({})
}

const {user} = require('../../../src/reducers')

const reducer = handleActions(user.reducers, user.initialState)

// Default State Test
it('should handle default state', () => {
expect(reducer(undefined, { type: 'blah', payload: {} })).toMatchSnapshot()
})

// Specific Handler Tests
it('should handle log out', () => {
const action = { type: 'log out', payload: {} }
expect(reducer(undefined, action)).toMatchSnapshot()
})

it('should handle set auth0 user', () => {
const action = { type: 'set auth0 user', payload: { id: 1 } }
expect(reducer(undefined, action)).toMatchSnapshot()
})

it('should handle set id token', () => {
const action = { type: 'set id token', payload: 'abcd' }
expect(reducer(undefined, action)).toMatchSnapshot()
})
})
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"cover": "mastarm lint && mastarm test -e test --coverage --coverage-paths \"src/**/*.js\"",
"clean": "rm -r components store ./*.js ./*.js.map",
"clean": "rm -r actions components reducers store ./*.js ./*.js.map",
"prepublish": "mastarm prepublish src --outdir ./",
"test": "mastarm lint && mastarm test -e test",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
Expand All @@ -26,14 +26,14 @@
"react-router": "^3.0.2",
"react-router-redux": "^4.0.7",
"redux": "^3.6.0",
"redux-actions": "^1.2.1",
"redux-logger": "^2.7.4",
"redux-actions": "^2.0.2",
"redux-logger": "^3.0.1",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"enzyme": "^2.7.1",
"enzyme-to-json": "^1.4.5",
"mastarm": "^3.5.1",
"mastarm": "^3.8.0",
"nock": "^9.0.2",
"react": "^15.4.2",
"react-addons-test-utils": "^15.4.2",
Expand Down
3 changes: 3 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
user: require('./user')
}
3 changes: 3 additions & 0 deletions src/actions/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {createAction} from 'redux-actions'

export const setAuth0User = createAction('set auth0 user')
2 changes: 0 additions & 2 deletions src/auth0.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Auth0Lock from 'auth0-lock'
import {createAction} from 'redux-actions'

const AUTH0_CLIENT_ID = process.env.AUTH0_CLIENT_ID
const AUTH0_DOMAIN = process.env.AUTH0_DOMAIN

export const setAuth0User = createAction('set auth0 user')
export const authIsRequired = AUTH0_CLIENT_ID && AUTH0_DOMAIN
export const defaultLockOptions = {
auth: {
Expand Down
3 changes: 2 additions & 1 deletion src/components/auth0-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {PropTypes} from 'react'
import {connect} from 'react-redux'
import {push} from 'react-router-redux'

import {defaultLockOptions, getLock, setAuth0User} from '../auth0'
import {setAuth0User} from '../actions/user'
import {defaultLockOptions, getLock} from '../auth0'
import Pure from './pure'

class Auth0 extends Pure {
Expand Down
24 changes: 17 additions & 7 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* globals fetch */

import isObject from 'lodash.isobject'
import {createAction} from 'redux-actions'
if (typeof (fetch) === 'undefined') {
require('isomorphic-fetch')
}
import isObject from 'lodash.isobject'
import {createAction} from 'redux-actions'

export const INCREMENT_FETCH = 'increment outstanding fetches'
export const DECREMENT_FETCH = 'decrement outstanding fetches'
Expand Down Expand Up @@ -43,14 +43,24 @@ export function runFetch ({
retry = false,
url
}, state) {
const headers = {
...createAuthorizationHeader(state),
...createContentHeader(options.body),
...(options.headers || {})
}

const filteredHeaders = {}

// allow removing generated headers by specifiying { header: null } in options.headers
// do this in two steps because otherwise we're modifying the object as we're iterating over it
Object.keys(headers)
.filter(key => headers[key] !== null && headers[key] !== undefined)
.forEach(key => { filteredHeaders[key] = headers[key] })

return fetch(url, {
...options,
body: serialize(options.body),
headers: {
...createAuthorizationHeader(state),
...createContentHeader(options.body),
...(options.headers || {})
}
headers: filteredHeaders
})
.then(checkStatus)
.then(createResponse)
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
actions: require('./actions'),
auth0: require('./auth0'),
Auth0Lock: require('./components/auth0-lock'),
fetch: require('./fetch'),
Expand Down
3 changes: 3 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
user: require('./user')
}
22 changes: 22 additions & 0 deletions src/reducers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export const reducers = {
'log out' (state, action) {
return {}
},
'set auth0 user' (state, action) {
return {
...state,
...action.payload
}
},
'set id token' (state, action) {
return {
...state,
idToken: action.payload
}
}
}

export const initialState = {
...JSON.parse(typeof window === 'undefined' ? '{}' : window.localStorage.getItem('user'))
}
2 changes: 1 addition & 1 deletion src/store/store.development.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {browserHistory} from 'react-router'
import {routerMiddleware} from 'react-router-redux'
import {applyMiddleware, createStore} from 'redux'
import createLogger from 'redux-logger'
import {createLogger} from 'redux-logger'
import thunkMiddleware from 'redux-thunk'

import {middleware as fetch} from '../fetch'
Expand Down
Loading