Skip to content

Commit

Permalink
add composite service
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault committed Oct 20, 2019
1 parent c33b716 commit b117bd7
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 21 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ deploy:
on:
repo: guillotinaweb/grange-core
branch: master
env:
global:
secure: XA3j4Q1ZTgmteIfnNRdRvZI4iuNmDgcvmbYzdNcKPVIslFVskZ5dP/t5t+owjR4ARTcEWqcnyryKrKy+J0UUhUMcEvMRSqZnvfKwU3gN0jr0C7tVfLai5NplOlrnyNxqGK6G/XG3riPdzxCnEPkp3WPcimtOoK+QgSucBMZCtN2uIBMqD5KCg+27xeJvGx8U7JYa0sL2GnEvR6Pqsbket71El5vrdx6cGglIPKKwiWTzFDhHkSeTBiQCcfHvkzYxIh9Ur2ObDYAL+oAFNijetmc3dOXNG0eWuZOrrxsqKA23x7ef8InfSoCCwRQOfeTq103sJdv/BI1Yc2624uQDV7INX8u9troNAI8V2Ocm2G4gkN0PHo2zQayIZBXJdWGDpET490NvVPl+DqaU3dDSSzP6V2ALTEngYZR1y1U1iYvsCWD5CoFERQ7Iw3FyKW5rdyFx+JEVP3bLqvvSdO6vW35irAHw7OXwqKotpNIct7B05H4BwPG3qIWZdn201CllYHJ+k/dqTWYRA66Gj/LUIGtd1oAiE+2R+warCiDZ6zAOnPirchcf9GSGLW0AK/WF/l20mcVr43mmDm2DqYhAcpJATmJeqMEBn1jfJB+Rnx6V7kbwqdDpbXP5eQQ7ZGngEyw8qx3RQhGj9QWs+UYNVmnbocQuoukOjtbYooiVUmU=
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.1 (2019-10-20)

- Fix authentication
- Add a CoreService aggregating all the internal services

# 1.0.0 (2019-10-16)

- Backport existing services from @plone/restapi-angular
2 changes: 1 addition & 1 deletion projects/grange-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grange-core",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"author": {
"name": "Eric Brehault",
Expand Down
1 change: 1 addition & 0 deletions projects/grange-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { GrangeCore } from './lib/core.service';
export { APIService } from './lib/api.service';
export { AuthenticationService } from './lib/authentication.service';
export { CacheService } from './lib/cache.service';
Expand Down
36 changes: 21 additions & 15 deletions projects/grange-core/src/lib/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { AuthenticatedStatus, Error, PasswordResetInfo, UserInfoTokenParts, LoginToken } from './interfaces';
import { tap, catchError } from 'rxjs/operators';
import { tap, catchError, map } from 'rxjs/operators';
import { ConfigurationService } from './configuration.service';

@Injectable({
Expand Down Expand Up @@ -79,28 +79,33 @@ export class AuthenticationService {
});
}

setAuthToken(token: string) {
localStorage.setItem('auth', token);
localStorage.setItem(
'auth_time',
new Date().toISOString(),
);
this.isAuthenticated.next({
state: true,
pending: false,
username: this.getUsername(),
});
}

login(login: string, password: string, path?: string): Observable<any> {
const headers = this.getHeaders();
const body = JSON.stringify({
login: login, // on plone.restapi login endpoint, username key is login
password: password,
login,
password,
});
return this.http
.post(this.config.get('BACKEND_URL') + (path || '') + '/@login', body, {
headers: headers,
headers,
}).pipe(
tap((data: LoginToken) => {
map((data: LoginToken) => {
if (data.token) {
localStorage.setItem('auth', data['token']);
localStorage.setItem(
'auth_time',
new Date().toISOString(),
);
this.isAuthenticated.next({
state: true,
pending: false,
username: this.getUsername(),
});
this.setAuthToken(data.token);
return true;
} else {
localStorage.removeItem('auth');
localStorage.removeItem('auth_time');
Expand All @@ -109,6 +114,7 @@ export class AuthenticationService {
pending: false,
username: null,
});
return false;
}
}),
catchError((errorResponse: HttpErrorResponse) => {
Expand Down
20 changes: 20 additions & 0 deletions projects/grange-core/src/lib/core.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@angular/core';
import { APIService } from './api.service';
import { AuthenticationService } from './authentication.service';
import { CacheService } from './cache.service';
import { LoadingService } from './loading.service';
import { ResourceService } from './resource.service';

@Injectable({
providedIn: 'root'
})
export class GrangeCore {

constructor(
public api: APIService,
public auth: AuthenticationService,
public cache: CacheService,
public loading: LoadingService,
public resource: ResourceService
) { }
}
12 changes: 11 additions & 1 deletion projects/grange-core/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,14 @@ export interface SearchResults {
items_total: number;
items: any[];
batching: Batching;
}
}

export interface Resource {
'@id': string;
'@name': string;
'@type': string;
'@uid': string;
title: string;
description?: string;
is_folderish: boolean;
}
3 changes: 2 additions & 1 deletion projects/grange-core/src/lib/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WorkflowHistoryItem,
WorkflowInformation,
WorkflowTransitionOptions,
Resource,
} from './interfaces';
import { Vocabulary } from './vocabularies';
import { APIService } from './api.service';
Expand Down Expand Up @@ -193,7 +194,7 @@ export class ResourceService {
return this.cache.get(path);
}

items(path: string, page = 1): Observable<any> {
items(path: string, page = 1): Observable<{items: Resource[], page: number, page_size: number, total: number}> {
return this.cache.get(`${path}/@items?page=${page}`);
}

Expand Down

0 comments on commit b117bd7

Please sign in to comment.