Skip to content

Commit

Permalink
fix: Parsing complex query strings
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed Feb 1, 2019
1 parent 63c46a9 commit 8ae5795
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
},
"dependencies": {
"chalk": "^2.4.1",
"graphql": "^14.0.2",
"graphql": "^14.1.1",
"meow": "^5.0.0",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"node-fetch": "^2.2.0"
"node-fetch": "^2.2.0",
"querystringify": "^2.1.0"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
Expand All @@ -36,8 +37,9 @@
"@types/mkdirp": "^0.5.2",
"@types/node": "^10.12.0",
"@types/node-fetch": "^2.1.2",
"@types/querystringify": "^2.0.0",
"jest": "^23.6.0",
"jest-fetch-mock": "^2.0.1",
"jest-fetch-mock": "^2.1.0",
"prettier": "^1.15.3",
"semantic-release": "^15.12.4",
"ts-jest": "^23.10.5",
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mkdirp = require('mkdirp')
import { introspectionQuery } from 'graphql/utilities/introspectionQuery'
import { buildClientSchema } from 'graphql/utilities/buildClientSchema'
import { printSchema } from 'graphql/utilities/schemaPrinter'
import * as query from 'querystringify'

/**
*
Expand All @@ -18,13 +19,15 @@ export function getHeadersFromInput(
): { key: string; value: string }[] {
switch (typeof cli.flags.header) {
case 'string': {
const [key, value] = cli.flags.header.split('=')
return [{ key, value }]
const keys = query.parse(cli.flags.header)
const key = Object.keys(keys)[0]
return [{ key: key, value: keys[key] }]
}
case 'object': {
return cli.flags.header.map(header => {
const [key, value] = header.split('=')
return { key, value }
const keys = query.parse(header)
const key = Object.keys(keys)[0]
return { key: key, value: keys[key] }
})
}
default: {
Expand Down
17 changes: 17 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,29 @@ describe('core function works as expected', () => {
},
} as any)

const complexHeader = getHeadersFromInput({
flags: {
header: 'token=123fadshfkj=$',
},
} as any)

const multipleComplexHeaders = getHeadersFromInput({
flags: {
header: ['token=123fadshfkj=$', 'token=123fadshfkj=$'],
},
} as any)

expect(noheader).toEqual([])
expect(singleHeader).toEqual([{ key: 'key', value: 'pass' }])
expect(multipleHeaders).toEqual([
{ key: 'key', value: 'pass' },
{ key: 'key', value: 'pass' },
])
expect(complexHeader).toEqual([{ key: 'token', value: '123fadshfkj=$' }])
expect(multipleComplexHeaders).toEqual([
{ key: 'token', value: '123fadshfkj=$' },
{ key: 'token', value: '123fadshfkj=$' },
])
})

/**
Expand Down
26 changes: 18 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.0.tgz#ea6dcbddbc5b584c83f06c60e82736d8fbb0c235"
integrity sha512-3TUHC3jsBAB7qVRGxT6lWyYo2v96BMmD2PTcl47H25Lu7UXtFH/2qqmKiVrnel6Ne//0TFYf6uvNX+HW2FRkLQ==

"@types/querystringify@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/querystringify/-/querystringify-2.0.0.tgz#d1eab3214ee2b57c3bd7eba0ab94b231028522fb"
integrity sha512-9WgEGTevECrXJC2LSWPqiPYWq8BRmeaOyZn47js/3V6UF0PWtcVfvvR43YjeO8BzBsthTz98jMczujOwTw+WYg==

JSONStream@^1.0.4, JSONStream@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -2208,10 +2213,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@~4.1.11
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==

graphql@^14.0.2:
version "14.0.2"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.0.2.tgz#7dded337a4c3fd2d075692323384034b357f5650"
integrity sha512-gUC4YYsaiSJT1h40krG3J+USGlwhzNTXSb4IOZljn9ag5Tj+RkoXrWp+Kh7WyE3t1NCfab5kzCuxBIvOMERMXw==
graphql@^14.1.1:
version "14.1.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.1.1.tgz#d5d77df4b19ef41538d7215d1e7a28834619fac0"
integrity sha512-C5zDzLqvfPAgTtP8AUPIt9keDabrdRAqSWjj2OPRKrKxI9Fb65I36s1uCs1UUBFnSWTdO7hyHi7z1ZbwKMKF6Q==
dependencies:
iterall "^1.2.2"

Expand Down Expand Up @@ -3032,10 +3037,10 @@ jest-environment-node@^23.4.0:
jest-mock "^23.2.0"
jest-util "^23.4.0"

jest-fetch-mock@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-2.0.1.tgz#aef284ee851f6c0f8b78c5367576edb6f64ca526"
integrity sha512-ulFW7tpQ7ihe89an+gWW8p7sMt5/smk7I0RJcZ2MXkgoM0jHVaBgqeE6XoLbXi1lWF42hK2zdnGzD8I6gSC35A==
jest-fetch-mock@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-2.1.0.tgz#49c16451b82f311158ec897e467d704e0cb118f9"
integrity sha512-jrTNlxDsZZCq6tMhdyH7gIbt4iDUHRr6C4Jp+kXItLaaaladOm9/wJjIwU3tCAEohbuW/7/naOSfg2A8H6/35g==
dependencies:
cross-fetch "^2.2.2"
promise-polyfill "^7.1.1"
Expand Down Expand Up @@ -5013,6 +5018,11 @@ query-string@^6.1.0:
decode-uri-component "^0.2.0"
strict-uri-encode "^2.0.0"

querystringify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.1.0.tgz#7ded8dfbf7879dcc60d0a644ac6754b283ad17ef"
integrity sha512-sluvZZ1YiTLD5jsqZcDmFyV2EwToyXZBfpoVOmktMmW+VEnhgakFHnasVph65fOjGPTWN0Nw3+XQaSeMayr0kg==

quick-lru@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"
Expand Down

0 comments on commit 8ae5795

Please sign in to comment.