From dd15279227b754c0b2f0ef1954ad8ffa1edf5673 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 15 Oct 2015 14:31:37 -0500 Subject: [PATCH] chore(): remove Ionic's HTTP service --- demos/i18n/index.ts | 2 +- demos/native/index.ts | 2 +- demos/net/index.ts | 65 ------- demos/net/main.html | 21 -- demos/slides/index.ts | 3 +- demos/storage/index.ts | 2 +- demos/weather/flickr.ts | 3 +- demos/weather/weather.ts | 3 +- ionic/components/slides/test/basic/index.ts | 3 +- ionic/components/slides/test/intro/index.ts | 7 +- ionic/ionic.ts | 2 - ionic/net/http.ts | 203 -------------------- 12 files changed, 11 insertions(+), 305 deletions(-) delete mode 100644 demos/net/index.ts delete mode 100644 demos/net/main.html delete mode 100644 ionic/net/http.ts diff --git a/demos/i18n/index.ts b/demos/i18n/index.ts index 89abd22a45..2a44884c34 100644 --- a/demos/i18n/index.ts +++ b/demos/i18n/index.ts @@ -1,6 +1,6 @@ import {Component, Control, ControlGroup} from 'angular2/angular2'; -import {IonicApp, App, Http} from 'ionic/ionic'; +import {IonicApp, App} from 'ionic/ionic'; import {Translate, TranslatePipe} from 'ionic/ionic'; diff --git a/demos/native/index.ts b/demos/native/index.ts index 317c09d1f2..71f185552a 100644 --- a/demos/native/index.ts +++ b/demos/native/index.ts @@ -1,6 +1,6 @@ import {Component, Control, ControlGroup} from 'angular2/angular2'; -import {IonicApp, App, Http} from 'ionic/ionic'; +import {IonicApp, App} from 'ionic/ionic'; import {Camera, Geolocation, Vibration, Battery, Device, Events} from 'ionic/ionic'; diff --git a/demos/net/index.ts b/demos/net/index.ts deleted file mode 100644 index f8ddf716d3..0000000000 --- a/demos/net/index.ts +++ /dev/null @@ -1,65 +0,0 @@ -import {Component, Control, ControlGroup} from 'angular2/angular2'; - -import {App, Http} from 'ionic/ionic'; - - -let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; -let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; - - -@App({ - templateUrl: 'main.html' -}) -class IonicApp { - constructor() { - this.form = new ControlGroup({ - requestData: new Control('') - }) - } - doGet() { - Http.get('http://swapi.co/api/people/1').then((resp) => { - this.resp = resp; - }, (err) => { - this.err = err; - }) - } - doGet404() { - Http.get(testUrl404).then((resp) => { - this.resp = resp; - }, (err) => { - this.err = err; - }) - } - doPost() { - let data = this.form.controls.requestData.value; - Http.post(testUrl, data).then((resp) => { - this.resp = resp; - }, (err) => { - this.err = err; - }) - } - doPut() { - let data = this.form.controls.requestData.value; - Http.put(testUrl, data).then((resp) => { - this.resp = resp; - }, (err) => { - this.err = err; - }) - } - doDelete() { - let data = this.form.controls.requestData.value; - Http.delete(testUrl, data).then((resp) => { - this.resp = resp; - }, (err) => { - this.err = err; - }) - } - doPatch() { - let data = this.form.controls.requestData.value; - Http.patch(testUrl, data).then((resp) => { - this.resp = resp; - }, (err) => { - this.err = err; - }) - } -} diff --git a/demos/net/main.html b/demos/net/main.html deleted file mode 100644 index ccd1d42609..0000000000 --- a/demos/net/main.html +++ /dev/null @@ -1,21 +0,0 @@ - -
-
-
-
- - - - - - - - -
- -
-
- {{resp}} -
-
-
diff --git a/demos/slides/index.ts b/demos/slides/index.ts index f1e754eab7..a67d426058 100644 --- a/demos/slides/index.ts +++ b/demos/slides/index.ts @@ -1,4 +1,4 @@ -import {App, IonicApp, Http} from 'ionic/ionic'; +import {App, IonicApp} from 'ionic/ionic'; @App({ @@ -17,6 +17,7 @@ class MyApp { let baseUrl = 'https://api.flickr.com/services/rest/'; + // TODO: update to use angular2's HTTP Service Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags, { method: 'jsonp' }).then((val) => { diff --git a/demos/storage/index.ts b/demos/storage/index.ts index a2f7724a94..f1973347ab 100644 --- a/demos/storage/index.ts +++ b/demos/storage/index.ts @@ -1,6 +1,6 @@ import {Component, Control, ControlGroup} from 'angular2/angular2'; -import {App, Http, Storage, LocalStorage, SqlStorage} from 'ionic/ionic'; +import {App, Storage, LocalStorage, SqlStorage} from 'ionic/ionic'; let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; diff --git a/demos/weather/flickr.ts b/demos/weather/flickr.ts index 8ae5dd975d..1e0fba9b6f 100755 --- a/demos/weather/flickr.ts +++ b/demos/weather/flickr.ts @@ -1,5 +1,3 @@ -import {Http} from 'ionic/net/http'; - let FLICKR_API_KEY = '504fd7414f6275eb5b657ddbfba80a2c'; let baseUrl = 'https://api.flickr.com/services/rest/'; @@ -24,6 +22,7 @@ export class Flickr { } static search(tags, lat, lng) { + // TODO: update to angular2 HTTP service return new Promise((resolve, reject) => { Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags + '&lat=' + lat + '&lng=' + lng, { method: 'jsonp' diff --git a/demos/weather/weather.ts b/demos/weather/weather.ts index e68102427e..6be800598f 100755 --- a/demos/weather/weather.ts +++ b/demos/weather/weather.ts @@ -1,11 +1,10 @@ -import {Http} from 'ionic/net/http'; - let WUNDERGROUND_API_KEY = '1cc2d3de40fa5af0'; let FORECASTIO_KEY = '4cd3c5673825a361eb5ce108103ee84a'; export class Weather { static getAtLocation(lat, lng) { + // TODO: update to angular2 HTTP service let url = 'https://api.forecast.io/forecast/' + FORECASTIO_KEY + '/'; return Http.get(url + lat + ',' + lng + '?callback=JSON_CALLBACK', { method: 'jsonp' diff --git a/ionic/components/slides/test/basic/index.ts b/ionic/components/slides/test/basic/index.ts index f1e754eab7..a67d426058 100644 --- a/ionic/components/slides/test/basic/index.ts +++ b/ionic/components/slides/test/basic/index.ts @@ -1,4 +1,4 @@ -import {App, IonicApp, Http} from 'ionic/ionic'; +import {App, IonicApp} from 'ionic/ionic'; @App({ @@ -17,6 +17,7 @@ class MyApp { let baseUrl = 'https://api.flickr.com/services/rest/'; + // TODO: update to use angular2's HTTP Service Http.get(baseUrl + '?method=flickr.groups.pools.getPhotos&group_id=1463451@N25&safe_search=1&api_key=' + FLICKR_API_KEY + '&jsoncallback=JSON_CALLBACK&format=json&tags=' + tags, { method: 'jsonp' }).then((val) => { diff --git a/ionic/components/slides/test/intro/index.ts b/ionic/components/slides/test/intro/index.ts index e9ee1f8efb..65d0fd5d9f 100644 --- a/ionic/components/slides/test/intro/index.ts +++ b/ionic/components/slides/test/intro/index.ts @@ -1,10 +1,7 @@ -import {App, IonicApp, Http} from 'ionic/ionic'; +import {App} from 'ionic/ionic'; @App({ templateUrl: 'main.html' }) -class MyApp { - constructor(private app: IonicApp) { - } -} +class MyApp {} diff --git a/ionic/ionic.ts b/ionic/ionic.ts index 959b756407..57df12b7a7 100644 --- a/ionic/ionic.ts +++ b/ionic/ionic.ts @@ -4,8 +4,6 @@ export * from './config/config' export * from './config/modes' export * from './config/decorators' -export * from './net/http' - export * from './components' export * from './platform/platform' diff --git a/ionic/net/http.ts b/ionic/net/http.ts deleted file mode 100644 index 567ee056b7..0000000000 --- a/ionic/net/http.ts +++ /dev/null @@ -1,203 +0,0 @@ -import * as util from 'ionic/util'; - - -//TODO(mlynch): surely, there must be another way, sir? -window._jsonpcallbacks = { - counter: 0 -}; - -/** - * The Http class makes it easy to send GET/POST/PUT/DELETE/PATCH requests - * and send/receive JSON (or anything else) through a simple API. - * - * Http uses the `fetch()` API underneath, or a polyfill if it's not natively supported. - */ -export class Http { - - /** - * The raw fetch() operation. - * - * Generally, you want to use one of get()/post()/put()/delete() but - * this is useful if you want to do something crazy. - * - * @param url the URL to pass to fetch - * @param options the options to configure the fetch - * @return es6 promise from the fetch. - */ - static fetch(url, options) { - return window.fetch(url, options).then((response) => { - // status "0" to handle local files fetching (e.g. Cordova/Phonegap etc.) - if (response.status === 200 || response.status === 0) { - - // We have a good response, let's check the response headers and return - // deserialized JSON or return the text from the response. - if (response.headers.get('Content-Type') === 'application/json') { - return response.json(); - } - return response.text(); - } else { - return Promise.reject(response, new Error(response.statusText)); - } - }).catch((err) => { - return Promise.reject(err); - }); - } - - static jsonp(url, callbackId, options) { - return new Promise((resolve, reject) => { - - var script = document.createElement('script'); - script.src = url; - script.async = true; - script.type = 'text/javascript'; - - var callback = (event) => { - script.removeEventListener('load', callback); - script.removeEventListener('error', callback); - - document.body.removeChild(script); - - let text, status; - if(event) { - if(event.type === "load" && !window._jsonpcallbacks[callbackId].called) { - event = { type: "error" }; - } - text = event.type; - status = event.type === "error" ? 404 : 200; - resolve(window._jsonpcallbacks[callbackId].data, status, text); - } else { - reject(); - } - - /* - var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), - callbackId, function(status, text) { - completeRequest(callback, status, callbacks[callbackId].data, "", text); - callbacks[callbackId] = noop; - }); - */ - }; - - script.addEventListener('load', callback); - script.addEventListener('error', callback); - document.body.appendChild(script); - return callback; - - }); - } - - static _method(method, url, data, options, sendsJson) { - options = util.defaults(options, { - method: method, - headers: { - 'Accept': 'application/json,text/plain,*/*', - }, - }); - - if(options.body) { - options.body = (typeof data === 'string') ? data : JSON.stringify(data) - } - - if(sendsJson) { - options.headers['Content-Type'] = 'application/json'; - } - - if(options.method == 'jsonp') { - // Adopted from Angular 1 - let callbacks = window._jsonpcallbacks; - - var callbackId = '_' + (callbacks.counter++).toString(36); - callbacks[callbackId] = function(data) { - callbacks[callbackId].data = data; - callbacks[callbackId].called = true; - }; - - - /* - var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), - callbackId, function(status, text) { - completeRequest(callback, status, callbacks[callbackId].data, "", text); - callbacks[callbackId] = noop; - }); - */ - - url = url.replace('JSON_CALLBACK', '_jsonpcallbacks.' + callbackId); - - return Http.jsonp(url, callbackId, options); - } else { - return Http.fetch(url, options); - } - - } - - /** - * Send a GET request to the given URL. - * - * By default, options sends the `Accept` header as `application/json,text/plain,* / *`, - * - * @param url the URL to POST to - * @param options the options to configure the post with. - * @return promise - */ - static get(url, options = {}) { - return Http._method('get', url, {}, options); - } - - /** - * Send a POST request to the given URL. - * - * By default, options sends the `Accept` header as `application/json,text/plain,* / *`, - * and the `Content-Type` header as `application/json` - * - * @param url the URL to POST to - * @param options the options to configure the post with. - * @return promise - */ - static post(url, data = {}, options = {}) { - return Http._method('post', url, data, options, true); - } - - /** - * Send a PUT request to the given URL. - * - * By default, options sends the `Accept` header as `application/json,text/plain,* / *`, - * and the `Content-Type` header as `application/json` - * - * @param url the URL to PUT to - * @param data the JSON data to send - * @param options the options to configure the post with. - * @return promise - */ - static put(url, data = {}, options = {}) { - return Http._method('put', url, data, options, true); - } - - /** - * Send a DELETE request to the given URL. - * - * By default, options sends the `Accept` header as `application/json,text/plain,* / *`, - * and the `Content-Type` header as `application/json` - * - * @param url the URL to DELETE to - * @param data the JSON data to send - * @param options the options to configure the post with. - * @return promise - */ - static delete(url, data = {}, options = {}) { - return Http._method('delete', url, data, options, true); - } - - /** - * Send a PATH request to the given URL. - * - * By default, options sends the `Accept` header as `application/json,text/plain,* / *`, - * and the `Content-Type` header as `application/json` - * - * @param url the URL to PATH to - * @param options the options to configure the post with. - * @return promise - */ - static patch(url, data = {}, options = {}) { - return Http._method('patch', url, data, options, true); - } -}