Skip to content

zyra/cordova-plugin-native-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cordova-plugin-native-http

Native HTTP Client for Cordova Apps.

Work in progress. So far it seem to be x2 - x2.5 times faster than XHR/Angular Http on Android, barely any difference on iOS.


Usage will be like:

const http = cordova.plugins.NativeHttp;

http.get('https://path/to/something')
  .then(res => {
    // nice utility to convert res.body to json
    res = res.json();
    
    console.log(res.body);
  })
  .catch(e => {
    // nice utility to convert e.body to json
    e = e.json();
      
    console.log(e.body, e.error);
  });

Goals:

  • Build a good native HTTP client for Cordova apps in general
  • Ability to integrate with Ionic 2 apps, by overriding provider or by conditional use. (this functionality might be added through another library, i.e. Ionic Native)
@NgModule({
  providers: [
    { provide: Http, useClass: NativeHttp }
  ]
})

// OR

@Injectable()
export class APIService {

  private http: any;
  
  constructor(
    http: Http,
    platform: Platform
  ) {
   
    this.http = http;
    
    platform.ready().then(() => {
    
      if (nativePluginIsAvailalbe) {
        
        this.http = NativeHttp
      
      }
    
    
    });
  
  }
  
  
  get(url: string, params?: any, headers?: any): Promise<any> {
  
    return this.http.get(url, params, headers).map(res => res.json());
  
  }

}