-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from bytewitchcraft/feature/pass-headers-throgh…
…-middlware added passing headers from context
- Loading branch information
Showing
11 changed files
with
2,939 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"presets": ["env"], | ||
"plugins": [ | ||
"transform-object-rest-spread" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"plugins": [ | ||
"prettier" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => ({ variables: {}, files: [{ file: {}, name: 'file' }] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default jest.fn(opts => ({ | ||
url: opts.url, | ||
method: 'POST', | ||
body: opts.body, | ||
headers: opts.headers, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createUploadMiddleware } from '../index' | ||
|
||
jest.mock('../request') | ||
jest.mock('../extractFiles') | ||
|
||
const generateOperations = (context = { headers: {} }) => ({ | ||
variables: {}, | ||
getContext: () => context, | ||
}) | ||
|
||
describe('#createUploadMiddleware', () => { | ||
it('should pass headers from context', () => { | ||
const { request } = createUploadMiddleware({ uri: 'http://example.com' }) | ||
const headers = { authorization: '1234' } | ||
const operations = generateOperations({ headers }) | ||
const forward = jest.fn() | ||
|
||
const result = request(operations, forward) | ||
|
||
expect(forward).toHaveBeenCalledTimes(0) | ||
expect(result.headers).toEqual(headers) | ||
}) | ||
it('should pass headers from options', () => { | ||
const headers = { authorization: '1234' } | ||
const { request } = createUploadMiddleware({ | ||
uri: 'http://example.com', | ||
headers, | ||
}) | ||
const operations = generateOperations() | ||
const forward = jest.fn() | ||
|
||
const result = request(operations, forward) | ||
|
||
expect(result.headers).toEqual(headers) | ||
}) | ||
it('should combine headers from options and context', () => { | ||
const optionsHeaders = { 'x-spree-token': 'token' } | ||
const contextHeaders = { authorization: '1234' } | ||
const { request } = createUploadMiddleware({ | ||
uri: 'http://example.com', | ||
headers: optionsHeaders, | ||
}) | ||
const operations = generateOperations({ headers: contextHeaders }) | ||
const forward = jest.fn() | ||
|
||
const result = request(operations, forward) | ||
|
||
expect(result.headers).toEqual({ ...contextHeaders, ...optionsHeaders }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { pipe, append, join } from 'ramda' | ||
import { isFileList, isObject, isUploadFile } from './validators' | ||
|
||
const extractFiles = variables => { | ||
const files = [] | ||
const walkTree = (tree, path = []) => { | ||
const mapped = Array.isArray(tree) ? tree : Object.assign({}, tree) | ||
Object.keys(mapped).forEach(key => { | ||
const value = mapped[key] | ||
const name = pipe(append(key), join('.'))(path) | ||
|
||
if (isUploadFile(value) || isFileList(value)) { | ||
const file = isFileList(value) | ||
? Array.prototype.slice.call(value) | ||
: value | ||
|
||
files.push({ file, name }) | ||
mapped[key] = name | ||
} else if (isObject(value)) { | ||
mapped[key] = walkTree(value, name) | ||
} | ||
}) | ||
|
||
return mapped | ||
} | ||
|
||
return { | ||
files, | ||
variables: walkTree(variables), | ||
} | ||
} | ||
|
||
export default extractFiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Observable } from 'rxjs/Observable' | ||
import 'rxjs/add/observable/dom/ajax' | ||
import 'rxjs/add/operator/map' | ||
|
||
/** | ||
* Request function | ||
* | ||
* @param {Object} opts | ||
*/ | ||
const request = opts => | ||
Observable.ajax({ | ||
url: opts.uri, | ||
body: opts.body, | ||
method: 'POST', | ||
headers: opts.headers, | ||
}).map(({ response }) => response) | ||
|
||
export default request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const isObject = value => value !== null && typeof value === 'object' | ||
export const isFileList = value => | ||
typeof FileList !== 'undefined' && value instanceof FileList | ||
|
||
export const isUploadFile = value => | ||
typeof File !== 'undefined' && value instanceof File |
Oops, something went wrong.