Merge pull request #616 from atmire/disregard-embeds-when-checking-cache

Disregard embeds when checking request index
This commit is contained in:
Tim Donohue
2020-03-11 15:04:32 -05:00
committed by GitHub
5 changed files with 72 additions and 6 deletions

View File

@@ -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';

View File

@@ -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
);
})

View File

@@ -0,0 +1,32 @@
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);
});
});
});

View File

@@ -1,8 +1,41 @@
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
);
/**

View File

@@ -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&');