Refactored code to always encode the RequestParams

This commit is contained in:
Alexandre Vryghem
2024-03-24 13:32:25 +01:00
parent 404ccd9b0e
commit d5cf236938
9 changed files with 34 additions and 66 deletions

View File

@@ -1,9 +1,14 @@
/** /**
* Class representing a query parameter (query?fieldName=fieldValue) used in FindListOptions object * Class representing a query parameter (query?fieldName=fieldValue) used in FindListOptions object
*/ */
export class RequestParam { export class RequestParam {
constructor(public fieldName: string, public fieldValue: any) { constructor(
public fieldName: string,
public fieldValue: any,
public encodeValue = true,
) {
if (encodeValue) {
this.fieldValue = encodeURIComponent(fieldValue);
}
} }
} }

View File

@@ -492,40 +492,18 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
* @param arrayOfItemIds The uuid of the items to be found on the other side of returned relationships * @param arrayOfItemIds The uuid of the items to be found on the other side of returned relationships
*/ */
searchByItemsAndType(typeId: string,itemUuid: string,relationshipLabel: string, arrayOfItemIds: string[] ): Observable<RemoteData<PaginatedList<Relationship>>> { searchByItemsAndType(typeId: string,itemUuid: string,relationshipLabel: string, arrayOfItemIds: string[] ): Observable<RemoteData<PaginatedList<Relationship>>> {
const searchParams: RequestParam[] = [
const searchParams = [ new RequestParam('typeId', typeId),
{ new RequestParam('focusItem', itemUuid),
fieldName: 'typeId', new RequestParam('relationshipLabel', relationshipLabel),
fieldValue: typeId new RequestParam('size', arrayOfItemIds.length),
}, new RequestParam('embed', 'leftItem'),
{ new RequestParam('embed', 'rightItem'),
fieldName: 'focusItem', ];
fieldValue: itemUuid
},
{
fieldName: 'relationshipLabel',
fieldValue: relationshipLabel
},
{
fieldName: 'size',
fieldValue: arrayOfItemIds.length
},
{
fieldName: 'embed',
fieldValue: 'leftItem'
},
{
fieldName: 'embed',
fieldValue: 'rightItem'
},
];
arrayOfItemIds.forEach( (itemId) => { arrayOfItemIds.forEach( (itemId) => {
searchParams.push( searchParams.push(
{ new RequestParam('relatedItem', itemId),
fieldName: 'relatedItem',
fieldValue: itemId,
}
); );
}); });

View File

@@ -17,6 +17,7 @@ import { FindAllDataImpl } from './base/find-all-data';
import { SearchDataImpl } from './base/search-data'; import { SearchDataImpl } from './base/search-data';
import { ObjectCacheService } from '../cache/object-cache.service'; import { ObjectCacheService } from '../cache/object-cache.service';
import { dataService } from './base/data-service.decorator'; import { dataService } from './base/data-service.decorator';
import { RequestParam } from '../cache/models/request-param.model';
/** /**
* Check if one side of a RelationshipType is the ItemType with the given label * Check if one side of a RelationshipType is the ItemType with the given label
@@ -130,14 +131,8 @@ export class RelationshipTypeDataService extends BaseDataService<RelationshipTyp
'byEntityType', 'byEntityType',
{ {
searchParams: [ searchParams: [
{ new RequestParam('type', type),
fieldName: 'type', new RequestParam('size', 100),
fieldValue: type,
},
{
fieldName: 'size',
fieldValue: 100,
},
], ],
}, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow, }, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow,
).pipe( ).pipe(

View File

@@ -96,7 +96,7 @@ describe('EPersonDataService', () => {
it('search by default scope (byMetadata) and no query', () => { it('search by default scope (byMetadata) and no query', () => {
service.searchByScope(null, ''); service.searchByScope(null, '');
const options = Object.assign(new FindListOptions(), { const options = Object.assign(new FindListOptions(), {
searchParams: [Object.assign(new RequestParam('query', encodeURIComponent('')))] searchParams: [Object.assign(new RequestParam('query', ''))],
}); });
expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options, true, true); expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options, true, true);
}); });
@@ -104,7 +104,7 @@ describe('EPersonDataService', () => {
it('search metadata scope and no query', () => { it('search metadata scope and no query', () => {
service.searchByScope('metadata', ''); service.searchByScope('metadata', '');
const options = Object.assign(new FindListOptions(), { const options = Object.assign(new FindListOptions(), {
searchParams: [Object.assign(new RequestParam('query', encodeURIComponent('')))] searchParams: [Object.assign(new RequestParam('query', ''))],
}); });
expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options, true, true); expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options, true, true);
}); });
@@ -112,7 +112,7 @@ describe('EPersonDataService', () => {
it('search metadata scope and with query', () => { it('search metadata scope and with query', () => {
service.searchByScope('metadata', 'test'); service.searchByScope('metadata', 'test');
const options = Object.assign(new FindListOptions(), { const options = Object.assign(new FindListOptions(), {
searchParams: [Object.assign(new RequestParam('query', encodeURIComponent('test')))] searchParams: [Object.assign(new RequestParam('query', 'test'))],
}); });
expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options, true, true); expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options, true, true);
}); });
@@ -122,7 +122,7 @@ describe('EPersonDataService', () => {
spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(null)); spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(null));
service.searchByScope('email', ''); service.searchByScope('email', '');
const options = Object.assign(new FindListOptions(), { const options = Object.assign(new FindListOptions(), {
searchParams: [Object.assign(new RequestParam('email', encodeURIComponent('')))] searchParams: [Object.assign(new RequestParam('email', ''))],
}); });
expect((service as any).searchData.getSearchByHref).toHaveBeenCalledWith('byEmail', options); expect((service as any).searchData.getSearchByHref).toHaveBeenCalledWith('byEmail', options);
expect(service.findByHref).toHaveBeenCalledWith(epersonsEndpoint, true, true); expect(service.findByHref).toHaveBeenCalledWith(epersonsEndpoint, true, true);
@@ -133,7 +133,7 @@ describe('EPersonDataService', () => {
spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(EPersonMock)); spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(EPersonMock));
service.searchByScope('email', EPersonMock.email); service.searchByScope('email', EPersonMock.email);
const options = Object.assign(new FindListOptions(), { const options = Object.assign(new FindListOptions(), {
searchParams: [Object.assign(new RequestParam('email', encodeURIComponent(EPersonMock.email)))] searchParams: [Object.assign(new RequestParam('email', EPersonMock.email))],
}); });
expect((service as any).searchData.getSearchByHref).toHaveBeenCalledWith('byEmail', options); expect((service as any).searchData.getSearchByHref).toHaveBeenCalledWith('byEmail', options);
expect(service.findByHref).toHaveBeenCalledWith(epersonsEndpoint, true, true); expect(service.findByHref).toHaveBeenCalledWith(epersonsEndpoint, true, true);

View File

@@ -130,7 +130,7 @@ export class EPersonDataService extends IdentifiableDataService<EPerson> impleme
*/ */
public getEPersonByEmail(query: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<EPerson>[]): Observable<RemoteData<EPerson | NoContent>> { public getEPersonByEmail(query: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<EPerson>[]): Observable<RemoteData<EPerson | NoContent>> {
const findListOptions = new FindListOptions(); const findListOptions = new FindListOptions();
findListOptions.searchParams = [new RequestParam('email', encodeURIComponent(query))]; findListOptions.searchParams = [new RequestParam('email', query)];
const href$ = this.searchData.getSearchByHref(this.searchByEmailPath, findListOptions, ...linksToFollow); const href$ = this.searchData.getSearchByHref(this.searchByEmailPath, findListOptions, ...linksToFollow);
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
} }
@@ -147,7 +147,7 @@ export class EPersonDataService extends IdentifiableDataService<EPerson> impleme
* {@link HALLink}s should be automatically resolved * {@link HALLink}s should be automatically resolved
*/ */
private getEpeopleByMetadata(query: string, options?: FindListOptions, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<EPerson>[]): Observable<RemoteData<PaginatedList<EPerson>>> { private getEpeopleByMetadata(query: string, options?: FindListOptions, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<EPerson>[]): Observable<RemoteData<PaginatedList<EPerson>>> {
const searchParams = [new RequestParam('query', encodeURIComponent(query))]; const searchParams = [new RequestParam('query', query)];
return this.getEPeopleBy(searchParams, this.searchByMetadataPath, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); return this.getEPeopleBy(searchParams, this.searchByMetadataPath, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
} }

View File

@@ -15,6 +15,7 @@ import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
import { RemoteData } from '../data/remote-data'; import { RemoteData } from '../data/remote-data';
import { PaginatedList } from '../data/paginated-list.model'; import { PaginatedList } from '../data/paginated-list.model';
import { dataService } from '../data/base/data-service.decorator'; import { dataService } from '../data/base/data-service.decorator';
import { RequestParam } from '../cache/models/request-param.model';
/** /**
* A service to retrieve {@link UsageReport}s from the REST API * A service to retrieve {@link UsageReport}s from the REST API
@@ -45,10 +46,7 @@ export class UsageReportDataService extends IdentifiableDataService<UsageReport>
searchStatistics(uri: string, page: number, size: number): Observable<UsageReport[]> { searchStatistics(uri: string, page: number, size: number): Observable<UsageReport[]> {
return this.searchBy('object', { return this.searchBy('object', {
searchParams: [ searchParams: [
{ new RequestParam('uri', uri),
fieldName: `uri`,
fieldValue: uri,
},
], ],
currentPage: page, currentPage: page,
elementsPerPage: size, elementsPerPage: size,

View File

@@ -16,6 +16,7 @@ import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
import { RemoteData } from '../data/remote-data'; import { RemoteData } from '../data/remote-data';
import { PaginatedList } from '../data/paginated-list.model'; import { PaginatedList } from '../data/paginated-list.model';
import { dataService } from '../data/base/data-service.decorator'; import { dataService } from '../data/base/data-service.decorator';
import { RequestParam } from '../cache/models/request-param.model';
@Injectable() @Injectable()
@dataService(SUBMISSION_CC_LICENSE_URL) @dataService(SUBMISSION_CC_LICENSE_URL)
@@ -43,17 +44,8 @@ export class SubmissionCcLicenseUrlDataService extends BaseDataService<Submissio
return this.searchData.getSearchByHref( return this.searchData.getSearchByHref(
'rightsByQuestions',{ 'rightsByQuestions',{
searchParams: [ searchParams: [
{ new RequestParam('license', ccLicense.id),
fieldName: 'license', ...ccLicense.fields.map((field: Field) => new RequestParam(`answer_${field.id}`, options.get(field).id)),
fieldValue: ccLicense.id
},
...ccLicense.fields.map(
(field) => {
return {
fieldName: `answer_${field.id}`,
fieldValue: options.get(field).id,
};
}),
] ]
} }
).pipe( ).pipe(

View File

@@ -107,7 +107,7 @@ export class WorkflowItemDataService extends IdentifiableDataService<WorkflowIte
*/ */
public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> { public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> {
const findListOptions = new FindListOptions(); const findListOptions = new FindListOptions();
findListOptions.searchParams = [new RequestParam('uuid', encodeURIComponent(uuid))]; findListOptions.searchParams = [new RequestParam('uuid', uuid)];
const href$ = this.searchData.getSearchByHref(this.searchByItemLinkPath, findListOptions, ...linksToFollow); const href$ = this.searchData.getSearchByHref(this.searchByItemLinkPath, findListOptions, ...linksToFollow);
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
} }

View File

@@ -54,7 +54,7 @@ export class WorkspaceitemDataService extends IdentifiableDataService<WorkspaceI
*/ */
public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> { public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> {
const findListOptions = new FindListOptions(); const findListOptions = new FindListOptions();
findListOptions.searchParams = [new RequestParam('uuid', encodeURIComponent(uuid))]; findListOptions.searchParams = [new RequestParam('uuid', uuid)];
const href$ = this.getSearchByHref(this.searchByItemLinkPath, findListOptions, ...linksToFollow); const href$ = this.getSearchByHref(this.searchByItemLinkPath, findListOptions, ...linksToFollow);
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
} }