├── CHANGELOG.md ├── .babelrc ├── .gitignore ├── .editorconfig ├── test ├── index.spec.js └── support │ └── server.js ├── LICENSE ├── rollup.config.js ├── karma.conf.js ├── .travis.yml ├── package.json ├── README.md └── src └── index.js /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "env" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | npm-debug.log 4 | .DS_Store 5 | coverage 6 | test-server.pid 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | [*] 3 | # Change these settings to your own preference 4 | indent_style = space 5 | indent_size = 2 6 | # We recommend you to keep these unchanged 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /test/index.spec.js: -------------------------------------------------------------------------------- 1 | const expect = require('expect') 2 | const $$observable = require('symbol-observable') 3 | const rxhr = require('../src') 4 | const baseUrl = '//localhost:8070' 5 | 6 | describe('rxhr', () => { 7 | it('should export a function', done => { 8 | expect(typeof rxhr).toBe('function') 9 | let request = rxhr({ 10 | method: 'get', 11 | url: baseUrl + '/json' 12 | }) 13 | expect(typeof request[$$observable]).toBe('function') 14 | expect(typeof request[$$observable]()).toBe('object') 15 | const subscription = request.subscribe( 16 | res => { 17 | done() 18 | }, 19 | err => { 20 | console.log(err) 21 | done() 22 | }, 23 | () => { 24 | console.log('done') 25 | } 26 | ) 27 | expect(typeof subscription.unsubscribe).toBe('function') 28 | // subscription.unsubscribe() 29 | }) 30 | }) 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-present Alessandro Arnodo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel' 2 | import uglify from 'rollup-plugin-uglify' 3 | import resolve from 'rollup-plugin-node-resolve' 4 | import commonjs from 'rollup-plugin-commonjs' 5 | import es3 from 'rollup-plugin-es3' 6 | 7 | function makeDest (format) { 8 | return `dist/${pkg.name}.${format}${minify ? `.min` : ``}.js` 9 | } 10 | 11 | const minify = !!process.env.MINIFY 12 | const pkg = require('./package.json') 13 | 14 | let targets = [ 15 | { dest: makeDest('cjs'), format: 'cjs' }, 16 | { dest: makeDest('umd'), format: 'umd', moduleName: pkg.name } 17 | ] 18 | 19 | export default { 20 | entry: 'src/index.js', 21 | useStrict: false, 22 | sourceMap: minify, 23 | plugins: [ 24 | resolve({ 25 | jsnext: true 26 | // main: false, 27 | // browser: false 28 | }), 29 | commonjs(), 30 | babel({ 31 | exclude: 'node_modules/**', 32 | babelrc: false, 33 | presets: [ 34 | [ 35 | 'env', 36 | { 37 | modules: false 38 | } 39 | ] 40 | ] 41 | }), 42 | minify ? uglify() : {}, 43 | es3() 44 | ], 45 | targets: minify 46 | ? targets 47 | : targets.concat([{ dest: makeDest('es'), format: 'es' }]) 48 | } 49 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function (config) { 2 | config.set({ 3 | frameworks: ['mocha'], 4 | reporters: ['mocha', 'coverage-istanbul'], 5 | browsers: ['Chrome'], 6 | 7 | customLaunchers: { 8 | Chrome_travis_ci: { 9 | base: 'Chrome', 10 | flags: ['--no-sandbox'] 11 | } 12 | }, 13 | 14 | files: ['test/**/*.spec.js'], 15 | 16 | preprocessors: { 17 | '**/*': ['webpack', 'sourcemap'] 18 | }, 19 | 20 | coverageIstanbulReporter: { 21 | dir: 'coverage', 22 | reports: ['html', 'lcovonly', 'text-summary'], 23 | fixWebpackSourcePaths: true 24 | }, 25 | 26 | webpack: { 27 | module: { 28 | rules: [ 29 | { 30 | test: /\.js$/, 31 | exclude: /node_modules/, 32 | loader: 'babel-loader' 33 | }, 34 | { 35 | test: /\.js$/, 36 | exclude: /(node_modules|test)/, 37 | enforce: 'pre', 38 | loader: 'istanbul-instrumenter-loader', 39 | query: { 40 | esModules: true 41 | } 42 | } 43 | ] 44 | } 45 | }, 46 | webpackMiddleware: { 47 | noInfo: true 48 | }, 49 | singleRun: true 50 | }) 51 | 52 | if (process.env.TRAVIS) { 53 | config.browsers = ['Chrome_travis_ci'] 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6 4 | cache: 5 | directories: 6 | - node_modules 7 | notifications: 8 | email: false 9 | before_install: 10 | - export CHROME_BIN=chromium-browser 11 | - export DISPLAY=:99.0 12 | - sh -e /etc/init.d/xvfb start 13 | after_success: 14 | - npm install -g codecov 15 | - codecov 16 | branches: 17 | only: 18 | - master 19 | env: 20 | global: 21 | - secure: Ok+JHub1XDkidt18XgW09qV8Q2sAq9Wz1vmDYX66guJiwnLOGeEEqgmIqPxbVSgK9xIsfrnyf6uxpz1+r20A2EWxzeiAVpEN15SzPs1QGvYVSnDxIiXmOEkJLsW5XOA1NItKVnwI1krbnEBM0jf8oRqIMNQYVhGsucd2rPtx7VbY0HjH/f2cXWBB37/7F1TEkbv/DUu9Rjm9jW9QbhKkWrotBj5KMt5jekHa/rG0AlRH67/ECDV0yjR+HfQWx+L7JR4j6ZHR0SPAMYCfFmynBOzk3y2bkzHxmpRQi1gsoeacbZ1eFkTag9YCXiSXzwZkxGY95d8ooRtOIgdh6Mn/HhGmQqHrB+/KP4AchZIi/Ruf7Bw46u5SrlNqRkCUNGB0OOY82k0CNKl8PuJK5uaqODQkit+dljiImB319F3IVWrVTXD4MbNBQb1leULQ+eGLWjvd4uSlFiDq2O9JYtUYsbaaxJbT+EvDUxBLPkdwvXT8E2UiYxbfSJ+vFtsuzuZnsmwkXB/LucRW+Mq1QAUMU497zWWlRaekAf8KSmr9lr/pFj5+VPDLfhgfXloxCPM19ZBPheRAEpjCWYp8+7PKeRe+Si/kWwkVYbHQVFlQ/M19zXQGPd3xz+VAWNGyYsdia120KwlTzWEda7r5KZ66wYbk6ZbIKYLFDr/COLP/s6w= 22 | - secure: t98sxBNgofaBFOCOtXDTFATkcS73ZYnejSllfWZjFSAIxjmKT4f4i25WRPiD91dKgHgpZb4Gy2ED6RLDSAnrCQ5BicX7Q5T8VOR70JgUEje7SxvEsKgd6+u/9qmBizXkfih9zvbW3KVV+2CAxb15dBZHzYQCmAaWTAP9rcu0UkSz1yBP2Qxrozb5mydare4mFw2fofuVne/uSgmWwiYEHx/Ml2I2TRg3FCcLJsoPu6n7izhZ3gTux/ESq7daiW/T617eDqlFGrNoYRXys5HNG4dKK/v/++A4EDC0LmRFdix/og5deUO9jmzcDKJbKcqhRjPMYGVt5JOzgWai7fwV7HSuQ8WWPC3HMsBiy4behWhHfhA02o4a5unLgUVE0l6nyfNaGoLZqw1BDUbqj8z88shML7WSI+dTp8/OqdtF3n1aGM9nQgWzteSeRWSduD1DD8iQLKSMOH1WYWSxdWm95NZ/MHDbU1SnKKtMxJt+tKq+a0N1jzGeVNkbd798mWivZWi7NythusLs67HUQhumTxH6uzTGtrbwR7bqm43KFXT7JcUHekftwErxdiX9Cq/K3pJ8I4hASi+Yhor8JnZwzd1ros81KJCtQUcf0n3dApIZ+S8UUmnzy/nnj7VEC/YsCTdU73lQ1CyKTVCIsvjmVVhLAOMW6JKZ2gYnAfk48bA= 23 | -------------------------------------------------------------------------------- /test/support/server.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const bodyParser = require('body-parser') 3 | const cors = require('cors') 4 | const cookieParser = require('cookie-parser') 5 | 6 | const app = express() 7 | app.use(cors()) 8 | 9 | app.use(function (req, res, next) { 10 | res.set('Cache-Control', 'no-cache, no-store') 11 | next() 12 | }) 13 | app.use(bodyParser.urlencoded({ extended: true })) 14 | app.use(bodyParser.json()) 15 | app.use(cookieParser()) 16 | 17 | app.get('/hello', function (req, res) { 18 | setTimeout(function () { 19 | const contentTypeHeader = req.get('Content-Type') 20 | if (!contentTypeHeader) { 21 | res.send('Hello World') 22 | } else { 23 | res 24 | .status(500) 25 | .send( 26 | 'Expected Content-Type request header to be undefined, but got ' + 27 | contentTypeHeader 28 | ) 29 | } 30 | }, 150) 31 | }) 32 | 33 | app.post('/pet', function (req, res) { 34 | setTimeout(function () { 35 | const result = 'added ' + req.body.name + ' the ' + req.body.species 36 | 37 | res.send(result) 38 | }, 150) 39 | }) 40 | 41 | app.get('/json', function (req, res) { 42 | setTimeout(function () { 43 | res.status(200).json({ name: 'manny' }) 44 | }, 150) 45 | }) 46 | 47 | app.get('/querystring', function (req, res) { 48 | setTimeout(function () { 49 | res.send(req.query) 50 | }, 150) 51 | }) 52 | 53 | app.get('/error', function (req, res) { 54 | setTimeout(function () { 55 | res.status(500).send('boom') 56 | }, 150) 57 | }) 58 | 59 | app.delete('/delete', function (req, res) { 60 | setTimeout(function () { 61 | res.status(200).json({ deleted: true }) 62 | }, 150) 63 | }) 64 | 65 | app.get('/binary', function (req, res) { 66 | setTimeout(function () { 67 | const result = Buffer.alloc(3) 68 | res.writeHead(200, { 69 | 'Content-Type': 'application/octet-stream', 70 | 'Content-Length': result.byteLength 71 | }) 72 | res.status(200).write(result, 'binary') 73 | }, 150) 74 | }) 75 | 76 | app.listen(process.env.PORT) 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rxhr", 3 | "amdName": "rxhr", 4 | "version": "1.0.0-alpha.1", 5 | "description": "Tiny Observable based HTTP client", 6 | "jsnext:main": "dist/rxhr.es.js", 7 | "module": "dist/rxhr.es.js", 8 | "main": "dist/rxhr.cjs.js", 9 | "umd:main": "dist/rxhr.umd.js", 10 | "scripts": { 11 | "precommit": "lint-staged", 12 | "bump": "standard-version", 13 | "test:karma": "karma start --single-run && npm run stop-server", 14 | "test:watch": "karma start", 15 | "start-server": "PORT=8070 node test/support/server & echo $! > test-server.pid", 16 | "stop-server": "if ps -p $(cat test-support-server.pid)> /dev/null; then kill -KILL $(cat test-support-server.pid); fi && rimraf test-server.pid", 17 | "lint": "standard", 18 | "format": "prettier --write --semi false '*.js' && standard --fix", 19 | "test": "npm-run-all start-server test:karma", 20 | "build": "npm-run-all clean rollup rollup:min size", 21 | "clean": "rimraf dist", 22 | "rollup": "rollup -c", 23 | "rollup:min": "cross-env MINIFY=minify rollup -c", 24 | "size": "echo \"Gzipped Size: $(cat dist/rxhr.umd.min.js | gzip-size)\"", 25 | "release": "npm run build && npm run bump && git push --follow-tags origin master && npm publish" 26 | }, 27 | "repository": "vesparny/rxhr", 28 | "keywords": [ 29 | "xhr", 30 | "http", 31 | "ajax", 32 | "observable", 33 | "rxjs", 34 | "reactive", 35 | "request" 36 | ], 37 | "homepage": "https://github.com/vesparny/rxhr", 38 | "authors": [ 39 | "Alessandro Arnodo " 40 | ], 41 | "license": "MIT", 42 | "files": [ 43 | "dist", 44 | "src", 45 | "test" 46 | ], 47 | "devDependencies": { 48 | "babel-core": "^6.24.1", 49 | "babel-eslint": "^7.2.2", 50 | "babel-loader": "^7.0.0", 51 | "babel-preset-env": "^1.5.2", 52 | "body-parser": "^1.17.2", 53 | "cookie-parser": "^1.4.3", 54 | "cors": "^2.8.3", 55 | "cross-env": "^5.0.0", 56 | "expect": "^1.20.2", 57 | "express": "^4.15.3", 58 | "gzip-size-cli": "^2.0.0", 59 | "husky": "^0.13.3", 60 | "istanbul-instrumenter-loader": "^2.0.0", 61 | "karma": "^1.7.0", 62 | "karma-chrome-launcher": "^2.1.1", 63 | "karma-coverage-istanbul-reporter": "^1.2.1", 64 | "karma-mocha": "^1.3.0", 65 | "karma-mocha-reporter": "^2.2.3", 66 | "karma-sauce-launcher": "^1.1.0", 67 | "karma-sourcemap-loader": "^0.3.7", 68 | "karma-webpack": "^2.0.3", 69 | "lint-staged": "^3.4.2", 70 | "mocha": "^3.4.1", 71 | "npm-run-all": "^4.0.2", 72 | "prettier": "^1.3.1", 73 | "rimraf": "^2.5.2", 74 | "rollup": "^0.42.0", 75 | "rollup-plugin-babel": "^2.7.1", 76 | "rollup-plugin-commonjs": "^8.0.2", 77 | "rollup-plugin-es3": "^1.0.3", 78 | "rollup-plugin-node-resolve": "^3.0.0", 79 | "rollup-plugin-uglify": "^2.0.1", 80 | "standard": "^10.0.2", 81 | "standard-version": "^4.0.0", 82 | "webpack": "^2.6.0" 83 | }, 84 | "dependencies": { 85 | "symbol-observable": "^1.0.4" 86 | }, 87 | "standard": { 88 | "parser": "babel-eslint", 89 | "globals": [ 90 | "it", 91 | "describe", 92 | "Blob", 93 | "XMLHttpRequest" 94 | ] 95 | }, 96 | "lint-staged": { 97 | "*.js": [ 98 | "prettier --write --semi false --single-quote", 99 | "standard --fix", 100 | "git add" 101 | ] 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rxhr 2 | > A tiny Observable based HTTP client 3 | 4 | [![Travis](https://img.shields.io/travis/vesparny/rxhr.svg)](https://travis-ci.org/vesparny/rxhr) 5 | [![Code Coverage](https://img.shields.io/codecov/c/github/vesparny/rxhr.svg?style=flat-square)](https://codecov.io/github/vesparny/rxhr) 6 | [![David](https://img.shields.io/david/vesparny/rxhr.svg)](https://david-dm.org/vesparny/rxhr) 7 | [![npm](https://img.shields.io/npm/v/rxhr.svg)](https://www.npmjs.com/package/rxhr) 8 | [![npm](https://img.shields.io/npm/dm/rxhr.svg)](https://npm-stat.com/charts.html?package=rxhr&from=2017-05-19) 9 | [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) 10 | [![MIT License](https://img.shields.io/npm/l/rxhr.svg?style=flat-square)](https://github.com/vesparny/rxhr/blob/master/LICENSE) 11 | 12 | The current size of `rxhr/dist/rxhr.umd.min.js` is: 13 | 14 | [![gzip size](http://img.badgesize.io/https://unpkg.com/rxhr/dist/rxhr.umd.min.js?compression=gzip&label=gzip%20size&style=flat-square)](https://unpkg.com/rxhr/dist/) 15 | 16 | ## Install 17 | 18 | This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed. 19 | 20 | ```sh 21 | $ npm i rxhr 22 | ``` 23 | 24 | Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else: 25 | 26 | ```javascript 27 | // using ES6 modules 28 | import rxhr from 'rxhr' 29 | 30 | // using CommonJS modules 31 | var rxhr = require('rxhr') 32 | ``` 33 | 34 | The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com): 35 | 36 | ```html 37 | 38 | ``` 39 | 40 | You can find the library on `window.rhxr`. 41 | 42 | ## Usage 43 | 44 | ```js 45 | import rxhr from 'rxhr' 46 | 47 | const req$ = rxhr({ 48 | method: 'get', 49 | responseType: 'json', 50 | url: 'https://jsonplaceholder.typicode.com/posts' 51 | }) 52 | .subscribe( 53 | res => res.json().forEach(e => console.log(e.title)), 54 | err => console.log(err), 55 | () => console.log('completed') 56 | ) 57 | 58 | // abort request 59 | req$.unsubscribe() 60 | ``` 61 | 62 | It's easy to combine with rxjs 63 | 64 | ```js 65 | const req$ = rxhr({ 66 | method: 'get', 67 | responseType: 'json', 68 | url: 'https://jsonplaceholder.typicode.com/posts' 69 | }) 70 | 71 | const sub$ = Rx.Observable 72 | .timer(0, 1000) 73 | .switchMap(() => Rx.Observable.from(req$)) 74 | .map(res => res.json()) 75 | .subscribe( 76 | res => console.log(res.length), 77 | err => console.log('err', err), 78 | () => console.log('completed') 79 | ) 80 | ``` 81 | 82 | It supports blob request type 83 | 84 | ```js 85 | const req$ = rxhr({ 86 | method: 'get', 87 | responseType: 'blob', 88 | url: 'https://avatars2.githubusercontent.com/u/82070?v=3&s=460' 89 | }) 90 | 91 | const sub$ = Rx.Observable 92 | .timer(0, 1000) 93 | .take(3) 94 | .switchMap(() => Rx.Observable.from(req$)) 95 | .map(res => res.blob()) 96 | .subscribe( 97 | blob => { 98 | const fr = new FileReader(); 99 | fr.onload = function(e) { 100 | const img = new Image(); // width, height values are optional params 101 | img.src = e.target.result 102 | document.body.appendChild(img) 103 | } 104 | fr.readAsDataURL(blob); 105 | }, 106 | err => console.log('err', err), 107 | () => console.log('completed') 108 | ) 109 | ``` 110 | 111 | ## Tests 112 | 113 | ```sh 114 | $ npm run test 115 | ``` 116 | 117 | [MIT License](LICENSE.md) © [Alessandro Arnodo](https://alessandro.arnodo.net/) 118 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const $$observable = require('symbol-observable') 2 | 3 | const encodeParams = params => 4 | Object.keys(params) 5 | .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) 6 | .join('&') 7 | 8 | const buildUrl = (url, params) => 9 | params ? url + '?' + encodeParams(params) : url 10 | 11 | const fromResponseHeaderString = headersString => { 12 | const headers = {} 13 | headersString.split('\n').forEach(line => { 14 | const index = line.indexOf(':') 15 | if (index > 0) headers[line.slice(0, index)] = line.slice(index + 1).trim() 16 | }) 17 | return headers 18 | } 19 | 20 | const noop = () => {} 21 | 22 | const rxhr = options => { 23 | return { 24 | subscribe (onNext, onError, onComplete) { 25 | let observer = onNext 26 | let request = new XMLHttpRequest() 27 | if (typeof onNext === 'function') { 28 | observer = { 29 | next: onNext, 30 | error: onError || noop, 31 | complete: onComplete || noop 32 | } 33 | } 34 | try { 35 | const buildResponse = err => { 36 | const body = 37 | err || 38 | (!options.responseType || options.responseType === 'text' 39 | ? request.responseText 40 | : request.response) 41 | let response = { 42 | // normalize IE9 bug (http://bugs.jquery.com/ticket/1450) 43 | status: request.status === 1223 ? 204 : request.status, 44 | ok: request.status >= 200 && request.status < 300, 45 | type: err ? 'error' : 'default', 46 | statusText: err ? request.statusText : request.statusText || 'OK', 47 | headers: fromResponseHeaderString(request.getAllResponseHeaders()), 48 | url: request.responseURL, 49 | text: () => 50 | typeof body === 'object' ? JSON.stringify(body) : body, 51 | json: () => (typeof body === 'string' ? JSON.parse(body) : body), 52 | blob: () => new Blob([body]) 53 | } 54 | return response 55 | } 56 | 57 | const onReqLoad = () => { 58 | let response = buildResponse() 59 | if (response.ok) { 60 | observer.next(response) 61 | observer.complete() 62 | return 63 | } 64 | observer.error(response) 65 | } 66 | 67 | const onReqError = () => { 68 | const response = buildResponse(new Error('Network Error')) 69 | observer.error(response) 70 | } 71 | 72 | const onReqTimeout = () => { 73 | const response = buildResponse(new Error('ECONNABORTED')) 74 | observer.error(response) 75 | } 76 | 77 | const onReqProgress = evt => { 78 | typeof options.progressObserver[$$observable] === 'function' 79 | ? options.progressObserver.next(evt) 80 | : options.progressObserver(evt) 81 | } 82 | 83 | request.open( 84 | options.method.toUpperCase(), 85 | buildUrl(options.url, options.params) 86 | ) 87 | // response type 88 | options.responseType && (request.responseType = options.responseType) 89 | // with credentials 90 | request.withCredentials = options.withCredentials === true 91 | // headers 92 | for (let i in options.headers) { 93 | request.setRequestHeader(i, options.headers[i]) 94 | } 95 | // timeout in ms 96 | request.timeout = options.timeout 97 | 98 | request.send(options.body || null) 99 | 100 | request.onload = onReqLoad 101 | request.onerror = onReqError 102 | request.ontimeout = onReqTimeout 103 | if (options.progressObserver) { 104 | request.onprogress = onReqProgress 105 | if (request.upload) { 106 | request.upload.onprogress = onReqProgress 107 | } 108 | } 109 | } catch (err) { 110 | observer.error(err) 111 | } 112 | return { 113 | unsubscribe () { 114 | request.abort() 115 | } 116 | } 117 | }, 118 | [$$observable] () { 119 | return this 120 | } 121 | } 122 | } 123 | 124 | module.exports = rxhr 125 | --------------------------------------------------------------------------------