Add Personal Management API client implementation.

This commit is contained in:
Jonathan Bernard 2019-02-24 06:56:39 -07:00
parent 1b91926d3a
commit 62eaf2ec2a
6 changed files with 213 additions and 0 deletions

View File

@ -0,0 +1,6 @@
export default interface ApiToken {
id: string;
userId: string;
name: string;
value?: string;
}

6
web/src/models/index.ts Normal file
View File

@ -0,0 +1,6 @@
import ApiToken from './api-token';
import Measure from './measure';
import Measurement from './measurement';
import User from './user';
export { ApiToken, Measure, Measurement, User };

12
web/src/models/measure.ts Normal file
View File

@ -0,0 +1,12 @@
export default interface Measure {
id: string;
userId: string;
slug: string;
name: string;
description: string;
domainSource?: string;
domainUnites: string;
rangeSource?: string;
rangeUnits: string;
analysis: string[];
}

View File

@ -0,0 +1,7 @@
export default interface Measurement {
id: string;
measureId: string;
value: number;
timestamp: Date;
extData: object;
}

7
web/src/models/user.ts Normal file
View File

@ -0,0 +1,7 @@
export default interface User {
id: string;
displayName: string;
email: string;
isAdmin: boolean;
authToken?: string;
}

View File

@ -0,0 +1,175 @@
import { default as Axios, AxiosInstance } from 'axios';
import { ApiToken, Measure, Measurement, User } from '@/models';
import merge from 'lodash.merge';
export class PmApiClient {
private http: AxiosInstance;
constructor(apiBase: string) {
this.http = Axios.create({
baseURL: apiBase
/*
headers: {
post: {
'Content-Type': 'application/json'
}
}
*/
});
}
public version(): Promise<any> {
return this.http
.get('/version')
.then((resp) => resp.data);
}
public createAuthToken(email: string, password: string): Promise<string> {
return this.http
.post('/auth-token', { email, password })
.then((resp) => resp.data);
}
public getUser(authToken: string): Promise<User> {
return this.http
.get('/user', { headers: { Authorization: 'Bearer ' + authToken }})
.then((resp) => return merge(resp.data, { authToken }); );
}
public getAllUsers(user: User): Promise<User[]> {
return this.http
.get('/users', { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public getUserById(curUser: User, reqUserId: string): Promise<User> {
return this.http
.get(`/users/${reqUserId}`, { headers: this.authHeader(curUser) })
.then((resp) => resp.data);
}
public createUser(curUser: User, newUser: User): Promise<User> {
return this.http
.post('/users', newUser, { headers: this.authHeader(curUser) })
.then((resp) => resp.data);
}
public deleteUser(curUser: User, toDeleteUserId: string): Promise<boolean> {
return this.http
.delete(`/users/${toDeleteUserId}`, { headers: this.authHeader(curUser) })
.then((resp) => true);
}
public changePwd(user: User, oldPassword: string, newPassword: string): Promise<boolean> {
return this.http
.post(
'/change-pwd',
{ oldPassword, newPassword },
{ headers: this.authHeader(user) })
.then((resp) => true);
}
public changePwdForUser(user: User, forUserId: string, newPassword: string): Promise<boolean> {
return this.http
.post(
`/change-pwd/${forUserId}`,
{ newPassword },
{ headers: this.authHeader(user) })
.then((resp) => true);
}
public createApiToken(user: User, token: ApiToken): Promise<ApiToken[]> {
return this.http
.post(`/api-tokens`, token, { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public getAllApiTokens(user: User): Promise<ApiToken[]> {
return this.http
.get('/api-tokens', { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public getApiToken(user: User, tokenId: string): Promise<ApiToken[]> {
return this.http
.get(`/api-tokens/${tokenId}`, { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public deleteApiToken(user: User, tokenId: string): Promise<boolean> {
return this.http
.delete(`/api-tokens/${tokenId}`, { headers: this.authHeader(user) })
.then((resp) => true);
}
public getAllMeasures(user: User): Promise<Measure[]> {
return this.http
.get(`/measures`, { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public createMeasure(user: User, measure: Measure): Promise<Measure> {
return this.http
.post(`/measures`, measure, { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public getMeasure(user: User, slug: string): Promise<Measure> {
return this.http
.get(`/measures/${slug}`, { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public deleteMeasure(user: User, slug: string): Promise<boolean> {
return this.http
.delete(`/measures/${slug}`, { headers: this.authHeader(user) })
.then((resp) => true);
}
public getMeasurements(user: User, measureSlug: string):
Promise<Measurement[]> {
return this.http
.get(`/measure/${measureSlug}`, { headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public createMeasurement(user: User, measureSlug: string,
measurement: Measurement): Promise<Measurement> {
return this.http
.post(`/measure/${measureSlug}`,
measurement,
{ headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public getMeasurement(user: User, measureSlug: string, measurementId: string):
Promise<Measurement> {
return this.http
.get(`/measure/${measureSlug}/${measurementId}`,
{ headers: this.authHeader(user) })
.then((resp) => resp.data);
}
public updateMeasurement(user: User, measureSlug: string,
measurement: Measurement): Promise<Measurement> {
return this.http
.put(`/measure/${measureSlug}/${measurement.id}`,
measurement,
{ headers: this.authHeader(user) })
.then((resp) => resp.data) ;
}
public deleteMeasurement(user: User, measureSlug: string, measurementId: string): Promise<boolean> {
return this.http
.delete(`/measure/${measureSlug}/${measurementId}`,
{headers: this.authHeader(user) })
.then((resp) => true);
}
private authHeader(user: User): { [key: string]: string } {
return { Authorization: 'Bearer ' + user.authToken };
}