All Projects → DarkXaHTeP → angular-progress-http

DarkXaHTeP / angular-progress-http

Licence: MIT license
[DEPRECATED] Use @angular/common/http instead

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to angular-progress-http

Uploader
A lightweight and very configurable jQuery plugin for file uploading using ajax(a sync); includes support for queues, progress tracking and drag and drop.
Stars: ✭ 1,042 (+2323.26%)
Mutual labels:  progress, upload, ajax
Cj Upload
Higher order React components for file uploading (with progress) react file upload
Stars: ✭ 589 (+1269.77%)
Mutual labels:  xhr, progress, upload
Ironwing
universal, framework agnostic, transport layer
Stars: ✭ 17 (-60.47%)
Mutual labels:  xhr, ajax
Xhr.js
🌎 xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.
Stars: ✭ 12 (-72.09%)
Mutual labels:  xhr, ajax
angular2-signature-pad
Signature pad component for Angular 2.x and above.
Stars: ✭ 17 (-60.47%)
Mutual labels:  angular2, ng2
Atomic
A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
Stars: ✭ 526 (+1123.26%)
Mutual labels:  xhr, ajax
Bootstrap Fileinput
An enhanced HTML 5 file input for Bootstrap 5.x/4.x./3.x with file preview, multiple selection, and more features.
Stars: ✭ 5,097 (+11753.49%)
Mutual labels:  xhr, upload
Axios Tutorial
axios实例应用及源码剖析 - xhr篇 (走心教程)
Stars: ✭ 219 (+409.3%)
Mutual labels:  xhr, ajax
Unfetch
🐕 Bare minimum 500b fetch polyfill.
Stars: ✭ 5,239 (+12083.72%)
Mutual labels:  xmlhttprequest, ajax
upload-file-to-backblaze-b2-from-browser-example
Demonstrates calling the b2_upload_file Backblaze B2 Cloud Storage API from a web browser using AJAX.
Stars: ✭ 28 (-34.88%)
Mutual labels:  xhr, ajax
ic-datepicker
Angular (2+) datepicker component
Stars: ✭ 27 (-37.21%)
Mutual labels:  angular2, ng2
ajax
Ajax wrapper for nim js backend
Stars: ✭ 18 (-58.14%)
Mutual labels:  xmlhttprequest, ajax
Resource Loader
A middleware-style generic resource loader built with web games in mind.
Stars: ✭ 364 (+746.51%)
Mutual labels:  xhr, ajax
Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (+523.26%)
Mutual labels:  xhr, ajax
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (+4.65%)
Mutual labels:  xhr, ajax
Rext
🎈A lightweight (< 5kb gzipped) and Promise-supported HTTP request library, for all browsers.
Stars: ✭ 14 (-67.44%)
Mutual labels:  xhr, ajax
ng2-storage
A local and session storage wrapper for angular 2.
Stars: ✭ 14 (-67.44%)
Mutual labels:  angular2, ng2
Vue Ui For Pc
基于Vue2.x的一套PC端UI组件,包括了Carousel 跑马灯、Cascader 级联、Checkbox 多选框、Collapse 折叠面板、DatePicker 日期选择、Dialog 对话框、Form 表单、Input 输入框、InputNumber 数字输入框、Layer 弹窗层、Loading 加载、Menu 菜单、Page 分页、Progress 进度条、Radio 单选框、SelectDropDown 仿select、Switch 开关、Table 表格、Tabs 标签页、Textarea 文本框、Tooltip 文字提示、BackTop 返回顶部、steps 步骤条、Transfer 穿梭框、Tree 树形、Upload 文件上传、Lazy 图片懒加载、Loading 加载、Pagination 分页等等
Stars: ✭ 156 (+262.79%)
Mutual labels:  progress, upload
ng-data-picker
🏄🏼 A data picker based on Angular 4+ (like native datetime picker of iOS)
Stars: ✭ 24 (-44.19%)
Mutual labels:  angular2, ng2
ng2-fontawesome
An easy-to-use directive for font awesome icons.
Stars: ✭ 20 (-53.49%)
Mutual labels:  angular2, ng2

angular-progress-http

A thin wrapper around Angular 2+ Http service that adds ability to work with upload/download progress

npm

NPM Version NPM Downloads

build info

Build Status Coverage Status

Usage

Import HttpModule and ProgressHttpModule

import { NgModule } from "@angular/core";
import { HttpModule } from "@angular/http";
import { ProgressHttpModule } from "angular-progress-http";

@NgModule({
    imports: [
        HttpModule,
        ProgressHttpModule
    ]
})
export class AppModule {}

Inject ProgressHttp into your component and you are ready to go. See API description below for available methods.

import {Component} from "@angular/core";
import { ProgressHttp } from "angular-progress-http";

@Component({})
export class AppComponent {
    constructor(private http: ProgressHttp) {
        const form = new FormData();
        form.append("data", "someValue or file");

        this.http
            .withUploadProgressListener(progress => { console.log(`Uploading ${progress.percentage}%`); })
            .withDownloadProgressListener(progress => { console.log(`Downloading ${progress.percentage}%`); })
            .post("/fileUpload", form)
            .subscribe((response) => {
                console.log(response)
            })
    }
}

Supported Angular versions

  • Both Angular 4 and 5 are supported by the latest version
  • Angular 2 is supported in v0.5.1. Use command npm install [email protected] to get it

Releases

For release notes please see CHANGELOG.md

API description

ProgressHttp service extends Http service provided by Angular/Http which means that you get all of the Http methods including

