disregard embed url params when indexing and checking indexed request urls

This commit is contained in:
Art Lowel
2020-03-11 14:53:55 +01:00
parent 8fd46d9c88
commit a970aeaab8
6 changed files with 92 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
import { NgZone } from '@angular/core';
import * as ngrx from '@ngrx/store';
import { ActionsSubject, Store } from '@ngrx/store';
import { cold, getTestScheduler, hot } from 'jasmine-marbles';
@@ -62,6 +63,7 @@ describe('RequestService', () => {
objectCache,
uuidService,
store,
new NgZone({}),
undefined
);
serviceAsAny = service as any;

View File

@@ -1,9 +1,9 @@
import { Injectable } from '@angular/core';
import { Injectable, NgZone } from '@angular/core';
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';
@@ -80,6 +80,7 @@ export class RequestService {
constructor(private objectCache: ObjectCacheService,
private uuidService: UUIDService,
private store: Store<CoreState>,
private zone: NgZone,
private indexStore: Store<MetaIndexState>) {
}
@@ -147,6 +148,7 @@ export class RequestService {
* @param {RestRequest} request The request to send out
*/
configure<T extends CacheableObject>(request: RestRequest): void {
this.zone.runOutsideAngular(() => {
const isGetRequest = request.method === RestRequestMethod.GET;
if (!isGetRequest || request.forceBypassCache || !this.isCachedOrPending(request)) {
this.dispatchRequest(request);
@@ -162,6 +164,7 @@ export class RequestService {
}
)
}
});
}
/**

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