mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-09 19:13:08 +00:00
[DURACOM-191] Remove DataService decorator
This commit is contained in:
@@ -1,60 +0,0 @@
|
|||||||
/* eslint-disable max-classes-per-file */
|
|
||||||
/**
|
|
||||||
* The contents of this file are subject to the license and copyright
|
|
||||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
|
||||||
* tree and available online at
|
|
||||||
*
|
|
||||||
* http://www.dspace.org/license/
|
|
||||||
*/
|
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
|
||||||
|
|
||||||
import { ResourceType } from '../../shared/resource-type';
|
|
||||||
import { BaseDataService } from './base-data.service';
|
|
||||||
import {
|
|
||||||
dataService,
|
|
||||||
getDataServiceFor,
|
|
||||||
} from './data-service.decorator';
|
|
||||||
import { HALDataService } from './hal-data-service.interface';
|
|
||||||
|
|
||||||
class TestService extends BaseDataService<any> {
|
|
||||||
}
|
|
||||||
|
|
||||||
class AnotherTestService implements HALDataService<any> {
|
|
||||||
public findListByHref(href$, findListOptions, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow): any {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
public findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow): any {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let testType;
|
|
||||||
|
|
||||||
describe('@dataService/getDataServiceFor', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
testType = new ResourceType(`testType-${uuidv4()}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should register a resourcetype for a dataservice', () => {
|
|
||||||
dataService(testType)(TestService);
|
|
||||||
expect(getDataServiceFor(testType)).toBe(TestService);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe(`when the resource type isn't specified`, () => {
|
|
||||||
it(`should throw an error`, () => {
|
|
||||||
expect(() => {
|
|
||||||
dataService(undefined)(TestService);
|
|
||||||
}).toThrow();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe(`when there already is a registered dataservice for a resourcetype`, () => {
|
|
||||||
it(`should throw an error`, () => {
|
|
||||||
dataService(testType)(TestService);
|
|
||||||
expect(() => {
|
|
||||||
dataService(testType)(AnotherTestService);
|
|
||||||
}).toThrow();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@@ -1,55 +0,0 @@
|
|||||||
/**
|
|
||||||
* The contents of this file are subject to the license and copyright
|
|
||||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
|
||||||
* tree and available online at
|
|
||||||
*
|
|
||||||
* http://www.dspace.org/license/
|
|
||||||
*/
|
|
||||||
import { InjectionToken } from '@angular/core';
|
|
||||||
|
|
||||||
import {
|
|
||||||
hasNoValue,
|
|
||||||
hasValue,
|
|
||||||
} from '../../../shared/empty.util';
|
|
||||||
import { CacheableObject } from '../../cache/cacheable-object.model';
|
|
||||||
import { GenericConstructor } from '../../shared/generic-constructor';
|
|
||||||
import { ResourceType } from '../../shared/resource-type';
|
|
||||||
import { HALDataService } from './hal-data-service.interface';
|
|
||||||
|
|
||||||
export const DATA_SERVICE_FACTORY = new InjectionToken<(resourceType: ResourceType) => GenericConstructor<HALDataService<any>>>('getDataServiceFor', {
|
|
||||||
providedIn: 'root',
|
|
||||||
factory: () => getDataServiceFor,
|
|
||||||
});
|
|
||||||
const dataServiceMap = new Map();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class decorator to indicate that this class is a data service for a given HAL resource type.
|
|
||||||
*
|
|
||||||
* In most cases, a data service should extend {@link BaseDataService}.
|
|
||||||
* At the very least it must implement {@link HALDataService} in order for it to work with {@link LinkService}.
|
|
||||||
*
|
|
||||||
* @param resourceType the resource type the class is a dataservice for
|
|
||||||
*/
|
|
||||||
export function dataService(resourceType: ResourceType) {
|
|
||||||
return (target: GenericConstructor<HALDataService<any>>): void => {
|
|
||||||
if (hasNoValue(resourceType)) {
|
|
||||||
throw new Error(`Invalid @dataService annotation on ${target}, resourceType needs to be defined`);
|
|
||||||
}
|
|
||||||
const existingDataservice = dataServiceMap.get(resourceType.value);
|
|
||||||
|
|
||||||
if (hasValue(existingDataservice)) {
|
|
||||||
throw new Error(`Multiple dataservices for ${resourceType.value}: ${existingDataservice} and ${target}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
dataServiceMap.set(resourceType.value, target);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the dataservice matching the given resource type
|
|
||||||
*
|
|
||||||
* @param resourceType the resource type you want the matching dataservice for
|
|
||||||
*/
|
|
||||||
export function getDataServiceFor<T extends CacheableObject>(resourceType: ResourceType): GenericConstructor<HALDataService<any>> {
|
|
||||||
return dataServiceMap.get(resourceType.value);
|
|
||||||
}
|
|
Reference in New Issue
Block a user