WIP UI implementation: fixed store dependencies, login flow.

This commit is contained in:
2019-03-08 03:03:47 -06:00
parent 9a9fa7c5d9
commit 12b2e5edca
17 changed files with 145 additions and 161 deletions

View File

@ -2,7 +2,6 @@ import { default as Axios, AxiosInstance } from 'axios';
import { ApiToken, LoginSubmit, Measure, Measurement, User } from '@/models';
import { Logger, logService } from '@/services/logging';
import merge from 'lodash.merge';
import authStore from '@/store-modules/auth';
export class PmApiClient {
private http: AxiosInstance;
@ -24,6 +23,15 @@ export class PmApiClient {
this.log.trace('Initialized PmApiClient');
}
public setAuthToken(authToken: string) {
/*tslint:disable:no-string-literal*/
this.http.defaults.headers.common['Authorization'] = `Bearer ${authToken}`;
}
public clearAuthToken() {
this.http.defaults.headers.common['Authorization'] = '';
}
public async version(): Promise<string> {
const resp = await this.http.get('/version');
return resp.data;
@ -33,129 +41,97 @@ export class PmApiClient {
: Promise<string> {
const resp = await this.http.post('/auth-token', creds);
this.setAuthToken(resp.data);
return resp.data;
}
public async getUser(): Promise<User> {
const resp = await this.http.get('/user',
{ headers: this.authHeader() });
return merge(resp.data) ;
const resp = await this.http.get('/user');
return resp.data;
}
public async getAllUsers(): Promise<User[]> {
const resp = await this.http.get('/users',
{ headers: this.authHeader() });
const resp = await this.http.get('/users');
return resp.data;
}
public async getUserById(reqUserId: string): Promise<User> {
const resp = await this.http.get(`/users/${reqUserId}`,
{ headers: this.authHeader() });
const resp = await this.http.get(`/users/${reqUserId}`);
return resp.data;
}
public async createUser(newUser: User): Promise<User> {
const resp = await this.http.post('/users',
newUser, { headers: this.authHeader() });
const resp = await this.http.post('/users', newUser);
return resp.data;
}
public async deleteUser(toDeleteUserId: string)
: Promise<boolean> {
await this.http.delete(`/users/${toDeleteUserId}`,
{ headers: this.authHeader() });
await this.http.delete(`/users/${toDeleteUserId}`);
return true;
}
public async changePwd(oldPassword: string, newPassword: string)
: Promise<boolean> {
await this.http.post('/change-pwd',
{ oldPassword, newPassword }, { headers: this.authHeader() });
await this.http.post('/change-pwd', { oldPassword, newPassword });
return true;
}
public async changePwdForUser(forUserId: string, newPassword: string)
: Promise<boolean> {
await this.http.post(`/change-pwd/${forUserId}`,
{ newPassword }, { headers: this.authHeader() });
await this.http.post(`/change-pwd/${forUserId}`, { newPassword });
return true;
}
public async createApiToken(token: ApiToken)
: Promise<ApiToken[]> {
const resp = await this.http.post(`/api-tokens`,
token, { headers: this.authHeader() });
const resp = await this.http.post(`/api-tokens`, token);
return resp.data;
}
public async getAllApiTokens(): Promise<ApiToken[]> {
const resp = await this.http.get('/api-tokens',
{ headers: this.authHeader() });
const resp = await this.http.get('/api-tokens');
return resp.data;
}
public async getApiToken(tokenId: string): Promise<ApiToken[]> {
const resp = await this.http.get(`/api-tokens/${tokenId}`,
{ headers: this.authHeader() });
const resp = await this.http.get(`/api-tokens/${tokenId}`);
return resp.data;
}
public async deleteApiToken(tokenId: string): Promise<boolean> {
const resp = await this.http.delete(`/api-tokens/${tokenId}`,
{ headers: this.authHeader() });
const resp = await this.http.delete(`/api-tokens/${tokenId}`);
return true;
}
public async getAllMeasures(): Promise<Measure[]> {
const resp = await this.http.get(`/measures`,
{ headers: this.authHeader() });
const resp = await this.http.get(`/measures`);
return resp.data;
}
public async createMeasure(measure: Measure): Promise<Measure> {
const resp = await this.http.post(`/measures`,
measure, { headers: this.authHeader() });
const resp = await this.http.post(`/measures`);
return resp.data;
}
public async getMeasure(slug: string): Promise<Measure> {
const resp = await this.http.get(`/measures/${slug}`,
{ headers: this.authHeader() });
const resp = await this.http.get(`/measures/${slug}`);
return resp.data;
}
public async deleteMeasure(slug: string): Promise<boolean> {
const resp = await this.http.delete(`/measures/${slug}`,
{ headers: this.authHeader() });
const resp = await this.http.delete(`/measures/${slug}`);
return true;
}
public async getMeasurements(measureSlug: string)
: Promise<Measurement[]> {
const resp = await this.http.get(`/measure/${measureSlug}`,
{ headers: this.authHeader() });
const resp = await this.http.get(`/measure/${measureSlug}`);
return resp.data;
}
@ -164,9 +140,7 @@ export class PmApiClient {
measurement: Measurement)
: Promise<Measurement> {
const resp = await this.http.post(`/measure/${measureSlug}`,
measurement, { headers: this.authHeader() });
const resp = await this.http.post(`/measure/${measureSlug}`, measurement);
return resp.data;
}
@ -175,9 +149,8 @@ export class PmApiClient {
measurementId: string)
: Promise<Measurement> {
const resp = await this.http.get(`/measure/${measureSlug}/${measurementId}`,
{ headers: this.authHeader() });
const resp = await this.http
.get(`/measure/${measureSlug}/${measurementId}`);
return resp.data;
}
@ -186,9 +159,8 @@ export class PmApiClient {
measurement: Measurement)
: Promise<Measurement> {
const resp = await this.http.put(`/measure/${measureSlug}/${measurement.id}`,
measurement, { headers: this.authHeader() });
const resp = await this.http
.put(`/measure/${measureSlug}/${measurement.id}`, measurement);
return resp.data;
}
@ -197,19 +169,10 @@ export class PmApiClient {
measurementId: string)
: Promise<boolean> {
const resp = await this.http.delete(`/measure/${measureSlug}/${measurementId}`,
{headers: this.authHeader() });
const resp = await this.http
.delete(`/measure/${measureSlug}/${measurementId}`);
return true;
}
private authHeader(): { [key: string]: string } {
if (authStore.authToken) {
return { Authorization: 'Bearer ' + authStore.authToken };
} else {
throw new Error('no authenticated user');
}
}
}
export const api = new PmApiClient(process.env.VUE_APP_PM_API_BASE);