Skip to content

Commit

Permalink
replace isomorphic fetch with whatwg-fetch and node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
hampsterx committed Apr 21, 2016
1 parent 4da4cb3 commit ecda499
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test
.babelrc
.eslintrc
*.yml
build.js
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
console.info(event.title)
})

## NodeJS

You will need to bring your own fetch library

// npm install node-fetch
global.fetch = require('node-fetch')
var Client = require('predicthq')

// or
var fetch = require('node-fetch')
var Client = require('predicthq')
var phq = new Client({access_token: "ACCESS_TOKEN", fetch: fetch})



## Endpoints

Expand Down
81 changes: 45 additions & 36 deletions dist/predicthq.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/predicthq.min.js

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions index.js

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "predicthq",
"version": "0.0.4",
"description": "PredictHQ Event Intelligence",
"main": "index.js",
"main": "dist/predicthq.js",
"scripts": {
"lint": " node node_modules/eslint/bin/eslint.js ./src/",
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --require babel-core/register test",
Expand Down Expand Up @@ -43,10 +43,10 @@
"core-js": "2.1.4",
"dotenv": "2.0.0",
"es6-promise": "3.1.2",
"isomorphic-fetch": "2.2.1",
"jsonschema": "1.1.0",
"lodash": "4.6.1",
"loglevel": "1.4.0",
"whatwg-fetch": "^0.11.0",
"youarei": "1.0.2"
},
"devDependencies": {
Expand All @@ -71,6 +71,7 @@
"module-deps": "4.0.5",
"mversion": "1.10.1",
"nock": "7.2.2",
"node-fetch": "^1.5.1",
"npm-check-updates": "2.5.8",
"uglify-js": "2.6.2"
}
Expand Down
21 changes: 17 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import Accounts from "./endpoints/v1/accounts"
import {polyfill} from 'es6-promise'
polyfill()

import 'isomorphic-fetch'
// For the browser only (server side must bring their own fetch lib)
import 'whatwg-fetch';

import YouAreI from 'youarei'

import dotenv from 'dotenv'
dotenv.config({silent: true})


let log = logger.getLogger("predicthq.client")

class Client {

constructor(options){
constructor(options) {

options = options || {}

Expand All @@ -33,8 +33,21 @@ class Client {
this.users = new Users(this)
this.accounts = new Accounts(this)

this.fetch = options.fetch || false
if (!this.fetch){
// Server
if (typeof(global) != 'undefined' && global.hasOwnProperty('fetch'))
this.fetch = global.fetch
// Browser
else if (typeof(window) != 'undefined' && window.hasOwnProperty('fetch'))
this.fetch = window.fetch
else {
this.fetch = ()=> { throw "No Fetch Library present. You must provide one!" }
}
}
}


request(method, path, returnClass, options){

let access_token = this.options.access_token
Expand All @@ -47,7 +60,7 @@ class Client {

log.debug(uri.to_string())

fetch(uri.to_string(), {
this.fetch(uri.to_string(), {
method: method,
headers: {
'Authorization': 'Bearer ' + access_token,
Expand Down
2 changes: 2 additions & 0 deletions test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

global.fetch = require('node-fetch')

import expect from 'expect'
import nock from 'nock'

Expand Down
1 change: 0 additions & 1 deletion test/test_events_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ logger.getLogger("predicthq.client").setLevel('DEBUG')
let access_token = process.env.ACCESS_TOKEN
let test_account_id = process.env.TEST_ACCOUNT_ID


describe('Events', () => {
it('Search', (done) => {

Expand Down

0 comments on commit ecda499

Please sign in to comment.