Skip to content
Snippets Groups Projects
Commit 404b1053 authored by Kristof De Langhe's avatar Kristof De Langhe
Browse files

Merge branch 'master' into w2p-68729_List-version-history

parents ba25bd61 a9cd9f03
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ import { HttpHeaders } from '@angular/common/http';
import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
import { Observable, race as observableRace } from 'rxjs';
import { filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
import { filter, map, mergeMap, take } from 'rxjs/operators';
import { cloneDeep, remove } from 'lodash';
import { hasValue, isEmpty, isNotEmpty } from '../../shared/empty.util';
import { CacheableObject } from '../cache/object-cache.reducer';
......
......@@ -12,6 +12,7 @@ import { AddToIndexAction, RemoveFromIndexByValueAction } from './index.actions'
import { hasValue } from '../../shared/empty.util';
import { IndexName } from './index.reducer';
import { RestRequestMethod } from '../data/rest-request-method';
import { getUrlWithoutEmbedParams } from './index.selectors';
@Injectable()
export class UUIDIndexEffects {
......@@ -47,7 +48,7 @@ export class UUIDIndexEffects {
map((action: RequestConfigureAction) => {
return new AddToIndexAction(
IndexName.REQUEST,
action.payload.href,
getUrlWithoutEmbedParams(action.payload.href),
action.payload.uuid
);
})
......
import { getUrlWithoutEmbedParams } from './index.selectors';
describe(`index selectors`, () => {
describe(`getUrlWithoutEmbedParams`, () => {
it(`should return a url without its embed params`, () => {
const source = 'https://rest.api/resource?a=1&embed=2&b=3&embed=4/5&c=6&embed=7/8/9';
const result = getUrlWithoutEmbedParams(source);
expect(result).toBe('https://rest.api/resource?a=1&b=3&c=6');
});
it(`should return a url without embed params unmodified`, () => {
const source = 'https://rest.api/resource?a=1&b=3&c=6';
const result = getUrlWithoutEmbedParams(source);
expect(result).toBe(source);
});
it(`should return a string that isn't a url unmodified`, () => {
const source = 'a=1&embed=2&b=3&embed=4/5&c=6&embed=7/8/9';
const result = getUrlWithoutEmbedParams(source);
expect(result).toBe(source);
});
it(`should return undefined or null unmodified`, () => {
expect(getUrlWithoutEmbedParams(undefined)).toBe(undefined);
expect(getUrlWithoutEmbedParams(null)).toBe(null);
});
});
});
import { createSelector, MemoizedSelector } from '@ngrx/store';
import { hasValue } from '../../shared/empty.util';
import { hasValue, isNotEmpty } from '../../shared/empty.util';
import { CoreState } from '../core.reducers';
import { coreSelector } from '../core.selectors';
import { URLCombiner } from '../url-combiner/url-combiner';
import { IndexName, IndexState, MetaIndexState } from './index.reducer';
import * as parse from 'url-parse';
/**
* Return the given url without `embed` params.
*
* E.g. https://rest.api/resource?size=5&embed=subresource&rpp=3
* becomes https://rest.api/resource?size=5&rpp=3
*
* When you index a request url you don't want to include
* embed params because embedded data isn't relevant when
* you want to know
*
* @param url The url to use
*/
export const getUrlWithoutEmbedParams = (url: string): string => {
if (isNotEmpty(url)) {
const parsed = parse(url);
if (isNotEmpty(parsed.query)) {
const parts = parsed.query.split(/[?|&]/)
.filter((part: string) => isNotEmpty(part))
.filter((part: string) => !part.startsWith('embed='));
let args = '';
if (isNotEmpty(parts)) {
args = `?${parts.join('&')}`;
}
url = new URLCombiner(parsed.origin, parsed.pathname, args).toString();
return url;
}
}
return url;
};
/**
* Return the MetaIndexState based on the CoreSate
......@@ -74,7 +107,7 @@ export const selfLinkFromUuidSelector =
export const uuidFromHrefSelector =
(href: string): MemoizedSelector<CoreState, string> => createSelector(
requestIndexSelector,
(state: IndexState) => hasValue(state) ? state[href] : undefined
(state: IndexState) => hasValue(state) ? state[getUrlWithoutEmbedParams(href)] : undefined
);
/**
......
......@@ -41,8 +41,8 @@ export class URLCombiner {
// remove consecutive slashes
url = url.replace(/([^:\s])\/+/g, '$1/');
// remove trailing slash before parameters or hash
url = url.replace(/\/(\?|&|#[^!])/g, '$1');
// remove trailing slash
url = url.replace(/\/($|\?|&|#[^!])/g, '$1');
// replace ? in parameters with &
url = url.replace(/(\?.+)\?/g, '$1&');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment