Skip to content

Commit

Permalink
feat: add support for providing options for the 'resolveLink/resolveL…
Browse files Browse the repository at this point in the history
…inks' functionality
  • Loading branch information
shauke committed Apr 1, 2021
1 parent 3b828bc commit f8b2be6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/app/core/services/api/api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,37 @@ describe('Api Service', () => {

httpTestingController.expectNone(`${REST_URL}/dummy`);
});

it('should append additional headers when resolveLink is used with header options', () => {
const someHeader = { headers: new HttpHeaders({ dummy: 'linkHeaderTest' }) };

apiService.get('something', someHeader).pipe(apiService.resolveLink(someHeader)).subscribe(fail, fail, fail);

const req = httpTestingController.expectOne(`${REST_URL}/something`);
expect(req.request.headers.get('dummy')).toEqual('linkHeaderTest');
req.flush({ type: 'Link', uri: 'site/-/dummy' });

const req2 = httpTestingController.expectOne(`${REST_URL}/dummy`);
expect(req2.request.headers.get('dummy')).toEqual('linkHeaderTest');
});

it('should append additional headers to all link requests when resolveLinks is used with header options', () => {
const someHeader = { headers: new HttpHeaders({ dummy: 'linkHeaderTest' }) };

apiService.get('something', someHeader).pipe(apiService.resolveLinks(someHeader)).subscribe(fail, fail, fail);

const req = httpTestingController.expectOne(`${REST_URL}/something`);
expect(req.request.headers.get('dummy')).toEqual('linkHeaderTest');
req.flush([
{ type: 'Link', uri: 'site/-/dummy1' },
{ type: 'Link', uri: 'site/-/dummy2' },
] as Link[]);

const req2 = httpTestingController.expectOne(`${REST_URL}/dummy1`);
expect(req2.request.headers.get('dummy')).toEqual('linkHeaderTest');
const req3 = httpTestingController.expectOne(`${REST_URL}/dummy2`);
expect(req3.request.headers.get('dummy')).toEqual('linkHeaderTest');
});
});

describe('API Service URL construction', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/app/core/services/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class ApiService {
* Pipeable operator for link translation (resolving one single link).
* @returns The link resolved to its actual REST response data.
*/
resolveLink<T>(): OperatorFunction<Link, T> {
resolveLink<T>(options?: AvailableOptions): OperatorFunction<Link, T> {
return stream$ =>
stream$.pipe(
withLatestFrom(this.store.pipe(select(getICMServerURL))),
Expand All @@ -244,7 +244,7 @@ export class ApiService {
// check if link data is properly formatted
() => link?.type === 'Link' && !!link.uri,
// flat map to API request
this.get<T>(`${icmServerURL}/${link.uri}`),
this.get<T>(`${icmServerURL}/${link.uri}`, options),
// throw if link is not properly supplied
throwError(new Error('link was not properly formatted'))
)
Expand All @@ -256,14 +256,14 @@ export class ApiService {
* Pipeable operator for link translation (resolving multiple links).
* @returns The links resolved to their actual REST response data.
*/
resolveLinks<T>(): OperatorFunction<Link[], T[]> {
resolveLinks<T>(options?: AvailableOptions): OperatorFunction<Link[], T[]> {
return source$ =>
source$.pipe(
// filter for all real Link elements
map(links => links.filter(el => el?.type === 'Link' && !!el.uri)),
withLatestFrom(this.store.pipe(select(getICMServerURL))),
// transform Link elements to API Observables
map(([links, icmServerURL]) => links.map(item => this.get<T>(`${icmServerURL}/${item.uri}`))),
map(([links, icmServerURL]) => links.map(item => this.get<T>(`${icmServerURL}/${item.uri}`, options))),
// flatten to API requests O<O<T>[]> -> O<T[]>
concatMap(obsArray => iif(() => !!obsArray.length, forkJoin(obsArray), of([])))
);
Expand Down

0 comments on commit f8b2be6

Please sign in to comment.