Skip to content

Commit fb925bb

Browse files
committed
Merge branch 'master' of github.com:graphcool/nodeql
2 parents f809125 + 3e30a09 commit fb925bb

File tree

7 files changed

+131
-16
lines changed

7 files changed

+131
-16
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
"scripts": {
2727
"prepublish": "npm run build",
2828
"build": "rm -rf dist && tsc -d",
29-
"test": "npm run build && ava --serial"
29+
"lint": "tslint --type-check --project tsconfig.json {src,test}/**/*.ts",
30+
"test": "npm run lint && npm run build && ava --serial"
3031
},
3132
"devDependencies": {
3233
"@types/fetch-mock": "^5.8.2",
3334
"@types/node": "^7.0.18",
3435
"ava": "^0.19.1",
3536
"fetch-mock": "^5.11.0",
37+
"tslint": "^5.5.0",
38+
"tslint-config-standard": "^6.0.1",
3639
"typescript": "^2.3.2"
3740
},
3841
"dependencies": {

src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ClientError, Options, Variables } from './types'
22
export { ClientError } from './types'
33
import 'isomorphic-fetch'
44

5-
export async function request<T extends any>(url: string, query: string, variables?: Variables): Promise<T> {
5+
export async function request<T extends any> (url: string, query: string, variables?: Variables): Promise<T> {
66
const client = new GraphQLClient(url)
77

88
return client.request<T>(query, variables)
@@ -14,21 +14,20 @@ export class GraphQLClient {
1414
private url: string
1515
private options: Options
1616

17-
constructor(url: string, options?: Options) {
17+
constructor (url: string, options?: Options) {
1818
this.url = url
19-
this.options = {
20-
headers: (options && options.headers) ? options.headers : {},
21-
}
19+
this.options = options || {}
2220
}
2321

24-
async request<T extends any>(query: string, variables?: Variables): Promise<T> {
22+
async request<T extends any> (query: string, variables?: Variables): Promise<T> {
2523
const body = JSON.stringify({
2624
query,
2725
variables: variables ? variables : undefined,
2826
})
2927

3028
const response = await fetch(this.url, {
3129
method: 'POST',
30+
...this.options,
3231
headers: Object.assign({'Content-Type': 'application/json'}, this.options.headers),
3332
body,
3433
})
@@ -43,8 +42,8 @@ export class GraphQLClient {
4342
}
4443
}
4544

46-
async function getResult(response: Response): Promise<any> {
47-
const contentType = response.headers.get('Content-Type');
45+
async function getResult (response: Response): Promise<any> {
46+
const contentType = response.headers.get('Content-Type')
4847
if (contentType && contentType.startsWith('application/json')) {
4948
return await response.json()
5049
} else {

src/types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
export type Variables = { [key: string]: any }
22

33
export interface Options {
4+
method?: RequestInit['method']
45
headers?: { [key: string]: string }
6+
mode?: RequestInit['mode']
7+
credentials?: RequestInit['credentials']
8+
cache?: RequestInit['cache']
9+
redirect?: RequestInit['redirect']
10+
referrer?: RequestInit['referrer']
11+
referrerPolicy?: RequestInit['referrerPolicy']
12+
integrity?: RequestInit['integrity']
513
}
614

715
export interface GraphQLError {
@@ -27,7 +35,7 @@ export class ClientError extends Error {
2735
response: GraphQLResponse
2836
request: GraphQLRequestContext
2937

30-
constructor(response: GraphQLResponse, request: GraphQLRequestContext) {
38+
constructor (response: GraphQLResponse, request: GraphQLRequestContext) {
3139
const message = `${ClientError.extractMessage(response)}: ${JSON.stringify({ response, request })}`
3240

3341
super(message)
@@ -38,12 +46,11 @@ export class ClientError extends Error {
3846
Error.captureStackTrace(this, ClientError)
3947
}
4048

41-
private static extractMessage(response: GraphQLResponse): string {
49+
private static extractMessage (response: GraphQLResponse): string {
4250
try {
4351
return response.errors![0].message
4452
} catch (e) {
4553
return `GraphQL Error (Code: ${response.status})`
4654
}
4755
}
4856
}
49-

tests/index.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava'
22
import * as fetchMock from 'fetch-mock'
3-
import { ClientError, request } from '../src/index'
3+
import { ClientError, request, GraphQLClient } from '../src/index'
4+
import { Options } from '../src/types'
45

56
test('minimal query', async (t) => {
67
const data = {
@@ -46,6 +47,26 @@ test('content-type with charset', async (t) => {
4647
})
4748
})
4849

50+
51+
test('extra fetch options', async (t) => {
52+
const options: Options = {
53+
credentials: 'include',
54+
mode: 'cors',
55+
cache: 'reload',
56+
}
57+
58+
const client = new GraphQLClient('https://mock-api.com/graphql', options)
59+
await mock({
60+
body: { data: {test: 'test'} }
61+
}, async () => {
62+
await client.request('{ test }')
63+
const actualOptions = fetchMock.lastCall()[1]
64+
for (let name in options) {
65+
t.deepEqual(actualOptions[name], options[name])
66+
}
67+
})
68+
})
69+
4970
async function mock(response: any, testFn: () => Promise<void>) {
5071
fetchMock.mock({
5172
matcher: '*',

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"sourceMap": true,
88
"strictNullChecks": true,
99
"outDir": "dist",
10-
"lib": ["es2016", "dom"]
10+
"lib": ["es2015", "es2016", "dom"]
1111
},
1212
"exclude": [
1313
"node_modules"

tslint.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": ["tslint-config-standard"],
3+
"rules": {
4+
"trailing-comma": [
5+
true,
6+
{
7+
"multiline": "always",
8+
"singleline": "never"
9+
}
10+
],
11+
"no-use-before-declare": false
12+
}
13+
}

yarn.lock

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,20 @@ color-name@^1.1.1:
768768
version "1.1.2"
769769
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.2.tgz#5c8ab72b64bd2215d617ae9559ebb148475cf98d"
770770

771+
colors@^1.1.2:
772+
version "1.1.2"
773+
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"
774+
771775
combined-stream@^1.0.5, combined-stream@~1.0.5:
772776
version "1.0.5"
773777
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009"
774778
dependencies:
775779
delayed-stream "~1.0.0"
776780

781+
commander@^2.9.0:
782+
version "2.11.0"
783+
resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563"
784+
777785
common-path-prefix@^1.0.0:
778786
version "1.0.0"
779787
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-1.0.0.tgz#cd52f6f0712e0baab97d6f9732874f22f47752c0"
@@ -910,10 +918,17 @@ diff-match-patch@^1.0.0:
910918
version "1.0.0"
911919
resolved "https://registry.yarnpkg.com/diff-match-patch/-/diff-match-patch-1.0.0.tgz#1cc3c83a490d67f95d91e39f6ad1f2e086b63048"
912920

913-
diff@^3.0.0, diff@^3.0.1:
921+
diff@^3.0.0, diff@^3.0.1, diff@^3.2.0:
914922
version "3.2.0"
915923
resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"
916924

925+
doctrine@^0.7.2:
926+
version "0.7.2"
927+
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-0.7.2.tgz#7cb860359ba3be90e040b26b729ce4bfa654c523"
928+
dependencies:
929+
esutils "^1.1.6"
930+
isarray "0.0.1"
931+
917932
dot-prop@^4.1.0:
918933
version "4.1.1"
919934
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-4.1.1.tgz#a8493f0b7b5eeec82525b5c7587fa7de7ca859c1"
@@ -984,6 +999,10 @@ estraverse@^4.0.0, estraverse@^4.1.1:
984999
version "4.2.0"
9851000
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13"
9861001

1002+
esutils@^1.1.6:
1003+
version "1.1.6"
1004+
resolved "https://registry.yarnpkg.com/esutils/-/esutils-1.1.6.tgz#c01ccaa9ae4b897c6d0c3e210ae52f3c7a844375"
1005+
9871006
esutils@^2.0.2:
9881007
version "2.0.2"
9891008
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
@@ -1195,7 +1214,7 @@ glob-to-regexp@^0.3.0:
11951214
version "0.3.0"
11961215
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab"
11971216

1198-
glob@^7.0.3, glob@^7.0.5:
1217+
glob@^7.0.3, glob@^7.0.5, glob@^7.1.1:
11991218
version "7.1.2"
12001219
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
12011220
dependencies:
@@ -2104,6 +2123,10 @@ path-key@^2.0.0:
21042123
version "2.0.1"
21052124
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
21062125

2126+
path-parse@^1.0.5:
2127+
version "1.0.5"
2128+
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"
2129+
21072130
path-to-regexp@^1.7.0:
21082131
version "1.7.0"
21092132
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
@@ -2408,6 +2431,12 @@ resolve-from@^2.0.0:
24082431
version "2.0.0"
24092432
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-2.0.0.tgz#9480ab20e94ffa1d9e80a804c7ea147611966b57"
24102433

2434+
resolve@^1.3.2:
2435+
version "1.3.3"
2436+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5"
2437+
dependencies:
2438+
path-parse "^1.0.5"
2439+
24112440
restore-cursor@^2.0.0:
24122441
version "2.0.0"
24132442
resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
@@ -2670,6 +2699,49 @@ trim-right@^1.0.1:
26702699
version "1.0.1"
26712700
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
26722701

2702+
tslib@^1.0.0, tslib@^1.7.1:
2703+
version "1.7.1"
2704+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.7.1.tgz#bc8004164691923a79fe8378bbeb3da2017538ec"
2705+
2706+
tslint-config-standard@^6.0.1:
2707+
version "6.0.1"
2708+
resolved "https://registry.yarnpkg.com/tslint-config-standard/-/tslint-config-standard-6.0.1.tgz#a04ba0a794759e877287056f549b081e47a56d6c"
2709+
dependencies:
2710+
tslint-eslint-rules "^4.0.0"
2711+
2712+
tslint-eslint-rules@^4.0.0:
2713+
version "4.1.1"
2714+
resolved "https://registry.yarnpkg.com/tslint-eslint-rules/-/tslint-eslint-rules-4.1.1.tgz#7c30e7882f26bc276bff91d2384975c69daf88ba"
2715+
dependencies:
2716+
doctrine "^0.7.2"
2717+
tslib "^1.0.0"
2718+
tsutils "^1.4.0"
2719+
2720+
tslint@^5.5.0:
2721+
version "5.5.0"
2722+
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.5.0.tgz#10e8dab3e3061fa61e9442e8cee3982acf20a6aa"
2723+
dependencies:
2724+
babel-code-frame "^6.22.0"
2725+
colors "^1.1.2"
2726+
commander "^2.9.0"
2727+
diff "^3.2.0"
2728+
glob "^7.1.1"
2729+
minimatch "^3.0.4"
2730+
resolve "^1.3.2"
2731+
semver "^5.3.0"
2732+
tslib "^1.7.1"
2733+
tsutils "^2.5.1"
2734+
2735+
tsutils@^1.4.0:
2736+
version "1.9.1"
2737+
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.9.1.tgz#b9f9ab44e55af9681831d5f28d0aeeaf5c750cb0"
2738+
2739+
tsutils@^2.5.1:
2740+
version "2.7.1"
2741+
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.7.1.tgz#411a0e9466525a2b2869260a55620d7292155e24"
2742+
dependencies:
2743+
tslib "^1.7.1"
2744+
26732745
tunnel-agent@^0.6.0:
26742746
version "0.6.0"
26752747
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"

0 commit comments

Comments
 (0)