11 |
Ng2 Http Interceptor Demo
12 |
13 |
14 |
15 |
16 |
Requests log:
17 |
18 |
19 | #{{ i + 1 }}: {{ request.method | uppercase }} request to {{ request.url }}
20 |
21 |
22 |
Response:
23 |
Error: {{ error }}
24 |
{{ res | json }}
25 |
26 | `,
27 | })
28 | export class AppComponent implements OnInit {
29 | requests = [];
30 | res = null;
31 | error = null;
32 |
33 | constructor(
34 | private http: Http,
35 | private httpInterceptor: HttpInterceptorService
36 | ) {
37 | this.httpInterceptor.request().addInterceptor((data, method) => {
38 | this.error = null;
39 | this.requests.push({
40 | method: method,
41 | url: data[0]
42 | });
43 |
44 | return data;
45 | });
46 |
47 | this.httpInterceptor.response().addInterceptor(
48 | res => res.do(null, e => this.error = e));
49 | }
50 |
51 | ngOnInit() {
52 | this.makeRequest();
53 | }
54 |
55 | makeRequest(url = '/') {
56 | this.http.get(url).subscribe(r => this.res = r.text());
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/e2e/demo/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { HttpModule } from '@angular/http';
2 | import { BrowserModule } from '@angular/platform-browser';
3 | import { NgModule } from '@angular/core';
4 | import { HttpInterceptorModule } from 'ng2-http-interceptor';
5 | import { AppComponent } from './app.component';
6 |
7 | @NgModule({
8 | imports: [
9 | BrowserModule,
10 | HttpModule,
11 | HttpInterceptorModule
12 | ],
13 | declarations: [AppComponent],
14 | bootstrap: [AppComponent]
15 | })
16 | export class AppModule { }
17 |
--------------------------------------------------------------------------------
/e2e/demo/src/main.ts:
--------------------------------------------------------------------------------
1 | import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
2 | import {AppModule} from './app.module';
3 |
4 | platformBrowserDynamic().bootstrapModule(AppModule);
5 |
--------------------------------------------------------------------------------
/karma.conf.js:
--------------------------------------------------------------------------------
1 | // Karma configuration file, see link for more information
2 | // https://karma-runner.github.io/0.13/config/configuration-file.html
3 |
4 | module.exports = function (config) {
5 | var harmony_flags = '--js-flags="' + [
6 | '--harmony-arrow-functions',
7 | '--harmony-classes',
8 | '--harmony-computed-property-names',
9 | '--harmony-spreadcalls',
10 | ].join(' ') + '"';
11 |
12 | var configuration = {
13 | basePath: '',
14 | frameworks: ['jasmine'],
15 | plugins: [
16 | require('karma-jasmine'),
17 | require('karma-chrome-launcher'),
18 | require('karma-remap-istanbul'),
19 | require('karma-sourcemap-loader'),
20 | require('karma-webpack')
21 | ],
22 | webpack: require('./webpack.config.js'),
23 | webpackMiddleware: {
24 | noInfo: true, // Hide webpack output because its noisy.
25 | stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
26 | assets: false,
27 | colors: true,
28 | version: false,
29 | hash: false,
30 | timings: false,
31 | chunks: false,
32 | chunkModules: false
33 | }
34 | },
35 | files: [
36 | { pattern: './src/test.ts', watched: false }
37 | ],
38 | preprocessors: {
39 | './src/test.ts': ['webpack', 'sourcemap']
40 | },
41 | mime: {
42 | 'text/x-typescript': ['ts', 'tsx']
43 | },
44 | remapIstanbulReporter: {
45 | reports: {
46 | html: 'coverage',
47 | lcovonly: './coverage/coverage.lcov',
48 | json: './coverage/coverage.json'
49 | },
50 | remapOptions: {
51 | exclude: /(test|polyfills|rxjs).ts$/
52 | }
53 | },
54 | reporters: ['progress', 'karma-remap-istanbul'],
55 | port: 9876,
56 | colors: true,
57 | logLevel: config.LOG_INFO,
58 | autoWatch: true,
59 | browsers: ['DebuggableChrome'],
60 | singleRun: false,
61 |
62 | // browser for travis-ci
63 | customLaunchers: {
64 | DebuggableChrome: {
65 | base: 'Chrome',
66 | flags: ['--remote-debugging-port=9222']
67 | },
68 | Chrome_travis_ci: {
69 | base: 'Chrome',
70 | flags: ['--no-sandbox', harmony_flags]
71 | }
72 | }
73 | };
74 |
75 | if (process.env.TRAVIS) {
76 | configuration.browsers = ['Chrome_travis_ci'];
77 | }
78 |
79 | config.set(configuration);
80 | };
81 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ng-http-interceptor",
3 | "version": "0.0.0-semantically-relesed",
4 | "scripts": {
5 | "build": "node scripts/build.js",
6 | "check-coverage": "istanbul check-coverage --functions 75 --lines 75 --branches 75 --statements 75",
7 | "ci": "npm run lint && npm run test:single && npm run check-coverage",
8 | "commit": "git-cz",
9 | "lint": "tslint src/**/*.ts",
10 | "prepublish": "npm run build",
11 | "test": "karma start",
12 | "test:single": "npm run test -- --single-run",
13 | "test:report": "cd coverage && codecov",
14 | "presemantic-release": "node scripts/release.js",
15 | "semantic-release": "cd dist && semantic-release pre && npm publish && semantic-release post",
16 | "e2e": "npm-run-all -p -r e2e:server e2e:protractor",
17 | "e2e:server": "node e2e/demo/server.js",
18 | "e2e:protractor": "protractor",
19 | "e2e:update": "webdriver-manager update"
20 | },
21 | "repository": {
22 | "type": "git",
23 | "url": "https://github.com/gund/ng-http-interceptor"
24 | },
25 | "author": {
26 | "name": "Alex Malkevich",
27 | "email": "malkevich.alex@gmail.com"
28 | },
29 | "keywords": [
30 | "ng",
31 | "ng2",
32 | "angular",
33 | "angular2",
34 | "http",
35 | "interceptor",
36 | "library"
37 | ],
38 | "license": "MIT",
39 | "bugs": {
40 | "url": "https://github.com/gund/ng-http-interceptor/issues"
41 | },
42 | "main": "./dist/bundles/ng-http-interceptor.umd.js",
43 | "module": "./dist/bundles/ng-http-interceptor.es5.js",
44 | "es2015": "./dist/bundles/ng-http-interceptor.es2015.js",
45 | "typings": "./dist/ng-http-interceptor.d.ts",
46 | "peerDependencies": {
47 | "@angular/core": "^5.0.0",
48 | "@angular/http": "^5.0.0",
49 | "rxjs": "^5.0.0",
50 | "tslib": "^1.0.0"
51 | },
52 | "devDependencies": {
53 | "@angular/common": "^5.0.0",
54 | "@angular/compiler": "^5.0.0",
55 | "@angular/compiler-cli": "^5.0.0",
56 | "@angular/core": "^5.0.0",
57 | "@angular/forms": "^5.0.0",
58 | "@angular/http": "^5.0.0",
59 | "@angular/platform-browser": "^5.0.0",
60 | "@angular/platform-browser-dynamic": "^5.0.0",
61 | "@angular/platform-server": "^5.0.0",
62 | "@angular/router": "^5.0.0",
63 | "@types/jasmine": "^2.5.45",
64 | "angular2-template-loader": "^0.6.2",
65 | "awesome-typescript-loader": "^3.1.2",
66 | "codecov": "^1.0.1",
67 | "codelyzer": "^3.0.0-beta.4",
68 | "commitizen": "^2.9.6",
69 | "copy-dir": "^0.3.0",
70 | "copyfiles": "^1.2.0",
71 | "core-js": "^2.4.1",
72 | "cz-conventional-changelog": "^2.0.0",
73 | "express": "^4.15.2",
74 | "ghooks": "^2.0.0",
75 | "istanbul": "^0.4.5",
76 | "jasmine-core": "2.5.2",
77 | "jasmine-spec-reporter": "^3.2.0",
78 | "json-loader": "^0.5.4",
79 | "karma": "^1.5.0",
80 | "karma-chrome-launcher": "^2.0.0",
81 | "karma-cli": "^1.0.1",
82 | "karma-jasmine": "^1.1.0",
83 | "karma-remap-istanbul": "^0.6.0",
84 | "karma-sourcemap-loader": "^0.3.7",
85 | "karma-webpack": "^2.0.2",
86 | "npm-run-all": "^4.0.2",
87 | "postcss-loader": "^1.3.3",
88 | "protractor": "^5.2.0",
89 | "put-cli": "^0.1.0",
90 | "raw-loader": "^0.5.1",
91 | "rimraf": "^2.6.1",
92 | "rollup": "^0.41.6",
93 | "rollup-globals-regex": "^0.0.1",
94 | "rollup-plugin-node-resolve": "^2.0.0",
95 | "rxjs": "^5.3.0",
96 | "semantic-release": "^6.3.2",
97 | "source-map": "^0.5.6",
98 | "source-map-loader": "^0.2.0",
99 | "sourcemap-istanbul-instrumenter-loader": "^0.2.0",
100 | "ts-node": "^3.3.0",
101 | "tslib": "^1.6.0",
102 | "tslint": "^4.5.1",
103 | "tslint-loader": "^3.4.3",
104 | "typescript": "~2.4.0",
105 | "typings": "^2.1.0",
106 | "uglifyjs": "^2.4.10",
107 | "url-loader": "^0.5.8",
108 | "webpack": "^2.6.1",
109 | "webpack-dev-server": "^2.4.5",
110 | "webpack-md5-hash": "^0.0.5",
111 | "webpack-merge": "^4.1.0",
112 | "webpack-sources": "^0.2.3",
113 | "zone.js": "^0.8.11"
114 | },
115 | "engines": {
116 | "node": ">=6.0.0"
117 | },
118 | "config": {
119 | "ghooks": {
120 | "pre-commit": "echo \"Verifying commit...\" && npm run ci"
121 | },
122 | "commitizen": {
123 | "path": "./node_modules/cz-conventional-changelog"
124 | }
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/protractor.conf.js:
--------------------------------------------------------------------------------
1 | require('ts-node').register({
2 | compilerOptions: {
3 | module: 'commonjs'
4 | }
5 | });
6 |
7 | var path = require('path');
8 |
9 | exports.config = {
10 | baseUrl: 'http://localhost:8080/',
11 |
12 | specs: [
13 | path.resolve('e2e', '*.e2e.ts')
14 | ],
15 | exclude: [],
16 |
17 | framework: 'jasmine2',
18 |
19 | allScriptsTimeout: 110000,
20 |
21 | jasmineNodeOpts: {
22 | showTiming: true,
23 | showColors: true,
24 | isVerbose: false,
25 | includeStackTrace: false,
26 | defaultTimeoutInterval: 400000
27 | },
28 | directConnect: true,
29 |
30 | capabilities: {
31 | 'browserName': 'chrome',
32 | 'chromeOptions': {
33 | 'args': ['show-fps-counter=true']
34 | }
35 | },
36 |
37 | onPrepare: function () {
38 | browser.ignoreSynchronization = true;
39 | },
40 |
41 | useAllAngular2AppRoots: true
42 | };
43 |
--------------------------------------------------------------------------------
/rollup.config.js:
--------------------------------------------------------------------------------
1 | import nodeResolve from 'rollup-plugin-node-resolve';
2 | import { globalsRegex, GLOBAL } from 'rollup-globals-regex';
3 |
4 | export default {
5 | entry: 'dist/ng-http-interceptor.js',
6 | dest: 'dist/bundles/ng-http-interceptor.es2015.js',
7 | format: 'es',
8 | moduleName: 'httpInterceptor',
9 | plugins: [
10 | nodeResolve({ jsnext: true, browser: true })
11 | ],
12 | globals: globalsRegex({
13 | 'tslib': 'tslib',
14 | [GLOBAL.NG2]: GLOBAL.NG2.TPL,
15 | [GLOBAL.RX]: GLOBAL.RX.TPL,
16 | [GLOBAL.RX_OBSERVABLE]: GLOBAL.RX_OBSERVABLE.TPL,
17 | [GLOBAL.RX_OPERATOR]: GLOBAL.RX_OPERATOR.TPL,
18 | }),
19 | external: (moduleId) => {
20 | if (/^(\@angular|rxjs|tslib)\/?/.test(moduleId)) {
21 | return true;
22 | }
23 |
24 | return false;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/rollup.config.umd.js:
--------------------------------------------------------------------------------
1 | import nodeResolve from 'rollup-plugin-node-resolve';
2 | import { globalsRegex, GLOBAL } from 'rollup-globals-regex';
3 |
4 | export default {
5 | entry: 'dist/bundles/ng-http-interceptor.es5.js',
6 | dest: 'dist/bundles/ng-http-interceptor.umd.js',
7 | format: 'umd',
8 | moduleName: 'httpInterceptor',
9 | plugins: [
10 | nodeResolve({ jsnext: true, browser: true })
11 | ],
12 | globals: globalsRegex({
13 | 'tslib': 'tslib',
14 | [GLOBAL.NG2]: GLOBAL.NG2.TPL,
15 | [GLOBAL.RX]: GLOBAL.RX.TPL,
16 | [GLOBAL.RX_OBSERVABLE]: GLOBAL.RX_OBSERVABLE.TPL,
17 | [GLOBAL.RX_OPERATOR]: GLOBAL.RX_OPERATOR.TPL,
18 | }),
19 | external: (moduleId) => {
20 | if (/^(\@angular|rxjs)\/?/.test(moduleId)) {
21 | return true;
22 | }
23 |
24 | return false;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/scripts/build.js:
--------------------------------------------------------------------------------
1 | const fs = require('fs')
2 | const exec = require('child_process').exec
3 |
4 | const NGC = `"node_modules/.bin/ngc"`
5 | const TSC = `"node_modules/.bin/tsc"`
6 | const ROLLUP = `"node_modules/.bin/rollup"`
7 | const RIMRAF = `"node_modules/.bin/rimraf"`
8 | const UGLIFYJS = `"node_modules/.bin/uglifyjs"`
9 |
10 | const cleanup = `${RIMRAF} dist`
11 | const buildMain = `${NGC} -p tsconfig.es2015.json`
12 | const buildFesmEs2015 = `${ROLLUP} -c rollup.config.js`
13 | const buildFesmEs5 = `${TSC} -p tsconfig.es5.json`
14 | const buildUmd = `${ROLLUP} -c rollup.config.umd.js`
15 | const buildUmdMin = `${UGLIFYJS} -c --screw-ie8 --comments -o dist/bundles/ng-http-interceptor.umd.min.js dist/bundles/ng-http-interceptor.umd.js`
16 | const removeTmpFesmEs5 = `${RIMRAF} dist/bundles/es5`
17 |
18 | execP(cleanup)
19 | .then(() => console.log('Compiling project...'))
20 | .then(() => execP(buildMain))
21 | .then(() => console.log('OK.\n\nBuilding FESM ES2015...'))
22 | .then(() => execP(buildFesmEs2015))
23 | .then(() => console.log('OK.\n\nBuilding FESM ES5...'))
24 | .then(() => execP(buildFesmEs5))
25 | .then(() => moveFesmEs5())
26 | .then(() => execP(removeTmpFesmEs5))
27 | .then(() => console.log('OK.\n\nBuilding UMD...'))
28 | .then(() => execP(buildUmd))
29 | .then(() => console.log('OK.\n\nMinifiyng UMD...'))
30 | .then(() => execP(buildUmdMin))
31 | .then(() => console.log('OK.\n\n'))
32 | .catch(e => console.error(e))
33 |
34 | function moveFesmEs5() {
35 | fs.renameSync('dist/bundles/es5/ng-http-interceptor.es2015.js', 'dist/bundles/ng-http-interceptor.es5.js')
36 | }
37 |
38 | function execP(string) {
39 | return new Promise((res, rej) => {
40 | exec(string, (err, stdout) => err ? rej(err) : res(stdout))
41 | })
42 | }
43 |
--------------------------------------------------------------------------------
/scripts/release.js:
--------------------------------------------------------------------------------
1 | const { writeFileSync } = require('fs')
2 | const copyfiles = require('copyfiles')
3 | const copyDir = require('copy-dir')
4 |
5 | writeNewPackage('dist/package.json')
6 | copyFiles({
7 | files: [
8 | 'README.MD',
9 | 'LICENSE',
10 | '.npmignore',
11 | 'yarn.lock',
12 | ],
13 | to: 'dist',
14 | })
15 | copyGit('dist')
16 |
17 | function writeNewPackage(to) {
18 | const package = require('../package.json')
19 |
20 | const pathKeys = ['main', 'typings', 'module', 'es2015']
21 | pathKeys.forEach(k => package[k] = updatePath(package[k]))
22 |
23 | delete package.scripts
24 | delete package.devDependencies
25 | delete package.config
26 |
27 | console.log(`Writing new package.json to ${to}...`)
28 | writeFileSync(
29 | to,
30 | JSON.stringify(package, null, ' '),
31 | 'utf-8')
32 | console.log('OK')
33 | }
34 |
35 | function copyFiles({ files, to }) {
36 | console.log(`Copying files to ${to} [${files.join(', ')}]`)
37 | copyfiles([...files, to], {}, () => null)
38 | console.log('OK')
39 | }
40 |
41 | function copyGit(to) {
42 | console.log(`Copying .git folder to ${to}`)
43 | copyDir.sync('.git', to + '/.git')
44 | console.log('OK')
45 | }
46 |
47 | function updatePath(path) {
48 | return path.replace('dist/', '')
49 | }
50 |
--------------------------------------------------------------------------------
/src/http/helpers/getHttpHeadersOrInit.spec.ts:
--------------------------------------------------------------------------------
1 | import { Headers } from '@angular/http';
2 | import * as module from './getHttpOptionsAndIdx';
3 | import { getHttpHeadersOrInit } from './getHttpHeadersOrInit';
4 |
5 | describe('getHttpHeadersOrInit() function', () => {
6 | let getHttpOptionsAndIdxSpy: jasmine.Spy;
7 |
8 | beforeEach(() => getHttpOptionsAndIdxSpy = spyOn(module, 'getHttpOptionsAndIdx'));
9 |
10 | it('should call getHttpOptionsAndIdx() with `data` and `method` args', () => {
11 | getHttpHeadersOrInitTry([1], 'method');
12 | expect(getHttpOptionsAndIdxSpy).toHaveBeenCalledWith([1], 'method');
13 | });
14 |
15 | it('should return `Headers` from `options` and set them back', () => {
16 | getHttpOptionsAndIdxSpy.and.returnValue({
17 | options: { headers: 'headers' },
18 | idx: 1
19 | });
20 |
21 | const data = [0, 1];
22 | const res = getHttpHeadersOrInit(data, 'method');
23 |
24 | expect(res).toBe('headers' as any);
25 | expect(data).toEqual([0, { headers: 'headers' }] as any);
26 | });
27 |
28 | it('should create `Headers` if not found in `options`, return it and set back', () => {
29 | getHttpOptionsAndIdxSpy.and.returnValue({
30 | options: {},
31 | idx: 1
32 | });
33 |
34 | const data = [0, 1];
35 | const res = getHttpHeadersOrInit(data, 'method');
36 |
37 | expect(res).toEqual(jasmine.any(Headers));
38 | expect(data).toEqual([0, { headers: jasmine.any(Headers) }] as any);
39 | });
40 |
41 | });
42 |
43 | function getHttpHeadersOrInitTry(data: any[], method: string) {
44 | try {
45 | return getHttpHeadersOrInit(data, method);
46 | } catch (_) { }
47 | }
48 |
--------------------------------------------------------------------------------
/src/http/helpers/getHttpHeadersOrInit.ts:
--------------------------------------------------------------------------------
1 | import { Headers } from '@angular/http';
2 | import { getHttpOptionsAndIdx } from './getHttpOptionsAndIdx';
3 |
4 | /**
5 | * @description
6 | * Gets {@link Headers} from data array.
7 | * If no {@link RequestOptions} found - creates it and updates original data array.
8 | * If no {@link Headers} found - creates it and sets to {@link RequestOptions}.
9 | * @param data - Array of http data
10 | * @param method - Http method
11 | */
12 | export function getHttpHeadersOrInit(data: any[], method: string): Headers {
13 | const { options, idx } = getHttpOptionsAndIdx(data, method);
14 | let headers = options.headers;
15 |
16 | // Create and update Headers
17 | if (!options.headers) {
18 | headers = new Headers();
19 | options.headers = headers;
20 | }
21 |
22 | // Set Options back
23 | data[idx] = options;
24 |
25 | return headers;
26 | }
27 |
--------------------------------------------------------------------------------
/src/http/helpers/getHttpOptions.spec.ts:
--------------------------------------------------------------------------------
1 | import { RequestOptions } from '@angular/http';
2 | import * as module from './getHttpOptionsIdx';
3 | import { getHttpOptions } from './getHttpOptions';
4 |
5 | describe('getHttpOptions() function', () => {
6 | let getHttpOptionsIdxSpy: jasmine.Spy;
7 |
8 | beforeEach(() => getHttpOptionsIdxSpy = spyOn(module, 'getHttpOptionsIdx'));
9 |
10 | it('should call getHttpOptionsIdx() with `method` arg', () => {
11 | getHttpOptions([], 'method');
12 | expect(getHttpOptionsIdxSpy).toHaveBeenCalledWith('method');
13 | });
14 |
15 | it('should return value from `data` by index returned from getHttpOptionsIdx()', () => {
16 | getHttpOptionsIdxSpy.and.returnValue(2);
17 | expect(getHttpOptions([1, 2, 3], 'method')).toBe(3 as any);
18 | });
19 |
20 | it('should return `new RequestOptions` if no value at the index found by default', () => {
21 | getHttpOptionsIdxSpy.and.returnValue(1);
22 | expect(getHttpOptions([1], 'method')).toEqual(jasmine.any(RequestOptions));
23 | });
24 |
25 | it('should always return original value if `alwaysOriginal` set to `true`', () => {
26 | getHttpOptionsIdxSpy.and.returnValue(1);
27 | expect(getHttpOptions([1], 'method', true)).toBeUndefined();
28 | });
29 |
30 | });
31 |
--------------------------------------------------------------------------------
/src/http/helpers/getHttpOptions.ts:
--------------------------------------------------------------------------------
1 | import { RequestOptions } from '@angular/http';
2 | import { getHttpOptionsIdx } from './getHttpOptionsIdx';
3 |
4 | /**
5 | * @description
6 | * Gets http {@link RequestOptions} from data array.
7 | * If no options found and `alwaysOriginal = false` - returns new {@link RequestOptions}.
8 | * @param data - Array of http data
9 | * @param method - Http method
10 | * @param alwaysOriginal - `false` by default
11 | */
12 | export function getHttpOptions(data: any[], method: string, alwaysOriginal = false): RequestOptions {
13 | return alwaysOriginal ?
14 | data[getHttpOptionsIdx(method)] :
15 | data[getHttpOptionsIdx(method)] || new RequestOptions();
16 | }
17 |
--------------------------------------------------------------------------------
/src/http/helpers/getHttpOptionsAndIdx.spec.ts:
--------------------------------------------------------------------------------
1 | import * as module1 from './getHttpOptions';
2 | import * as module2 from './getHttpOptionsIdx';
3 | import { getHttpOptionsAndIdx } from './getHttpOptionsAndIdx';
4 |
5 | describe('getHttpOptionsAndIdx() function', () => {
6 | let getHttpOptionsSpy: jasmine.Spy
7 | , getHttpOptionsIdxSpy: jasmine.Spy;
8 |
9 | beforeEach(() => {
10 | getHttpOptionsSpy = spyOn(module1, 'getHttpOptions');
11 | getHttpOptionsIdxSpy = spyOn(module2, 'getHttpOptionsIdx');
12 | });
13 |
14 | it('should call getHttpOptions() with `data`, `method` and `alwaysOriginal`=true args by default', () => {
15 | getHttpOptionsAndIdx([1], 'method');
16 | expect(getHttpOptionsSpy).toHaveBeenCalledWith([1], 'method', false);
17 | });
18 |
19 | it('should call getHttpOptions() with `data`, `method` and `alwaysOriginal` args', () => {
20 | getHttpOptionsAndIdx([1], 'method', true);
21 | expect(getHttpOptionsSpy).toHaveBeenCalledWith([1], 'method', true);
22 | });
23 |
24 | it('should call getHttpOptionsIdx() with `method` arg', () => {
25 | getHttpOptionsAndIdx([], 'method');
26 | expect(getHttpOptionsIdxSpy).toHaveBeenCalledWith('method');
27 | });
28 |
29 | it('should return object with values from getHttpOptions() and getHttpOptionsIdx() functions', () => {
30 | getHttpOptionsSpy.and.returnValue('options');
31 | getHttpOptionsIdxSpy.and.returnValue(5);
32 |
33 | expect(getHttpOptionsAndIdx([], 'method')).toEqual({
34 | options: 'options',
35 | idx: 5
36 | } as any);
37 | });
38 |
39 | });
40 |
--------------------------------------------------------------------------------
/src/http/helpers/getHttpOptionsAndIdx.ts:
--------------------------------------------------------------------------------
1 | import { RequestOptions } from '@angular/http';
2 | import { getHttpOptionsIdx } from './getHttpOptionsIdx';
3 | import { getHttpOptions } from './getHttpOptions';
4 |
5 | /**
6 | * @description
7 | * Gets {@link RequestOptions} and it's index location in data array.
8 | * If no options found and `alwaysOriginal = false` - creates new {@link RequestOptions}.
9 | * @param data - Array of http data
10 | * @param method - Http method
11 | * @param alwaysOriginal - `false` by default
12 | */
13 | export function getHttpOptionsAndIdx(data: any[], method: string, alwaysOriginal = false) {
14 | return {
15 | options: