Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
feat(api): Breaking changes in SpaceService (#197)
Browse files Browse the repository at this point in the history
* feat(api): SpaceService.deleteSpace renamed

BREAKING CHANGE: Method deleteSpace of SpaceServie renamed to delete to follow established naming conventions

* feat(api): make SpaceService.getSpacesDelegate private
  • Loading branch information
ppitonak authored and joshuawilson committed Jan 10, 2019
1 parent fbb23f4 commit 8e3160a
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions src/app/spaces/space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export class SpaceService {
return observableThrowError('Page limit cannot be less or equal 0');
}
const url: string = `${this.spacesUrl}?page[limit]=${pageSize}`;
return this.getSpacesDelegate(url, true);
return this.getSpacesDelegate(url);
}

getMoreSpaces(): Observable<Space[]> {
if (this.nextLink) {
return this.getSpacesDelegate(this.nextLink, false);
return this.getSpacesDelegate(this.nextLink);
} else {
return observableThrowError('No more spaces found');
}
Expand All @@ -78,36 +78,6 @@ export class SpaceService {
);
}

getSpacesDelegate(url: string, isAll: boolean, params?: HttpParams): Observable<Space[]> {
// Set the GET options to include the search params if they are sent, if not then just the headers.
const options = params ? { headers: this.headers, params: params } : { headers: this.headers };

return this.http.get(url, options)
.pipe(
map((response: any): Space[] => {
// Extract links from JSON API response.
// and set the nextLink, if server indicates more resources
// in paginated collection through a 'next' link.
const links: any = response.links;
if (links && links.hasOwnProperty('next')) {
this.nextLink = links.next;
} else {
this.nextLink = null;
}
let meta = response.meta;
if (meta && meta.hasOwnProperty('totalCount')) {
this.totalCount = meta.totalCount;
} else {
this.totalCount = -1;
}
// Extract data from JSON API response, and assert to an array of spaces.
return response.data as Space[];
}),
flatMap((spaces: Space[]): Observable<Space[]> => this.resolveOwners(spaces)),
catchError((error: any): Observable<Space[]> => this.handleError(error))
);
}

create(space: Space): Observable<Space> {
if (!space) {
return observableThrowError('Space cannot be undefined');
Expand Down Expand Up @@ -136,7 +106,7 @@ export class SpaceService {
);
}

deleteSpace(space: Space, skipCluster: boolean = false): Observable<Space> {
delete(space: Space, skipCluster: boolean = false): Observable<Space> {
if (!space) {
return observableThrowError('Space cannot be undefined');
}
Expand Down Expand Up @@ -167,12 +137,12 @@ export class SpaceService {
.append('page[offset]', (pageSize * pageNumber).toString())
.append('page[limit]', pageSize.toString());

return this.getSpacesDelegate(url, false, params);
return this.getSpacesDelegate(url, params);
}

getMoreSearchResults(): Observable<Space[]> {
if (this.nextLink) {
return this.getSpacesDelegate(this.nextLink, false);
return this.getSpacesDelegate(this.nextLink);
} else {
return observableThrowError('No more spaces found');
}
Expand All @@ -194,7 +164,7 @@ export class SpaceService {
return observableThrowError('Page limit cannot be less or equal 0');
}
const url: string = `${this.namedSpacesUrl}/${encodeURIComponent(userName)}?page[limit]=${pageSize}`;
return this.getSpacesDelegate(url, false);
return this.getSpacesDelegate(url);
}

/**
Expand All @@ -206,7 +176,7 @@ export class SpaceService {

getMoreSpacesByName(): Observable<Space[]> {
if (this.nextLink) {
return this.getSpacesDelegate(this.nextLink, false);
return this.getSpacesDelegate(this.nextLink);
} else {
return observableThrowError('No more spaces found');
}
Expand All @@ -231,6 +201,36 @@ export class SpaceService {
return observableOf(this.totalCount);
}

private getSpacesDelegate(url: string, params?: HttpParams): Observable<Space[]> {
// Set the GET options to include the search params if they are sent, if not then just the headers.
const options = params ? { headers: this.headers, params: params } : { headers: this.headers };

return this.http.get(url, options)
.pipe(
map((response: any): Space[] => {
// Extract links from JSON API response.
// and set the nextLink, if server indicates more resources
// in paginated collection through a 'next' link.
const links: any = response.links;
if (links && links.hasOwnProperty('next')) {
this.nextLink = links.next;
} else {
this.nextLink = null;
}
let meta = response.meta;
if (meta && meta.hasOwnProperty('totalCount')) {
this.totalCount = meta.totalCount;
} else {
this.totalCount = -1;
}
// Extract data from JSON API response, and assert to an array of spaces.
return response.data as Space[];
}),
flatMap((spaces: Space[]): Observable<Space[]> => this.resolveOwners(spaces)),
catchError((error: any): Observable<Space[]> => this.handleError(error))
);
}

private handleError(error: any): Observable<any> {
this.logger.error(error);
return observableThrowError(error.message || error);
Expand Down

0 comments on commit 8e3160a

Please sign in to comment.