request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
get(url: string, options?: RequestOptionsArgs): Observable<Response>;
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;

and others.

In addition it provides two methods for handling progress:

withDownloadProgressListener(listener: (progress: Progress) => void): HttpWithDownloadProgressListener;
withUploadProgressListener(listener: (progress: Progress) => void): HttpWithUploadProgressListener;

They both take callback as argument and return new instances of the service.

The interfaces returned from methods are described below:

interface HttpWithDownloadProgressListener extends Http {
    withUploadProgressListener(listener: (progress: Progress) => void): Http;
}

interface HttpWithUploadProgressListener extends Http {
    withDownloadProgressListener(listener: (progress: Progress) => void): Http;
}

Their purpose is to make libary easier to use and add compile-time checks for method calls

progressHttp //can use http api or call withUploadProgressListener or withDownloadProgressListener
    .withUploadProgressListener(progress => {}) //can use http api or call withDownloadProgressListener
    .withDownloadProgressListener(progress => {}) //here and on lines below can only use http api
    .post("/fileUpload", form)
    .subscribe((response) => {})

This restriction is used to make sure that there are now repeating calls to add progress listeners that will overwrite previously assigned handlers and may confuse developer

Calls to both methods are immutable (return new instances and do not change the internal state of the service), so you may do next things

let http1 = this.progressHttp.withUploadProgressListener(progress => { console.log("Uploading 1") });
let http2 = this.progressHttp.withUploadProgressListener(progress => { console.log("Uploading 2") });
let http3 = http1.withDownloadProgressListener(progress => { console.log("Downloading 1") });

In the code above http1 and http2 will have different upload listeners. http3 will have same upload listener as http1 and a download listener

This behavior may be useful when uploading multiple files simultaneously e.g.

this.files.forEach(f => {
    const form = new FormData();
    form.append("file", f.file);

    this.progressHttp
        .withUploadProgressListener(progress => { f.percentage = progress.percentage; })
        .post("/fileUpload", form)
        .subscribe((r) => {
            f.uploaded = true;
        })
});

Progress interface

Both upload and download progress listeners accept single argument that implements Progress interface

interface Progress {
    event: ProgressEvent, //event emitted by XHR
    lengthComputable: boolean, //if false percentage and total are undefined
    percentage?: number, //percentage of work finished
    loaded: number, //amount of data loaded in bytes
    total?: number // amount of data total in bytes
}

How it works internally

The library tries to rely on Angular code as much as possible instead of reinventing the wheel.

It extends BrowserXhr class with logic that adds event listeners to XMLHttpRequest and executes progress listeners. Other parts that are responsible for http calls (Http, XhrConnection, XhrBackend) are used as is, which means that angular-progress-http will automatically receive fixes and new features from newer versions of angular/http

Using custom HTTP implementations

If you want to use custom Http service with progress you need to follow certain steps. Let's review them on example of ng2-adal library - a library for accessing APIs restricted by Azure AD.

  1. create factory class that will implement HttpFactory interface
interface HttpFactory {
    create<T extends Http>(backend: ConnectionBackend, requestOptions: RequestOptions): T;
}

This interface contains single method to create instances of class derived from Http. The create method accepts ConnectionBackend and default RequestOptions which are always required for Http to make creation of factory easier.

Let's examine AuthHttp (Http implementation from ng2-adal) constructor to understand what dependencies it has:

constructor(http: Http, adalService: AdalService);

As you can see, it needs an instance of http service and adalService to work properly. With this knowledge we can now create the factory class.

The factory for ng2-adal is quite simple and will look next way:

import { Injectable } from "@angular/core";
import { ConnectionBackend, RequestOptions } from "@angular/http";
import { AuthHttp, AdalService } from "ng2-adal/core";
import { HttpFactory, AngularHttpFactory } from "angular-progress-http";

@Injectable()
export class AuthHttpFactory implements HttpFactory {
  constructor(
    private adalService: AdalService,
    private angularHttpFactory: AngularHttpFactory
  ) {}

  public create(backend: ConnectionBackend, requestOptions: RequestOptions) {
    const http = this.angularHttpFactory.create(backend, requestOptions);
    return new AuthHttp(http, this.adalService);
  }
}
  1. Register created factory as a provider in your application
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { ProgressHttpModule, HTTP_FACTORY } from 'angular-progress-http';
import { AuthHttpModule } from "ng2-adal/core";
import { AuthHttpFactory } from "./ng2-adal.http.factory.service";

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpModule,
    ProgressHttpModule,
    AuthHttpModule
  ],
  providers: [
    { provide: HTTP_FACTORY, useClass: AuthHttpFactory }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

That's it. Now each time when you will call methods of ProgressHttp it will use your custom http implementation internally and add progress listeners to it.

Building from sources

  1. Clone the repository to the local PC
  2. Run
npm install
npm run build
  1. The built library can be found in "build" folder

Running tests

  1. Clone the repository to the local PC
  2. Run
npm install
npm test

Running examples

There are two example projects at the moment

  • basic example of upload progress functionality (examples/upload-download)
  • an example that uses custom http implementation (examples/custom-http)
  1. Make sure that you built library from sources as described above
  2. Navigate to selected example folder
  3. Run
npm install
npm start
  1. Open browser on http://localhost:3000
  2. Choose some files (big size of the files will let you see the progress bar) and click upload
  3. Use throttling in Chrome dev tools to slow down network if progress jumps from 0 to 100 immediately

Сontribution

Feel free to ask questions and post bugs/ideas in the issues, as well as send pull requests.

License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].