Remove usages of url-parse per feedback

This commit is contained in:
Tim Donohue
2022-10-27 16:19:51 -05:00
parent 35171dbd48
commit 6ceefb4977
2 changed files with 27 additions and 23 deletions

View File

@@ -3,8 +3,6 @@ import { hasValue, isNotEmpty } from '../../shared/empty.util';
import { coreSelector } from '../core.selectors'; import { coreSelector } from '../core.selectors';
import { URLCombiner } from '../url-combiner/url-combiner'; import { URLCombiner } from '../url-combiner/url-combiner';
import { IndexState, MetaIndexState } from './index.reducer'; import { IndexState, MetaIndexState } from './index.reducer';
// eslint-disable-next-line import/no-namespace
import * as parse from 'url-parse';
import { IndexName } from './index-name.model'; import { IndexName } from './index-name.model';
import { CoreState } from '../core-state.model'; import { CoreState } from '../core-state.model';
@@ -22,17 +20,21 @@ import { CoreState } from '../core-state.model';
*/ */
export const getUrlWithoutEmbedParams = (url: string): string => { export const getUrlWithoutEmbedParams = (url: string): string => {
if (isNotEmpty(url)) { if (isNotEmpty(url)) {
const parsed = parse(url); try {
if (isNotEmpty(parsed.query)) { const parsed = new URL(url);
const parts = parsed.query.split(/[?|&]/) if (isNotEmpty(parsed.search)) {
.filter((part: string) => isNotEmpty(part)) const parts = parsed.search.split(/[?|&]/)
.filter((part: string) => !(part.startsWith('embed=') || part.startsWith('embed.size='))); .filter((part: string) => isNotEmpty(part))
let args = ''; .filter((part: string) => !(part.startsWith('embed=') || part.startsWith('embed.size=')));
if (isNotEmpty(parts)) { let args = '';
args = `?${parts.join('&')}`; if (isNotEmpty(parts)) {
args = `?${parts.join('&')}`;
}
url = new URLCombiner(parsed.origin, parsed.pathname, args).toString();
return url;
} }
url = new URLCombiner(parsed.origin, parsed.pathname, args).toString(); } catch (e) {
return url; // Ignore parsing errors. By default, we return the original string below.
} }
} }
@@ -45,15 +47,19 @@ export const getUrlWithoutEmbedParams = (url: string): string => {
*/ */
export const getEmbedSizeParams = (url: string): { name: string, size: number }[] => { export const getEmbedSizeParams = (url: string): { name: string, size: number }[] => {
if (isNotEmpty(url)) { if (isNotEmpty(url)) {
const parsed = parse(url); try {
if (isNotEmpty(parsed.query)) { const parsed = new URL(url);
return parsed.query.split(/[?|&]/) if (isNotEmpty(parsed.search)) {
.filter((part: string) => isNotEmpty(part)) return parsed.search.split(/[?|&]/)
.map((part: string) => part.match(/^embed.size=([^=]+)=(\d+)$/)) .filter((part: string) => isNotEmpty(part))
.filter((matches: RegExpMatchArray) => hasValue(matches) && hasValue(matches[1]) && hasValue(matches[2])) .map((part: string) => part.match(/^embed.size=([^=]+)=(\d+)$/))
.map((matches: RegExpMatchArray) => { .filter((matches: RegExpMatchArray) => hasValue(matches) && hasValue(matches[1]) && hasValue(matches[2]))
return { name: matches[1], size: Number(matches[2]) }; .map((matches: RegExpMatchArray) => {
}); return { name: matches[1], size: Number(matches[2]) };
});
}
} catch (e) {
// Ignore parsing errors. By default, we return an empty result below.
} }
} }

View File

@@ -7,8 +7,6 @@ import { RestRequestMethod } from '../../../core/data/rest-request-method';
import { RawRestResponse } from '../../../core/dspace-rest/raw-rest-response.model'; import { RawRestResponse } from '../../../core/dspace-rest/raw-rest-response.model';
import { DspaceRestService, HttpOptions } from '../../../core/dspace-rest/dspace-rest.service'; import { DspaceRestService, HttpOptions } from '../../../core/dspace-rest/dspace-rest.service';
import { MOCK_RESPONSE_MAP, ResponseMapMock } from './mocks/response-map.mock'; import { MOCK_RESPONSE_MAP, ResponseMapMock } from './mocks/response-map.mock';
// eslint-disable-next-line import/no-namespace
import * as URL from 'url-parse';
import { environment } from '../../../../environments/environment'; import { environment } from '../../../../environments/environment';
/** /**