|
|
@ -65,6 +65,7 @@ import { |
|
|
|
} from 'core-app/features/hal/resources/hal-resource'; |
|
|
|
} from 'core-app/features/hal/resources/hal-resource'; |
|
|
|
import { initializeHalProperties } from '../helpers/hal-resource-builder'; |
|
|
|
import { initializeHalProperties } from '../helpers/hal-resource-builder'; |
|
|
|
import { HalError } from 'core-app/features/hal/services/hal-error'; |
|
|
|
import { HalError } from 'core-app/features/hal/services/hal-error'; |
|
|
|
|
|
|
|
import { getPaginatedResults } from 'core-app/core/apiv3/helpers/get-paginated-results'; |
|
|
|
|
|
|
|
|
|
|
|
export interface HalResourceFactoryConfigInterface { |
|
|
|
export interface HalResourceFactoryConfigInterface { |
|
|
|
cls?:any; |
|
|
|
cls?:any; |
|
|
@ -142,46 +143,23 @@ export class HalResourceService { |
|
|
|
* Return all potential pages to the request, when the elements returned from API is smaller |
|
|
|
* Return all potential pages to the request, when the elements returned from API is smaller |
|
|
|
* than the expected. |
|
|
|
* than the expected. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param href |
|
|
|
* @param href The URL to request |
|
|
|
* @param expected The expected number of elements |
|
|
|
* @param params Parameters to pass to each paged request |
|
|
|
* @param params |
|
|
|
* @param headers Headers to pass to each paged request |
|
|
|
* @param headers |
|
|
|
* @return {Observable<CollectionResource[]>} |
|
|
|
* @return {Promise<CollectionResource[]>} |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public async getAllPaginated<T extends CollectionResource>( |
|
|
|
public getAllPaginated<T extends CollectionResource>( |
|
|
|
href:string, |
|
|
|
href:string, |
|
|
|
expected:number, |
|
|
|
|
|
|
|
params:Record<string, string|number> = {}, |
|
|
|
params:Record<string, string|number> = {}, |
|
|
|
headers:HTTPClientHeaders = {}, |
|
|
|
headers:HTTPClientHeaders = {}, |
|
|
|
):Promise<T[]> { |
|
|
|
):Observable<T[]> { |
|
|
|
// Total number retrieved
|
|
|
|
return getPaginatedResults( |
|
|
|
let retrieved = 0; |
|
|
|
(pageParams) => { |
|
|
|
// Current offset page
|
|
|
|
const requestParams = { ...params, pageParams }; |
|
|
|
let page = 1; |
|
|
|
return this.request<CollectionResource<T>>('get', href, this.toEprops(requestParams), headers); |
|
|
|
// Accumulated results
|
|
|
|
}, |
|
|
|
const allResults:T[] = []; |
|
|
|
(params.pageSize as number|undefined) || -1, |
|
|
|
// If possible, request all at once.
|
|
|
|
); |
|
|
|
const requestParams = { ...params }; |
|
|
|
|
|
|
|
requestParams.pageSize = expected; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (retrieved < expected) { |
|
|
|
|
|
|
|
requestParams.offset = page; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const promise = this.request<T>('get', href, this.toEprops(requestParams), headers).toPromise(); |
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
|
|
|
|
const results = await promise; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (results.count === 0) { |
|
|
|
|
|
|
|
throw new Error('No more results for this query, but expected more.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allResults.push(results); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
retrieved += results.count; |
|
|
|
|
|
|
|
page += 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return allResults; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|