forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebb6a60
commit f463deb
Showing
6 changed files
with
705 additions
and
101 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
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
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,66 +1,90 @@ | ||
'use strict'; | ||
|
||
const rp = require('request-promise-native'); | ||
const auth = require('./auth'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
async function request(query, variables) { | ||
const options = { | ||
uri: 'https://api.github.com/graphql', | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Basic ${await auth()}`, | ||
'User-Agent': 'node-check-pr' | ||
}, | ||
json: true, | ||
gzip: true, | ||
body: { | ||
query: query, | ||
variables: variables | ||
} | ||
}; | ||
// console.log(options); | ||
const result = await rp(options); | ||
if (result.errors) { | ||
const err = new Error('GraphQL request Error'); | ||
err.data = { | ||
// query: query, | ||
variables: variables, | ||
errors: result.errors | ||
}; | ||
throw err; | ||
class Request { | ||
constructor(credentials) { | ||
this.credentials = credentials; | ||
} | ||
return result.data; | ||
} | ||
|
||
async function requestAll(query, variables, path) { | ||
let after = null; | ||
let all = []; | ||
// first page | ||
do { | ||
const varWithPage = Object.assign({ | ||
after | ||
}, variables); | ||
const data = await request(query, varWithPage); | ||
let current = data; | ||
for (const step of path) { | ||
current = current[step]; | ||
} | ||
// current should have: | ||
// totalCount | ||
// pageInfo { hasNextPage, endCursor } | ||
// nodes | ||
all = all.concat(current.nodes); | ||
if (current.pageInfo.hasNextPage) { | ||
after = current.pageInfo.endCursor; | ||
loadQuery(file) { | ||
const filePath = path.resolve(__dirname, '..', 'queries', `${file}.gql`); | ||
return fs.readFileSync(filePath, 'utf8'); | ||
} | ||
|
||
async promise() { | ||
return rp(...arguments); | ||
} | ||
|
||
async gql(name, variables, path) { | ||
const query = this.loadQuery(name); | ||
if (path) { | ||
const result = await this.requestAll(query, variables, path); | ||
return result; | ||
} else { | ||
after = null; | ||
const result = await this.request(query, variables); | ||
return result; | ||
} | ||
} while (after !== null); | ||
} | ||
|
||
return all; | ||
async request(query, variables) { | ||
const options = { | ||
uri: 'https://api.github.com/graphql', | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Basic ${this.credentials}`, | ||
'User-Agent': 'node-core-utils' | ||
}, | ||
json: true, | ||
gzip: true, | ||
body: { | ||
query: query, | ||
variables: variables | ||
} | ||
}; | ||
// console.log(options); | ||
const result = await rp(options); | ||
if (result.errors) { | ||
const err = new Error('GraphQL request Error'); | ||
err.data = { | ||
// query: query, | ||
variables: variables, | ||
errors: result.errors | ||
}; | ||
throw err; | ||
} | ||
return result.data; | ||
} | ||
|
||
async requestAll(query, variables, path) { | ||
let after = null; | ||
let all = []; | ||
// first page | ||
do { | ||
const varWithPage = Object.assign({ | ||
after | ||
}, variables); | ||
const data = await this.request(query, varWithPage); | ||
let current = data; | ||
for (const step of path) { | ||
current = current[step]; | ||
} | ||
// current should have: | ||
// totalCount | ||
// pageInfo { hasNextPage, endCursor } | ||
// nodes | ||
all = all.concat(current.nodes); | ||
if (current.pageInfo.hasNextPage) { | ||
after = current.pageInfo.endCursor; | ||
} else { | ||
after = null; | ||
} | ||
} while (after !== null); | ||
|
||
return all; | ||
} | ||
} | ||
|
||
module.exports = { | ||
request, | ||
requestAll | ||
}; | ||
module.exports = Request; |
Oops, something went wrong.