Skip to content

Commit

Permalink
style(fetch): update type definition for fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jan 4, 2017
1 parent d1225ee commit 8abcd94
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
57 changes: 0 additions & 57 deletions source/api/fetch.js

This file was deleted.

59 changes: 59 additions & 0 deletions source/api/fetch.ts
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;
});
}

0 comments on commit 8abcd94

Please sign in to comment.