Skip to content

Commit

Permalink
added passing headers from link options
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorkatkov committed Feb 9, 2018
1 parent 7eb8a59 commit c534e13
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"presets": ["env"]
"presets": ["env"],
"plugins": [
"transform-object-rest-spread"
]
}
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser": "babel-eslint",
"plugins": [
"prettier"
],
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-jest": "^22.2.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"eslint": "^4.14.0",
"eslint-plugin-prettier": "^2.4.0",
Expand Down
37 changes: 33 additions & 4 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,48 @@ 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 = {
variables: {},
getContext: () => ({ headers }),
}
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 })
})
})
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import request from './request'
import extractFiles from './extractFiles'
import { isObject } from './validators'

export const createUploadMiddleware = ({ uri }) =>
export const createUploadMiddleware = ({ uri, headers }) =>
new ApolloLink((operation, forward) => {
if (typeof FormData !== 'undefined' && isObject(operation.variables)) {
const { variables, files } = extractFiles(operation.variables)
Expand All @@ -18,7 +18,11 @@ export const createUploadMiddleware = ({ uri }) =>
formData.append('variables', JSON.stringify(variables))
files.forEach(({ name, file }) => formData.append(name, file))

return request({ uri, body: formData, headers: contextHeaders })
return request({
uri,
body: formData,
headers: { ...contextHeaders, ...headers },
})
}
}

Expand Down
92 changes: 87 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
# yarn lockfile v1


"@babel/code-frame@7.0.0-beta.36":
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.36.tgz#2349d7ec04b3a06945ae173280ef8579b63728e4"
dependencies:
chalk "^2.0.0"
esutils "^2.0.2"
js-tokens "^3.0.0"

"@babel/code-frame@^7.0.0-beta.35":
version "7.0.0-beta.39"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.39.tgz#91c90bb65207fc5a55128cb54956ded39e850457"
Expand All @@ -10,6 +18,50 @@
esutils "^2.0.2"
js-tokens "^3.0.0"

"@babel/helper-function-name@7.0.0-beta.36":
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.36.tgz#366e3bc35147721b69009f803907c4d53212e88d"
dependencies:
"@babel/helper-get-function-arity" "7.0.0-beta.36"
"@babel/template" "7.0.0-beta.36"
"@babel/types" "7.0.0-beta.36"

"@babel/helper-get-function-arity@7.0.0-beta.36":
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.36.tgz#f5383bac9a96b274828b10d98900e84ee43e32b8"
dependencies:
"@babel/types" "7.0.0-beta.36"

"@babel/template@7.0.0-beta.36":
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.36.tgz#02e903de5d68bd7899bce3c5b5447e59529abb00"
dependencies:
"@babel/code-frame" "7.0.0-beta.36"
"@babel/types" "7.0.0-beta.36"
babylon "7.0.0-beta.36"
lodash "^4.2.0"

"@babel/traverse@7.0.0-beta.36":
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.36.tgz#1dc6f8750e89b6b979de5fe44aa993b1a2192261"
dependencies:
"@babel/code-frame" "7.0.0-beta.36"
"@babel/helper-function-name" "7.0.0-beta.36"
"@babel/types" "7.0.0-beta.36"
babylon "7.0.0-beta.36"
debug "^3.0.1"
globals "^11.1.0"
invariant "^2.2.0"
lodash "^4.2.0"

"@babel/types@7.0.0-beta.36":
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.36.tgz#64f2004353de42adb72f9ebb4665fc35b5499d23"
dependencies:
esutils "^2.0.2"
lodash "^4.2.0"
to-fast-properties "^2.0.0"

"@types/async@2.0.45":
version "2.0.45"
resolved "https://registry.yarnpkg.com/@types/async/-/async-2.0.45.tgz#0cfe971d7ed5542695740338e0455c91078a0e83"
Expand Down Expand Up @@ -288,6 +340,17 @@ babel-core@^6.0.0, babel-core@^6.26.0:
slash "^1.0.0"
source-map "^0.5.6"

babel-eslint@^8.2.1:
version "8.2.1"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.1.tgz#136888f3c109edc65376c23ebf494f36a3e03951"
dependencies:
"@babel/code-frame" "7.0.0-beta.36"
"@babel/traverse" "7.0.0-beta.36"
"@babel/types" "7.0.0-beta.36"
babylon "7.0.0-beta.36"
eslint-scope "~3.7.1"
eslint-visitor-keys "^1.0.0"

babel-generator@^6.18.0, babel-generator@^6.26.0:
version "6.26.1"
resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90"
Expand Down Expand Up @@ -441,7 +504,7 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"

babel-plugin-syntax-object-rest-spread@^6.13.0:
babel-plugin-syntax-object-rest-spread@^6.13.0, babel-plugin-syntax-object-rest-spread@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"

Expand Down Expand Up @@ -633,6 +696,13 @@ babel-plugin-transform-exponentiation-operator@^6.22.0:
babel-plugin-syntax-exponentiation-operator "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-object-rest-spread@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06"
dependencies:
babel-plugin-syntax-object-rest-spread "^6.8.0"
babel-runtime "^6.26.0"

babel-plugin-transform-regenerator@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f"
Expand Down Expand Up @@ -740,6 +810,10 @@ babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.26
lodash "^4.17.4"
to-fast-properties "^1.0.3"

babylon@7.0.0-beta.36:
version "7.0.0-beta.36"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.36.tgz#3a3683ba6a9a1e02b0aa507c8e63435e39305b9e"

babylon@^6.18.0:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3"
Expand Down Expand Up @@ -1011,7 +1085,7 @@ debug@^2.2.0, debug@^2.6.8:
dependencies:
ms "2.0.0"

debug@^3.1.0:
debug@^3.0.1, debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies:
Expand Down Expand Up @@ -1148,7 +1222,7 @@ eslint-plugin-prettier@^2.4.0:
fast-diff "^1.1.1"
jest-docblock "^21.0.0"

eslint-scope@^3.7.1:
eslint-scope@^3.7.1, eslint-scope@~3.7.1:
version "3.7.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8"
dependencies:
Expand Down Expand Up @@ -1511,6 +1585,10 @@ globals@^11.0.1:
version "11.1.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.1.0.tgz#632644457f5f0e3ae711807183700ebf2e4633e4"

globals@^11.1.0:
version "11.3.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.3.0.tgz#e04fdb7b9796d8adac9c8f64c14837b2313378b0"

globals@^9.18.0:
version "9.18.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a"
Expand Down Expand Up @@ -1708,7 +1786,7 @@ inquirer@^3.0.6:
strip-ansi "^4.0.0"
through "^2.3.6"

invariant@^2.2.2:
invariant@^2.2.0, invariant@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360"
dependencies:
Expand Down Expand Up @@ -2347,7 +2425,7 @@ lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

lodash@^4.13.1, lodash@^4.14.0:
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.2.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

Expand Down Expand Up @@ -3326,6 +3404,10 @@ to-fast-properties@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47"

to-fast-properties@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"

tough-cookie@>=2.3.3, tough-cookie@^2.3.3, tough-cookie@~2.3.0, tough-cookie@~2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.3.tgz#0b618a5565b6dea90bf3425d04d55edc475a7561"
Expand Down

0 comments on commit c534e13

Please sign in to comment.