Skip to content

Commit

Permalink
Harden the typing for network requests (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
orta authored and macklinu committed Mar 30, 2017
1 parent 9190cea commit 15c48c8
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const checkForNewDependencies = (packageDiff) => {
warn(`New dependencies added: ${sentence(newDependencies)}.`)

newDependencies.forEach(dep => {
const output = child_process.execSync(`yarn why jest --json`)
const output = child_process.execSync(`yarn why ${dep} --json`)

// Comes as a series of little JSON messages
const usefulJSONContents = output.toString().split(`{"type":"activityEnd","data":{"id":0}}`).pop() as string
Expand Down
10 changes: 5 additions & 5 deletions source/api/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "node-fetch"
import * as debug from "debug"
import * as node_fetch from "node-fetch"

const d = debug("danger:networking")
declare const global: any
Expand All @@ -10,7 +10,7 @@ declare const global: any
* @param {fetch.RequestInit} [init] the usual options
* @returns {Promise<fetch.Response>} network-y promise
*/
export function api(url: string | any, init: any): Promise<any> {
export function api(url: string | node_fetch.Request, init: node_fetch.RequestInit): Promise<node_fetch.Response> {
if (global.verbose && global.verbose === true) {
const output = ["curl", "-i"]

Expand Down Expand Up @@ -45,9 +45,9 @@ export function api(url: string | any, init: any): Promise<any> {

d(output.join(" "))
}

return fetch(url, init)
.then(async (response) => {
const originalFetch: any = node_fetch
return originalFetch(url, init)
.then(async (response: node_fetch.Response) => {
// Handle failing errors
if (!response.ok) {
const responseJSON = await response.json()
Expand Down
106 changes: 106 additions & 0 deletions source/platforms/_tests/fixtures/github_issue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"url": "https://api.github.com/repos/artsy/emission/issues/327",
"repository_url": "https://api.github.com/repos/artsy/emission",
"labels_url": "https://api.github.com/repos/artsy/emission/issues/327/labels{/name}",
"comments_url": "https://api.github.com/repos/artsy/emission/issues/327/comments",
"events_url": "https://api.github.com/repos/artsy/emission/issues/327/events",
"html_url": "https://github.com/artsy/emission/pull/327",
"id": 180313498,
"number": 327,
"title": "Adds support for showing the metadata and trending Artists to a Gene VC",
"user": {
"login": "orta",
"id": 49038,
"avatar_url": "https://avatars1.githubusercontent.com/u/49038?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/orta",
"html_url": "https://github.com/orta",
"followers_url": "https://api.github.com/users/orta/followers",
"following_url": "https://api.github.com/users/orta/following{/other_user}",
"gists_url": "https://api.github.com/users/orta/gists{/gist_id}",
"starred_url": "https://api.github.com/users/orta/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/orta/subscriptions",
"organizations_url": "https://api.github.com/users/orta/orgs",
"repos_url": "https://api.github.com/users/orta/repos",
"events_url": "https://api.github.com/users/orta/events{/privacy}",
"received_events_url": "https://api.github.com/users/orta/received_events",
"type": "User",
"site_admin": false
},
"labels": [

],
"state": "closed",
"locked": false,
"assignee": {
"login": "mennenia",
"id": 373860,
"avatar_url": "https://avatars3.githubusercontent.com/u/373860?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/mennenia",
"html_url": "https://github.com/mennenia",
"followers_url": "https://api.github.com/users/mennenia/followers",
"following_url": "https://api.github.com/users/mennenia/following{/other_user}",
"gists_url": "https://api.github.com/users/mennenia/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mennenia/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mennenia/subscriptions",
"organizations_url": "https://api.github.com/users/mennenia/orgs",
"repos_url": "https://api.github.com/users/mennenia/repos",
"events_url": "https://api.github.com/users/mennenia/events{/privacy}",
"received_events_url": "https://api.github.com/users/mennenia/received_events",
"type": "User",
"site_admin": false
},
"assignees": [
{
"login": "mennenia",
"id": 373860,
"avatar_url": "https://avatars3.githubusercontent.com/u/373860?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/mennenia",
"html_url": "https://github.com/mennenia",
"followers_url": "https://api.github.com/users/mennenia/followers",
"following_url": "https://api.github.com/users/mennenia/following{/other_user}",
"gists_url": "https://api.github.com/users/mennenia/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mennenia/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mennenia/subscriptions",
"organizations_url": "https://api.github.com/users/mennenia/orgs",
"repos_url": "https://api.github.com/users/mennenia/repos",
"events_url": "https://api.github.com/users/mennenia/events{/privacy}",
"received_events_url": "https://api.github.com/users/mennenia/received_events",
"type": "User",
"site_admin": false
}
],
"milestone": null,
"comments": 5,
"created_at": "2016-09-30T14:10:30Z",
"updated_at": "2017-03-12T23:45:59Z",
"closed_at": "2016-10-03T09:39:23Z",
"pull_request": {
"url": "https://api.github.com/repos/artsy/emission/pulls/327",
"html_url": "https://github.com/artsy/emission/pull/327",
"diff_url": "https://github.com/artsy/emission/pull/327.diff",
"patch_url": "https://github.com/artsy/emission/pull/327.patch"
},
"body": "Gap at the top is for follow button \n\n![screen shot 2016-09-30 at 15 08 40](https://cloud.githubusercontent.com/assets/49038/18994491/f46402d4-871f-11e6-85c8-21dc99addc7d.png)\n\n![screen shot 2016-09-30 at 15 08 43](https://cloud.githubusercontent.com/assets/49038/18994493/f64c635c-871f-11e6-8e95-fe2d7c0e0c1c.png)\n",
"closed_by": {
"login": "mennenia",
"id": 373860,
"avatar_url": "https://avatars3.githubusercontent.com/u/373860?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/mennenia",
"html_url": "https://github.com/mennenia",
"followers_url": "https://api.github.com/users/mennenia/followers",
"following_url": "https://api.github.com/users/mennenia/following{/other_user}",
"gists_url": "https://api.github.com/users/mennenia/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mennenia/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mennenia/subscriptions",
"organizations_url": "https://api.github.com/users/mennenia/orgs",
"repos_url": "https://api.github.com/users/mennenia/repos",
"events_url": "https://api.github.com/users/mennenia/events{/privacy}",
"received_events_url": "https://api.github.com/users/mennenia/received_events",
"type": "User",
"site_admin": false
}
}
10 changes: 6 additions & 4 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { RepoMetaData } from "../../ci_source/ci_source"
import { GitHubPRDSL, GitHubUser} from "../../dsl/GitHubDSL"
import * as find from "lodash.find"

import * as node_fetch from "node-fetch"

// The Handle the API specific parts of the github

export type APIToken = string
Expand Down Expand Up @@ -89,7 +91,7 @@ export class GitHubAPI {
const prID = this.repoMetadata.pullRequestID
const res = await this.get(`repos/${repo}/pulls/${prID}`)

return res.ok ? res.json() : {}
return res.ok ? res.json() as Promise<GitHubPRDSL> : {} as GitHubPRDSL
}

async getPullRequestCommits(): Promise<any> {
Expand Down Expand Up @@ -184,15 +186,15 @@ export class GitHubAPI {
})
}

get(path: string, headers: any = {}, body: any = {}) {
get(path: string, headers: any = {}, body: any = {}): Promise<node_fetch.Response> {
return this.api(path, headers, body, "GET")
}

post(path: string, headers: any = {}, body: any = {}) {
post(path: string, headers: any = {}, body: any = {}): Promise<node_fetch.Response> {
return this.api(path, headers, JSON.stringify(body), "POST")
}

patch(path: string, headers: any = {}, body: any = {}) {
patch(path: string, headers: any = {}, body: any = {}): Promise<node_fetch.Response> {
return this.api(path, headers, JSON.stringify(body), "PATCH")
}
}

0 comments on commit 15c48c8

Please sign in to comment.