|
|
|
@@ -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 };
|
|
|
|
|
}
|