-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(fetch): update type definition for fetch
- Loading branch information
Showing
2 changed files
with
59 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as fetch from 'node-fetch'; | ||
|
||
declare const global: any; | ||
|
||
/** | ||
* Adds logging to every fetch request if a global var for `verbose` is set to true | ||
* | ||
* @param {(string | fetch.Request)} url the request | ||
* @param {fetch.RequestInit} [init] the usual options | ||
* @returns {Promise<fetch.Response>} network-y promise | ||
*/ | ||
export function api(url: string | any, init: any): Promise<any> { | ||
if (global.verbose && global.verbose === true) { | ||
const output = ['curl', '-i']; | ||
|
||
if (init.method) { | ||
output.push(`-X ${init.method}`); | ||
} | ||
|
||
const showToken = process.env['DANGER_VERBOSE_SHOW_TOKEN']; | ||
const token = process.env['DANGER_GITHUB_API_TOKEN']; | ||
|
||
if (init.headers) { | ||
for (const prop in init.headers) { | ||
if (init.headers.hasOwnProperty(prop)) { | ||
// Don't show the token for normal verbose usage | ||
if (init.headers[prop].includes(token) && !showToken) { | ||
output.push('-H', `"${prop}: [API TOKEN]"`); | ||
continue; | ||
} | ||
output.push('-H', `"${prop}: ${init.headers[prop]}"`); | ||
} | ||
} | ||
} | ||
|
||
if (init.method === 'POST') { | ||
// const body:string = init.body | ||
// output.concat([init.body]) | ||
} | ||
|
||
if (typeof url === 'string') { | ||
output.push(url); | ||
} | ||
|
||
console.log(output.join(' ')); | ||
} | ||
|
||
return fetch(url, init) | ||
.then((response: any) => { | ||
// Handle failing errors | ||
if (!response.ok) { | ||
process.exitCode = 1; | ||
console.error(`Request failed: ${response}`); | ||
const msg = response.status === 0 ? 'Network Error' : response.statusText; | ||
throw new (Error as any)(response.status, msg, {response: response}); | ||
} | ||
return response; | ||
}); | ||
} |