├── .eslintignore ├── .eslintrc ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmignore ├── .prettierrc ├── .vscode └── settings.json ├── LICENSE.md ├── README.md ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── __snapshots__ │ └── index.test.ts.snap ├── index.test.ts └── index.ts └── tsconfig.json /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "parser": "@typescript-eslint/parser", 4 | "plugins": [ 5 | "@typescript-eslint", 6 | "prettier" 7 | ], 8 | "extends": [ 9 | "eslint:recommended", 10 | "plugin:@typescript-eslint/eslint-recommended", 11 | "plugin:@typescript-eslint/recommended", 12 | "prettier" 13 | ], 14 | "rules": { 15 | "no-console": 1, 16 | "prettier/prettier": 2 17 | } 18 | } -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: push 4 | 5 | jobs: 6 | test: 7 | name: Test 8 | runs-on: ubuntu-latest 9 | strategy: 10 | matrix: 11 | node-version: [14.x, 15.x, 16.x] 12 | steps: 13 | - uses: actions/checkout@v3 14 | - name: Use Node.js ${{ matrix.node-version }} 15 | uses: actions/setup-node@v3 16 | with: 17 | node-version: ${{ matrix.node-version }} 18 | - name: Install dependencies 19 | run: npm ci 20 | - name: Run tests 21 | run: npm run test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.eslintignore 3 | !.eslintrc 4 | !.github 5 | !.gitignore 6 | !.npmignore 7 | !.prettierrc 8 | 9 | dist 10 | node_modules -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcmunder/universal-rxjs-ajax/1d23dfd15532d434f49da1997c626b0fdcd365a6/.npmignore -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "semi": false, 4 | "bracketSpacing": false 5 | } 6 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017-present Matthias Christoph Munder 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # universal-rxjs-ajax 2 | 3 | [![Build Status](https://travis-ci.org/mcmunder/universal-rxjs-ajax.svg?branch=master)](https://travis-ci.org/mcmunder/universal-rxjs-ajax) [![npm version](https://badge.fury.io/js/universal-rxjs-ajax.svg)](https://badge.fury.io/js/universal-rxjs-ajax) [![Join the chat at https://gitter.im/mcmunder/universal-rxjs-ajax](https://badges.gitter.im/mcmunder/universal-rxjs-ajax.svg)](https://gitter.im/mcmunder/universal-rxjs-ajax?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | All credit goes to [jayphelps](https://github.com/jayphelps). Copied from his 6 | suggestion made 7 | [here](https://github.com/ReactiveX/rxjs/issues/2099#issuecomment-288140971). 8 | 9 | `universal-rxjs-ajax` makes `Observable.ajax` available in `Node.js` by throwing 10 | in [xhr2](https://github.com/pwnall/node-xhr2) when necessary. 11 | 12 | ## Usage 13 | 14 | ```ts 15 | const {map} = require('rxjs/operators') 16 | const {request} = require('universal-rxjs-ajax') 17 | 18 | interface Repo { 19 | name: string 20 | } 21 | 22 | // config as accepted by Observable.ajax() 23 | const config = { 24 | url: 'https://api.github.com/orgs/Reactive-Extensions/repos', 25 | method: 'GET', // and so on... 26 | } 27 | 28 | request(config) 29 | .pipe(map(({response}) => response.map((repo) => repo.name))) 30 | .subscribe((repoNames) => console.log(repoNames)) 31 | ``` 32 | 33 | ## Try it yourself 34 | 35 | https://runkit.com/mcmunder/universal-rxjs-ajax-playground 36 | 37 | ## Copyright and license 38 | 39 | Copyright 2018, Matthias Munder. 40 | Licensed under the [MIT license](./LICENSE). 41 | 42 | [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) 43 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | roots: ['/src'], 3 | testMatch: [ 4 | '**/__tests__/**/*.+(ts|tsx|js)', 5 | '**/?(*.)+(spec|test).+(ts|tsx|js)', 6 | ], 7 | transform: { 8 | '^.+\\.(ts|tsx)$': 'ts-jest', 9 | }, 10 | } 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "universal-rxjs-ajax", 3 | "description": "Makes rxjs's ajax available in Node.js", 4 | "version": "2.0.4", 5 | "type": "module", 6 | "source": "src/index.ts", 7 | "types": "dist/index.d.ts", 8 | "main": "dist/universal-rxjs-ajax.cjs", 9 | "umd:main": "dist/universal-rxjs-ajax.umd.js", 10 | "module": "dist/universal-rxjs-ajax.module.js", 11 | "exports": { 12 | "node": "./dist/universal-rxjs-ajax.cjs", 13 | "import": "./dist/universal-rxjs-ajax.cjs.modern.js" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/mcmunder/universal-rxjs-ajax.git" 18 | }, 19 | "keywords": [ 20 | "rxjs", 21 | "observable", 22 | "ajax", 23 | "universal", 24 | "isomorphic" 25 | ], 26 | "author": "mcmunder", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/mcmunder/universal-rxjs-ajax/issues" 30 | }, 31 | "homepage": "https://github.com/mcmunder/universal-rxjs-ajax", 32 | "scripts": { 33 | "build": "microbundle", 34 | "dev": "microbundle watch", 35 | "test": "jest", 36 | "test:watch": "jest --watch" 37 | }, 38 | "devDependencies": { 39 | "@types/jest": "^29.0.3", 40 | "@typescript-eslint/eslint-plugin": "^5.38.0", 41 | "@typescript-eslint/parser": "^5.38.0", 42 | "eslint": "^8.23.1", 43 | "eslint-config-prettier": "^8.5.0", 44 | "eslint-plugin-prettier": "^4.2.1", 45 | "jest": "^29.0.3", 46 | "microbundle": "^0.15.1", 47 | "prettier": "^2.7.1", 48 | "rxjs": "^7.5.6", 49 | "ts-jest": "^29.0.1", 50 | "typescript": "^4.8.3" 51 | }, 52 | "dependencies": { 53 | "xhr2": "^0.2.1" 54 | }, 55 | "peerDependencies": { 56 | "rxjs": ">=7.5.6" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/__snapshots__/index.test.ts.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`universal-rx-js-ajax request returns Observable 1`] = ` 4 | Observable { 5 | "_subscribe": [Function], 6 | } 7 | `; 8 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import {Observable} from 'rxjs' 2 | import {map} from 'rxjs/operators' 3 | import {request} from './index' 4 | 5 | type Repo = { 6 | name: string 7 | } 8 | 9 | describe('universal-rx-js-ajax', () => { 10 | test('request returns Observable', () => { 11 | const result = request({url: 'http://some-api.af'}) 12 | expect(result instanceof Observable).toBeTruthy() 13 | expect(result).toMatchSnapshot() 14 | }) 15 | 16 | test('README example works', () => { 17 | // config as accepted by Observable.ajax() 18 | const config = { 19 | url: 'https://api.github.com/orgs/Reactive-Extensions/repos', 20 | method: 'GET', // and so on... 21 | } 22 | 23 | request(config) 24 | .pipe(map(({response}) => response.map((repo: Repo) => repo.name))) 25 | .subscribe((repoNames) => { 26 | expect(repoNames.includes('RxJS')) 27 | }) 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import {ajax, AjaxConfig, AjaxResponse} from 'rxjs/ajax' 2 | import {Observable} from 'rxjs/internal/Observable' 3 | import xhr2 from 'xhr2' 4 | 5 | const XHR2 = typeof XMLHttpRequest !== 'undefined' ? XMLHttpRequest : xhr2 6 | 7 | export function request(config: AjaxConfig): Observable> { 8 | return ajax({createXHR: () => new XHR2(), ...config}) 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "module": "ESNext", 5 | "strict": false, 6 | "esModuleInterop": true, 7 | "forceConsistentCasingInFileNames": true, 8 | "moduleResolution": "node" 9 | } 10 | } 11 | --------------------------------------------------------------------------------