├── .babelrc ├── .gitignore ├── .npmignore ├── Makefile ├── Readme.md ├── package.json ├── src └── index.js └── test └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | presets: ["es2015", "stage-2"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | lib -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Vars 3 | # 4 | 5 | BIN = ./node_modules/.bin 6 | .DEFAULT_GOAL := all 7 | 8 | # 9 | # Tasks 10 | # 11 | 12 | node_modules: package.json 13 | @npm install 14 | @touch node_modules 15 | 16 | test: node_modules 17 | ${BIN}/babel-tape-runner test/*.js 18 | 19 | validate: node_modules 20 | @${BIN}/standard 21 | 22 | all: validate test 23 | 24 | # 25 | # Phony 26 | # 27 | 28 | .PHONY: test validate -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # redux-effects-credentials 3 | 4 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 5 | 6 | Add credentials to fetch requests when conditions are met 7 | 8 | ## Installation 9 | 10 | $ npm install redux-effects-credentials 11 | 12 | ## Usage 13 | 14 | Add the credential middleware of your choice to the effects stack before the fetch middleware. 15 | 16 | ### Query string 17 | 18 | ```javascript 19 | import effects from 'redux-effects' 20 | import {query} from 'redux-effects-credentials' 21 | import fetch from 'redux-effects-fetch' 22 | 23 | applyMiddleware(query(/https?\:\/\/myapiserver\.com.*/, 'access_token', ({getState}) => getState().accessToken), fetch)(createStore) 24 | ``` 25 | 26 | ### Bearer 27 | 28 | ```javascript 29 | import effects from 'redux-effects' 30 | import {bearer} from 'redux-effects-credentials' 31 | import fetch from 'redux-effects-fetch' 32 | 33 | applyMiddleware(effects, bearer(/https?\:\/\/myapiserver.com\.com.*/, ({getState}) => getState().accessToken), fetch))(createStore) 34 | ``` 35 | 36 | ### Pattern 37 | 38 | Pattern may be a regular expression or a function that returns true/false. 39 | 40 | ## License 41 | 42 | The MIT License 43 | 44 | Copyright © 2015, Weo.io <info@weo.io> 45 | 46 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 47 | 48 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 49 | 50 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-effects-credentials", 3 | "description": "Add credentials to fetch requests when conditions are met", 4 | "repository": "git://github.com/redux-effects/redux-effects-credentials.git", 5 | "version": "0.2.1", 6 | "license": "MIT", 7 | "main": "lib/index.js", 8 | "scripts": { 9 | "prepublish": "rm -rf lib && babel src --out-dir lib", 10 | "postpublish": "rm -rf lib" 11 | }, 12 | "dependencies": { 13 | "babel-tape-runner": "^2.0.1", 14 | "redux-effects-fetch": "^0.4.5" 15 | }, 16 | "devDependencies": { 17 | "babel-preset-es2015": "^6.3.13", 18 | "babel-preset-stage-2": "^6.3.13", 19 | "babel-tape-runner": "^2.0.0", 20 | "tape": "^4.2.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Imports 3 | */ 4 | 5 | import {FETCH} from 'redux-effects-fetch' 6 | 7 | /** 8 | * Credential middleware 9 | */ 10 | 11 | function query (pattern, name, getToken) { 12 | return abstractMw(pattern, getToken, (payload, token) => ({ 13 | ...payload, 14 | url: decorate(payload.url, token) 15 | })) 16 | 17 | function decorate (url, token) { 18 | if (!token) return url 19 | 20 | const [base, qs = ''] = url.split('?') 21 | const param = [name, token].map(encodeURIComponent).join('=') 22 | 23 | return [ 24 | base, 25 | qs.split('&').concat(param).filter(Boolean).join('&') 26 | ].join('?') 27 | } 28 | } 29 | 30 | function bearer (pattern, getToken, prefix = 'Bearer') { 31 | return abstractMw(pattern, getToken, (payload, token) => !token ? payload : ({ 32 | ...payload, 33 | params: { 34 | ...(payload.params || {}), 35 | headers: { 36 | ...((payload.params || {}).headers || {}), 37 | Authorization: prefix + ' ' + token 38 | } 39 | } 40 | })) 41 | } 42 | 43 | function abstractMw (pattern, getToken, xf) { 44 | const isMatch = matcher(pattern) 45 | return ctx => next => action => action.type === FETCH && isMatch(action.payload.url) 46 | ? next({...action, payload: xf(action.payload, getToken(ctx))}) 47 | : next(action) 48 | } 49 | 50 | function matcher (pattern) { 51 | return typeof pattern === 'function' 52 | ? pattern 53 | : url => pattern.test(url) 54 | } 55 | 56 | /** 57 | * Exports 58 | */ 59 | 60 | export { 61 | query, 62 | bearer 63 | } 64 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Imports 3 | */ 4 | 5 | import {fetch} from 'redux-effects-fetch' 6 | import {bearer, query} from '../src' 7 | import test from 'tape' 8 | 9 | /** 10 | * Tests 11 | */ 12 | 13 | test('query', ({equal, plan}) => { 14 | const qs = query(/.*/, 'access_token', ({getState}) => getState().token)({getState: () => ({token: 'someToken'})}) 15 | const mw = qs(effect => { 16 | equal(effect.payload.url, 'http://test/?access_token=someToken') 17 | }) 18 | 19 | plan(2) 20 | mw(fetch('http://test/')) 21 | mw(fetch('http://test/?')) 22 | }) 23 | 24 | test('bearer', ({equal, plan}) => { 25 | const b = bearer(/.*/, ({getState}) => getState().token)({getState: () => ({token: 'someToken'})}) 26 | const mw = b(effect => { 27 | equal(effect.payload.params.headers.Authorization, 'Bearer someToken') 28 | }) 29 | 30 | plan(1) 31 | mw(fetch('http://test/')) 32 | }) 33 | --------------------------------------------------------------------------------